/// <summary>
        /// Translating <see cref="SpanData"/> to Stackdriver's Span
        /// According to <see href="https://cloud.google.com/trace/docs/reference/v2/rpc/google.devtools.cloudtrace.v2"/> specifications.
        /// </summary>
        /// <param name="spanData">Span in OpenTelemetry format.</param>
        /// <param name="projectId">Google Cloud Platform Project Id.</param>
        /// <returns><see cref="ISpan"/>.</returns>
        public static Google.Cloud.Trace.V2.Span ToSpan(this SpanData spanData, string projectId)
        {
            var spanId = spanData.Context.SpanId.ToHexString();

            // Base span settings
            var span = new Google.Cloud.Trace.V2.Span
            {
                SpanName    = new SpanName(projectId, spanData.Context.TraceId.ToHexString(), spanId),
                SpanId      = spanId,
                DisplayName = new TruncatableString {
                    Value = spanData.Name
                },
                StartTime      = spanData.StartTimestamp.ToTimestamp(),
                EndTime        = spanData.EndTimestamp.ToTimestamp(),
                ChildSpanCount = spanData.ChildSpanCount,
            };

            if (spanData.ParentSpanId != null)
            {
                var parentSpanId = spanData.ParentSpanId.ToHexString();
                if (!string.IsNullOrEmpty(parentSpanId))
                {
                    span.ParentSpanId = parentSpanId;
                }
            }

            // Span Links
            if (spanData.Links != null)
            {
                span.Links = new Google.Cloud.Trace.V2.Span.Types.Links
                {
                    DroppedLinksCount = spanData.Links.DroppedLinksCount,
                    Link = { spanData.Links.Links.Select(l => l.ToLink()) },
                };
            }

            // Span Attributes
            if (spanData.Attributes != null)
            {
                span.Attributes = new Google.Cloud.Trace.V2.Span.Types.Attributes
                {
                    DroppedAttributesCount = spanData.Attributes != null ? spanData.Attributes.DroppedAttributesCount : 0,

                    AttributeMap =
                    {
                        spanData.Attributes?.AttributeMap?.ToDictionary(
                            s => s.Key,
                            s => s.Value?.ToAttributeValue()),
                    },
                };
            }

            return(span);
        }
Пример #2
0
        /// <summary>
        /// Translating <see cref="SpanData"/> to Stackdriver's Span
        /// According to <see href="https://cloud.google.com/trace/docs/reference/v2/rpc/google.devtools.cloudtrace.v2"/> specifications.
        /// </summary>
        /// <param name="spanData">Span in OpenTelemetry format.</param>
        /// <param name="projectId">Google Cloud Platform Project Id.</param>
        /// <returns><see cref="TelemetrySpan"/>.</returns>
        public static Google.Cloud.Trace.V2.Span ToSpan(this SpanData spanData, string projectId)
        {
            var spanId = spanData.Context.SpanId.ToHexString();

            // Base span settings
            var span = new Google.Cloud.Trace.V2.Span
            {
                SpanName    = new SpanName(projectId, spanData.Context.TraceId.ToHexString(), spanId),
                SpanId      = spanId,
                DisplayName = new TruncatableString {
                    Value = spanData.Name
                },
                StartTime      = spanData.StartTimestamp.ToTimestamp(),
                EndTime        = spanData.EndTimestamp.ToTimestamp(),
                ChildSpanCount = null,
            };

            if (spanData.ParentSpanId != null)
            {
                var parentSpanId = spanData.ParentSpanId.ToHexString();
                if (!string.IsNullOrEmpty(parentSpanId))
                {
                    span.ParentSpanId = parentSpanId;
                }
            }

            // Span Links
            if (spanData.Links != null)
            {
                span.Links = new Google.Cloud.Trace.V2.Span.Types.Links
                {
                    Link = { spanData.Links.Select(l => l.ToLink()) },
                };
            }

            // Span Attributes
            if (spanData.Attributes != null)
            {
                span.Attributes = new Google.Cloud.Trace.V2.Span.Types.Attributes
                {
                    AttributeMap =
                    {
                        spanData.Attributes?.ToDictionary(
                            s => s.Key,
                            s => s.Value?.ToAttributeValue()),
                    },
                };
            }

            // StackDriver uses different labels that are used to categorize spans
            // replace attribute keys with StackDriver version
            foreach (var entry in labelsToReplace)
            {
                if (span.Attributes.AttributeMap.TryGetValue(entry.Key, out var attrValue))
                {
                    span.Attributes.AttributeMap.Remove(entry.Key);
                    span.Attributes.AttributeMap.Add(entry.Value, attrValue);
                }
            }

            return(span);
        }