Fix ConsoleExporter fails silently when exporting an `ActivityLink` without Tags. (#3932)

This commit is contained in:
Timothy Mothra 2022-12-07 14:48:27 -08:00 committed by GitHub
parent b3a0a142d3
commit 840434981b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 2 deletions

View File

@ -2,6 +2,10 @@
## Unreleased
* Bug fix, ConsoleExporter fails silently when exporting an `ActivityLink`
without Tags.
([#3932](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3932))
## 1.4.0-beta.3
Released 2022-Nov-07

View File

@ -115,7 +115,7 @@ namespace OpenTelemetry.Exporter
foreach (var activityLink in activity.Links)
{
this.WriteLine($" {activityLink.Context.TraceId} {activityLink.Context.SpanId}");
foreach (var attribute in activityLink.Tags)
foreach (ref readonly var attribute in activityLink.EnumerateTagObjects())
{
if (ConsoleTagTransformer.Instance.TryTransformTag(attribute, out var result))
{

View File

@ -0,0 +1,61 @@
// <copyright file="ConsoleActivityExporterTest.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
using System.Diagnostics;
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
using Xunit;
namespace OpenTelemetry.Exporter.Console.Tests;
public class ConsoleActivityExporterTest
{
[Fact]
public void VerifyConsoleActivityExporterDoesntFailWithoutActivityLinkTags()
{
var activitySourceName = Utils.GetCurrentMethodName();
using var activitySource = new ActivitySource(activitySourceName);
var exportedItems = new List<Activity>();
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource(activitySourceName)
.AddInMemoryExporter(exportedItems)
.Build();
ActivityContext context;
using (var first = activitySource.StartActivity("first"))
{
context = first!.Context;
}
exportedItems.Clear();
var links = new[] { new ActivityLink(context) };
using (var secondActivity = activitySource.StartActivity(ActivityKind.Internal, links: links, name: "Second"))
{
}
// Assert that an Activity was exported where ActivityLink.Tags == null.
var activity = exportedItems[0];
Assert.Equal("Second", activity.DisplayName);
Assert.Null(activity.Links.First().Tags);
// Test that the ConsoleExporter correctly handles an Activity without Tags.
using var consoleExporter = new ConsoleActivityExporter(new ConsoleExporterOptions());
Assert.Equal(ExportResult.Success, consoleExporter.Export(new Batch<Activity>(new[] { activity }, 1)));
}
}

View File

@ -19,6 +19,9 @@
<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Console\OpenTelemetry.Exporter.Console.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.InMemory\OpenTelemetry.Exporter.InMemory.csproj" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\Utils.cs" Link="Includes\Utils.cs" />
</ItemGroup>
</Project>

View File

@ -25,7 +25,11 @@ namespace OpenTelemetry.Tests
public static string GetCurrentMethodName()
{
var method = new StackFrame(1).GetMethod();
return $"{method.DeclaringType.FullName}.{method.Name}";
Debug.Assert(method != null, "Failed to get Method from the executing stack.");
Debug.Assert(method!.DeclaringType != null, "DeclaringType is not expected to be null.");
return $"{method.DeclaringType!.FullName}.{method.Name}";
}
}
}