Skip to content

Commit

Permalink
add otel values
Browse files Browse the repository at this point in the history
  • Loading branch information
evilpilaf committed Mar 9, 2021
1 parent 6a38fdc commit c2a6313
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 7 deletions.
42 changes: 42 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/src/Honeycomb.Serilog.Sink/Honeycomb.Serilog.Sink.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/src/Honeycomb.Serilog.Sink/Honeycomb.Serilog.Sink.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/src/Honeycomb.Serilog.Sink/Honeycomb.Serilog.Sink.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
5 changes: 5 additions & 0 deletions HoneycombSerilogSink.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{87AFA633-E
EndProject
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}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ci", "ci", "{F33664C2-6205-4DB6-B00F-0C18887E8272}"
ProjectSection(SolutionItems) = preProject
ci\templates\build-and-package.yml = ci\templates\build-and-package.yml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
27 changes: 23 additions & 4 deletions src/Honeycomb.Serilog.Sink/Formatters/RawJsonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static void FormatContent(LogEvent logEvent, TextWriter output)
output.Write($"{{\"time\":\"{logEvent.Timestamp:O}\",");
output.Write("\"data\":{");
output.Write($"\"level\":\"{logEvent.Level}\"");
output.Write(",\"meta.annotation_type\":\"span_event\"");
output.Write(",\"messageTemplate\":");
JsonValueFormatter.WriteQuotedJsonString(logEvent.MessageTemplate.Text, output);
if (logEvent.Exception != null)
Expand Down Expand Up @@ -63,12 +64,30 @@ private static void WriteProperties(IReadOnlyDictionary<string, LogEventProperty
continue;
}
}
if (property.Key.Equals("TraceId", StringComparison.OrdinalIgnoreCase))
{
output.Write(precedingDelimiter);

output.Write(precedingDelimiter);
JsonValueFormatter.WriteQuotedJsonString("trace.trace_id", output);
output.Write(':');
ValueFormatter.Format(property.Value, output);
}
else if (property.Key.Equals("ParentId", StringComparison.OrdinalIgnoreCase))
{
output.Write(precedingDelimiter);

JsonValueFormatter.WriteQuotedJsonString(property.Key, output);
output.Write(':');
ValueFormatter.Format(property.Value, output);
JsonValueFormatter.WriteQuotedJsonString("trace.parent_id", output);
output.Write(':');
ValueFormatter.Format(property.Value, output);
}
else
{
output.Write(precedingDelimiter);

JsonValueFormatter.WriteQuotedJsonString(property.Key, output);
output.Write(':');
ValueFormatter.Format(property.Value, output);
}
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/Honeycomb.Serilog.Sink/HoneycombSinkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ namespace Honeycomb.Serilog.Sink
public static class HoneycombSinkExtensions
{
/// <param name="loggerConfiguration"></param>
/// <param name="teamId">The name of the team to submit the events to</param>
/// <param name="dataset">The name of the dataset where to send the events to</param>
/// <param name="apiKey">The API key given in the Honeycomb ui</param>
/// <param name="batchSizeLimit">The maximum number of events to include in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <summary>See the official Honeycomb <a href="https://docs.honeycomb.io/api/events/">documentation</a> for more details.</summary>
public static LoggerConfiguration HoneycombSink(this LoggerSinkConfiguration loggerConfiguration,
string teamId,
string dataset,
string apiKey,
int batchSizeLimit,
TimeSpan period)
Expand All @@ -25,7 +26,7 @@ public static LoggerConfiguration HoneycombSink(this LoggerSinkConfiguration log
Period = period
};

return loggerConfiguration.HoneycombSink(teamId, apiKey, batchingOptions);
return loggerConfiguration.HoneycombSink(dataset, apiKey, batchingOptions);
}

public static LoggerConfiguration HoneycombSink(this LoggerSinkConfiguration loggerConfiguration,
Expand Down
74 changes: 74 additions & 0 deletions test/Honeycomb.Serilog.Sink.Tests/HoneycombSerilogSinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,80 @@ public async Task Emit_GivenAMessageWithEmptyPropertyValue_SkipsSendingProperty(
}
}

[Fact]
public async Task Emit_GivenAMessageWithTraceId_WritesItAsOTELStandard()
{
const string dataset = nameof(dataset);
const string apiKey = nameof(apiKey);

HttpClientStub clientStub = A.HttpClient();

var sut = CreateSut(dataset, apiKey, clientStub);

var level = LogEventLevel.Information;

var property = 1;
const string spanId = nameof(spanId);

var messageTemplateString = $"Testing message property {{{nameof(property)}}} {{TraceId}}";

var eventToSend = Some.LogEvent(level, messageTemplateString, property, spanId);


await sut.EmitTestable(eventToSend);

var requestContent = clientStub.RequestContent;
using (var document = JsonDocument.Parse(requestContent))
using (new AssertionScope())
{
document.RootElement.ValueKind.Should().Be(JsonValueKind.Array);
document.RootElement.GetArrayLength().Should().Be(1);
JsonElement sentEvent = document.RootElement.EnumerateArray().Single();

sentEvent.GetProperty("time").GetDateTimeOffset().Should().Be(eventToSend.Timestamp);
sentEvent.GetProperty("data").ValueKind.Should().Be(JsonValueKind.Object);
sentEvent.GetProperty("data").GetProperty("trace.trace_id").ValueKind.Should().Be(JsonValueKind.String);
sentEvent.GetProperty("data").GetProperty("trace.trace_id").GetString().Should().Be(spanId);
}
}

[Fact]
public async Task Emit_GivenAMessageWithParentId_WritesItAsOTELStandard()
{
const string dataset = nameof(dataset);
const string apiKey = nameof(apiKey);

HttpClientStub clientStub = A.HttpClient();

var sut = CreateSut(dataset, apiKey, clientStub);

var level = LogEventLevel.Information;

var property = 1;
const string parentId = nameof(parentId);

var messageTemplateString = $"Testing message property {{{nameof(property)}}} {{ParentId}}";

var eventToSend = Some.LogEvent(level, messageTemplateString, property, parentId);


await sut.EmitTestable(eventToSend);

var requestContent = clientStub.RequestContent;
using (var document = JsonDocument.Parse(requestContent))
using (new AssertionScope())
{
document.RootElement.ValueKind.Should().Be(JsonValueKind.Array);
document.RootElement.GetArrayLength().Should().Be(1);
JsonElement sentEvent = document.RootElement.EnumerateArray().Single();

sentEvent.GetProperty("time").GetDateTimeOffset().Should().Be(eventToSend.Timestamp);
sentEvent.GetProperty("data").ValueKind.Should().Be(JsonValueKind.Object);
sentEvent.GetProperty("data").GetProperty("trace.parent_id").ValueKind.Should().Be(JsonValueKind.String);
sentEvent.GetProperty("data").GetProperty("trace.parent_id").GetString().Should().Be(parentId);
}
}

private HoneycombSerilogSinkStub CreateSut(string dataset, string apiKey, HttpClient client = null)
{
return new HoneycombSerilogSinkStub(client, dataset, apiKey);
Expand Down

0 comments on commit c2a6313

Please sign in to comment.