Real Web Developers

Service collection Jaeger

Author
Scott Rickman
Last updated
2026-08-19

ActivityHelper.cs


namespace RealWebDevelopers;

using System.Diagnostics;

public sealed class ActivityHelper
{
    private ActivitySource? ActivitySource { get; }

    public ActivityHelper(string name)
    {
        this.ActivitySource = new ActivitySource(name);
    }

    public Activity? StartActivity(string operationName)
    {
        return this.ActivitySource?.StartActivity(operationName);
    }
}
    

appSettings.json

    {
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "RabbitMqOptions": {
    "Host": "localhost"
  },
  "OTEL_EXPORTER_OTLP_ENDPOINT": "http://localhost:4317"
}

Program.cs


namespace RealWebDevelopers;

using Microsoft.Extensions.Options;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

public class Program
{
    public static void Main(string[] args)
    {
        // boiler plate web application
        var openTelemetryBuilder = builder.Services.AddOpenTelemetry();
        openTelemetryBuilder.ConfigureResource(configure => { configure.AddService(builder.Environment.ApplicationName); });
        openTelemetryBuilder.WithTracing(configure =>
        {
            configure
                .AddAspNetCoreInstrumentation()
                .AddHttpClientInstrumentation()
                .AddSource(builder.Environment.ApplicationName);
        });
        openTelemetryBuilder.WithMetrics(configure =>
        {
            configure
                .AddAspNetCoreInstrumentation()
                .AddHttpClientInstrumentation()
                .AddRuntimeInstrumentation()
                .AddPrometheusExporter();
        });
        openTelemetryBuilder.UseOtlpExporter();
        builder.Services.AddSingleton(provider => new ActivityHelper(builder.Environment.ApplicationName));
        // etc.
    }
}

Provider.cs


namespace RealWebDevelopers;

public class Provider
{
    private readonly ActivityHelper activityHelper;
    // other fields

    public UnderwritingProvider(ActivityHelper activityHelper //, other services)
    {
        this.activityHelper = activityHelper;
        // other setters
    }

    public async Task<IEnumerable<int>?> GetCoverLevelsAsync(string riskType, CancellationToken cancellationToken)
    {
        using var activity = this.activityHelper.StartActivity("GetCoverLevelsAsync");
        var httpClient = this.httpClientFactory.CreateClient(Constants.HttpClients.UnderwritingApi);
        var httpResponseMessage = await httpClient.GetAsync($"v1/CoverLevel/{riskType}", cancellationToken);
        if (!httpResponseMessage.IsSuccessStatusCode)
        {
            activity?.SetTag(ActivityTags.HttpError, await httpResponseMessage.Content.ReadAsStringAsync(cancellationToken));
            return null;
        }

        return await httpResponseMessage.Content.ReadFromJsonAsync<IEnumerable<int>?>(cancellationToken);
    }
}