Skip to content

Commit

Permalink
Fix null tags. (#566)
Browse files Browse the repository at this point in the history
* Fix null tags.
  • Loading branch information
SebastianStehle authored Sep 6, 2022
1 parent bf01da1 commit 0adee5a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ public static Span.Types.Link ToLink(this ActivityLink link)
{
link.Tags.ToDictionary(
att => att.Key,
att => att.Value.ToAttributeValue()),
att => att.Value?.ToAttributeValue()),
},
};
}

return ret;
}

public static AttributeValue ToAttributeValue(this object av)
public static AttributeValue ToAttributeValue(this object? av)
{
switch (av)
{
Expand All @@ -146,6 +146,8 @@ public static AttributeValue ToAttributeValue(this object av)
{
StringValue = new TruncatableString() { Value = d.ToString() },
};
case null:
return new AttributeValue();
default:
return new AttributeValue()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ public override ExportResult Export(in Batch<Activity> batchActivity)

foreach (var activity in batchActivity)
{
batchSpansRequest.Spans.Add(activity.ToSpan(this.googleCloudProjectId.ProjectId));
// It should never happen that the time has no correct kind, only if OpenTelemetry is used incorrectly.
if (activity.StartTimeUtc.Kind == DateTimeKind.Utc)
{
batchSpansRequest.Spans.Add(activity.ToSpan(this.googleCloudProjectId.ProjectId));
}
}

// avoid cancelling here: this is no return point: if we reached this point
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Unit test project for Stackdriver Exporter for OpenTelemetry</Description>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void StackdriverExporter_TraceClientThrows_ExportResultFailure()

for (int i = 0; i < 10; i++)
{
using Activity activity = source.StartActivity("Test Activity");
using Activity activity = CreateTestActivity();
processor.OnEnd(activity);
}

Expand Down Expand Up @@ -147,7 +147,7 @@ public void StackdriverExporter_TraceClientDoesNotTrow_ExportResultSuccess()

for (int i = 0; i < 10; i++)
{
using Activity activity = source.StartActivity("Test Activity");
using Activity activity = CreateTestActivity();
processor.OnEnd(activity);
}

Expand Down Expand Up @@ -192,6 +192,7 @@ internal static Activity CreateTestActivity(
{ "doubleKey", 1D },
{ "doubleKey2", 1F },
{ "boolKey", true },
{ "nullKey", null },
};
if (additionalAttributes != null)
{
Expand Down Expand Up @@ -224,7 +225,7 @@ internal static Activity CreateTestActivity(
var activitySource = new ActivitySource(nameof(CreateTestActivity));

var tags = setAttributes ?
attributes.Select(kvp => new KeyValuePair<string, object>(kvp.Key, kvp.Value.ToString()))
attributes.Where(x => x.Value != null).Select(kvp => new KeyValuePair<string, object>(kvp.Key, kvp.Value.ToString()))
: null;
var links = addLinks ?
new[]
Expand Down

0 comments on commit 0adee5a

Please sign in to comment.