Skip to content

Commit

Permalink
Merging itextFormat (#920)
Browse files Browse the repository at this point in the history
* Merging itextFormat

* removing unused eventsource that was used for spancontext

Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
  • Loading branch information
eddynaka and cijothomas authored Jul 24, 2020
1 parent 920b0ed commit 4904972
Show file tree
Hide file tree
Showing 21 changed files with 122 additions and 475 deletions.
23 changes: 16 additions & 7 deletions src/OpenTelemetry.Api/Context/Propagation/ITextFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// </copyright>
using System;
using System.Collections.Generic;
using OpenTelemetry.Trace;
using System.Diagnostics;

namespace OpenTelemetry.Context.Propagation
{
Expand All @@ -33,21 +33,30 @@ public interface ITextFormat
ISet<string> Fields { get; }

/// <summary>
/// Injects textual representation of span context to transmit over the wire.
/// Injects textual representation of activity context to transmit over the wire.
/// </summary>
/// <typeparam name="T">Type of an object to set context on. Typically HttpRequest or similar.</typeparam>
/// <param name="spanContext">Span context to transmit over the wire.</param>
/// <param name="activityContext">Activity context to transmit over the wire.</param>
/// <param name="carrier">Object to set context on. Instance of this object will be passed to setter.</param>
/// <param name="setter">Action that will set name and value pair on the object.</param>
void Inject<T>(SpanContext spanContext, T carrier, Action<T, string, string> setter);
void Inject<T>(ActivityContext activityContext, T carrier, Action<T, string, string> setter);

/// <summary>
/// Extracts span context from textual representation.
/// Extracts activity context from textual representation.
/// </summary>
/// <typeparam name="T">Type of object to extract context from. Typically HttpRequest or similar.</typeparam>
/// <param name="carrier">Object to extract context from. Instance of this object will be passed to the getter.</param>
/// <param name="getter">Function that will return string value of a key with the specified name.</param>
/// <returns>Span context from it's text representation.</returns>
SpanContext Extract<T>(T carrier, Func<T, string, IEnumerable<string>> getter);
/// <returns>Activity context from it's text representation.</returns>
ActivityContext Extract<T>(T carrier, Func<T, string, IEnumerable<string>> getter);

/// <summary>
/// Tests if an activity context has been injected into a carrier.
/// </summary>
/// <typeparam name="T">Type of object to extract context from. Typically HttpRequest or similar.</typeparam>
/// <param name="carrier">Object to extract context from. Instance of this object will be passed to the getter.</param>
/// <param name="getter">Function that will return string value of a key with the specified name.</param>
/// <returns><see langword="true" /> if the carrier has been injected with an activity context.</returns>
bool IsInjected<T>(T carrier, Func<T, string, IEnumerable<string>> getter);
}
}
62 changes: 0 additions & 62 deletions src/OpenTelemetry.Api/Context/Propagation/ITextFormatActivity.cs

This file was deleted.

