Skip to content

Commit

Permalink
Merge pull request #23 from digma-ai/upgrade-dependencies
Browse files Browse the repository at this point in the history
OtlpSamplerProbability
  • Loading branch information
shaykeren authored Sep 10, 2024
2 parents 5810792 + 1b45195 commit d51ef64
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
50 changes: 35 additions & 15 deletions Sample.MoneyTransfer.Api/RunWebApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<double?>("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!;
Expand All @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions Sample.MoneyTransfer.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
}
},
"OtlpExporterUrl":"http://localhost:3050",
"OtlpSamplerProbability":0.05,
// "RabbitMq": {
// "Host": "localhost",
// "Username": "admin",
Expand Down

0 comments on commit d51ef64

Please sign in to comment.