Skip to content

[bug] Not inject service.name into OpenTelemetry metric labels (e.g., dotnet_assembly_count{...}) #6352

@masterworgen

Description

@masterworgen

Package

OpenTelemetry

Package Version

    <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
    <PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.12.0-beta.1" />
    <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
    <PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0" />
    <PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.12.0-beta.2" />
    <PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0" />
    <PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="1.12.0-beta.1" />
    <PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.12.0" />

Runtime Version

net9.0

Description

I'm using OpenTelemetry in a .NET application and need to ensure that the service.name is included in the labels (attributes) of all emitted metrics, such as:

dotnet_assembly_count{otel_scope_name="System.Runtime", service_name="my-service"}

Currently, service.name is configured via a custom IResourceDetector:

Steps to Reproduce

public class MyResourceDetector(IWebHostEnvironment webHostEnvironment) : IResourceDetector
{
    public Resource Detect()
    {
        var entryAssembly = Assembly.GetEntryAssembly()?.GetName()!;

        return ResourceBuilder.CreateEmpty()
            .AddService(entryAssembly.Name!, entryAssembly.Name, entryAssembly.Version?.ToString(3), true, Environment.MachineName)
            .AddAttributes(new Dictionary<string, object>
            {
                { "service.name", entryAssembly.Name! },
                { "host.name", Environment.MachineName },
                { "host.environment", webHostEnvironment.EnvironmentName }
            })
            .Build();
    }
}

And the OpenTelemetry setup looks like this:

services.AddOpenTelemetry()
    .ConfigureResource(builder =>
        builder.AddDetector(provider => provider.GetRequiredService<MyResourceDetector>()))
    .WithMetrics(metrics =>
    {
        metrics.AddAspNetCoreInstrumentation();
        metrics.AddProcessInstrumentation();
        metrics.AddRuntimeInstrumentation();
        metrics.AddRebusInstrumentation();
        metrics.AddPrometheusExporter();
        metrics.AddHttpClientInstrumentation();
    })
    .WithTracing(tracing =>
    {
        tracing.AddAspNetCoreInstrumentation(options =>
        {
            options.Filter = context => !context.Request.Path.StartsWithSegments("/metrics");
        });
        tracing.AddHttpClientInstrumentation();
        tracing.AddEntityFrameworkCoreInstrumentation(options => { options.SetDbStatementForText = true; });
        tracing.AddRebusInstrumentation();
        tracing.AddSource(entryAssembly.Name!);
        tracing.AddOtlpExporter(options => { options.Endpoint = new Uri(option.Endpoint); });
    });

Expected Result

Get in prometheus dotnet_assembly_count{otel_scope_name="System.Runtime", service_name="my-service"}

Actual Result

dotnet_assembly_count{otel_scope_name="System.Runtime"}

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds-triageNew issues which have not been classified or triaged by a community memberpkg:OpenTelemetry.Exporter.Prometheus.AspNetCoreIssues related to OpenTelemetry.Exporter.Prometheus.AspNetCore NuGet package

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions