azureazure-application-insightsazure-monitoringazure-monitor

UseAzureMonitor duplicates all logs


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?


Solution

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

    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.

    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