Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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 @@ -116,11 +116,14 @@ 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. :)

if (activityLink.Tags != null)
{
if (ConsoleTagTransformer.Instance.TryTransformTag(attribute, out var result))
foreach (var attribute in activityLink.Tags)
{
this.WriteLine($" {result}");
if (ConsoleTagTransformer.Instance.TryTransformTag(attribute, out var result))
{
this.WriteLine($" {result}");
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/OpenTelemetry.Tests/OpenTelemetry.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry\OpenTelemetry.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.InMemory\OpenTelemetry.Exporter.InMemory.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Console\OpenTelemetry.Exporter.Console.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
72 changes: 72 additions & 0 deletions test/OpenTelemetry.Tests/Shared/UnitTestEventListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// <copyright file="UnitTestEventListener.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>

namespace OpenTelemetry.Tests.Shared
{
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;

/// <summary>
/// This EventListener can be used to subscribe to a specific EventSourceName and EventLevel.
/// </summary>
public class UnitTestEventListener : EventListener
{
public List<EventWrittenEventArgs> CapturedEvents = new();

private readonly List<EventSource> subscribedEventSources = new();
private readonly Guid currentThreadActivityId = Guid.NewGuid();
private readonly string eventName;
private readonly EventLevel eventLevel;

public UnitTestEventListener(string eventSourceName, EventLevel eventLevel)
{
EventSource.SetCurrentThreadActivityId(this.currentThreadActivityId);
this.eventName = eventSourceName;
this.eventLevel = eventLevel;
}

public override void Dispose()
{
foreach (var eventSource in this.subscribedEventSources)
{
this.DisableEvents(eventSource);
}

base.Dispose();
GC.SuppressFinalize(this);
}

protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource?.Name == this.eventName)
{
this.subscribedEventSources.Add(eventSource);
this.EnableEvents(eventSource, this.eventLevel, EventKeywords.All);
}

base.OnEventSourceCreated(eventSource);
}

protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if (eventData.ActivityId == this.currentThreadActivityId)
{
this.CapturedEvents.Add(eventData);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// <copyright file="VerifyNoEventSourceErrorsLoggedTestAttribute.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>

namespace OpenTelemetry.Tests.Shared
{
using System.Diagnostics.Tracing;
using System.Linq;
using System.Reflection;
using Xunit;
using Xunit.Sdk;

/// <summary>
/// Can be used to assert that no Errors were logged to EventSource during a unit test.
/// </summary>
internal class VerifyNoEventSourceErrorsLoggedTestAttribute : BeforeAfterTestAttribute
{
private readonly UnitTestEventListener myTestEventListener;

public VerifyNoEventSourceErrorsLoggedTestAttribute(string eventSourceName)
{
this.myTestEventListener = new UnitTestEventListener(eventSourceName, EventLevel.Error);
}

public override void After(MethodInfo methodUnderTest)
{
if (this.myTestEventListener.CapturedEvents.Any())
{
var eventNames = string.Join(",", this.myTestEventListener.CapturedEvents.Select(x => x.EventName));

Assert.Fail($"EventSource Exception captured during test run: {eventNames}");
}
}
}
}
61 changes: 61 additions & 0 deletions test/OpenTelemetry.Tests/Trace/ConsoleExporterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// <copyright file="ConsoleExporterTest.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>

namespace OpenTelemetry.Tests.Trace
{
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using OpenTelemetry.Tests.Shared;
using OpenTelemetry.Trace;
using Xunit;

[VerifyNoEventSourceErrorsLoggedTest("OpenTelemetry-Sdk")]
public class ConsoleExporterTest
{
/// <summary>
/// Test case for https://github.com/open-telemetry/opentelemetry-dotnet/issues/3863.
/// </summary>
[Fact]
public void VerifyConsoleActivityExporterDoesntFailWithoutActivityLinkTags()
{
var exportedItems = new List<Activity>();

using var source = new ActivitySource("Testing");

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("Testing")
.AddConsoleExporter()
.AddInMemoryExporter(exportedItems)
.Build();

ActivityContext context;
using (var first = source.StartActivity("first"))
{
context = first!.Context;
}

var links = new[] { new ActivityLink(context) };
using (var secondActivity = source.StartActivity(ActivityKind.Internal, links: links, name: "Second"))
{
}

// Assert that an Activity was exported where ActivityLink.Tags == null.
var activity = exportedItems.First(x => x.DisplayName == "Second");
Assert.Null(activity.Links.First().Tags);
}
}
}