Skip to content
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

Serialize ContextData from LogEventWireModel #1299

Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -24,6 +24,7 @@ public class LogEventWireModelCollectionJsonConverter : JsonConverter<LogEventWi
private const string ErrorStack = "error.stack";
private const string ErrorMessage = "error.message";
private const string ErrorClass = "error.class";
private const string Context = "context";

public override LogEventWireModelCollection ReadJson(JsonReader reader, Type objectType, LogEventWireModelCollection existingValue, bool hasExistingValue, JsonSerializer serializer)
{
Expand Down Expand Up @@ -100,6 +101,20 @@ private static void WriteJsonImpl(JsonWriter jsonWriter, LogEventWireModelCollec
jsonWriter.WriteValue(logEvent.TraceId);
}

if (logEvent.ContextData?.Count > 0)
{
jsonWriter.WritePropertyName(Attributes);
vuqtran88 marked this conversation as resolved.
Show resolved Hide resolved
jsonWriter.WriteStartObject();

foreach (var item in logEvent.ContextData)
{
jsonWriter.WritePropertyName(Context + "." + item.Key);
jsonWriter.WriteValue(item.Value);
}

jsonWriter.WriteEndObject();
}

jsonWriter.WriteEndObject();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using NUnit.Framework;

namespace NewRelic.Agent.Core.WireModels
Expand Down Expand Up @@ -40,5 +41,33 @@ public void ConstructorTest()
Assert.AreEqual("TestTraceId", loggingEvent.TraceId);
Assert.AreEqual(testContextData, loggingEvent.ContextData);
}

[Test]
public void LogEventWireModelCollection_SerializesCorrectly()
{
// Arrange
const string expected = @"{""common"":{""attributes"":{""entity.name"":""MyApplicationName"",""entity.guid"":""a4edc699-4c47-4599-90b5-3c4c55512e96"",""hostname"":""TestHostname""}},""logs"":[{""timestamp"":1,""message"":""TestMessage1"",""level"":""TestLevel"",""span.id"":""TestSpanId1"",""trace.id"":""TestTraceId1"",""attributes"":{""context.key1"":""value1"",""context.key2"":1}},{""timestamp"":1,""message"":""TestMessage2"",""level"":""TestLevel"",""span.id"":""TestSpanId2"",""trace.id"":""TestTraceId2""},{""timestamp"":1,""message"":""TestMessage3"",""level"":""TestLevel"",""span.id"":""TestSpanId3"",""trace.id"":""TestTraceId3""}]}";

var entityName = "MyApplicationName";
var entityGuid = "a4edc699-4c47-4599-90b5-3c4c55512e96";
var hostname = "TestHostname";

var testContextData = new Dictionary<string, object>() { { "key1", "value1" }, { "key2", 1 } };

var loggingEvents = new List<LogEventWireModel>
{
new LogEventWireModel(1, "TestMessage1", "TestLevel", "TestSpanId1", "TestTraceId1", testContextData),
new LogEventWireModel(1, "TestMessage2", "TestLevel", "TestSpanId2", "TestTraceId2", null),
new LogEventWireModel(1, "TestMessage3", "TestLevel", "TestSpanId3", "TestTraceId3", new Dictionary<string, object>())
};

var objectUnderTest = new LogEventWireModelCollection(entityName, entityGuid, hostname, loggingEvents);

// Act
var actual = JsonConvert.SerializeObject(objectUnderTest);

// Assert
Assert.AreEqual(expected, actual);
}
}
}