open-telemetry / opentelemetry-dotnet

The OpenTelemetry .NET Client
https://opentelemetry.io
Apache License 2.0
3.09k stars 736 forks source link

Detect `service.version` from Assembly Version? #5703

Open Aaronontheweb opened 2 weeks ago

Aaronontheweb commented 2 weeks ago

Package

OpenTelemetry

Is your feature request related to a problem?

I implemented this already in some of my own projects, was mostly interested in knowing if the project would accept it as a contribution if I cleaned it up and made a PR.

I usually use the AssemblyVersion as my service.version value in OTEL since the former is always synced with our Docker image tags in our build process. Therefore, I wrote an IResourceDetector that will just grab this data and use it throughout all of our OTEL-enabled projects:

public sealed class AssemblyVersionDetector : IResourceDetector
{
    public Resource Detect()
    {
        // care about the foreground assembly used in `dotnet run` / equivalent
        var version = Assembly.GetExecutingAssembly().GetName().Version?.ToString();

        IEnumerable<KeyValuePair<string, object>> attributes = Array.Empty<KeyValuePair<string, object>>();

        if (version != null)
        {
            attributes = new[]
            {
                new KeyValuePair<string, object>("service.version", version)
            };
        }

        return new Resource(attributes);
    }
}

Totally fine renaming it / cleaning it up to use resource dictionaries et al. But would it make sense to have this available as a built-in IResourceDetector in the core library? Even though it technically uses System.Reflection I think it should still be pass AOT-compatibility checks since it's not doing any dynamic-linking.

What is the expected behavior?

Populates the service.version with the appropriate value derived from the executing assembly's information.

You can see an example of what this looks like using Seq's OTEL log aggregation w/ data from one of our applications (0.2.4.0 is the correct value).

image

Which alternative solutions or features have you considered?

Just using my own middleware to do this

Additional context

No response

CodeBlanch commented 2 weeks ago

@Aaronontheweb

Here are my thoughts...

Kielek commented 2 weeks ago

@CodeBlanch, I would vote for your extended proposal, but in the contrib repository. I do not think that it is defined in spec/semantic convention to put it as a part of SDK.

If we speaking about detection, from particular assemblies, we are doing something similar for scope version. Keep in mind that part of the versioning system extends versions by suffixes (eg. GH commits). Ref: https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/9114d0b183def59bc0cd747bdee024aae7d35fbc/src/Shared/AssemblyVersionExtensions.cs