コード例 #1
0
        internal static OtlpTrace.Span ToOtlpSpan(this SpanData spanData)
        {
            // protobuf doesn't understand Span<T> yet: https://github.com/protocolbuffers/protobuf/issues/3431
            Span <byte> traceIdBytes = stackalloc byte[16];
            Span <byte> spanIdBytes  = stackalloc byte[8];

            spanData.Context.TraceId.CopyTo(traceIdBytes);
            spanData.Context.SpanId.CopyTo(spanIdBytes);

            var parentSpanIdString = ByteString.Empty;

            if (spanData.ParentSpanId != default)
            {
                Span <byte> parentSpanIdBytes = stackalloc byte[8];
                spanData.ParentSpanId.CopyTo(parentSpanIdBytes);
                parentSpanIdString = ByteString.CopyFrom(parentSpanIdBytes.ToArray());
            }

            var otlpSpan = new OtlpTrace.Span
            {
                Name = spanData.Name,

                Kind = spanData.Kind == null
                    ? OtlpTrace.Span.Types.SpanKind.Unspecified
                    : (OtlpTrace.Span.Types.SpanKind)spanData.Kind.Value,

                TraceId      = ByteString.CopyFrom(traceIdBytes.ToArray()),
                SpanId       = ByteString.CopyFrom(spanIdBytes.ToArray()),
                ParentSpanId = parentSpanIdString,

                Status = ToOtlpStatus(spanData.Status),

                StartTimeUnixNano = (ulong)spanData.StartTimestamp.ToUnixTimeNanoseconds(),
                EndTimeUnixNano   = (ulong)spanData.EndTimestamp.ToUnixTimeNanoseconds(),
            };

            if (!ReferenceEquals(spanData.Attributes, EmptySpanDataAttributes))
            {
                otlpSpan.Attributes.AddRange(spanData.Attributes.Select(ToOtlpAttribute));

                // TODO: get dropped count.
            }

            if (!ReferenceEquals(spanData.Events, EmptySpanDataEvents))
            {
                otlpSpan.Events.AddRange(spanData.Events.Select(ToOtlpEvent));

                // TODO: get dropped count.
            }

            if (!ReferenceEquals(spanData.Links, EmptySpanDataLinks))
            {
                otlpSpan.Links.AddRange(spanData.Links.Select(ToOtlpLink));

                // TODO: get dropped count.
            }

            return(otlpSpan);
        }
コード例 #2
0
        internal static OtlpTrace.Span ToOtlpSpan(this Activity activity)
        {
            if (activity.IdFormat != ActivityIdFormat.W3C)
            {
                // Only ActivityIdFormat.W3C is supported, in principle this should never be
                // hit under the OpenTelemetry SDK.
                return(null);
            }

            // protobuf doesn't understand Span<T> yet: https://github.com/protocolbuffers/protobuf/issues/3431
            Span <byte> traceIdBytes = stackalloc byte[16];
            Span <byte> spanIdBytes  = stackalloc byte[8];

            activity.TraceId.CopyTo(traceIdBytes);
            activity.SpanId.CopyTo(spanIdBytes);

            var parentSpanIdString = ByteString.Empty;

            if (activity.ParentSpanId != default)
            {
                Span <byte> parentSpanIdBytes = stackalloc byte[8];
                activity.ParentSpanId.CopyTo(parentSpanIdBytes);
                parentSpanIdString = ByteString.CopyFrom(parentSpanIdBytes.ToArray());
            }

            var startTimeUnixNano = activity.StartTimeUtc.ToUnixTimeNanoseconds();
            var otlpSpan          = new OtlpTrace.Span
            {
                Name = activity.DisplayName,

                Kind = (OtlpTrace.Span.Types.SpanKind)(activity.Kind + 1), // TODO: there is an offset of 1 on the enum.

                TraceId      = ByteString.CopyFrom(traceIdBytes.ToArray()),
                SpanId       = ByteString.CopyFrom(spanIdBytes.ToArray()),
                ParentSpanId = parentSpanIdString,

                Status = ToOtlpStatus(activity.GetStatus()),

                StartTimeUnixNano = (ulong)startTimeUnixNano,
                EndTimeUnixNano   = (ulong)(startTimeUnixNano + activity.Duration.ToNanoseconds()),
            };

            foreach (var kvp in activity.TagObjects)
            {
                var attributes = ToOtlpAttributes(kvp);
                if (attributes != null &&
                    attributes.All(a => a.Key != SpanAttributeConstants.StatusCodeKey && a.Key != SpanAttributeConstants.StatusDescriptionKey))
                {
                    otlpSpan.Attributes.AddRange(attributes);
                }
            }

            otlpSpan.Events.AddRange(activity.Events.Select(ToOtlpEvent));
            otlpSpan.Links.AddRange(activity.Links.Select(ToOtlpLink));

            // Activity does not limit number of attributes, events, links, etc so drop counts are always zero.

            return(otlpSpan);
        }
コード例 #3
0
        internal static OtlpTrace.Span ToOtlpSpan(this Activity activity)
        {
            if (activity.IdFormat != ActivityIdFormat.W3C)
            {
                // Only ActivityIdFormat.W3C is supported, in principle this should never be
                // hit under the OpenTelemetry SDK.
                return(null);
            }

            // protobuf doesn't understand Span<T> yet: https://github.com/protocolbuffers/protobuf/issues/3431
            Span <byte> traceIdBytes = stackalloc byte[16];
            Span <byte> spanIdBytes  = stackalloc byte[8];

            activity.TraceId.CopyTo(traceIdBytes);
            activity.SpanId.CopyTo(spanIdBytes);

            var parentSpanIdString = ByteString.Empty;

            if (activity.ParentSpanId != default)
            {
                Span <byte> parentSpanIdBytes = stackalloc byte[8];
                activity.ParentSpanId.CopyTo(parentSpanIdBytes);
                parentSpanIdString = ByteString.CopyFrom(parentSpanIdBytes.ToArray());
            }

            var startTimeUnixNano = activity.StartTimeUtc.ToUnixTimeNanoseconds();
            var otlpSpan          = new OtlpTrace.Span
            {
                Name = activity.DisplayName,

                Kind = (OtlpTrace.Span.Types.SpanKind)(activity.Kind + 1), // TODO: there is an offset of 1 on the enum.

                TraceId      = ByteString.CopyFrom(traceIdBytes.ToArray()),
                SpanId       = ByteString.CopyFrom(spanIdBytes.ToArray()),
                ParentSpanId = parentSpanIdString,

                // TODO: Status is still pending, need to pursue OTEL spec change.

                StartTimeUnixNano = (ulong)startTimeUnixNano,
                EndTimeUnixNano   = (ulong)(startTimeUnixNano + activity.Duration.ToNanoseconds()),
            };

            foreach (var kvp in activity.Tags)
            {
                // TODO: attempt to convert to supported types?
                // TODO: enforce no duplicate keys?
                // TODO: drop if Value is null?
                // TODO: reverse?
                var attrib = new OtlpCommon.AttributeKeyValue {
                    Key = kvp.Key, StringValue = kvp.Value
                };
                otlpSpan.Attributes.Add(attrib);
            }

            otlpSpan.Events.AddRange(activity.Events.Select(ToOtlpEvent));
            otlpSpan.Links.AddRange(activity.Links.Select(ToOtlpLink));

            // Activity does not limit number of attributes, events, links, etc so drop counts are always zero.

            return(otlpSpan);
        }