Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -41,8 +41,6 @@ public ComponentTelemetryContext(string componentType)
// Internal for testing
internal Dictionary<string, AspireTelemetryProperty> Properties { get; } = [];

private DashboardTelemetryService TelemetryService => _telemetryService ?? throw new ArgumentNullException(nameof(_telemetryService), "InitializeAsync has not been called");

public void Initialize(DashboardTelemetryService telemetryService, string? browserUserAgent)
{
_telemetryService = telemetryService;
Expand Down Expand Up @@ -92,7 +90,12 @@ public bool UpdateTelemetryProperties(ReadOnlySpan<ComponentTelemetryProperty> m

private void PostProperties()
{
TelemetryService.PostOperation(
if (_telemetryService == null)
{
throw new InvalidOperationException("InitializeAsync has not been called");
}

_telemetryService.PostOperation(
TelemetryEventKeys.ParametersSet,
TelemetryResult.Success,
properties: Properties,
Expand All @@ -103,7 +106,7 @@ public void Dispose()
{
if (!_disposed)
{
TelemetryService.PostOperation(
_telemetryService?.PostOperation(
TelemetryEventKeys.ComponentDispose,
TelemetryResult.Success,
properties: new Dictionary<string, AspireTelemetryProperty>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ public class ComponentTelemetryContextTests
[Fact]
public async Task ComponentTelemetryContext_TelemetryEnabled_EndToEnd()
{
// Arrange
var telemetryContext = new ComponentTelemetryContext(nameof(ComponentTelemetryContextTests));
var telemetrySender = new TestDashboardTelemetrySender { IsTelemetryEnabled = true };
var telemetryService = new DashboardTelemetryService(NullLogger<DashboardTelemetryService>.Instance, telemetrySender);
var telemetryContextProvider = new ComponentTelemetryContextProvider(telemetryService);
telemetryContextProvider.SetBrowserUserAgent("mozilla");
await telemetryService.InitializeAsync();

// Act & assert initialize
telemetryContextProvider.Initialize(telemetryContext);
for (var i = 0; i < telemetryService.GetDefaultProperties().Count; i++)
{
Expand All @@ -35,6 +37,7 @@ public async Task ComponentTelemetryContext_TelemetryEnabled_EndToEnd()

OperationContext? parametersUpdateOperation;

// Act & assert update properties
telemetryContext.UpdateTelemetryProperties([new ComponentTelemetryProperty("Test", new AspireTelemetryProperty("Value"))]);
Assert.Equal(3, telemetryContext.Properties.Count);
Assert.True(telemetrySender.ContextChannel.Reader.TryRead(out parametersUpdateOperation));
Expand All @@ -50,6 +53,7 @@ public async Task ComponentTelemetryContext_TelemetryEnabled_EndToEnd()
Assert.Equal(3, telemetryContext.Properties.Count);
Assert.True(telemetrySender.ContextChannel.Reader.TryRead(out parametersUpdateOperation));

// Act & assert dispose
telemetryContext.Dispose();
Assert.True(telemetrySender.ContextChannel.Reader.TryRead(out var disposeOperation));
Assert.Equal("/telemetry/operation - $aspire/dashboard/component/dispose", disposeOperation.Name);
Expand All @@ -58,21 +62,35 @@ public async Task ComponentTelemetryContext_TelemetryEnabled_EndToEnd()
[Fact]
public async Task ComponentTelemetryContext_TelemetryDisabled_EndToEnd()
{
// Arrange
var telemetryContext = new ComponentTelemetryContext(nameof(ComponentTelemetryContextTests));
var telemetrySender = new TestDashboardTelemetrySender { IsTelemetryEnabled = false };
var telemetryService = new DashboardTelemetryService(NullLogger<DashboardTelemetryService>.Instance, telemetrySender);
var telemetryContextProvider = new ComponentTelemetryContextProvider(telemetryService);
telemetryContextProvider.SetBrowserUserAgent("mozilla");
await telemetryService.InitializeAsync();

// Act & assert initialize
telemetryContextProvider.Initialize(telemetryContext);
Assert.False(telemetrySender.ContextChannel.Reader.TryRead(out _));

// Act & assert update properties
telemetryContext.UpdateTelemetryProperties([new ComponentTelemetryProperty("Test", new AspireTelemetryProperty("Value"))]);
Assert.Equal(3, telemetryContext.Properties.Count);
Assert.False(telemetrySender.ContextChannel.Reader.TryRead(out _));

// Act & assert dispose
telemetryContext.Dispose();
Assert.False(telemetrySender.ContextChannel.Reader.TryRead(out _));
}

[Fact]
public void ComponentTelemetryContext_DisposeWithoutInitialize_NoThrow()
{
// Arrange
var telemetryContext = new ComponentTelemetryContext(nameof(ComponentTelemetryContextTests));

// Act
telemetryContext.Dispose();
}
}
Loading