Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Refactored based on Reiley's comments
  • Loading branch information
rajkumar-rangaraj committed Jun 18, 2020
commit 37e6084e43976b51f9c27b1b7033c6e298bbca0e
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace OpenTelemetry.Exporter.Zipkin.Implementation
{
internal static class ZipkinActivityConversionExtensions
{
private const long TicksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000;

private static readonly Dictionary<string, int> RemoteEndpointServiceNameKeyResolutionDictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
{
[SpanAttributeConstants.PeerServiceKey] = 0, // RemoteEndpoint.ServiceName primary.
Expand All @@ -36,6 +38,8 @@ internal static class ZipkinActivityConversionExtensions
["db.instance"] = 4, // RemoteEndpoint.ServiceName for Redis.
};

private static readonly string InvalidSpanId = default(ActivitySpanId).ToHexString();

private static readonly ConcurrentDictionary<string, ZipkinEndpoint> LocalEndpointCache = new ConcurrentDictionary<string, ZipkinEndpoint>();
private static readonly ConcurrentDictionary<string, ZipkinEndpoint> RemoteEndpointCache = new ConcurrentDictionary<string, ZipkinEndpoint>();

Expand All @@ -47,10 +51,10 @@ internal static ZipkinSpan ToZipkinSpan(this Activity activity, ZipkinEndpoint d
var context = activity.Context;
var startTimestamp = activity.StartTimeUtc.ToEpochMicroseconds();

string parentId = null;
if (activity.ParentSpanId != default)
string parentId = EncodeSpanId(activity.ParentSpanId);
if (string.Equals(parentId, InvalidSpanId, StringComparison.Ordinal))
{
parentId = EncodeSpanId(activity.ParentSpanId);
parentId = null;
}

var attributeEnumerationState = new AttributeEnumerationState
Expand Down Expand Up @@ -105,7 +109,7 @@ internal static ZipkinSpan ToZipkinSpan(this Activity activity, ZipkinEndpoint d
ToActivityKind(activity),
activity.OperationName,
activity.StartTimeUtc.ToEpochMicroseconds(),
duration: (long)activity.Duration.TotalMilliseconds * 1000, // duration in Microseconds
duration: (long)activity.Duration.ToEpochMicroseconds(),
localEndpoint,
remoteEndpoint,
annotations,
Expand All @@ -119,15 +123,19 @@ internal static string EncodeSpanId(ActivitySpanId spanId)
return spanId.ToHexString();
}

internal static long ToEpochMicroseconds(this DateTimeOffset timestamp)
internal static long ToEpochMicroseconds(this DateTimeOffset dateTimeOffset)
{
return dateTimeOffset.Ticks / TicksPerMicrosecond;
}

internal static long ToEpochMicroseconds(this TimeSpan timeSpan)
{
return timestamp.ToUnixTimeMilliseconds() * 1000;
return timeSpan.Ticks / TicksPerMicrosecond;
}

internal static long ToEpochMicroseconds(this DateTime utcDateTime)
Copy link
Member

Choose a reason for hiding this comment

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

@cijothomas @CodeBlanch I think this might be a good function to move to a shared file (and use links to share across multiple projects)?

Copy link
Member

Choose a reason for hiding this comment

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

@reyang Sounds good to me. I think @eddynaka is adding the file on #732 so we'll just need to link & use in Zipkin.

{
const long TicksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000;
const long UnixEpochTicks = 621355968000000000; // = DateTimeOffset.FromUnixTimeMilliseconds(0).Ticks
const long UnixEpochTicks = 621355968000000000L; // = DateTimeOffset.FromUnixTimeMilliseconds(0).Ticks
const long UnixEpochMicroseconds = UnixEpochTicks / TicksPerMicrosecond;

// Truncate sub-microsecond precision before offsetting by the Unix Epoch to avoid
Expand Down Expand Up @@ -158,9 +166,11 @@ private static string ToActivityKind(Activity activity)
return "PRODUCER";
case ActivityKind.Consumer:
return "CONSUMER";
default:
case ActivityKind.Client:
return "CLIENT";
}

return null;
}

private static bool ProcessActivityEvents(ref PooledList<ZipkinAnnotation> annotations, ActivityEvent @event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ public void Write(Utf8JsonWriter writer)

writer.WriteString("id", this.Id);

writer.WriteString("kind", this.Kind);
if (this.Kind != null)
{
writer.WriteString("kind", this.Kind);
}

if (this.Timestamp.HasValue)
{
Expand Down