skip to Main Content

I am implementing a SDK capable of registering telemetries in external services and as I want it to be vendor neutral, I am using OpenTelemetry. One of the exporters that I am using is the Azure Monitor Exporter to send the telemetries to Application Insight, but all the telemetries(Span, metrics, logs) from OpenTelemetry are being registered as traces type or dependency type and I would like to be able to register telemetries as Custom Events(from Application Insights).
I saw this extension for Python but wasn’t able to found a similar solution for .Net https://github.com/Azure/azure-sdk-for-python/issues/33472

I already have tried to use all the instrumentations provided by OpenTelemetry(Span, Activity, Metrics and logs).

2

Answers


  1. How to register Custom Events in Application Insights by using OpenTelemetry .NET

    Using below code I am able to register the custom events by using Open telemetry in C#.

    Code:

    using System;
    using Microsoft.Extensions.Logging;
    using OpenTelemetry;
    using OpenTelemetry.Trace;
    using Microsoft.ApplicationInsights;
    using Microsoft.ApplicationInsights.DataContracts;
    using OpenTelemetry.Resources;
    using Azure.Monitor.OpenTelemetry.Exporter;
    
    class Program
    {
        [Obsolete]
        static void Main(string[] args)
        {
            // Initialize OpenTelemetry with Azure Monitor exporter
            using var tracerProvider = Sdk.CreateTracerProviderBuilder()
                .AddSource("YourApplication")
                .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("sample"))
                .AddAzureMonitorTraceExporter(options =>
                {
                    options.ConnectionString = "your-azure-monitor conn-string";
                })
                .Build();
    
            // Create a custom event
            var customEvent = new MyCustomEvent
            {
                Name = "CustomEvent",
                Property1 = "Value1",
                Property2 = "Value2"
            };
    
            // Send the custom event to Application Insights
            SendCustomEventToApplicationInsights(customEvent);
        }
    
        [Obsolete]
        static void SendCustomEventToApplicationInsights(MyCustomEvent customEvent)
        {
            // Initialize Application Insights TelemetryClient
            var telemetryClient = new TelemetryClient
            {
                InstrumentationKey = "your-application insights instrumentation-key"
            };
    
            // Track the custom event using TelemetryClient
            var properties = new Dictionary<string, string>
            {
                { "Property1", customEvent.Property1 },
                { "Property2", customEvent.Property2 }
            };
    
            telemetryClient.TrackEvent(customEvent.Name, properties);
            telemetryClient.Flush(); // Flush the telemetry to ensure it is sent immediately
        }
    }
    
    class MyCustomEvent
    {
        public string? Name { get; set; }
        public string? Property1 { get; set; }
        public string? Property2 { get; set; }
    }
    
    

    I am able to see custom events in application insights. check below:

    Output:

    enter image description here

    Login or Signup to reply.
  2. I found this issue on GitHub: https://github.com/Azure/azure-sdk-for-net/issues/42157

    It was created on the same day as this SO question so I tempted to think that it’s the question’s author who created it.

    Still, for anyone interested: based on the linked GH issue, it’s currently not possible to upload custom events with OpenTelemetry client as the OpenTelemetry specification doesn’t yet support it.

    So, at the moment using ApplicationInsights SDK is the only way to track custom events.

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