serilog-contrib / serilog-sinks-grafana-loki

A Serilog sink sending log events to Grafana Loki
MIT License
199 stars 30 forks source link

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

Open mishamyte opened 12 months ago

mishamyte commented 12 months ago

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 ```csharp // 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: ```cs _log.Information("ScansSelect {trace}",Activity.Current?.TraceId); ``` The output to the console is as expected. ```txt ScansSelect 1b24ba06553df0257f4d9604085d872d(1b24ba06553df0257f4d9604085d872d) ``` But the loki output is ![image](https://github.com/serilog-contrib/serilog-sinks-grafana-loki/assets/643179/806fb6bb-9853-4047-8ce6-15dc4d549ccc) Can someone please point me in the right direction? Thanks
px-mpavlovsky commented 9 months ago

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

mishamyte commented 9 months ago

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.

Usergitbit commented 4 months ago

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

mishamyte commented 4 months ago

Still in backlog 😞