Skip to content

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

Open
@mishamyte

Description

@mishamyte

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

internal LokiLogEvent CopyWithProperties(IEnumerable<KeyValuePair<string, LogEventPropertyValue>> properties)
{
LogEvent = new LogEvent(
LogEvent.Timestamp,
LogEvent.Level,
LogEvent.Exception,
LogEvent.MessageTemplate,
properties.Select(p => new LogEventProperty(p.Key, p.Value)));
return this;
}

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 #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

Metadata

Metadata

Assignees

Labels

breaking changeImplementation of that functionality will be a breaking changeenhancementNew feature or request

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions