Skip to content

Commit

Permalink
Fixed a couple bugs in Jaeger Exporter. (#1310)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Sep 26, 2020
1 parent 6529c43 commit 6633dc8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,42 @@ public static long ToEpochMicroseconds(this DateTimeOffset timestamp)
return microseconds - UnixEpochMicroseconds;
}

private static void ProcessJaegerTagArray(ref PooledList<JaegerTag> tags, KeyValuePair<string, object> activityTag)
{
if (activityTag.Value is int[] intArray)
{
foreach (var item in intArray)
{
JaegerTag jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.LONG, vLong: Convert.ToInt64(item));
PooledList<JaegerTag>.Add(ref tags, jaegerTag);
}
}
else if (activityTag.Value is string[] stringArray)
{
foreach (var item in stringArray)
{
JaegerTag jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.STRING, vStr: item);
PooledList<JaegerTag>.Add(ref tags, jaegerTag);
}
}
else if (activityTag.Value is bool[] boolArray)
{
foreach (var item in boolArray)
{
JaegerTag jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.BOOL, vBool: item);
PooledList<JaegerTag>.Add(ref tags, jaegerTag);
}
}
else if (activityTag.Value is double[] doubleArray)
{
foreach (var item in doubleArray)
{
JaegerTag jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.DOUBLE, vDouble: item);
PooledList<JaegerTag>.Add(ref tags, jaegerTag);
}
}
}

private static void ProcessJaegerTag(ref TagState state, string key, JaegerTag jaegerTag)
{
if (jaegerTag.VStr != null)
Expand Down Expand Up @@ -315,7 +351,15 @@ private static bool ProcessActivityEvent(ref PooledListState<JaegerLog> state, A

private static bool ProcessTag(ref PooledListState<JaegerTag> state, KeyValuePair<string, object> attribute)
{
PooledList<JaegerTag>.Add(ref state.List, attribute.ToJaegerTag());
if (attribute.Value is Array)
{
ProcessJaegerTagArray(ref state.List, attribute);
}
else if (attribute.Value != null)
{
PooledList<JaegerTag>.Add(ref state.List, attribute.ToJaegerTag());
}

return true;
}

Expand All @@ -335,43 +379,13 @@ private struct TagState : IActivityTagEnumerator

public bool ForEach(KeyValuePair<string, object> activityTag)
{
JaegerTag jaegerTag;
if (activityTag.Value is int[] intArray)
{
foreach (var item in intArray)
{
jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.LONG, vLong: Convert.ToInt64(item));
ProcessJaegerTag(ref this, activityTag.Key, jaegerTag);
}
}
else if (activityTag.Value is string[] stringArray)
{
foreach (var item in stringArray)
{
jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.STRING, vStr: item);
ProcessJaegerTag(ref this, activityTag.Key, jaegerTag);
}
}
else if (activityTag.Value is bool[] boolArray)
{
foreach (var item in boolArray)
{
jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.BOOL, vBool: item);
ProcessJaegerTag(ref this, activityTag.Key, jaegerTag);
}
}
else if (activityTag.Value is double[] doubleArray)
if (activityTag.Value is Array)
{
foreach (var item in doubleArray)
{
jaegerTag = new JaegerTag(activityTag.Key, JaegerTagType.DOUBLE, vDouble: item);
ProcessJaegerTag(ref this, activityTag.Key, jaegerTag);
}
ProcessJaegerTagArray(ref this.Tags, activityTag);
}
else
else if (activityTag.Value != null)
{
jaegerTag = activityTag.ToJaegerTag();
ProcessJaegerTag(ref this, activityTag.Key, jaegerTag);
ProcessJaegerTag(ref this, activityTag.Key, activityTag.ToJaegerTag());
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,18 @@ public void JaegerActivityConverterTest_ConvertActivityToJaegerSpan_AllPropertie
var logs = jaegerSpan.Logs.ToArray();
var jaegerLog = logs[0];
Assert.Equal(activity.Events.First().Timestamp.ToEpochMicroseconds(), jaegerLog.Timestamp);
Assert.Equal(2, jaegerLog.Fields.Count());
Assert.Equal(4, jaegerLog.Fields.Count());
var eventFields = jaegerLog.Fields.ToArray();
var eventField = eventFields[0];
Assert.Equal("key", eventField.Key);
Assert.Equal("value", eventField.VStr);
eventField = eventFields[1];
Assert.Equal("string_array", eventField.Key);
Assert.Equal("a", eventField.VStr);
eventField = eventFields[2];
Assert.Equal("string_array", eventField.Key);
Assert.Equal("b", eventField.VStr);
eventField = eventFields[3];
Assert.Equal("message", eventField.Key);
Assert.Equal("Event1", eventField.VStr);

Expand Down Expand Up @@ -160,12 +166,12 @@ public void JaegerActivityConverterTest_ConvertActivityToJaegerSpan_NoAttributes
var logs = jaegerSpan.Logs.ToArray();
var jaegerLog = logs[0];
Assert.Equal(activity.Events.First().Timestamp.ToEpochMicroseconds(), jaegerLog.Timestamp);
Assert.Equal(2, jaegerLog.Fields.Count());
Assert.Equal(4, jaegerLog.Fields.Count());
var eventFields = jaegerLog.Fields.ToArray();
var eventField = eventFields[0];
Assert.Equal("key", eventField.Key);
Assert.Equal("value", eventField.VStr);
eventField = eventFields[1];
eventField = eventFields[3];
Assert.Equal("message", eventField.Key);
Assert.Equal("Event1", eventField.VStr);

Expand Down Expand Up @@ -325,12 +331,12 @@ public void JaegerActivityConverterTest_ConvertActivityToJaegerSpan_NoLinks()
var logs = jaegerSpan.Logs.ToArray();
var jaegerLog = logs[0];
Assert.Equal(activity.Events.First().Timestamp.ToEpochMicroseconds(), jaegerLog.Timestamp);
Assert.Equal(2, jaegerLog.Fields.Count());
Assert.Equal(4, jaegerLog.Fields.Count());
var eventFields = jaegerLog.Fields.ToArray();
var eventField = eventFields[0];
Assert.Equal("key", eventField.Key);
Assert.Equal("value", eventField.VStr);
eventField = eventFields[1];
eventField = eventFields[3];
Assert.Equal("message", eventField.Key);
Assert.Equal("Event1", eventField.VStr);
Assert.Equal(activity.Events.First().Timestamp.ToEpochMicroseconds(), jaegerLog.Timestamp);
Expand Down Expand Up @@ -413,6 +419,19 @@ public void JaegerActivityConverterTest_GenerateJaegerSpan_RemoteEndpointResolut
Assert.Equal(testCase.ExpectedResult, tag.VStr);
}

[Fact]
public void JaegerActivityConverterTest_NullTagValueTest()
{
// Arrange
var activity = CreateTestActivity(additionalAttributes: new Dictionary<string, object> { ["nullTag"] = null });

// Act
var jaegerSpan = activity.ToJaegerSpan();

// Assert
Assert.DoesNotContain(jaegerSpan.Tags, t => t.Key == "nullTag");
}

internal static Activity CreateTestActivity(
bool setAttributes = true,
Dictionary<string, object> additionalAttributes = null,
Expand Down Expand Up @@ -457,6 +476,7 @@ internal static Activity CreateTestActivity(
new ActivityTagsCollection(new Dictionary<string, object>
{
{ "key", "value" },
{ "string_array", new string[] { "a", "b" } },
})),
new ActivityEvent(
"Event2",
Expand Down

0 comments on commit 6633dc8

Please sign in to comment.