From 0adee5aeccf67cecc4064af3f0037cfc864131bb Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Tue, 6 Sep 2022 19:51:35 +0200 Subject: [PATCH] Fix null tags. (#566) * Fix null tags. --- .../Implementation/ActivityExtensions.cs | 6 ++++-- .../StackdriverTraceExporter.cs | 6 +++++- .../OpenTelemetry.Exporter.Stackdriver.Tests.csproj | 2 +- .../StackdriverExporterTests.cs | 7 ++++--- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/OpenTelemetry.Exporter.Stackdriver/Implementation/ActivityExtensions.cs b/src/OpenTelemetry.Exporter.Stackdriver/Implementation/ActivityExtensions.cs index aef78c1e37..66cc89a6f0 100644 --- a/src/OpenTelemetry.Exporter.Stackdriver/Implementation/ActivityExtensions.cs +++ b/src/OpenTelemetry.Exporter.Stackdriver/Implementation/ActivityExtensions.cs @@ -120,7 +120,7 @@ public static Span.Types.Link ToLink(this ActivityLink link) { link.Tags.ToDictionary( att => att.Key, - att => att.Value.ToAttributeValue()), + att => att.Value?.ToAttributeValue()), }, }; } @@ -128,7 +128,7 @@ public static Span.Types.Link ToLink(this ActivityLink link) return ret; } - public static AttributeValue ToAttributeValue(this object av) + public static AttributeValue ToAttributeValue(this object? av) { switch (av) { @@ -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() { diff --git a/src/OpenTelemetry.Exporter.Stackdriver/StackdriverTraceExporter.cs b/src/OpenTelemetry.Exporter.Stackdriver/StackdriverTraceExporter.cs index 69df0cfd40..222c5c12f0 100644 --- a/src/OpenTelemetry.Exporter.Stackdriver/StackdriverTraceExporter.cs +++ b/src/OpenTelemetry.Exporter.Stackdriver/StackdriverTraceExporter.cs @@ -105,7 +105,11 @@ public override ExportResult Export(in Batch 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 diff --git a/test/OpenTelemetry.Exporter.Stackdriver.Tests/OpenTelemetry.Exporter.Stackdriver.Tests.csproj b/test/OpenTelemetry.Exporter.Stackdriver.Tests/OpenTelemetry.Exporter.Stackdriver.Tests.csproj index b641dccca8..75e8545346 100644 --- a/test/OpenTelemetry.Exporter.Stackdriver.Tests/OpenTelemetry.Exporter.Stackdriver.Tests.csproj +++ b/test/OpenTelemetry.Exporter.Stackdriver.Tests/OpenTelemetry.Exporter.Stackdriver.Tests.csproj @@ -1,4 +1,4 @@ - + Unit test project for Stackdriver Exporter for OpenTelemetry netcoreapp3.1;net6.0 diff --git a/test/OpenTelemetry.Exporter.Stackdriver.Tests/StackdriverExporterTests.cs b/test/OpenTelemetry.Exporter.Stackdriver.Tests/StackdriverExporterTests.cs index 57c9e58b79..509d831452 100644 --- a/test/OpenTelemetry.Exporter.Stackdriver.Tests/StackdriverExporterTests.cs +++ b/test/OpenTelemetry.Exporter.Stackdriver.Tests/StackdriverExporterTests.cs @@ -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); } @@ -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); } @@ -192,6 +192,7 @@ internal static Activity CreateTestActivity( { "doubleKey", 1D }, { "doubleKey2", 1F }, { "boolKey", true }, + { "nullKey", null }, }; if (additionalAttributes != null) { @@ -224,7 +225,7 @@ internal static Activity CreateTestActivity( var activitySource = new ActivitySource(nameof(CreateTestActivity)); var tags = setAttributes ? - attributes.Select(kvp => new KeyValuePair(kvp.Key, kvp.Value.ToString())) + attributes.Where(x => x.Value != null).Select(kvp => new KeyValuePair(kvp.Key, kvp.Value.ToString())) : null; var links = addLinks ? new[]