serilog-sinks-grafana-loki icon indicating copy to clipboard operation
serilog-sinks-grafana-loki copied to clipboard

Support the work with Serilog's 3.1.0 properties TraceId & SpanId

Open mishamyte opened this issue 1 year ago • 4 comments

The Serilog's release v3.1.0 brings us the new breaking change:

This release adds two new first-class properties to LogEvent: TraceId and SpanId. These are set automatically in Logger.Write() to the corresponding property values from System.Diagnostics.Activity.Current.

The major benefit of this change is that sinks, once updated, can reliably propagate trace and span ids through to back-ends that support them (in much the same way that first-class timestamps, messages, levels, and exceptions are used today).

The sinks maintained under serilog/serilog, along with formatting helpers such as Serilog.Formatting.Compact and Serilog.Expressions, are already compatible with this change or have pending releases that add compatibility.

In the current implementation we have a minimal version of Serilog - 2.12.0, in which those properties were not present. So in the next place the are dropped out during the creation of the new LogEvent

https://github.com/serilog-contrib/serilog-sinks-grafana-loki/blob/1bbf8370dc311d6114daad9f7306005b4d0edb90/src/Serilog.Sinks.Grafana.Loki/Models/LokiLogEvent.cs#L49-L59

After the investigation, the decision was taken. As v9 (WIP) will reference v3.1.0 as a minimal, we will work with those properties from the beginning. As v8 would reach the end of the life after the v9 release, that version would not work with those properties.

Originally found in:

Discussed in https://github.com/serilog-contrib/serilog-sinks-grafana-loki/discussions/232

Originally posted by rolfwessels November 27, 2023 First, thank you to the creators for this package. It's been handy.

Unfortunately, I would like to add TraceId to the labels but I do not seem to be able to do this

// Serilog.Sinks.Grafana.Loki 8.2.0
builder.Host.UseSerilog((_, lc) =>
{
  lc.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
    .Enrich.FromLogContext()
    .WriteTo.Console(
      outputTemplate: "{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}({TraceId:l}){NewLine}{Exception}")
    .WriteTo.GrafanaLoki(
      telemetrySettings.LokiUrl,
      labels: lokiLabels,
      propertiesAsLabels: PropertiesAsLabels(),
      credentials: FromUrl(telemetrySettings.LokiUrl),
      textFormatter: new MessageTemplateTextFormatter("{Message:lj} ({TraceId:l}){Exception}")
      );
});

Calling the following:

_log.Information("ScansSelect {trace}",Activity.Current?.TraceId);

The output to the console is as expected.

ScansSelect 1b24ba06553df0257f4d9604085d872d(1b24ba06553df0257f4d9604085d872d)

But the loki output is

image

Can someone please point me in the right direction?

Thanks

mishamyte avatar Nov 27 '23 18:11 mishamyte

I would like to take a look at the issue. @mishamyte it is assigned to you. I hope you don't mind.

px-mpavlovsky avatar Feb 12 '24 21:02 px-mpavlovsky

Hey @px-mpavlovsky!

Yeah, no problems. If you have an idea how it could be added to v8 without changing the minimum Serilog's version, I would be happy to see the request from you.

mishamyte avatar Feb 12 '24 23:02 mishamyte

Is there any progress on this? Would be really nice.

Usergitbit avatar Jul 01 '24 23:07 Usergitbit

Still in backlog 😞

mishamyte avatar Jul 04 '24 20:07 mishamyte