Skip to content
Merged
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
@@ -0,0 +1 @@
#nullable enable
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
OpenTelemetry.OpAmp.Client.OpAmpClient
OpenTelemetry.OpAmp.Client.OpAmpClient.Dispose() -> void
OpenTelemetry.OpAmp.Client.OpAmpClient.OpAmpClient(System.Action<OpenTelemetry.OpAmp.Client.Settings.OpAmpClientSettings!>? configure = null) -> void
OpenTelemetry.OpAmp.Client.OpAmpClient.StartAsync(System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!
OpenTelemetry.OpAmp.Client.OpAmpClient.StopAsync(System.Threading.CancellationToken token = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!
OpenTelemetry.OpAmp.Client.Settings.ConnectionType
OpenTelemetry.OpAmp.Client.Settings.ConnectionType.Http = 0 -> OpenTelemetry.OpAmp.Client.Settings.ConnectionType
OpenTelemetry.OpAmp.Client.Settings.ConnectionType.WebSocket = 1 -> OpenTelemetry.OpAmp.Client.Settings.ConnectionType
OpenTelemetry.OpAmp.Client.Settings.HeartbeatSettings
OpenTelemetry.OpAmp.Client.Settings.HeartbeatSettings.HeartbeatSettings() -> void
OpenTelemetry.OpAmp.Client.Settings.HeartbeatSettings.Interval.get -> System.TimeSpan
OpenTelemetry.OpAmp.Client.Settings.HeartbeatSettings.Interval.set -> void
OpenTelemetry.OpAmp.Client.Settings.HeartbeatSettings.IsEnabled.get -> bool
OpenTelemetry.OpAmp.Client.Settings.HeartbeatSettings.IsEnabled.set -> void
OpenTelemetry.OpAmp.Client.Settings.OpAmpClientSettings
OpenTelemetry.OpAmp.Client.Settings.OpAmpClientSettings.ConnectionType.get -> OpenTelemetry.OpAmp.Client.Settings.ConnectionType
OpenTelemetry.OpAmp.Client.Settings.OpAmpClientSettings.ConnectionType.set -> void
OpenTelemetry.OpAmp.Client.Settings.OpAmpClientSettings.Heartbeat.get -> OpenTelemetry.OpAmp.Client.Settings.HeartbeatSettings!
OpenTelemetry.OpAmp.Client.Settings.OpAmpClientSettings.Heartbeat.set -> void
OpenTelemetry.OpAmp.Client.Settings.OpAmpClientSettings.InstanceUid.get -> System.Guid
OpenTelemetry.OpAmp.Client.Settings.OpAmpClientSettings.InstanceUid.set -> void
OpenTelemetry.OpAmp.Client.Settings.OpAmpClientSettings.OpAmpClientSettings() -> void
OpenTelemetry.OpAmp.Client.Settings.OpAmpClientSettings.ServerUrl.get -> System.Uri!
OpenTelemetry.OpAmp.Client.Settings.OpAmpClientSettings.ServerUrl.set -> void
2 changes: 1 addition & 1 deletion src/OpenTelemetry.OpAmp.Client/Internal/FrameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Google.Protobuf;
using OpAmp.Proto.V1;
using OpenTelemetry.OpAmp.Client.Internal.Services.Heartbeat;
using OpenTelemetry.OpAmp.Client.Internal.Settings;
using OpenTelemetry.OpAmp.Client.Settings;

namespace OpenTelemetry.OpAmp.Client.Internal;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using OpenTelemetry.Internal;
using OpenTelemetry.OpAmp.Client.Internal.Services.Heartbeat;
using OpenTelemetry.OpAmp.Client.Internal.Settings;
using OpenTelemetry.OpAmp.Client.Internal.Transport;
using OpenTelemetry.OpAmp.Client.Settings;

namespace OpenTelemetry.OpAmp.Client.Internal;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using OpenTelemetry.OpAmp.Client.Internal.Listeners;
using OpenTelemetry.OpAmp.Client.Internal.Listeners.Messages;
using OpenTelemetry.OpAmp.Client.Internal.Settings;
using OpenTelemetry.OpAmp.Client.Settings;

namespace OpenTelemetry.OpAmp.Client.Internal.Services.Heartbeat;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using OpenTelemetry.OpAmp.Client.Internal.Settings;
using OpenTelemetry.OpAmp.Client.Settings;

namespace OpenTelemetry.OpAmp.Client.Internal.Services;

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using OpenTelemetry.OpAmp.Client.Internal;
using OpenTelemetry.OpAmp.Client.Internal.Services;
using OpenTelemetry.OpAmp.Client.Internal.Services.Heartbeat;
using OpenTelemetry.OpAmp.Client.Internal.Settings;
using OpenTelemetry.OpAmp.Client.Internal.Transport;
using OpenTelemetry.OpAmp.Client.Internal.Transport.Http;
using OpenTelemetry.OpAmp.Client.Internal.Transport.WebSocket;
using OpenTelemetry.OpAmp.Client.Settings;

namespace OpenTelemetry.OpAmp.Client.Internal;
namespace OpenTelemetry.OpAmp.Client;

internal sealed class OpAmpClient : IDisposable
/// <summary>
/// OpenTelemetry OpAMP Client.
/// </summary>
public sealed class OpAmpClient : IDisposable
{
private readonly OpAmpClientSettings settings = new();
private readonly FrameProcessor processor = new();
private readonly Dictionary<string, IBackgroundService> services = [];
private readonly FrameDispatcher dispatcher;
private readonly IOpAmpTransport transport;

/// <summary>
/// Initializes a new instance of the <see cref="OpAmpClient"/> class.
/// </summary>
/// <param name="configure">Configure OpAMP client settings callback.</param>
public OpAmpClient(Action<OpAmpClientSettings>? configure = null)
{
configure?.Invoke(this.settings);
Expand All @@ -28,6 +36,51 @@ public OpAmpClient(Action<OpAmpClientSettings>? configure = null)
this.ConfigureServices();
}

/// <summary>
/// Starts the <see cref="OpAmpClient"/> instance, establishing the connection to the server and starting all configured services.
/// </summary>
/// <param name="token">Cancellation token.</param>
/// <returns>A task that represents the asynchronous start operation.</returns>
public async Task StartAsync(CancellationToken token = default)
{
if (this.transport is WsTransport wsTransport)
{
await wsTransport.StartAsync(token)
.ConfigureAwait(false);
}

// TODO: Dispatch identification message here

foreach (var service in this.services.Values)
{
service.Start();
}
}

/// <summary>
/// Stops the <see cref="OpAmpClient"/> instance, terminating the connection to the server and stopping all running services.
/// </summary>
/// <param name="token">Cancellation token.</param>
/// <returns>A task that represents the asynchronous stop operation.</returns>
public async Task StopAsync(CancellationToken token = default)
{
// TODO: Identify if there is any messages to send to the server before stopping services

foreach (var service in this.services.Values)
{
service.Stop();
}

if (this.transport is WsTransport wsTransport)
{
await wsTransport.StopAsync(token)
.ConfigureAwait(false);
}
}

/// <summary>
/// Disposes the OpAmpClient instance and releases all associated resources.
/// </summary>
public void Dispose()
{
this.dispatcher.Dispose();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

namespace OpenTelemetry.OpAmp.Client.Internal.Settings;
namespace OpenTelemetry.OpAmp.Client.Settings;

/// <summary>
/// Specifies the type of transport protocol to be used for communication.
/// </summary>
/// <remarks>This enumeration defines the available transport protocols for communication. Use <see
/// cref="WebSocket"/> for WebSocket-based communication, or <see cref="Http"/> for
/// HTTP-based communication.</remarks>
internal enum ConnectionType
public enum ConnectionType
{
/// <summary>
/// Use HTTP transport.
Expand Down
28 changes: 28 additions & 0 deletions src/OpenTelemetry.OpAmp.Client/Settings/HeartbeatSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

namespace OpenTelemetry.OpAmp.Client.Settings;

/// <summary>
/// Configuration settings for the heartbeat mechanism of the client.
/// </summary>
public sealed class HeartbeatSettings
{
/// <summary>
/// Gets or sets a value indicating whether the heartbeat mechanism is enabled.
/// </summary>
/// <value>
/// <c>true</c> if heartbeat messages should be sent periodically; otherwise, <c>false</c>.
/// Default is <c>true</c>.
/// </value>
public bool IsEnabled { get; set; } = true;

/// <summary>
/// Gets or sets the interval between heartbeat messages.
/// </summary>
/// <value>
/// A <see cref="TimeSpan"/> representing how often heartbeat messages are sent.
/// The default value is 30 seconds.
/// </value>
public TimeSpan Interval { get; set; } = TimeSpan.FromSeconds(30);
}
89 changes: 89 additions & 0 deletions src/OpenTelemetry.OpAmp.Client/Settings/OpAmpClientSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

namespace OpenTelemetry.OpAmp.Client.Settings;

/// <summary>
/// OpAMP client settings.
/// </summary>
public sealed class OpAmpClientSettings
{
private Uri? serverUrl;

/// <summary>
/// Gets or sets the unique identifier for the current client instance.
/// </summary>
/// <value>
/// A <see cref="Guid"/> that uniquely identifies this instance.
/// By default:
/// <list type="bullet">
/// <item>
/// <description>On .NET 9.0 or greater: initialized with a Version 7 GUID.</description>
/// </item>
/// <item>
/// <description>On earlier versions: initialized with a randomly generated GUID.</description>
/// </item>
/// </list>
/// </value>
public Guid InstanceUid { get; set; }
#if NET9_0_OR_GREATER
= Guid.CreateVersion7();
#else
= Guid.NewGuid();
#endif

/// <summary>
/// Gets or sets the type of connection used to communicate with the server
/// (for example, HTTP or WebSocket).
/// </summary>
/// <value>
/// A <see cref="ConnectionType"/> value that specifies the transport protocol.
/// The default is <see cref="ConnectionType.Http"/>.
/// </value>
public ConnectionType ConnectionType { get; set; } = ConnectionType.Http;

/// <summary>
/// Gets or sets the server URL to connect to.
/// </summary>
/// <value>
/// The <see cref="Uri"/> representing the server endpoint.
/// If not explicitly set, a default URL is returned based on the <see cref="ConnectionType"/>:
/// <list type="bullet">
/// <item>
/// <description><see cref="ConnectionType.Http"/> -> <c>https://localhost:4318/v1/opamp</c></description>
/// </item>
/// <item>
/// <description><see cref="ConnectionType.WebSocket"/> -> <c>wss://localhost:4318/v1/opamp</c></description>
/// </item>
/// </list>
/// </value>
/// <exception cref="InvalidOperationException">
/// Thrown when the <see cref="ConnectionType"/> is not recognized.
/// </exception>
public Uri ServerUrl
{
get
{
if (this.serverUrl != null)
{
return this.serverUrl;
}

switch (this.ConnectionType)
{
case ConnectionType.Http:
return new("https://localhost:4318/v1/opamp");
case ConnectionType.WebSocket:
return new("wss://localhost:4318/v1/opamp");
default:
throw new InvalidOperationException("Unknown connection type");
}
}
set => this.serverUrl = value;
}

/// <summary>
/// Gets or sets the heartbeat settings.
/// </summary>
public HeartbeatSettings Heartbeat { get; set; } = new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#if NET

using OpenTelemetry.OpAmp.Client.Internal;
using OpenTelemetry.OpAmp.Client.Internal.Settings;
using OpenTelemetry.OpAmp.Client.Settings;
using OpenTelemetry.OpAmp.Client.Tests.Mocks;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using OpenTelemetry.OpAmp.Client.Internal;
using OpenTelemetry.OpAmp.Client.Internal.Services.Heartbeat;
using OpenTelemetry.OpAmp.Client.Internal.Settings;
using OpenTelemetry.OpAmp.Client.Settings;
using OpenTelemetry.OpAmp.Client.Tests.Mocks;
using Xunit;

Expand Down
Loading