JasperFx / marten

.NET Transactional Document DB and Event Store on PostgreSQL
https://martendb.io
MIT License
2.75k stars 429 forks source link

Bug: Marten does not export db.statement tag to activities since version 7.0.0 #3205

Closed dvlprx closed 2 months ago

dvlprx commented 2 months ago

Hi @jeremydmiller,

In version 6.4.1, the code below exported the db.statment tag correctly to created activities. Since 7.0.0 the db.statement.tag is not present in the activity anymore.

You can see what is received in zipkin in this image:

image

Here is the full console app, to reproduce the issue:

using Marten;
using Marten.Events;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Trace;
using System.Diagnostics;

internal class Constants
{
    internal const string TESTAPP_TRACER_NAME = "OtelTestApp";
    internal const string STREAM_KEY = "SomeTestStream#42834348346328";
    internal const string POSTGRES_CNSTRING = "Host=host.docker.internal;Port=54320;Database=EventLogDb;User Id=postgresDev;Password=postgresDev;";
}

internal class SomeEvent
{
    public string Text { get; set; }
    public SomeEvent(string text)
    {
        Text = text;
    }
}

internal class Program
{
    private static async Task Main(string[] args)
    {
        // configure and start host
        var host = ConfigureHost();
        await host.StartAsync();

        // execute test queries
        var svc = host.Services.GetRequiredService<TestService>();
        await svc.DoTests();

        // allow the zipkin exporter to finish
        await Task.Delay(10000);
    }

    private static IHost ConfigureHost()
    {
        var hostBuilder = Host.CreateDefaultBuilder();
        hostBuilder.ConfigureServices(sc =>
        {
            // TELEMETRY
            sc.AddOpenTelemetry()
                .WithTracing(tracerBuilder =>
                {
                    // observe all sources
                    tracerBuilder.AddSource(Constants.TESTAPP_TRACER_NAME);
                    tracerBuilder.AddSource("Npgsql");
                    tracerBuilder.AddSource("Marten");

                    // export to zipkin
                    tracerBuilder.AddZipkinExporter(o => { });
                });

            // MARTEN
            sc.AddMarten(sp =>
            {
                var opts = new StoreOptions();
                opts.Connection(Constants.POSTGRES_CNSTRING);
                opts.Events.StreamIdentity = StreamIdentity.AsString;
                return opts;
            });

            sc.AddSingleton<TestService>();
        });

        return hostBuilder.Build();
    }
}

internal class TestService
{
    readonly IServiceProvider _sp;
    readonly ActivitySource _tracer;

    public TestService(IServiceProvider sp)
    {
        _sp = sp;
        _tracer = new ActivitySource(Constants.TESTAPP_TRACER_NAME);
    }

    public async Task DoTests()
    {
        using var a = _tracer.StartActivity();

        await TestRead();
        await TestWrite();
    }

    private async Task TestRead()
    {
        using var a = _tracer.StartActivity();

        await using var session = _sp.GetRequiredService<IDocumentStore>().QuerySession();
        var _ = await session.Events.FetchStreamAsync(Constants.STREAM_KEY);
    }

    private async Task TestWrite()
    {
        using var a = _tracer.StartActivity();

        await using var session = _sp.GetRequiredService<IDocumentStore>().LightweightSession();
        var newEvent = new SomeEvent($"Doing tests {DateTime.UtcNow}");
        var _ = session.Events.Append(Constants.STREAM_KEY, newEvent);

        await session.SaveChangesAsync();
    }
}

ProjectFile:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <LangVersion>11.0</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Marten" Version="7.12.0" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
    <PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.8.1" />
    <PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.8.1" />
    <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
  </ItemGroup>

</Project>
jeremydmiller commented 2 months ago

@dvlprx "db.statement.tag" has absolutely nothing to do with Marten. I'm not sure what it is you're expecting us to do here.

I think you need to talk to the Npgsql team. I somewhat remember that they didn't yet add Otel tracing to NpgsqlBatch yet, and Marten uses that. Until then, I don't think this is actionable from the Marten side of things. If this is fixed on the Npgsql side, feel free to send a PR updating that to Marten.

dvlprx commented 2 months ago

Thank you for clarification, I will post in npgsql forum and update here :)