Skip to content

It looks like tracing is not working for initializer. #20

@voroninp

Description

@voroninp

I have a simple initializer with defined ActivitySource.

When I call StartActivity I always get null despite source name is registered in WithTracing method:

builder.Services.AddOpenTelemetry()
        .WithTracing(builder =>
        {
            builder
                .AddAspNetCoreInstrumentation()
                .AddEntityFrameworkCoreInstrumentation()
                .AddSource(customSources); // <--- added in the list of `customSources`

In other types (hosted services) tracing works fine.

It looks like the issue is caused by how Otel library works:

public static OpenTelemetryBuilder AddOpenTelemetry(this IServiceCollection services)
{
    Guard.ThrowIfNull(services);

    if (!services.Any((ServiceDescriptor d) => d.ServiceType == typeof(IHostedService) && d.ImplementationType == typeof(TelemetryHostedService)))
    {
        services.Insert(0, ServiceDescriptor.Singleton<IHostedService, TelemetryHostedService>());
    }

    return new(services);
}

And the underlying issue seems to be the fact that host has no AfterInit event.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions