Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.57k stars 4.82k forks source link

Azure Monitor OTel Exporter does not support array attributes on metrics #47262

Open lmolkova opened 1 week ago

lmolkova commented 1 week ago

Try

using Azure.Monitor.OpenTelemetry.AspNetCore;
using System.Diagnostics;
using System.Diagnostics.Metrics;

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddOpenTelemetry()
    .WithTracing(tp => tp.AddSource("test"))
    .WithMetrics(mp => mp.AddMeter("test"))
    .UseAzureMonitor(o => o.ConnectionString = "secret");

var app = builder.Build();

var test = new ActivitySource("test");
var testMeter = new Meter("test");
var testCounter = testMeter.CreateCounter<int>("this.is.a.test.counter");

app.MapGet("/", async () =>
{
    using var act = test.StartActivity("test me");
    act?.AddTag("array.attribute", new[] { 1, 2, 3 });
    testCounter.Add(1, new KeyValuePair<string, object?>("array.attribute", new[] { 1, 2, 3 }));
});

app.Run();

Array attribute on spans works fine, but is converted to string with ToString() on metrics here https://github.com/Azure/azure-sdk-for-net/blob/e1e91d2dd8fb2c854b75f46a9a294116942d2418/sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/MetricsData.cs#L34

Image

The suggestion is to do it consistently (comma separated list).

context: https://github.com/open-telemetry/semantic-conventions/pull/1525 use-case: as a user I might want to filter my metrics checking if array attribute contains a specific value among others.

lmolkova commented 1 week ago

Seems to violate common schema mappings:

Mapping OpenTelemetry Attributes to properties: If attribute value is of primitive type array, then the property value should be a comma-separated array elements.

https://msazure.visualstudio.com/One/_git/CommonSchema?path=/v4.0/Mappings/AzureMonitor-AI.md&version=GBmain&line=50&lineEnd=53&lineStartColumn=1&lineEndColumn=69&lineStyle=plain&_a=contents