Skip to content

Commit 4f22fef

Browse files
authored
Optionally register OpenTelemetry (#2472)
* Optionally register OpenTelemetry * Set env var for integration tests * fix dispose pattern
1 parent 679c7ad commit 4f22fef

File tree

9 files changed

+57
-20
lines changed

9 files changed

+57
-20
lines changed

docs-builder.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
<s:Boolean x:Key="/Default/UserDictionary/Words/=frontmatter/@EntryIndexedValue">True</s:Boolean>
55
<s:Boolean x:Key="/Default/UserDictionary/Words/=linenos/@EntryIndexedValue">True</s:Boolean>
66
<s:Boolean x:Key="/Default/UserDictionary/Words/=literalinclude/@EntryIndexedValue">True</s:Boolean>
7+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Otlp/@EntryIndexedValue">True</s:Boolean>
78
<s:Boolean x:Key="/Default/UserDictionary/Words/=_0060_0060inli/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/Elastic.Documentation.ServiceDefaults/Extensions.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where
3737

3838
public static TBuilder AddOpenTelemetryDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
3939
{
40+
41+
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
42+
if (!useOtlpExporter)
43+
return builder;
44+
4045
_ = builder.Logging.AddOpenTelemetry(logging =>
4146
{
4247
logging.IncludeFormattedMessage = true;
@@ -86,14 +91,6 @@ private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builde
8691

8792
_ = builder.Services.AddOpenTelemetry().UseOtlpExporter();
8893
}
89-
90-
// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
91-
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
92-
//{
93-
// builder.Services.AddOpenTelemetry()
94-
// .UseAzureMonitor();
95-
//}
96-
9794
return builder;
9895
}
9996

src/api/Elastic.Documentation.Api.Infrastructure/MappingsExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ namespace Elastic.Documentation.Api.Infrastructure;
1414

1515
public static class MappingsExtension
1616
{
17-
public static void MapElasticDocsApiEndpoints(this IEndpointRouteBuilder group)
17+
public static void MapElasticDocsApiEndpoints(this IEndpointRouteBuilder group, bool mapOtlpEndpoints = true)
1818
{
19+
1920
_ = group.MapGet("/", () => Results.Empty);
2021
_ = group.MapPost("/", () => Results.Empty);
2122
MapAskAiEndpoint(group);
2223
MapNavigationSearch(group);
2324
MapFullSearch(group);
24-
MapOtlpProxyEndpoint(group);
25+
if (mapOtlpEndpoints)
26+
MapOtlpProxyEndpoint(group);
2527
}
2628

2729
private static void MapAskAiEndpoint(IEndpointRouteBuilder group)

src/api/Elastic.Documentation.Api.Infrastructure/OpenTelemetry/OpenTelemetryExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,13 @@ public static TracerProviderBuilder AddDocsApiTracing(this TracerProviderBuilder
7171
/// </summary>
7272
/// <param name="builder">The web application builder</param>
7373
/// <returns>The builder for chaining</returns>
74-
public static TBuilder AddDocsApiOpenTelemetry<TBuilder>(
75-
this TBuilder builder)
74+
public static TBuilder AddDocsApiOpenTelemetry<TBuilder>(this TBuilder builder)
7675
where TBuilder : IHostApplicationBuilder
7776
{
77+
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
78+
if (!useOtlpExporter)
79+
return builder;
80+
7881
var options = new ElasticOpenTelemetryOptions
7982
{
8083
// In AOT mode, assembly scanning is not supported, so we skip it

src/api/Elastic.Documentation.Api.Lambda/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
_ = app.UseDeveloperExceptionPage();
5050

5151
var v1 = app.MapGroup("/docs/_api/v1");
52-
v1.MapElasticDocsApiEndpoints();
52+
53+
var mapOtlpEndpoints = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
54+
v1.MapElasticDocsApiEndpoints(mapOtlpEndpoints);
5355
Console.WriteLine("API endpoints mapped");
5456

5557
Console.WriteLine("Application startup completed successfully");

src/tooling/docs-builder/Http/DocumentationWebHost.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ private void SetUpRoutes()
142142

143143
var apiV1 = _webApplication.MapGroup("/docs/_api/v1");
144144
#if DEBUG
145-
apiV1.MapElasticDocsApiEndpoints();
145+
var mapOtlpEndpoints = !string.IsNullOrWhiteSpace(_webApplication.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
146+
apiV1.MapElasticDocsApiEndpoints(mapOtlpEndpoints);
146147
#endif
147148

148149
_ = _webApplication.MapGet("{**slug}", (string slug, ReloadableGeneratorState holder, Cancel ctx) =>

src/tooling/docs-builder/Http/StaticWebHost.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ private void SetUpRoutes()
8585

8686
var apiV1 = WebApplication.MapGroup("/docs/_api/v1");
8787
#if DEBUG
88-
apiV1.MapElasticDocsApiEndpoints();
88+
var mapOtlpEndpoints = !string.IsNullOrWhiteSpace(WebApplication.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
89+
apiV1.MapElasticDocsApiEndpoints(mapOtlpEndpoints);
8990
#endif
9091

9192
}

tests-integration/Elastic.Documentation.Api.IntegrationTests/EuidEnrichmentIntegrationTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,23 @@ namespace Elastic.Documentation.Api.IntegrationTests;
1717
/// Integration tests for euid cookie enrichment in OpenTelemetry traces and logging.
1818
/// Uses WebApplicationFactory to test the real API configuration with mocked AskAi services.
1919
/// </summary>
20-
public class EuidEnrichmentIntegrationTests
20+
public class EuidEnrichmentIntegrationTests : IAsyncLifetime
2121
{
22+
private const string OtlpEndpoint = "http://localhost:4318";
23+
24+
public ValueTask InitializeAsync()
25+
{
26+
Environment.SetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT", OtlpEndpoint);
27+
return ValueTask.CompletedTask;
28+
}
29+
30+
public ValueTask DisposeAsync()
31+
{
32+
GC.SuppressFinalize(this);
33+
Environment.SetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT", null);
34+
return ValueTask.CompletedTask;
35+
}
36+
2237
/// <summary>
2338
/// Test that verifies euid cookie is added to both HTTP span and custom AskAi span,
2439
/// and appears in log entries - using the real API configuration.

tests-integration/Elastic.Documentation.Api.IntegrationTests/OtlpProxyIntegrationTests.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,23 @@
1313

1414
namespace Elastic.Documentation.Api.IntegrationTests;
1515

16-
public class OtlpProxyIntegrationTests
16+
public class OtlpProxyIntegrationTests : IAsyncLifetime
1717
{
18+
private const string OtlpEndpoint = "http://localhost:4318";
19+
20+
public ValueTask InitializeAsync()
21+
{
22+
Environment.SetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT", OtlpEndpoint);
23+
return ValueTask.CompletedTask;
24+
}
25+
26+
public ValueTask DisposeAsync()
27+
{
28+
GC.SuppressFinalize(this);
29+
Environment.SetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT", null);
30+
return ValueTask.CompletedTask;
31+
}
32+
1833
[Fact]
1934
public async Task OtlpProxyTracesEndpointForwardsToCorrectUrl()
2035
{
@@ -71,7 +86,7 @@ public async Task OtlpProxyTracesEndpointForwardsToCorrectUrl()
7186
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
7287
capturedRequest.Should().NotBeNull();
7388
capturedRequest!.RequestUri.Should().NotBeNull();
74-
capturedRequest.RequestUri!.ToString().Should().Be("http://localhost:4318/v1/traces");
89+
capturedRequest.RequestUri!.ToString().Should().Be($"{OtlpEndpoint}/v1/traces");
7590
capturedRequest.Method.Should().Be(HttpMethod.Post);
7691
capturedRequest.Content.Should().NotBeNull();
7792
capturedRequest.Content!.Headers.ContentType!.MediaType.Should().Be("application/json");
@@ -131,7 +146,7 @@ public async Task OtlpProxyLogsEndpointForwardsToCorrectUrl()
131146
// Assert - verify the enum ToStringFast() generates "logs" (lowercase)
132147
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
133148
capturedRequest.Should().NotBeNull();
134-
capturedRequest!.RequestUri!.ToString().Should().Be("http://localhost:4318/v1/logs");
149+
capturedRequest!.RequestUri!.ToString().Should().Be($"{OtlpEndpoint}/v1/logs");
135150

136151
// Cleanup mock response
137152
mockResponse.Dispose();
@@ -184,7 +199,7 @@ public async Task OtlpProxyMetricsEndpointForwardsToCorrectUrl()
184199
// Assert - verify the enum ToStringFast() generates "metrics" (lowercase)
185200
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
186201
capturedRequest.Should().NotBeNull();
187-
capturedRequest!.RequestUri!.ToString().Should().Be("http://localhost:4318/v1/metrics");
202+
capturedRequest!.RequestUri!.ToString().Should().Be($"{OtlpEndpoint}/v1/metrics");
188203

189204
// Cleanup mock response
190205
mockResponse.Dispose();

0 commit comments

Comments
 (0)