skip to Main Content
builder.Services
.AddOpenTelemetry()
.UseAzureMonitor()
.ConfigureResource(r => r.AddService("XYZ"))
.WithTracing(b => b
    .AddSource("X") 
    .AddAzureMonitorTraceExporter())
.WithMetrics(b => b
    .AddMeter("X") 
    .AddAzureMonitorMetricExporter());

I am trying to integrate my app with Azure Monitor by using UseAzureMonitor extension and my custom Tracing and Metrics.

Separately they work as excepted, but when I use them both, all logs, counters etc duplicates.

When I comment UseAzureMonitor out I lose logs and http traces.
When I comment out WithTracing and ‘WithMetrics’ I lose all my custom telemetry data and yet when I use them both, all data (logs,counters etc.) is duplicated.

Is there a way to use them both? How to configure that?

2

Answers


  1. Chosen as BEST ANSWER

    Here is my fixed configuration, that meets my needs

    builder.Services
        .AddOpenTelemetry()
        .WithLogging(b =>
        {
            b.ConfigureResource(r => r.AddService(builder.Environment.ApplicationName));
        }, o =>
        {
            o.IncludeFormattedMessage = true;
            o.IncludeScopes = true;
            o.AddAzureMonitorLogExporter();
        })
        .WithTracing(b => b
            .AddSource("X")
            .AddHttpClientInstrumentation()
            .AddAzureMonitorTraceExporter()
            .ConfigureResource(ConfigureResource))
        .WithMetrics(b => b
            .AddMeter("X") 
            .AddHttpClientInstrumentation()
            .AddAzureMonitorMetricExporter()
            .ConfigureResource(ConfigureResource));
    

  2. I tried configuring both UseAzureMonitor and custom Open Telemetry tracing & metrics, its giving duplicate logs.

    • This happens because both Azure Monitor’s default settings and your custom setup are recording the same data, which causes duplicate logs in your telemetry.

    • To prevent duplicate telemetry logs, it’s better to skip UseAzureMonitor and set up OpenTelemetry manually. This ensures your custom configuration remains clear and avoids any overlap with default telemetry.

    If you want to use both UseAzureMonitor and custom OpenTelemetry tracing and metrics without duplicate logs, you can apply filters to prevent multiple logs.

    Refer this doc for better understanding about how to add filters.

    • You can implement custom processors to filter your trace logs or manage duplicate logs in your custom metrics.

    This my DuplicateTraceFilterProcessor.cs file

    using OpenTelemetry;
    using OpenTelemetry.Trace;
    using System.Collections.Generic;
    using System.Diagnostics;
    namespace CustomMetricsSample.Processors
    {
        public class DuplicateTraceFilterProcessor : ActivityProcessor
        {
            private readonly HashSet<string> _traceNames = new HashSet<string>();
            public override void OnStart(Activity activity)
            {
                        }
            public override void OnEnd(Activity activity)
            {
                if (_traceNames.Contains(activity.DisplayName))
                {
                    return; 
                }
                _traceNames.Add(activity.DisplayName);        
            }
        }
    }
    

    Here I got the logs without any duplicate traces.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search