Updating Link/TelemetrySpan to accept SpanAttributes (#1120)

* 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
This commit is contained in:
Eddy Nakamura 2020-08-21 11:51:55 -03:00 committed by GitHub
parent 7c46d45de7
commit c75d85ba6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 18 deletions

View File

@ -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

View File

@ -40,9 +40,9 @@ namespace OpenTelemetry.Trace
/// </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>

View File

@ -237,10 +237,9 @@ namespace OpenTelemetry.Trace
/// <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;
}
@ -252,10 +251,9 @@ namespace OpenTelemetry.Trace
/// <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;
}
@ -329,8 +327,7 @@ namespace OpenTelemetry.Trace
/// <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);
@ -346,7 +343,7 @@ namespace OpenTelemetry.Trace
attributes.Add(SemanticConventions.AttributeExceptionMessage, message);
}
if (attributes.Count != 0)
if (attributes.Attributes.Count != 0)
{
this.AddEvent(SemanticConventions.AttributeExceptionEventName, attributes);
}

View File

@ -92,15 +92,49 @@ namespace OpenTelemetry.Shims.OpenTracing
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;

View File

@ -16,6 +16,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Xunit;
namespace OpenTelemetry.Trace.Tests
@ -24,7 +25,7 @@ namespace OpenTelemetry.Trace.Tests
{
private readonly IDictionary<string, object> attributesMap = new Dictionary<string, object>();
private readonly SpanContext spanContext;
private readonly ActivityTagsCollection tags;
private readonly SpanAttributes tags;
public LinkTest()
{
@ -34,7 +35,19 @@ namespace OpenTelemetry.Trace.Tests
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]
@ -51,7 +64,11 @@ namespace OpenTelemetry.Trace.Tests
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]
@ -91,7 +108,7 @@ namespace OpenTelemetry.Trace.Tests
[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);