84 changes: 65 additions & 19 deletions src/OpenTelemetry.Api/Context/Propagation/TraceContextFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using OpenTelemetry.Api.Context.Propagation;
using System.Text;
using OpenTelemetry.Internal;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Context.Propagation
{
Expand All @@ -44,8 +43,51 @@ public class TraceContextFormat : ITextFormat
public ISet<string> Fields => new HashSet<string> { TraceState, TraceParent };

/// <inheritdoc/>
public SpanContext Extract<T>(T carrier, Func<T, string, IEnumerable<string>> getter)
public bool IsInjected<T>(T carrier, Func<T, string, IEnumerable<string>> getter)
{
if (carrier == null)
{
OpenTelemetryApiEventSource.Log.FailedToInjectActivityContext("null carrier");
return false;
}

if (getter == null)
{
OpenTelemetryApiEventSource.Log.FailedToExtractContext("null getter");
return false;
}

try
{
var traceparentCollection = getter(carrier, TraceParent);

// There must be a single traceparent
return traceparentCollection != null && traceparentCollection.Count() == 1;
}
catch (Exception ex)
{
OpenTelemetryApiEventSource.Log.ActivityContextExtractException(ex);
}

// in case of exception indicate to upstream that there is no parseable context from the top
return false;
}

/// <inheritdoc/>
public ActivityContext Extract<T>(T carrier, Func<T, string, IEnumerable<string>> getter)
{
if (carrier == null)
{
OpenTelemetryApiEventSource.Log.FailedToInjectActivityContext("null carrier");
return default;
}

if (getter == null)
{
OpenTelemetryApiEventSource.Log.FailedToExtractContext("null getter");
return default;
}

try
{
var traceparentCollection = getter(carrier, TraceParent);
Expand All @@ -64,52 +106,52 @@ public SpanContext Extract<T>(T carrier, Func<T, string, IEnumerable<string>> ge
return default;
}

List<KeyValuePair<string, string>> tracestate = null;
string tracestate = null;
var tracestateCollection = getter(carrier, TraceState);
if (tracestateCollection != null)
{
this.TryExtractTracestate(tracestateCollection.ToArray(), out tracestate);
}

return new SpanContext(traceId, spanId, traceoptions, true, tracestate);
return new ActivityContext(traceId, spanId, traceoptions, tracestate);
}
catch (Exception ex)
{
OpenTelemetryApiEventSource.Log.SpanContextExtractException(ex);
OpenTelemetryApiEventSource.Log.ActivityContextExtractException(ex);
}

// in case of exception indicate to upstream that there is no parseable context from the top
return default;
}

/// <inheritdoc/>
public void Inject<T>(SpanContext spanContext, T carrier, Action<T, string, string> setter)
public void Inject<T>(ActivityContext activityContext, T carrier, Action<T, string, string> setter)
{
if (!spanContext.IsValid)
if (activityContext == default)
{
OpenTelemetryApiEventSource.Log.FailedToInjectSpanContext("Invalid context");
OpenTelemetryApiEventSource.Log.FailedToInjectActivityContext("Invalid context");
return;
}

if (carrier == null)
{
OpenTelemetryApiEventSource.Log.FailedToInjectSpanContext("null carrier");
OpenTelemetryApiEventSource.Log.FailedToInjectActivityContext("null carrier");
return;
}

if (setter == null)
{
OpenTelemetryApiEventSource.Log.FailedToInjectSpanContext("null setter");
OpenTelemetryApiEventSource.Log.FailedToInjectActivityContext("null setter");
return;
}

var traceparent = string.Concat("00-", spanContext.TraceId.ToHexString(), "-", spanContext.SpanId.ToHexString());
traceparent = string.Concat(traceparent, (spanContext.TraceFlags & ActivityTraceFlags.Recorded) != 0 ? "-01" : "-00");
var traceparent = string.Concat("00-", activityContext.TraceId.ToHexString(), "-", activityContext.SpanId.ToHexString());
traceparent = string.Concat(traceparent, (activityContext.TraceFlags & ActivityTraceFlags.Recorded) != 0 ? "-01" : "-00");

setter(carrier, TraceParent, traceparent);

string tracestateStr = TraceStateUtilsNew.GetString(spanContext.TraceState);
if (tracestateStr.Length > 0)
string tracestateStr = activityContext.TraceState;
if (tracestateStr?.Length > 0)
{
setter(carrier, TraceState, tracestateStr);
}
Expand Down Expand Up @@ -237,23 +279,27 @@ private byte HexCharToByte(char c)
throw new ArgumentOutOfRangeException(nameof(c), $"Invalid character: {c}.");
}

private bool TryExtractTracestate(string[] tracestateCollection, out List<KeyValuePair<string, string>> tracestateResult)
private bool TryExtractTracestate(string[] tracestateCollection, out string tracestateResult)
{
tracestateResult = null;
tracestateResult = string.Empty;

if (tracestateCollection != null)
{
tracestateResult = new List<KeyValuePair<string, string>>();
var result = new StringBuilder();

// Iterate in reverse order because when call builder set the elements is added in the
// front of the list.
for (int i = tracestateCollection.Length - 1; i >= 0; i--)
{
if (!TraceStateUtilsNew.AppendTraceState(tracestateCollection[i], tracestateResult))
if (string.IsNullOrEmpty(tracestateCollection[i]))
{
return false;
}

result.Append(tracestateCollection[i]);
}

tracestateResult = result.ToString();
}

return true;
Expand Down
Loading

0 comments on commit 4904972

Please sign in to comment.