Skip to content

Commit c0ecf81

Browse files
sgryphonrusscam
andauthored
Add trace context fields in ElasticsearchLogger (#115)
This commit adds trace context fields to log events when using ElasticsearchLogger. The trace context fields are extracted from the current System.Diagnostics.Activity if not null. When the Id format used is W3C, the Activity.TraceId and Activity.SpanId will be used to set Trace.Id and Span.Id, respectively. For any other id format, Trace.Id and Span.Id will be set from Activity.RootId and Activity.Id, respectively. When there is no current Activity but CorrelationManager has an ActivityId, this will be used for the Trace.Id. Co-authored-by: Russ Cam <[email protected]>
1 parent d1d5b77 commit c0ecf81

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/Elasticsearch.Extensions.Logging/ElasticsearchLogger.cs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,25 @@ private void AddTracing(LogEvent logEvent)
129129

130130
if (activity != null)
131131
{
132-
// Unique identifier of the trace.
133-
// A trace groups multiple events like transactions that belong together. For example, a user request handled by multiple inter-connected services.
134-
logEvent.Trace = new Trace() { Id = activity.RootId };
132+
if (activity.IdFormat == ActivityIdFormat.W3C)
133+
{
134+
// Unique identifier of the trace.
135+
// A trace groups multiple events like transactions that belong together. For example, a user request handled by multiple inter-connected services.
136+
logEvent.Trace = new Trace { Id = activity.TraceId.ToString() };
137+
logEvent.Span = new Span { Id = activity.SpanId.ToString() };
138+
}
139+
else
140+
{
141+
if (activity.RootId != null) logEvent.Trace = new Trace { Id = activity.RootId };
142+
if (activity.Id != null) logEvent.Span = new Span { Id = activity.Id };
143+
}
135144
}
136145
else
137146
{
138147
if (!System.Diagnostics.Trace.CorrelationManager.ActivityId.Equals(Guid.Empty))
139148
{
140149
logEvent.Trace =
141-
new Trace() { Id = System.Diagnostics.Trace.CorrelationManager.ActivityId.ToString() };
150+
new Trace { Id = System.Diagnostics.Trace.CorrelationManager.ActivityId.ToString() };
142151
}
143152
}
144153
}
@@ -157,7 +166,6 @@ Func<TState, Exception, string> formatter
157166
Event = new Event { Action = eventId.Name, Code = eventId.Id.ToString(), Severity = LogEventToEcsHelper.GetSeverity(logLevel) }
158167
};
159168

160-
161169
if (exception != null) AddException(exception, logEvent);
162170

163171
logEvent.Agent = LogEventToEcsHelper.GetAgent();
@@ -189,13 +197,24 @@ Func<TState, Exception, string> formatter
189197

190198
private bool CheckTracingValues(LogEvent logEvent, KeyValuePair<string, object> kvp)
191199
{
192-
if (kvp.Key == "trace.id")
200+
if (kvp.Key == "span.id")
193201
{
194202
var value = FormatValue(kvp.Value);
195203
if (!string.IsNullOrWhiteSpace(value))
196204
{
197-
if (logEvent.Trace == null) logEvent.Trace = new Trace();
205+
logEvent.Span ??= new Span();
206+
logEvent.Span.Id = value;
207+
}
198208

209+
return true;
210+
}
211+
212+
if (kvp.Key == "trace.id")
213+
{
214+
var value = FormatValue(kvp.Value);
215+
if (!string.IsNullOrWhiteSpace(value))
216+
{
217+
logEvent.Trace ??= new Trace();
199218
logEvent.Trace.Id = value;
200219
}
201220

@@ -207,8 +226,7 @@ private bool CheckTracingValues(LogEvent logEvent, KeyValuePair<string, object>
207226
var value = FormatValue(kvp.Value);
208227
if (!string.IsNullOrWhiteSpace(value))
209228
{
210-
if (logEvent.Transaction == null) logEvent.Transaction = new Transaction();
211-
229+
logEvent.Transaction ??= new Transaction();
212230
logEvent.Transaction.Id = value;
213231
}
214232

src/Elasticsearch.Extensions.Logging/LogEvent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public class LogEvent : Base
1717
private const string ScopesPropertyName = nameof(Scopes);
1818

1919
/// <summary>
20-
/// Custom field with the original message template, e.g. "Unexpected error processing customer {CustomerId}."
20+
/// Custom field with the original template used to generate the message, with token placeholders
21+
/// for inserted label values, e.g. "Unexpected error processing customer {CustomerId}."
2122
/// </summary>
2223
[DataMember(Name = MessageTemplatePropertyName)]
2324
public string? MessageTemplate { get; set; }

0 commit comments

Comments
 (0)