-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move our AgentBuilder to IOpenTelemetryBuilder (#47)
* Move our AgentBuilder to IOpenTelemetryBuilder This is not yet released see open-telemetry/opentelemetry-dotnet#5265 for more background. For now we include a temporary copy with hacks to call internal bits. This allows AgentBuilder to be a native opentelemetry builder and we'd inherit all extension methods from the OpenTelemetry community * clean up AddElasticOpenTelemetry()
- Loading branch information
Showing
16 changed files
with
393 additions
and
386 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
8 changes: 5 additions & 3 deletions
8
src/Elastic.OpenTelemetry.AspNetCore/AgentBuilderExtensions.cs
This file contains 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 |
---|---|---|
@@ -1,18 +1,20 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using OpenTelemetry; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace Elastic.OpenTelemetry.AspNetCore; | ||
|
||
/// <summary> | ||
/// | ||
/// | ||
/// </summary> | ||
public static class AgentBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// | ||
/// | ||
/// </summary> | ||
/// <param name="agentBuilder"></param> | ||
public static AgentBuilder AddAspNetCore(this AgentBuilder agentBuilder) => agentBuilder.ConfigureTracer(tpb => tpb.AddAspNetCoreInstrumentation()); | ||
public static IOpenTelemetryBuilder AddAspNetCore(this AgentBuilder agentBuilder) => agentBuilder.WithTracing(tpb => tpb.AddAspNetCoreInstrumentation()); | ||
} |
This file contains 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 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,96 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Elastic.OpenTelemetry.Diagnostics; | ||
using Elastic.OpenTelemetry.Diagnostics.Logging; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Exporter; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace Elastic.OpenTelemetry; | ||
|
||
/// <summary> TODO </summary> | ||
public static class OpenTelemetryBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Build an instance of <see cref="IAgent"/>. | ||
/// </summary> | ||
/// <returns>A new instance of <see cref="IAgent"/>.</returns> | ||
public static IAgent Build(this IOpenTelemetryBuilder builder, ILogger? logger = null, IServiceProvider? serviceProvider = null) | ||
{ | ||
// this happens if someone calls Build() while using IServiceCollection and AddOpenTelemetry() and NOT Add*Elastic*OpenTelemetry() | ||
// we treat this a NOOP | ||
// NOTE for AddElasticOpenTelemetry(this IServiceCollection services) calling Build() manually is NOT required. | ||
if (builder is not AgentBuilder agentBuilder) | ||
return new EmptyAgent(); | ||
|
||
var log = agentBuilder.Logger; | ||
|
||
log.SetAdditionalLogger(logger); | ||
|
||
var otelBuilder = agentBuilder.Services.AddOpenTelemetry(); | ||
otelBuilder | ||
.WithTracing(tracing => | ||
{ | ||
if (!agentBuilder.SkipOtlpRegistration) | ||
tracing.AddOtlpExporter(agentBuilder.OtlpExporterName, agentBuilder.OtlpExporterConfiguration); | ||
log.LogAgentBuilderBuiltTracerProvider(); | ||
}) | ||
.WithMetrics(metrics => | ||
{ | ||
if (!agentBuilder.SkipOtlpRegistration) | ||
{ | ||
metrics.AddOtlpExporter(agentBuilder.OtlpExporterName, o => | ||
{ | ||
o.ExportProcessorType = ExportProcessorType.Simple; | ||
o.Protocol = OtlpExportProtocol.HttpProtobuf; | ||
}); | ||
} | ||
log.LogAgentBuilderBuiltMeterProvider(); | ||
}); | ||
|
||
var sp = serviceProvider ?? agentBuilder.Services.BuildServiceProvider(); | ||
var tracerProvider = sp.GetService<TracerProvider>()!; | ||
var meterProvider = sp.GetService<MeterProvider>()!; | ||
|
||
var agent = new ElasticAgent(log, agentBuilder.EventListener, tracerProvider, meterProvider); | ||
log.LogAgentBuilderBuiltAgent(); | ||
return agent; | ||
} | ||
} | ||
|
||
internal class EmptyAgent : IAgent | ||
{ | ||
public void Dispose() { } | ||
|
||
public ValueTask DisposeAsync() => ValueTask.CompletedTask; | ||
} | ||
|
||
internal class ElasticAgent( | ||
AgentCompositeLogger logger, | ||
LoggingEventListener loggingEventListener, | ||
TracerProvider tracerProvider, | ||
MeterProvider meterProvider | ||
) : IAgent | ||
{ | ||
public void Dispose() | ||
{ | ||
tracerProvider.Dispose(); | ||
meterProvider.Dispose(); | ||
loggingEventListener.Dispose(); | ||
logger.Dispose(); | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
tracerProvider.Dispose(); | ||
meterProvider.Dispose(); | ||
await loggingEventListener.DisposeAsync().ConfigureAwait(false); | ||
await logger.DisposeAsync().ConfigureAwait(false); | ||
} | ||
} | ||
|
Oops, something went wrong.