0

I have a .NET console application where I want to add Open telemetry traces to be sent to Grafana. The app does not use Service Collection and DI. Instead we use the AppBuilder for all the application initialization and configuration.

I am using the TrcaeProvider and MeterProvider to set up open telemetry inside the AppBuilder.cs file

public static MeterProvider MeterProvider { get; private set; }
        public static TracerProvider TracerProvider { get; private set; }

public static void ConfigureOpenTelemetry()
        {
            var resourceBuilder = ResourceBuilder.CreateDefault().AddService(
                OtlpEndpoint.OtlpServiceName,
                serviceVersion: assemblyVersion);


            // Configure Tracer Provider
            TracerProvider = Sdk.CreateTracerProviderBuilder()
                .AddSource(ActivitySourceName)
                .SetResourceBuilder(resourceBuilder)
                .SetSampler(new AlwaysOnSampler())
                .AddAspNetCoreInstrumentation()
                .AddOtlpExporter(exporterOptions =>
                {
                    exporterOptions.Endpoint = new  Uri(OtlpEndpoint.OtlpEndpointTracesUrl);
                }).Build();

            // Configure Meter Provider
            MeterProvider  = Sdk.CreateMeterProviderBuilder()
                .SetResourceBuilder(resourceBuilder)
                .AddRuntimeInstrumentation()
                .AddProcessInstrumentation()
                .AddMeter(MeterName)
                .AddOtlpExporter((otlpOptions, readerOptions) =>
                {
                    otlpOptions.Endpoint = new Uri(OtlpEndpoint.OtlpEndpointMetricsUrl);
                    readerOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds =
                        OtlpEndpoint.OtlpExportIntervalMilliseconds;
                }).Build(); 
        }

The traces are being written to Grafana as expected but the problem is the span depth is only 2.. which means only 2 calls within the entire application are being recorded. The traces added outside of the AppBuilder are not being recorded

for example :

public class Command : ICommandStep<PipelineContext>
    {
       
        private readonly Tracer _tracer;
        public Command (
          
            Meter meter,
            Tracer tracer)
        {
          
            _tracer = tracer;
            // Initialize metrics
            _counter = meter.CreateCounter<long>(
                "number_processed",
                description: "Counts the number of transactions processed");
        }

        public async Task<xxx> FunctionAsync(ActivitySource activitySource)
        {
            using var activity = activitySource.StartActivity($"{nameof(Command)}.{nameof(FunctionAsync)}");
        // rest of the function code

The activity span from Command .cs is not being recorded as part of the service structure in Grafana.

I tried injecting the Tracer in the other parts of the application but the traces do not continue. The metrics are working as expected and I see the metrics written to Grafana under Drilldowns.

1
  • Have you added anything that will force a flush of any inmemory spans before your application shuts down? Also, you don't need to inject Tracer, just use a static ActivitySource and listen to the name of that in the opentelemetry SDK. Commented May 18 at 11:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.