-
Notifications
You must be signed in to change notification settings - Fork 906
Add OpenTelemetry support on yarp container #2750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
b9a9c7f
6123fef
a7f38f3
f90be38
88f3500
d42087b
a71793b
0aeb040
9ee0ab6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,15 @@ | ||
| <Project> | ||
| <!-- Arcade currently requires this file, though it doesn't require any content in it I believe. --> | ||
| <ItemGroup Label="File signing information"> | ||
| <!-- Third-party components which should be signed. --> | ||
| <FileSignInfo Include="HealthChecks.ApplicationStatus.dll" CertificateName="3PartySHA2" /> | ||
| <FileSignInfo Include="OpenTelemetry.dll" CertificateName="3PartySHA2" /> | ||
| <FileSignInfo Include="OpenTelemetry.Api.dll" CertificateName="3PartySHA2" /> | ||
| <FileSignInfo Include="OpenTelemetry.Api.ProviderBuilderExtensions.dll" CertificateName="3PartySHA2" /> | ||
| <FileSignInfo Include="OpenTelemetry.Exporter.OpenTelemetryProtocol.dll" CertificateName="3PartySHA2" /> | ||
| <FileSignInfo Include="OpenTelemetry.Extensions.Hosting.dll" CertificateName="3PartySHA2" /> | ||
| <FileSignInfo Include="OpenTelemetry.Instrumentation.AspNetCore.dll" CertificateName="3PartySHA2" /> | ||
| <FileSignInfo Include="OpenTelemetry.Instrumentation.GrpcNetClient.dll" CertificateName="3PartySHA2" /> | ||
| <FileSignInfo Include="OpenTelemetry.Instrumentation.Http.dll" CertificateName="3PartySHA2" /> | ||
| <FileSignInfo Include="OpenTelemetry.Instrumentation.Runtime.dll" CertificateName="3PartySHA2" /> | ||
| </ItemGroup> | ||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| using System.Net; | ||
| using System.Security.Cryptography; | ||
| using System.Security.Cryptography.X509Certificates; | ||
| using HealthChecks.ApplicationStatus.DependencyInjection; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Diagnostics.HealthChecks; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using OpenTelemetry; | ||
| using OpenTelemetry.Exporter; | ||
| using OpenTelemetry.Logs; | ||
| using OpenTelemetry.Metrics; | ||
| using OpenTelemetry.Trace; | ||
| using static System.Net.WebRequestMethods; | ||
|
|
||
| namespace Microsoft.Extensions.Hosting; | ||
|
|
||
| // Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry. | ||
| // This project should be referenced by each service project in your solution. | ||
| // To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults | ||
| public static class Extensions | ||
| { | ||
| public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder | ||
| { | ||
| builder.ConfigureOpenTelemetry(); | ||
|
|
||
| builder.AddDefaultHealthChecks(); | ||
|
|
||
| builder.Services.AddServiceDiscovery(); | ||
benjaminpetit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| builder.Services.ConfigureHttpClientDefaults(http => | ||
| { | ||
| // Turn on resilience by default | ||
| http.AddStandardResilienceHandler(); | ||
benjaminpetit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Turn on service discovery by default | ||
| http.AddServiceDiscovery(); | ||
| }); | ||
|
|
||
| return builder; | ||
| } | ||
|
|
||
| public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder | ||
| { | ||
| builder.Logging.AddOpenTelemetry(logging => | ||
| { | ||
| logging.IncludeFormattedMessage = true; | ||
| logging.IncludeScopes = true; | ||
| }); | ||
|
|
||
| builder.Services.AddOpenTelemetry() | ||
| .WithMetrics(metrics => | ||
| { | ||
| metrics.AddAspNetCoreInstrumentation() | ||
| .AddHttpClientInstrumentation() | ||
| .AddRuntimeInstrumentation() | ||
| .SetExemplarFilter(ExemplarFilterType.TraceBased); | ||
| }) | ||
| .WithTracing(tracing => | ||
| { | ||
| tracing.AddAspNetCoreInstrumentation() | ||
| .AddGrpcClientInstrumentation() | ||
benjaminpetit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .AddHttpClientInstrumentation() | ||
| .AddOtlpExporter(); | ||
| }); | ||
|
|
||
| builder.AddOpenTelemetryExporters(); | ||
|
|
||
| return builder; | ||
| } | ||
|
|
||
| private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder | ||
| { | ||
| var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); | ||
|
|
||
| if (useOtlpExporter) | ||
| { | ||
|
|
||
| if (string.Equals(Environment.GetEnvironmentVariable("YARP_UNSAFE_SKIP_OLTP_CERT_VALIDATION"), "true", StringComparison.InvariantCultureIgnoreCase)) | ||
|
||
| { | ||
| // We cannot use UseOtlpExporter() since it doesn't support configuration via OtlpExporterOptions | ||
| // https://github.com/open-telemetry/opentelemetry-dotnet/issues/5802 | ||
| builder.Services.Configure<OtlpExporterOptions>(ConfigureOtlpExporterOptions); | ||
| builder.Services.AddOpenTelemetry() | ||
| .WithLogging(logging => logging.AddOtlpExporter()) | ||
| .WithMetrics(metrics => metrics.AddOtlpExporter()) | ||
| .WithTracing(tracing => tracing.AddOtlpExporter()); | ||
| } | ||
| else | ||
| { | ||
| builder.Services.AddOpenTelemetry().UseOtlpExporter(); | ||
| } | ||
| } | ||
|
|
||
| static void ConfigureOtlpExporterOptions(OtlpExporterOptions options) | ||
| { | ||
| options.HttpClientFactory = () => | ||
| { | ||
| var handler = new HttpClientHandler(); | ||
| handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; | ||
| var httpClient = new HttpClient(handler); | ||
| return httpClient; | ||
| }; | ||
| } | ||
|
|
||
| return builder; | ||
| } | ||
|
|
||
| public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder | ||
| { | ||
| builder.Services.AddHealthChecks() | ||
| // Add a default liveness check on application status. | ||
| .AddApplicationStatus(tags: ["live"]); | ||
|
|
||
| return builder; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.