Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of RecordException in TelemetrySpan #1116

Merged
merged 11 commits into from
Aug 21, 2020
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Added `RecordException` in `TelemetrySpan`
([#1116](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1116))
* `PropagationContext` is now used instead of `ActivityContext` in the
`ITextFormat` API
([#1048](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1048))
Expand Down
8 changes: 7 additions & 1 deletion src/OpenTelemetry.Api/Trace/SemanticConventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ namespace OpenTelemetry.Trace
internal static class SemanticConventions
{
// The set of constants matches the specification as of this commit.
// https://github.com/open-telemetry/opentelemetry-specification/tree/709293fe132709705f0e0dd4252992e87a6ec899/specification/trace/semantic_conventions
// https://github.com/open-telemetry/opentelemetry-specification/tree/master/specification/trace/semantic_conventions
Copy link
Member

Choose a reason for hiding this comment

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

nit: The comment above says this matches a particular commit. So lets link to that particular commit, not master as master changes.

// https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/exceptions.md
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public const string AttributeServiceName = "service.name";
public const string AttributeServiceNamespace = "service.namespace";
Expand Down Expand Up @@ -147,6 +148,11 @@ internal static class SemanticConventions
public const string AttributeMessagingPayloadSize = "messaging.message_payload_size_bytes";
public const string AttributeMessagingPayloadCompressedSize = "messaging.message_payload_compressed_size_bytes";
public const string AttributeMessagingOperation = "messaging.operation";

public const string AttributeExceptionEventName = "exception";
public const string AttributeExceptionType = "exception.type";
public const string AttributeExceptionMessage = "exception.message";
public const string AttributeExceptionStacktrace = "exception.stacktrace";
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
}
}
51 changes: 51 additions & 0 deletions src/OpenTelemetry.Api/Trace/TelemetrySpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Trace
{
Expand Down Expand Up @@ -303,6 +304,56 @@ public TelemetrySpan AddBaggage(string key, string value)
return this;
}

/// <summary>
/// Record Exception.
/// </summary>
/// <param name="ex">Exception to be recorded.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan RecordException(Exception ex)
{
if (ex == null)
{
return this;
}
eddynaka marked this conversation as resolved.
Show resolved Hide resolved

return this.RecordException(ex.GetType().Name, ex.Message, ex.ToInvariantString());
}

/// <summary>
/// Record Exception.
/// </summary>
/// <param name="type">Type of the exception to be recorded.</param>
/// <param name="message">Message of the exception to be recorded.</param>
/// <param name="stacktrace">Stacktrace of the exception to be recorded.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
public TelemetrySpan RecordException(string type, string message, string stacktrace)
{
Dictionary<string, object> attributes = new Dictionary<string, object>();

if (!string.IsNullOrWhiteSpace(type))
{
attributes.Add(SemanticConventions.AttributeExceptionType, type);
}

if (!string.IsNullOrWhiteSpace(stacktrace))
{
attributes.Add(SemanticConventions.AttributeExceptionStacktrace, stacktrace);
}

if (!string.IsNullOrWhiteSpace(message))
{
attributes.Add(SemanticConventions.AttributeExceptionMessage, message);
}

if (attributes.Count != 0)
{
this.AddEvent(SemanticConventions.AttributeExceptionEventName, attributes);
}

return this;
}

/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
Expand Down
72 changes: 72 additions & 0 deletions test/OpenTelemetry.Tests/Trace/TelemetrySpanTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// <copyright file="TelemetrySpanTest.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;
using System.Diagnostics;
using System.Linq;
using OpenTelemetry.Trace;
using Xunit;

namespace OpenTelemetry.Tests.Trace
{
public class TelemetrySpanTest
{
[Fact]
public void CheckRecordExceptionData()
{
string message = "message";

using Activity activity = new Activity("exception-test");
using TelemetrySpan telemetrySpan = new TelemetrySpan(activity);
telemetrySpan.RecordException(new ArgumentNullException(message, new Exception("new-exception")));
Assert.Single(activity.Events);

var @event = telemetrySpan.Activity.Events.FirstOrDefault(q => q.Name == SemanticConventions.AttributeExceptionEventName);
Assert.Equal(message, @event.Tags.FirstOrDefault(t => t.Key == SemanticConventions.AttributeExceptionMessage).Value);
Assert.Equal(typeof(ArgumentNullException).Name, @event.Tags.FirstOrDefault(t => t.Key == SemanticConventions.AttributeExceptionType).Value);
}

[Fact]
public void CheckRecordExceptionData2()
{
string type = "ArgumentNullException";
string message = "message";
string stack = "stack";

using Activity activity = new Activity("exception-test");
using TelemetrySpan telemetrySpan = new TelemetrySpan(activity);
telemetrySpan.RecordException(type, message, stack);
Assert.Single(activity.Events);

var @event = telemetrySpan.Activity.Events.FirstOrDefault(q => q.Name == SemanticConventions.AttributeExceptionEventName);
Assert.Equal(message, @event.Tags.FirstOrDefault(t => t.Key == SemanticConventions.AttributeExceptionMessage).Value);
Assert.Equal(type, @event.Tags.FirstOrDefault(t => t.Key == SemanticConventions.AttributeExceptionType).Value);
Assert.Equal(stack, @event.Tags.FirstOrDefault(t => t.Key == SemanticConventions.AttributeExceptionStacktrace).Value);
}

[Fact]
public void CheckRecordExceptionEmpty()
{
using Activity activity = new Activity("exception-test");
using TelemetrySpan telemetrySpan = new TelemetrySpan(activity);
telemetrySpan.RecordException(string.Empty, string.Empty, string.Empty);
Assert.Empty(activity.Events);

telemetrySpan.RecordException(null);
Assert.Empty(activity.Events);
}
}
}