Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mishamyte opened this issue Nov 27, 2023 Discussed in #232 · 2 comments
Open

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

mishamyte opened this issue Nov 27, 2023 Discussed in #232 · 2 comments
Assignees
Labels
breaking change Implementation of that functionality will be a breaking change enhancement New feature or request
Milestone

Comments

@mishamyte
Copy link
Member

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

@mishamyte mishamyte added the enhancement New feature or request label Nov 27, 2023
@mishamyte mishamyte added this to the v9.0.0 milestone Nov 27, 2023
@mishamyte mishamyte self-assigned this Nov 27, 2023
@mishamyte mishamyte added the breaking change Implementation of that functionality will be a breaking change label Nov 27, 2023
@px-mpavlovsky
Copy link

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

@mishamyte
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Implementation of that functionality will be a breaking change enhancement New feature or request
Development

No branches or pull requests

2 participants