Skip to content

Commit

Permalink
Updating Link/TelemetrySpan to accept SpanAttributes (#1120)
Browse files Browse the repository at this point in the history
* Updating to use SpanAttributes

* updating link tests

* adding changelog and testing all supported types

* Update CHANGELOG.md

* updating changelog

* updating TelemetrySpan

* updating changelog

* updating to pass in tests

* changing to spanAttributes
  • Loading branch information
eddynaka authored Aug 21, 2020
1 parent 7c46d45 commit c75d85b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* `Link` and `TelemetrySpan` are using `SpanAttributes` instead of
`ActivityTagsCollection` or `Dictionary`
([#1120](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1120))
* Added `RecordException` in `TelemetrySpan`
([#1116](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1116))
* `PropagationContext` is now used instead of `ActivityContext` in the
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry.Api/Trace/Link.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public Link(in SpanContext spanContext)
/// </summary>
/// <param name="spanContext">Span context of a linked span.</param>
/// <param name="attributes">Link attributes.</param>
public Link(in SpanContext spanContext, ActivityTagsCollection attributes)
public Link(in SpanContext spanContext, SpanAttributes attributes)
{
this.ActivityLink = new ActivityLink(spanContext.ActivityContext, attributes);
this.ActivityLink = new ActivityLink(spanContext.ActivityContext, attributes.Attributes);
}

/// <summary>
Expand Down
15 changes: 6 additions & 9 deletions src/OpenTelemetry.Api/Trace/TelemetrySpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,9 @@ public TelemetrySpan AddEvent(string name, DateTimeOffset timestamp)
/// <param name="attributes">Attributes for the event.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan AddEvent(string name, IDictionary<string, object> attributes)
public TelemetrySpan AddEvent(string name, SpanAttributes attributes)
{
ActivityTagsCollection eventTags = new ActivityTagsCollection(attributes);
this.Activity?.AddEvent(new ActivityEvent(name, default, eventTags));
this.Activity?.AddEvent(new ActivityEvent(name, default, attributes.Attributes));
return this;
}

Expand All @@ -252,10 +251,9 @@ public TelemetrySpan AddEvent(string name, IDictionary<string, object> attribute
/// <param name="attributes">Attributes for the event.</param>
/// <returns>The <see cref="TelemetrySpan"/> instance for chaining.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TelemetrySpan AddEvent(string name, DateTimeOffset timestamp, IDictionary<string, object> attributes)
public TelemetrySpan AddEvent(string name, DateTimeOffset timestamp, SpanAttributes attributes)
{
var eventTags = new ActivityTagsCollection(attributes);
this.Activity?.AddEvent(new ActivityEvent(name, timestamp, eventTags));
this.Activity?.AddEvent(new ActivityEvent(name, timestamp, attributes.Attributes));
return this;
}

Expand Down Expand Up @@ -329,8 +327,7 @@ public TelemetrySpan RecordException(Exception ex)
/// <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>();

SpanAttributes attributes = new SpanAttributes();
if (!string.IsNullOrWhiteSpace(type))
{
attributes.Add(SemanticConventions.AttributeExceptionType, type);
Expand All @@ -346,7 +343,7 @@ public TelemetrySpan RecordException(string type, string message, string stacktr
attributes.Add(SemanticConventions.AttributeExceptionMessage, message);
}

if (attributes.Count != 0)
if (attributes.Attributes.Count != 0)
{
this.AddEvent(SemanticConventions.AttributeExceptionEventName, attributes);
}
Expand Down
40 changes: 37 additions & 3 deletions src/OpenTelemetry.Shims.OpenTracing/SpanShim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,49 @@ public string GetBaggageItem(string key)

var payload = ConvertToEventPayload(fields);
var eventName = payload.Item1;
var eventAttributes = payload.Item2;

var spanAttributes = new SpanAttributes();
foreach (var field in payload.Item2)
{
switch (field.Value)
{
case long value:
spanAttributes.Add(field.Key, value);
break;
case long[] value:
spanAttributes.Add(field.Key, value);
break;
case bool value:
spanAttributes.Add(field.Key, value);
break;
case bool[] value:
spanAttributes.Add(field.Key, value);
break;
case double value:
spanAttributes.Add(field.Key, value);
break;
case double[] value:
spanAttributes.Add(field.Key, value);
break;
case string value:
spanAttributes.Add(field.Key, value);
break;
case string[] value:
spanAttributes.Add(field.Key, value);
break;

default:
break;
}
}

if (timestamp == DateTimeOffset.MinValue)
{
this.Span.AddEvent(eventName, eventAttributes);
this.Span.AddEvent(eventName, spanAttributes);
}
else
{
this.Span.AddEvent(eventName, timestamp, eventAttributes);
this.Span.AddEvent(eventName, timestamp, spanAttributes);
}

return this;
Expand Down
25 changes: 21 additions & 4 deletions test/OpenTelemetry.Tests/Trace/LinkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Xunit;

namespace OpenTelemetry.Trace.Tests
Expand All @@ -24,7 +25,7 @@ public class LinkTest : IDisposable
{
private readonly IDictionary<string, object> attributesMap = new Dictionary<string, object>();
private readonly SpanContext spanContext;
private readonly ActivityTagsCollection tags;
private readonly SpanAttributes tags;

public LinkTest()
{
Expand All @@ -34,7 +35,19 @@ public LinkTest()
this.attributesMap.Add("MyAttributeKey1", 10L);
this.attributesMap.Add("MyAttributeKey2", true);
this.attributesMap.Add("MyAttributeKey3", 0.005);
this.tags = new ActivityTagsCollection(this.attributesMap);
this.attributesMap.Add("MyAttributeKey4", new long[] { 1, 2 });
this.attributesMap.Add("MyAttributeKey5", new string[] { "a", "b" });
this.attributesMap.Add("MyAttributeKey6", new bool[] { true, false });
this.attributesMap.Add("MyAttributeKey7", new double[] { 0.1, -0.1 });
this.tags = new SpanAttributes();
this.tags.Add("MyAttributeKey0", "MyStringAttribute");
this.tags.Add("MyAttributeKey1", 10L);
this.tags.Add("MyAttributeKey2", true);
this.tags.Add("MyAttributeKey3", 0.005);
this.tags.Add("MyAttributeKey4", new long[] { 1, 2 });
this.tags.Add("MyAttributeKey5", new string[] { "a", "b" });
this.tags.Add("MyAttributeKey6", new bool[] { true, false });
this.tags.Add("MyAttributeKey7", new double[] { 0.1, -0.1 });
}

[Fact]
Expand All @@ -51,7 +64,11 @@ public void FromSpanContext_WithAttributes()
var link = new Link(this.spanContext, this.tags);
Assert.Equal(this.spanContext.TraceId, link.Context.TraceId);
Assert.Equal(this.spanContext.SpanId, link.Context.SpanId);
Assert.Equal(this.attributesMap, link.Attributes);

foreach (var attributemap in this.attributesMap)
{
Assert.Equal(attributemap.Value, link.Attributes.FirstOrDefault(a => a.Key == attributemap.Key).Value);
}
}

[Fact]
Expand Down Expand Up @@ -91,7 +108,7 @@ public void NotEquality()
[Fact]
public void NotEquality_WithAttributes()
{
var tag1 = new ActivityTagsCollection(new Dictionary<string, object>());
var tag1 = new SpanAttributes();
var tag2 = this.tags;
var link1 = new Link(this.spanContext, tag1);
var link2 = new Link(this.spanContext, tag2);
Expand Down

0 comments on commit c75d85b

Please sign in to comment.