Skip to content

Commit c2a6313

Browse files
committed
add otel values
1 parent 6a38fdc commit c2a6313

File tree

5 files changed

+148
-7
lines changed

5 files changed

+148
-7
lines changed

.vscode/tasks.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/src/Honeycomb.Serilog.Sink/Honeycomb.Serilog.Sink.csproj",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/src/Honeycomb.Serilog.Sink/Honeycomb.Serilog.Sink.csproj",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"${workspaceFolder}/src/Honeycomb.Serilog.Sink/Honeycomb.Serilog.Sink.csproj",
36+
"/property:GenerateFullPaths=true",
37+
"/consoleloggerparameters:NoSummary"
38+
],
39+
"problemMatcher": "$msCompile"
40+
}
41+
]
42+
}

HoneycombSerilogSink.sln

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{87AFA633-E
2424
EndProject
2525
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Honeycomb.Serilog.Sink.Tests", "test\Honeycomb.Serilog.Sink.Tests\Honeycomb.Serilog.Sink.Tests.csproj", "{3153A916-94B4-418D-84BD-EB1649449CFF}"
2626
EndProject
27+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ci", "ci", "{F33664C2-6205-4DB6-B00F-0C18887E8272}"
28+
ProjectSection(SolutionItems) = preProject
29+
ci\templates\build-and-package.yml = ci\templates\build-and-package.yml
30+
EndProjectSection
31+
EndProject
2732
Global
2833
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2934
Debug|Any CPU = Debug|Any CPU

src/Honeycomb.Serilog.Sink/Formatters/RawJsonFormatter.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static void FormatContent(LogEvent logEvent, TextWriter output)
3333
output.Write($"{{\"time\":\"{logEvent.Timestamp:O}\",");
3434
output.Write("\"data\":{");
3535
output.Write($"\"level\":\"{logEvent.Level}\"");
36+
output.Write(",\"meta.annotation_type\":\"span_event\"");
3637
output.Write(",\"messageTemplate\":");
3738
JsonValueFormatter.WriteQuotedJsonString(logEvent.MessageTemplate.Text, output);
3839
if (logEvent.Exception != null)
@@ -63,12 +64,30 @@ private static void WriteProperties(IReadOnlyDictionary<string, LogEventProperty
6364
continue;
6465
}
6566
}
67+
if (property.Key.Equals("TraceId", StringComparison.OrdinalIgnoreCase))
68+
{
69+
output.Write(precedingDelimiter);
6670

67-
output.Write(precedingDelimiter);
71+
JsonValueFormatter.WriteQuotedJsonString("trace.trace_id", output);
72+
output.Write(':');
73+
ValueFormatter.Format(property.Value, output);
74+
}
75+
else if (property.Key.Equals("ParentId", StringComparison.OrdinalIgnoreCase))
76+
{
77+
output.Write(precedingDelimiter);
6878

69-
JsonValueFormatter.WriteQuotedJsonString(property.Key, output);
70-
output.Write(':');
71-
ValueFormatter.Format(property.Value, output);
79+
JsonValueFormatter.WriteQuotedJsonString("trace.parent_id", output);
80+
output.Write(':');
81+
ValueFormatter.Format(property.Value, output);
82+
}
83+
else
84+
{
85+
output.Write(precedingDelimiter);
86+
87+
JsonValueFormatter.WriteQuotedJsonString(property.Key, output);
88+
output.Write(':');
89+
ValueFormatter.Format(property.Value, output);
90+
}
7291
}
7392
}
7493
}

src/Honeycomb.Serilog.Sink/HoneycombSinkExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ namespace Honeycomb.Serilog.Sink
99
public static class HoneycombSinkExtensions
1010
{
1111
/// <param name="loggerConfiguration"></param>
12-
/// <param name="teamId">The name of the team to submit the events to</param>
12+
/// <param name="dataset">The name of the dataset where to send the events to</param>
1313
/// <param name="apiKey">The API key given in the Honeycomb ui</param>
1414
/// <param name="batchSizeLimit">The maximum number of events to include in a single batch.</param>
1515
/// <param name="period">The time to wait between checking for event batches.</param>
16+
/// <summary>See the official Honeycomb <a href="https://docs.honeycomb.io/api/events/">documentation</a> for more details.</summary>
1617
public static LoggerConfiguration HoneycombSink(this LoggerSinkConfiguration loggerConfiguration,
17-
string teamId,
18+
string dataset,
1819
string apiKey,
1920
int batchSizeLimit,
2021
TimeSpan period)
@@ -25,7 +26,7 @@ public static LoggerConfiguration HoneycombSink(this LoggerSinkConfiguration log
2526
Period = period
2627
};
2728

28-
return loggerConfiguration.HoneycombSink(teamId, apiKey, batchingOptions);
29+
return loggerConfiguration.HoneycombSink(dataset, apiKey, batchingOptions);
2930
}
3031

