From 1b45195ac5fc9329242cfc3d9db9b515dee32706 Mon Sep 17 00:00:00 2001 From: Shay Keren Date: Tue, 10 Sep 2024 08:20:35 +0300 Subject: [PATCH] OtlpSamplerProbability --- Sample.MoneyTransfer.Api/RunWebApp.cs | 50 ++++++++++++++++------- Sample.MoneyTransfer.Api/appsettings.json | 1 + 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/Sample.MoneyTransfer.Api/RunWebApp.cs b/Sample.MoneyTransfer.Api/RunWebApp.cs index 2937db0..a7e4dbe 100644 --- a/Sample.MoneyTransfer.Api/RunWebApp.cs +++ b/Sample.MoneyTransfer.Api/RunWebApp.cs @@ -16,7 +16,22 @@ namespace Sample.MoneyTransfer.API; public class RunWebApp { - static void AddOpenTelemetry(IServiceCollection services, IConfiguration configuration, string otlpExporterUrl, string ? commitHash) + static void SetSampler(IConfiguration configuration, TracerProviderBuilder builder) + { + var samplerProbability = configuration.GetValue("OtlpSamplerProbability"); + if (samplerProbability is not null) + { + Console.WriteLine($"TracerProviderBuilder OtlpSamplerProbability was set to: {samplerProbability}"); + builder.SetSampler(new ParentBasedSampler(new TraceIdRatioBasedSampler(samplerProbability.Value))); + } + else + { + builder.SetSampler(new ParentBasedSampler(new AlwaysOnSampler())); + } + } + + static void AddOpenTelemetry(IServiceCollection services, IConfiguration configuration, string otlpExporterUrl, + string? commitHash) { var otelBuilder = services.AddOpenTelemetry(); var serviceName = typeof(RunWebApp).Assembly.GetName().Name!; @@ -26,27 +41,32 @@ static void AddOpenTelemetry(IServiceCollection services, IConfiguration configu .AddService(serviceName) .AddDigmaAttributes(configure => { - if(commitHash is not null) configure.CommitId = commitHash; + if (commitHash is not null) configure.CommitId = commitHash; configure.NamespaceRoot = "Sample"; }) .AddEnvironmentVariableDetector(); + + otelBuilder - .WithTracing(builder => builder - .AddHttpClientInstrumentation() - .AddAspNetCoreInstrumentation(config => config.RecordException = true) - .SetResourceBuilder(resourceBuilder) - .AddOtlpExporter(c => + .WithTracing(builder => { - - c.Endpoint = new Uri(otlpExporterUrl); - c.Protocol = OtlpExportProtocol.Grpc; - }) - .SetErrorStatusOnException() - .AddSource("*") - ); - } + SetSampler(configuration, builder); + builder + .AddHttpClientInstrumentation() + .AddAspNetCoreInstrumentation(config => config.RecordException = true) + .SetResourceBuilder(resourceBuilder) + .AddOtlpExporter(c => + { + c.Endpoint = new Uri(otlpExporterUrl); + c.Protocol = OtlpExportProtocol.Grpc; + }) + .SetErrorStatusOnException() + .AddSource("*"); + } + ); +} public static void Run(string[] args) { diff --git a/Sample.MoneyTransfer.Api/appsettings.json b/Sample.MoneyTransfer.Api/appsettings.json index 8872942..2a4f538 100644 --- a/Sample.MoneyTransfer.Api/appsettings.json +++ b/Sample.MoneyTransfer.Api/appsettings.json @@ -15,6 +15,7 @@ } }, "OtlpExporterUrl":"http://localhost:3050", + "OtlpSamplerProbability":0.05, // "RabbitMq": { // "Host": "localhost", // "Username": "admin",