Skip to content

Commit 58e5c1e

Browse files
CopilotaaronpowellOdonno
authored
Add OTEL export support to GoFeatureFlag integration (#966)
* Initial plan * Add OTEL support to GoFeatureFlag integration Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com> * Add OtlpExporterAnnotation assertion to test as requested Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com> * Simplifying the asserts as we don't really need to assert the internals of aspire Just verifying that we have the annotation should do enough * no need to do async * Update tests/CommunityToolkit.Aspire.Hosting.GoFeatureFlag.Tests/AddGoFeatureFlagTests.cs Co-authored-by: David Bottiau <bottiau.david@laposte.net> * Add back WithLogLevel test as requested Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com> Co-authored-by: Aaron Powell <me@aaron-powell.com> Co-authored-by: David Bottiau <bottiau.david@laposte.net>
1 parent 0c9dad9 commit 58e5c1e

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed

src/CommunityToolkit.Aspire.Hosting.GoFeatureFlag/GoFeatureFlagBuilderExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public static IResourceBuilder<GoFeatureFlagResource> AddGoFeatureFlag(
6060
name: GoFeatureFlagResource.PrimaryEndpointName)
6161
.WithHttpHealthCheck("/health")
6262
.WithEntrypoint("/go-feature-flag")
63-
.WithArgs(args);
63+
.WithArgs(args)
64+
.WithOtlpExporter();
6465
}
6566

6667
/// <summary>

tests/CommunityToolkit.Aspire.Hosting.GoFeatureFlag.Tests/AddGoFeatureFlagTests.cs

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Net.Sockets;
5+
using Aspire.Components.Common.Tests;
56
using Aspire.Hosting;
7+
using Aspire.Hosting.ApplicationModel;
8+
using Microsoft.Extensions.DependencyInjection;
69
using Microsoft.Extensions.Logging;
710

811
namespace CommunityToolkit.Aspire.Hosting.GoFeatureFlag.Tests;
@@ -90,42 +93,86 @@ public async Task GoFeatureFlagCreatesConnectionString()
9093
Assert.Equal($"Endpoint=http://localhost:27020", connectionString);
9194
Assert.Equal("Endpoint=http://{goff.bindings.http.host}:{goff.bindings.http.port}", connectionStringResource.ConnectionStringExpression.ValueExpression);
9295
}
93-
96+
9497
[Theory]
9598
[InlineData(LogLevel.Debug, "DEBUG")]
9699
[InlineData(LogLevel.Information, "INFO")]
97100
[InlineData(LogLevel.Warning, "WARN")]
98101
[InlineData(LogLevel.Error, "ERROR")]
99-
public async Task AddSurrealServerContainerWithLogLevel(LogLevel logLevel, string? expected)
102+
public void AddGoFeatureFlagWithLogLevel(LogLevel logLevel, string? expected)
100103
{
101104
var appBuilder = DistributedApplication.CreateBuilder();
102-
105+
103106
var goff = appBuilder
104107
.AddGoFeatureFlag("goff")
105108
.WithLogLevel(logLevel);
106-
107-
using var app = appBuilder.Build();
108-
109-
var config = await goff.Resource.GetEnvironmentVariableValuesAsync();
110109

111-
bool hasValue = config.TryGetValue("LOGLEVEL", out var value);
110+
using var app = appBuilder.Build();
112111

113-
Assert.True(hasValue);
114-
Assert.Equal(expected, value);
112+
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
113+
var resource = Assert.Single(appModel.Resources.OfType<GoFeatureFlagResource>());
114+
115+
// Get all environment callback annotations
116+
var envAnnotations = resource.Annotations.OfType<EnvironmentCallbackAnnotation>().ToList();
117+
Assert.NotEmpty(envAnnotations);
118+
119+
// Find the callback that sets LOGLEVEL (not the OTEL one)
120+
// We need to test each callback to find the one that sets LOGLEVEL
121+
var context = new EnvironmentCallbackContext(new DistributedApplicationExecutionContext(new DistributedApplicationExecutionContextOptions(DistributedApplicationOperation.Run)));
122+
123+
string? logLevelValue = null;
124+
foreach (var annotation in envAnnotations)
125+
{
126+
var testContext = new EnvironmentCallbackContext(new DistributedApplicationExecutionContext(new DistributedApplicationExecutionContextOptions(DistributedApplicationOperation.Run)));
127+
try
128+
{
129+
annotation.Callback(testContext);
130+
if (testContext.EnvironmentVariables.TryGetValue("LOGLEVEL", out var value))
131+
{
132+
logLevelValue = value?.ToString();
133+
break;
134+
}
135+
}
136+
catch (InvalidOperationException)
137+
{
138+
// This is the OTEL callback that requires service provider, skip it
139+
continue;
140+
}
141+
}
142+
143+
Assert.NotNull(logLevelValue);
144+
Assert.Equal(expected, logLevelValue);
115145
}
116-
146+
117147
[Theory]
118148
[InlineData(LogLevel.Trace)]
119149
[InlineData(LogLevel.Critical)]
120150
[InlineData(LogLevel.None)]
121-
public void AddSurrealServerContainerWithLogLevelThrowsOnUnsupportedLogLevel(LogLevel logLevel)
151+
public void AddGoFeatureFlagWithLogLevelThrowsOnUnsupportedLogLevel(LogLevel logLevel)
122152
{
123153
var appBuilder = DistributedApplication.CreateBuilder();
124-
154+
125155
var func = () => appBuilder
126156
.AddGoFeatureFlag("goff")
127157
.WithLogLevel(logLevel);
128158

129159
Assert.Throws<ArgumentOutOfRangeException>(func);
130160
}
161+
162+
[Fact]
163+
public void AddGoFeatureFlagAddsOtelAnnotation()
164+
{
165+
var appBuilder = DistributedApplication.CreateBuilder();
166+
167+
appBuilder.AddGoFeatureFlag("goff");
168+
169+
using var app = appBuilder.Build();
170+
171+
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
172+
var resource = Assert.Single(appModel.Resources.OfType<GoFeatureFlagResource>());
173+
174+
// Verify that OtlpExporterAnnotation is present (added by WithOtlpExporter)
175+
// This annotation marks the resource as an OTEL exporter
176+
Assert.True(resource.HasAnnotationOfType<OtlpExporterAnnotation>());
177+
}
131178
}

0 commit comments

Comments
 (0)