Git Product home page Git Product logo

Comments (2)

CodeBlanch avatar CodeBlanch commented on June 16, 2024

@VladyslavLishchyna I just spent some time trying to reproduce this and so far haven't been able to. Can you provide me with a small app that reproduces the problem? If not, some questions...

  • What versions of the OTel packages are you using?
  • Can you share your code for configuring the OTel SDK?

Here is how I tried to reproduce it using latest code. Everything seems to work fine.

// Program.cs
using OpenTelemetry.Trace;
using WorkerService;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddOpenTelemetry()
   .WithTracing(builder => builder.AddSource(Worker.MyActivitySource.Name).AddOtlpExporter());

builder.Services.AddHostedService<Worker>();

var host = builder.Build();

host.Run();

// Worker.cs
using System.Diagnostics;

namespace WorkerService
{
    public class Worker : BackgroundService
    {
        public static ActivitySource MyActivitySource { get; } = new(nameof(MyActivitySource));

        private readonly ILogger<Worker> _logger;

        public Worker(ILogger<Worker> logger)
        {
            _logger = logger;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                }
                await Task.Delay(1000, stoppingToken);
            }
        }

        public override Task StartAsync(CancellationToken ct)
        {
            using (MyActivitySource.StartActivity("worker.SomeBackgroundService.started"))
            {
                return base.StartAsync(ct);
            }
        }

        public override Task StopAsync(CancellationToken ct)
        {
            using (MyActivitySource.StartActivity("worker.SomeBackgroundService.stopped"))
            {
                return base.StopAsync(ct);
            }
        }
    }
}

from opentelemetry-dotnet.

VladyslavLishchyna avatar VladyslavLishchyna commented on June 16, 2024

Hi @CodeBlanch at first I want to say thanks for your help and attention to this question.

OpenTelemetry.Exporter.OpenTelemetryProtocol version - 1.7.0

Unfortunately I can't share all project, but I prepared little example how we register

    private static OpenTelemetryBuilder AddOpenTelemetry(this IServiceCollection services, MyOptions myOptions)
    {
      var builder = services
        .AddOpenTelemetry()
        .WithTracing(builder =>
        {
          builder
            .AddSource("Operation")
            .AddProcessor<MetricsFilteringProcessor>() //our processor
            .TryAddInstrumentation(myOptions.Telemetry); //our extension
        });

      builder
        .WithMetrics(metricsBuilder => ConfigureMetrics(metricsBuilder, myOptions));

      builder
        .WithTracing(tracerProviderBuilder =>
        {
          tracerProviderBuilder
            .AddSource(myOptions.ApplicationName)
            .AddSource(nameof(AddTraceIdToResponseMiddleware))
            .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(myOptions.ApplicationName, serviceVersion: "1.0.0", autoGenerateServiceInstanceId: false, serviceInstanceId: myOptions.Kubernetes.PodName))
            .TryAddOtlpExporter(myOptions.Telemetry);
        });

      return builder;
    }

    private static void ConfigureMetrics(MeterProviderBuilder builder, MyOptions myOptions)
    {
      var resourceBuilder = ResourceBuilder
        .CreateDefault()
        .AddService(myOptions.ApplicationName, serviceVersion: "1.0.0", autoGenerateServiceInstanceId: false, serviceInstanceId: myOptions.Kubernetes.PodName);

      builder
        .AddMeter(myOptions.ApplicationName)
        .AddRuntimeInstrumentation()
        .AddProcessInstrumentation()
        .SetResourceBuilder(resourceBuilder);

      builder.AddView(instrument =>
      {
        return instrument.GetType().GetGenericTypeDefinition() == typeof(Histogram<>)
          ? new ExplicitBucketHistogramConfiguration { Boundaries = new double[] { 5, 20, 60, 100, 140, 180, 220, 260, 320, 360, 400, 600, 800, 1100, 1300, 1500, 1900, 2500, 3000, 4000, 6000, 10000, 30000 } }
          : null;
      });

      builder.AddOtlpExporter(otlpOptions =>
      {
        otlpOptions.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
        otlpOptions.Endpoint = new Uri($"http://{myOptions.Telemetry.JaegerExporter.EnsureGetUdpHost()}:4317");

        if (Enum.TryParse<ExportProcessorType>(myOptions.Telemetry.JaegerExporter.ExportProcessorType, out var exportProcessorType))
          otlpOptions.ExportProcessorType = exportProcessorType;

        if (otlpOptions.ExportProcessorType == ExportProcessorType.Simple)
          return;

        otlpOptions.BatchExportProcessorOptions.MaxQueueSize = 8192;
        otlpOptions.BatchExportProcessorOptions.MaxExportBatchSize = 1024;
        otlpOptions.BatchExportProcessorOptions.ScheduledDelayMilliseconds = 200;
        otlpOptions.BatchExportProcessorOptions.ExporterTimeoutMilliseconds = 1000;
      });
    }

from opentelemetry-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.