Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Bug fix, ConsoleExporter fails silently when exporting an `ActivityLink`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Reword this to something like:
Bug fix to prevent ConsoleExporter from failing when exporting an ActivityLink without tags.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without Tags.
([#3932](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3932))

## 1.4.0-beta.3

Released 2022-Nov-07
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public override ExportResult Export(in Batch<Activity> batch)
foreach (var activityLink in activity.Links)
{
this.WriteLine($" {activityLink.Context.TraceId} {activityLink.Context.SpanId}");
foreach (var attribute in activityLink.Tags)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TimothyMothra FYI you could re-write this as:

foreach (ref readonly var attribute in activityLink.EnumerateTagObjects())

More efficient and it will handle the null case for you 😄

Might be worth re-writing all of the foreach loops in here to use the newer pattern just to have a good reference doing it correctly. Follow-up thing if you are interested 😉

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take this as a follow up after this PR merges. :)

foreach (ref readonly var attribute in activityLink.EnumerateTagObjects())
{
if (ConsoleTagTransformer.Instance.TryTransformTag(attribute, out var result))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>
6 changes: 5 additions & 1 deletion test/OpenTelemetry.Tests/Shared/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ internal class Utils
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}";
}
}
}