-
-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added initial set of changes for Open Telemetry support
- Loading branch information
1 parent
f439c7c
commit 7526f8d
Showing
6 changed files
with
99 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Diagnostics; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace Core.OpenTelemetry; | ||
|
||
public static class OpenTelemetryExtensions | ||
{ | ||
public static IServiceCollection AddOpenTelemetry( | ||
this IServiceCollection services, | ||
string serviceName | ||
) => AddOpenTelemetry(services, serviceName, OpenTelemetryOptions.Default); | ||
|
||
public static IServiceCollection AddOpenTelemetry( | ||
this IServiceCollection services, | ||
string serviceName, | ||
OpenTelemetryOptions options | ||
) | ||
{ | ||
Activity.DefaultIdFormat = ActivityIdFormat.W3C; | ||
|
||
AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true); | ||
|
||
services | ||
.AddOpenTelemetryTracing(builder => | ||
{ | ||
options.ConfigureTracerProvider(builder | ||
.AddAspNetCoreInstrumentation() | ||
.AddHttpClientInstrumentation() | ||
.SetResourceBuilder( | ||
ResourceBuilder.CreateDefault() | ||
.AddService(serviceName) | ||
.AddTelemetrySdk() | ||
)); | ||
|
||
if (!options.ShouldDisableConsoleExporter) | ||
builder.AddConsoleExporter(); | ||
}); | ||
|
||
return services; | ||
} | ||
} | ||
|
||
public class OpenTelemetryOptions | ||
{ | ||
public static OpenTelemetryOptions Default | ||
{ | ||
get | ||
{ | ||
return new OpenTelemetryOptions(); | ||
} | ||
} | ||
|
||
public Func<TracerProviderBuilder, TracerProviderBuilder> ConfigureTracerProvider { get; private set; } = p => p; | ||
|
||
public bool ShouldDisableConsoleExporter { get; private set; } | ||
|
||
private OpenTelemetryOptions() | ||
{ | ||
} | ||
|
||
public static OpenTelemetryOptions Build(Func<OpenTelemetryOptions, OpenTelemetryOptions> build) => | ||
build(Default); | ||
|
||
public OpenTelemetryOptions Configure(Func<TracerProviderBuilder, TracerProviderBuilder> configure) | ||
{ | ||
this.ConfigureTracerProvider = configure; | ||
return this; | ||
} | ||
|
||
public OpenTelemetryOptions DisableConsoleExporter(bool shouldDisable) | ||
{ | ||
ShouldDisableConsoleExporter = shouldDisable; | ||
return this; | ||
} | ||
} |
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