Skip to content

Add support for Trace ID and Span ID being sent to Loki #296

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
14 changes: 14 additions & 0 deletions src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ private void GenerateEntry(

var logEvent = lokiLogEvent.LogEvent;
var timestamp = logEvent.Timestamp;
var traceId = logEvent.TraceId;
var spanId = logEvent.SpanId;

if (_useInternalTimestamp)
{
Expand All @@ -179,6 +181,18 @@ private void GenerateEntry(
timestamp = lokiLogEvent.InternalTimestamp;
}

if (traceId.HasValue)
{
logEvent.AddPropertyIfAbsent(
new LogEventProperty("TraceId", new ScalarValue(traceId)));
}

if (spanId.HasValue)
{
logEvent.AddPropertyIfAbsent(
new LogEventProperty("SpanId", new ScalarValue(spanId)));
}

formatter.Format(logEvent, buffer);

stream.AddEntry(timestamp, buffer.ToString().TrimEnd('\r', '\n'));
Expand Down
4 changes: 3 additions & 1 deletion src/Serilog.Sinks.Grafana.Loki/Models/LokiLogEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ internal LokiLogEvent CopyWithProperties(IEnumerable<KeyValuePair<string, LogEve
LogEvent.Level,
LogEvent.Exception,
LogEvent.MessageTemplate,
properties.Select(p => new LogEventProperty(p.Key, p.Value)));
properties.Select(p => new LogEventProperty(p.Key, p.Value)),
LogEvent.TraceId ?? default,
LogEvent.SpanId ?? default);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AssemblyName>Serilog.Sinks.Grafana.Loki</AssemblyName>
<Description>A Serilog sink sending log events to Grafana Loki</Description>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFrameworks>netstandard2.0;net5.0;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<!--SourceLink -->
Expand All @@ -24,8 +24,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="System.Text.Json" Version="4.7.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
Loading