Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix heartbeat for short-running WorkerServices #2762

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ internal class HeartbeatProvider : IDisposable, IHeartbeatProvider

private bool disposedValue = false; // To detect redundant calls to dispose
private TimeSpan interval; // time between heartbeats emitted
private TimeSpan firstInterval; // time for the first interval
private TelemetryClient telemetryClient; // client to use in sending our heartbeat
private volatile bool isEnabled; // no need for locks or volatile here, we can skip/add a beat if the module is disabled between heartbeats

Expand All @@ -67,6 +68,7 @@ public HeartbeatProvider()
this.heartbeatProperties = new ConcurrentDictionary<string, HeartbeatPropertyPayload>(StringComparer.OrdinalIgnoreCase);
this.heartbeatsSent = 0; // count up from construction time
this.isEnabled = true;
this.firstInterval = TimeSpan.FromSeconds(1);
}

/// <summary>
Expand All @@ -89,6 +91,19 @@ public TimeSpan HeartbeatInterval
this.InitTimer();
}
}

/// <summary>
/// Gets or sets the currently defined interval of the first heartbeat. Defaults to 1 second.
/// </summary>
public TimeSpan HeartbeatFirstInterval
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial... Is maybe a better term

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InitialHeartbeatDelay

{
get => this.firstInterval;
set
{
this.firstInterval = value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some bounds checking? At least make sure it's not negative (less than TimeSpan.Zero)

Copy link
Author

@304NotModified 304NotModified Dec 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea 👍

this.InitTimer();
}
}

/// <summary>
/// Gets or sets the currently defined instrumentation key to send heartbeat telemetry items to.
Expand Down Expand Up @@ -272,11 +287,11 @@ internal void InitTimer()
{
if (this.IsHeartbeatEnabled && this.HeartbeatTimer == null)
{
this.HeartbeatTimer = new Timer(callback: this.HeartbeatPulse, state: this, dueTime: this.HeartbeatInterval, period: this.HeartbeatInterval);
this.HeartbeatTimer = new Timer(callback: this.HeartbeatPulse, state: this, dueTime: this.HeartbeatFirstInterval, period: this.HeartbeatInterval);
}
else if (this.IsHeartbeatEnabled)
{
this.HeartbeatTimer.Change(dueTime: this.HeartbeatInterval, period: this.HeartbeatInterval);
this.HeartbeatTimer.Change(dueTime: this.HeartbeatFirstInterval, period: this.HeartbeatInterval);
}
else if (this.HeartbeatTimer != null)
{
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## VNext
- [Populate required field Message with "n/a" if it is empty](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1066)
- [Fix heartbeat for short-running WorkerServices](https://github.com/microsoft/ApplicationInsights-dotnet/issues/2759)

## Version 2.22.0
- no changes since beta.
Expand Down