-
Notifications
You must be signed in to change notification settings - Fork 352
[OpAMP.Client] Add heartbeats #3095
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e7d70f0
add heartbeat service
RassK c578b7e
add missing seals
RassK 240942a
fix todo markers
RassK 3bdda26
unify log markers
RassK 7ed69cc
fix missing usings
RassK fb79a5f
update changelog
RassK 3deeeef
Update src/OpenTelemetry.OpAmp.Client/Internal/FrameBuilder.cs
RassK b68429e
Update src/OpenTelemetry.OpAmp.Client/Internal/Services/Heartbeat/Hea…
RassK 96f87a6
Update src/OpenTelemetry.OpAmp.Client/Internal/Settings/HeartbeatSett…
RassK 838d5cc
Update src/OpenTelemetry.OpAmp.Client/Internal/Services/Heartbeat/Hea…
RassK 8f5cef8
fix missing readonly
RassK 9e105e4
add missing readonly
RassK 2195988
use net9+ native lock
RassK a532bd2
Merge branch 'main' into opamp-client-heartbeats
RassK 6684f34
fix lock impl
RassK f5637ed
alphabetical order
Kielek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/OpenTelemetry.OpAmp.Client/Internal/Services/Heartbeat/ComponentHealthStatus.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
namespace OpenTelemetry.OpAmp.Client.Internal.Services.Heartbeat; | ||
|
||
/// <summary> | ||
/// Represents the health status of a system component. | ||
/// </summary> | ||
internal sealed class ComponentHealthStatus | ||
{ | ||
public ComponentHealthStatus(string componentName) | ||
{ | ||
this.ComponentName = componentName; | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the name of the component. | ||
/// </summary> | ||
public string ComponentName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the current status of the operation or entity. | ||
/// </summary> | ||
public string? Status { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the description of the most recent error encountered. | ||
/// </summary> | ||
public string? LastError { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a value indicating whether the system is in a healthy state. | ||
/// </summary> | ||
public bool IsHealthy { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the start time of the event or operation. | ||
/// </summary> | ||
public DateTimeOffset StartTime { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the timestamp indicating the current status update time. | ||
/// </summary> | ||
public DateTimeOffset StatusTime { get; set; } | ||
} |
19 changes: 19 additions & 0 deletions
19
src/OpenTelemetry.OpAmp.Client/Internal/Services/Heartbeat/HealthReport.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
namespace OpenTelemetry.OpAmp.Client.Internal.Services.Heartbeat; | ||
|
||
internal sealed class HealthReport | ||
{ | ||
public ulong StartTime { get; set; } | ||
|
||
public ulong StatusTime { get; set; } | ||
|
||
public bool IsHealthy { get; set; } | ||
|
||
public string? Status { get; set; } | ||
|
||
public string? LastError { get; set; } | ||
|
||
public IList<ComponentHealthStatus> Components { get; } = []; | ||
} |
114 changes: 114 additions & 0 deletions
114
src/OpenTelemetry.OpAmp.Client/Internal/Services/Heartbeat/HeartbeatService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using OpenTelemetry.OpAmp.Client.Internal.Listeners; | ||
using OpenTelemetry.OpAmp.Client.Internal.Listeners.Messages; | ||
using OpenTelemetry.OpAmp.Client.Internal.Settings; | ||
|
||
namespace OpenTelemetry.OpAmp.Client.Internal.Services.Heartbeat; | ||
|
||
internal sealed class HeartbeatService : IBackgroundService, IOpAmpListener<ConnectionSettingsMessage>, IDisposable | ||
{ | ||
public const string Name = "heartbeat-service"; | ||
|
||
private readonly FrameDispatcher dispatcher; | ||
private readonly FrameProcessor processor; | ||
private readonly CancellationTokenSource cts; | ||
private readonly Lock timerUpdateLock = new(); | ||
private Timer? timer; | ||
private TimeSpan tickInterval; | ||
private ulong startTime; | ||
|
||
public HeartbeatService(FrameDispatcher dispatcher, FrameProcessor processor) | ||
{ | ||
this.dispatcher = dispatcher; | ||
this.processor = processor; | ||
this.cts = new CancellationTokenSource(); | ||
|
||
this.processor.Subscribe(this); | ||
} | ||
|
||
public string ServiceName => Name; | ||
|
||
public void Configure(OpAmpClientSettings settings) | ||
{ | ||
this.tickInterval = settings.Heartbeat.Interval; | ||
} | ||
|
||
public void Start() | ||
{ | ||
this.startTime = GetCurrentTimeInNanoseconds(); | ||
this.CreateOrUpdateTimer(this.tickInterval); | ||
} | ||
|
||
public void Stop() | ||
{ | ||
this.cts.Cancel(); | ||
this.CreateOrUpdateTimer(Timeout.InfiniteTimeSpan); | ||
} | ||
|
||
public void HandleMessage(ConnectionSettingsMessage message) | ||
{ | ||
var newInterval = message.ConnectionSettings.Opamp?.HeartbeatIntervalSeconds ?? 0; | ||
if (newInterval > 0) | ||
{ | ||
// TODO: change to proper logging | ||
Console.WriteLine($"[debug] New heartbeat interval received: {newInterval}s"); | ||
|
||
this.CreateOrUpdateTimer(TimeSpan.FromSeconds(newInterval)); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this.processor.Unsubscribe(this); | ||
|
||
this.cts.Dispose(); | ||
this.timer?.Dispose(); | ||
} | ||
|
||
private static ulong GetCurrentTimeInNanoseconds() | ||
{ | ||
return (ulong)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() * 1_000_000; // Convert to nanoseconds | ||
} | ||
|
||
private void CreateOrUpdateTimer(TimeSpan interval) | ||
{ | ||
lock (this.timerUpdateLock) | ||
{ | ||
this.timer ??= new Timer(this.HeartbeatTick); | ||
this.timer.Change(interval, interval); | ||
} | ||
} | ||
|
||
private async void HeartbeatTick(object? state) | ||
{ | ||
try | ||
{ | ||
var report = this.CreateHealthReport(); | ||
|
||
await this.dispatcher.DispatchHeartbeatAsync(report, this.cts.Token) | ||
.ConfigureAwait(false); | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
// Ignore task cancellation | ||
} | ||
catch (Exception ex) | ||
{ | ||
// TODO: change to proper logging | ||
Console.WriteLine($"[error] Heartbeat error: {ex.Message}"); | ||
} | ||
} | ||
|
||
private HealthReport CreateHealthReport() | ||
{ | ||
return new HealthReport | ||
{ | ||
StartTime = this.startTime, | ||
StatusTime = GetCurrentTimeInNanoseconds(), | ||
IsHealthy = true, | ||
Status = "OK", | ||
}; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intentionally left for follow ups?
For now, all contrib packages are using EventSources similar to https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/8452d88e83e1c204bd4b43c7c3d8189f94ff87be/src/OpenTelemetry.Resources.Host/HostResourceEventSource.cs
(There are even tests for it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that was my plan as well. Will address logging in a separate PR.