3132
public static LoggerConfiguration HoneycombSink(this LoggerSinkConfiguration loggerConfiguration,

test/Honeycomb.Serilog.Sink.Tests/HoneycombSerilogSinkTests.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,80 @@ public async Task Emit_GivenAMessageWithEmptyPropertyValue_SkipsSendingProperty(
224224
}
225225
}
226226

227+
[Fact]
228+
public async Task Emit_GivenAMessageWithTraceId_WritesItAsOTELStandard()
229+
{
230+
const string dataset = nameof(dataset);
231+
const string apiKey = nameof(apiKey);
232+
233+
HttpClientStub clientStub = A.HttpClient();
234+
235+
var sut = CreateSut(dataset, apiKey, clientStub);
236+
237+
var level = LogEventLevel.Information;
238+
239+
var property = 1;
240+
const string spanId = nameof(spanId);
241+
242+
var messageTemplateString = $"Testing message property {{{nameof(property)}}} {{TraceId}}";
243+
244+
var eventToSend = Some.LogEvent(level, messageTemplateString, property, spanId);
245+
246+
247+
await sut.EmitTestable(eventToSend);
248+
249+
var requestContent = clientStub.RequestContent;
250+
using (var document = JsonDocument.Parse(requestContent))
251+
using (new AssertionScope())
252+
{
253+
document.RootElement.ValueKind.Should().Be(JsonValueKind.Array);
254+
document.RootElement.GetArrayLength().Should().Be(1);
255+
JsonElement sentEvent = document.RootElement.EnumerateArray().Single();
256+
257+
sentEvent.GetProperty("time").GetDateTimeOffset().Should().Be(eventToSend.Timestamp);
258+
sentEvent.GetProperty("data").ValueKind.Should().Be(JsonValueKind.Object);
259+
sentEvent.GetProperty("data").GetProperty("trace.trace_id").ValueKind.Should().Be(JsonValueKind.String);
260+
sentEvent.GetProperty("data").GetProperty("trace.trace_id").GetString().Should().Be(spanId);
261+
}
262+
}
263+
264+
[Fact]
265+
public async Task Emit_GivenAMessageWithParentId_WritesItAsOTELStandard()
266+
{
267+
const string dataset = nameof(dataset);
268+
const string apiKey = nameof(apiKey);
269+
270+
HttpClientStub clientStub = A.HttpClient();
271+
272+
var sut = CreateSut(dataset, apiKey, clientStub);
273+
274+
var level = LogEventLevel.Information;
275+
276+
var property = 1;
277+
const string parentId = nameof(parentId);
278+
279+
var messageTemplateString = $"Testing message property {{{nameof(property)}}} {{ParentId}}";
280+
281+
var eventToSend = Some.LogEvent(level, messageTemplateString, property, parentId);
282+
283+
284+
await sut.EmitTestable(eventToSend);
285+
286+
var requestContent = clientStub.RequestContent;
287+
using (var document = JsonDocument.Parse(requestContent))
288+
using (new AssertionScope())
289+
{
290+
document.RootElement.ValueKind.Should().Be(JsonValueKind.Array);
291+
document.RootElement.GetArrayLength().Should().Be(1);
292+
JsonElement sentEvent = document.RootElement.EnumerateArray().Single();
293+
294+
sentEvent.GetProperty("time").GetDateTimeOffset().Should().Be(eventToSend.Timestamp);
295+
sentEvent.GetProperty("data").ValueKind.Should().Be(JsonValueKind.Object);
296+
sentEvent.GetProperty("data").GetProperty("trace.parent_id").ValueKind.Should().Be(JsonValueKind.String);
297+
sentEvent.GetProperty("data").GetProperty("trace.parent_id").GetString().Should().Be(parentId);
298+
}
299+
}
300+
227301
private HoneycombSerilogSinkStub CreateSut(string dataset, string apiKey, HttpClient client = null)
228302
{
229303
return new HoneycombSerilogSinkStub(client, dataset, apiKey);

0 commit comments

Comments
 (0)