Cannot find OpenTelemetry Tags and Events in Azure Application Insights

237 views Asked by At

I have a sample ASP.NET Core 6 web service, which is configured to use OpenTelemetry and Azure Monitor like So:

// Program.cs.
using Azure.Monitor.OpenTelemetry.AspNetCore;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

var resourceAttributes = new Dictionary<string, object> {
    { "service.name", "my-service" },
    { "service.namespace", "my-namespace" },
    { "service.instance.id", "my-instance" }};

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenTelemetry().UseAzureMonitor(options => {
    options.ConnectionString = "<my-connection-string>";
});
builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => {
    builder.ConfigureResource(resourceBuilder => resourceBuilder.AddAttributes(resourceAttributes));
});

// Add services to the container.

builder.Services.AddControllersWithViews();

var app = builder.Build();

// The rest of the logic.

Then here is my controller's method:

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            _logger.LogInformation($"my-weather-{(Random.Shared.Next(1, 99)):00}");

            using (var activity = WeatherActivities.MainSource.StartActivity("Weather.Random"))
            {
                _logger.LogInformation("my-weather-inside-activity!");
                activity?.SetTag("my-tag", Random.Shared.Next(1, 99));
                activity?.AddEvent(new ActivityEvent("my-activity-event"));

                return Enumerable.Range(1, 5).Select(index => new WeatherForecast
                {
                    Date = DateTime.Now.AddDays(index),
                    TemperatureC = Random.Shared.Next(-20, 55),
                    Summary = Summaries[Random.Shared.Next(Summaries.Length)]
                })
                .ToArray();
            }
        }

When I hit the service, the information emitted by calls to _logger.LogInformation() is landing in Azure Monitor and can be queried. I cannot find the logged information from activity?.SetTag() and activity.AddEvent() calls. I am also not sure how to find the activity source name or resource attributes. It feels like they are not sent for some reason.

I tried the following queries unsuccessfully:

union dependencies, traces | where customDimensions["my-tag"] != ""
customEvents | where name == "my-activity-event"

What am I missing? Thanks in advance!

2

There are 2 answers

0
Vivek Vaibhav Shandilya On

Unfortunately, Custom Event currently does not support for OpenTelemetry, only custom metric, dependency, Exception and Resquest are supported. For reference check this Microsoft Document.

I Followed this Microsoft document for Opentelemetry in application Insights and it worked for me.

I have created a sample ASP.NET Core Web App in VS 2022.

Program.cs:

using Azure.Monitor.OpenTelemetry.AspNetCore;
 
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenTelemetry().UseAzureMonitor();

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

OUTPUT:

enter image description here

enter image description here

0
cijothomas On

If you are manually creating Activity using own ActivitySource, then it must be explicitly added to the TracerProvider as shown here:

builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => builder.AddSource("Name of activity source for WeatherActivities.MainSource"));

https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore#adding-custom-activitysource-to-traces

(Also, it may be better to simply enrich the existing activity with your custom tag instead of creating a new Activity. You can enrich the existing activity via Activity.Current.SetTag inside the controller.