Skip to content

Commit ec85412

Browse files
committed
Add option to set template as body
1 parent 3442e3a commit ec85412

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

src/Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/IncludedData.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,20 @@ public enum IncludedData
5656
/// means <c>service.name</c> if not supplied, along with the <c>telemetry.sdk.*</c> group of attributes.
5757
/// </summary>
5858
SpecRequiredResourceAttributes = 16,
59+
60+
/// <summary>
61+
/// Include the log event's message template in the OTLP <c>body</c> instead of the rendered messsage. For
62+
/// example, the string <c>Hello {Name}!</c>.
63+
/// </summary>
64+
/// <remarks>
65+
/// Note: It is often desirable to remove <see cref="IncludedData.MessageTemplateTextAttribute"/> when using
66+
/// <see cref="IncludedData.TemplateBody"/> but otherwise use defaults.
67+
/// <code>
68+
/// .WriteTo.OpenTelemetry(options =>
69+
/// {
70+
/// options.IncludedData = (options.IncludedData | IncludedData.TemplateBody) &amp; ~IncludedData.MessageTemplateTextAttribute;
71+
/// })
72+
/// </code>
73+
/// </remarks>
74+
TemplateBody = 32
5975
}

src/Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/LogRecordBuilder.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,33 @@ public static LogRecord ToLogRecord(LogEvent logEvent, IFormatProvider? formatPr
2828

2929
ProcessProperties(logRecord, logEvent);
3030
ProcessTimestamp(logRecord, logEvent);
31-
ProcessMessage(logRecord, logEvent, formatProvider);
31+
ProcessMessage(logRecord, logEvent, includedFields, formatProvider);
3232
ProcessLevel(logRecord, logEvent);
3333
ProcessException(logRecord, logEvent);
3434
ProcessIncludedFields(logRecord, logEvent, includedFields, activityContextCollector);
3535

3636
return logRecord;
3737
}
3838

39-
public static void ProcessMessage(LogRecord logRecord, LogEvent logEvent, IFormatProvider? formatProvider)
39+
public static void ProcessMessage(LogRecord logRecord, LogEvent logEvent, IncludedData includedFields, IFormatProvider? formatProvider)
4040
{
4141
var renderedMessage = CleanMessageTemplateFormatter.Format(logEvent.MessageTemplate, logEvent.Properties, formatProvider);
42-
if (renderedMessage.Trim() != "")
42+
43+
if (!includedFields.HasFlag(IncludedData.TemplateBody) && renderedMessage.Trim() != "")
4344
{
4445
logRecord.Body = new AnyValue
4546
{
4647
StringValue = renderedMessage
4748
};
4849
}
50+
51+
if (includedFields.HasFlag(IncludedData.TemplateBody) && logEvent.MessageTemplate.Text.Trim() != "")
52+
{
53+
logRecord.Body = new AnyValue
54+
{
55+
StringValue = logEvent.MessageTemplate.Text
56+
};
57+
}
4958
}
5059

5160
public static void ProcessLevel(LogRecord logRecord, LogEvent logEvent)

src/Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/OpenTelemetrySinkOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class OpenTelemetrySinkOptions
2626
internal const string DefaultEndpoint = "http://localhost:4317";
2727
internal const OtlpProtocol DefaultProtocol = OtlpProtocol.Grpc;
2828

29-
const IncludedData DefaultIncludedData = IncludedData.MessageTemplateTextAttribute |
29+
internal const IncludedData DefaultIncludedData = IncludedData.MessageTemplateTextAttribute |
3030
IncludedData.TraceIdField | IncludedData.SpanIdField |
3131
IncludedData.SpecRequiredResourceAttributes;
3232

test/Serilog.Sinks.OpenTelemetry.Tests/LogRecordBuilderTests.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public void TestProcessMessage()
2929
{
3030
var logRecord = new LogRecord();
3131

32-
LogRecordBuilder.ProcessMessage(logRecord, Some.SerilogEvent(messageTemplate: ""), null);
32+
LogRecordBuilder.ProcessMessage(logRecord, Some.SerilogEvent(messageTemplate: ""), OpenTelemetrySinkOptions.DefaultIncludedData, null);
3333
Assert.Null(logRecord.Body);
3434

35-
LogRecordBuilder.ProcessMessage(logRecord, Some.SerilogEvent(messageTemplate: "\t\f "), null);
35+
LogRecordBuilder.ProcessMessage(logRecord, Some.SerilogEvent(messageTemplate: "\t\f "), OpenTelemetrySinkOptions.DefaultIncludedData, null);
3636
Assert.Null(logRecord.Body);
3737

3838
const string message = "log message";
39-
LogRecordBuilder.ProcessMessage(logRecord, Some.SerilogEvent(messageTemplate: message), null);
39+
LogRecordBuilder.ProcessMessage(logRecord, Some.SerilogEvent(messageTemplate: message), OpenTelemetrySinkOptions.DefaultIncludedData, null);
4040
Assert.NotNull(logRecord.Body);
4141
Assert.Equal(message, logRecord.Body.StringValue);
4242
}
@@ -180,4 +180,12 @@ public void IncludeTraceIdAndSpanId()
180180
Assert.Equal(logRecord.TraceId, PrimitiveConversions.ToOpenTelemetryTraceId(Activity.Current.TraceId.ToHexString()));
181181
Assert.Equal(logRecord.SpanId, PrimitiveConversions.ToOpenTelemetrySpanId(Activity.Current.SpanId.ToHexString()));
182182
}
183+
184+
[Fact]
185+
public void IncludeTemplateBody()
186+
{
187+
var logRecord = LogRecordBuilder.ToLogRecord(Some.SerilogEvent(), null, IncludedData.TemplateBody, new());
188+
Assert.NotNull(logRecord.Body);
189+
Assert.Equal(Some.TestMessageTemplate, logRecord.Body.StringValue);
190+
}
183191
}

test/Serilog.Sinks.OpenTelemetry.Tests/PublicApiVisibilityTests.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace Serilog.Sinks.OpenTelemetry
2424
TraceIdField = 4,
2525
SpanIdField = 8,
2626
SpecRequiredResourceAttributes = 16,
27+
TemplateBody = 32,
2728
}
2829
public class OpenTelemetrySinkOptions
2930
{

0 commit comments

Comments
 (0)