|
| 1 | +// <copyright file="SpanDataExtentions.cs" company="OpenCensus Authors"> |
| 2 | +// Copyright 2018, OpenCensus Authors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of theLicense at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// </copyright> |
| 16 | + |
| 17 | +namespace OpenCensus.Exporter.Ocagent.Implementation |
| 18 | +{ |
| 19 | + using System; |
| 20 | + using System.Collections.Generic; |
| 21 | + using System.Linq; |
| 22 | + |
| 23 | + using Google.Protobuf; |
| 24 | + using Google.Protobuf.WellKnownTypes; |
| 25 | + |
| 26 | + using Opencensus.Proto.Trace.V1; |
| 27 | + using OpenCensus.Trace; |
| 28 | + using OpenCensus.Trace.Export; |
| 29 | + |
| 30 | + internal static class SpanDataExtentions |
| 31 | + { |
| 32 | + internal static Span ToProtoSpan(this ISpanData spanData) |
| 33 | + { |
| 34 | + try |
| 35 | + { |
| 36 | + return new Span |
| 37 | + { |
| 38 | + Name = new TruncatableString { Value = spanData.Name }, |
| 39 | + Kind = spanData.Kind == SpanKind.Client ? Span.Types.SpanKind.Client : Span.Types.SpanKind.Server, |
| 40 | + TraceId = ByteString.CopyFrom(spanData.Context.TraceId.Bytes), |
| 41 | + SpanId = ByteString.CopyFrom(spanData.Context.SpanId.Bytes), |
| 42 | + ParentSpanId = |
| 43 | + ByteString.CopyFrom(spanData.ParentSpanId?.Bytes ?? new byte[0]), |
| 44 | + |
| 45 | + StartTime = new Timestamp |
| 46 | + { |
| 47 | + Nanos = spanData.StartTimestamp.Nanos, |
| 48 | + Seconds = spanData.StartTimestamp.Seconds, |
| 49 | + }, |
| 50 | + EndTime = new Timestamp |
| 51 | + { |
| 52 | + Nanos = spanData.EndTimestamp.Nanos, |
| 53 | + Seconds = spanData.EndTimestamp.Seconds, |
| 54 | + }, |
| 55 | + Status = spanData.Status == null |
| 56 | + ? null |
| 57 | + : new Opencensus.Proto.Trace.V1.Status |
| 58 | + { |
| 59 | + Code = (int)spanData.Status.CanonicalCode, |
| 60 | + Message = spanData.Status.Description ?? string.Empty, |
| 61 | + }, |
| 62 | + SameProcessAsParentSpan = |
| 63 | + !spanData.HasRemoteParent.GetValueOrDefault() && spanData.ParentSpanId != null, |
| 64 | + ChildSpanCount = spanData.ChildSpanCount.HasValue ? (uint)spanData.ChildSpanCount.Value : 0, |
| 65 | + Attributes = FromIAttributes(spanData.Attributes), |
| 66 | + TimeEvents = FromITimeEvents(spanData.MessageEvents, spanData.Annotations), |
| 67 | + Links = new Span.Types.Links |
| 68 | + { |
| 69 | + DroppedLinksCount = spanData.Links.DroppedLinksCount, |
| 70 | + Link = { spanData.Links.Links.Select(FromILink), }, |
| 71 | + }, |
| 72 | + }; |
| 73 | + } |
| 74 | + catch (Exception e) |
| 75 | + { |
| 76 | + Console.WriteLine(e); |
| 77 | + |
| 78 | + // TODO: log |
| 79 | + } |
| 80 | + |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + private static Span.Types.Attributes FromIAttributes(IAttributes source) |
| 85 | + { |
| 86 | + var attributes = new Span.Types.Attributes |
| 87 | + { |
| 88 | + DroppedAttributesCount = source.DroppedAttributesCount, |
| 89 | + }; |
| 90 | + |
| 91 | + attributes.AttributeMap.Add(source.AttributeMap.ToDictionary( |
| 92 | + kvp => kvp.Key, |
| 93 | + kvp => new Opencensus.Proto.Trace.V1.AttributeValue |
| 94 | + { |
| 95 | + StringValue = new TruncatableString |
| 96 | + { |
| 97 | + Value = kvp.Value.Match(s => s, b => b.ToString(), l => l.ToString(), d => d.ToString(), o => o?.ToString()), |
| 98 | + }, |
| 99 | + |
| 100 | + // todo: how to determine AttributeValue type? |
| 101 | + })); |
| 102 | + |
| 103 | + return attributes; |
| 104 | + } |
| 105 | + |
| 106 | + private static Span.Types.TimeEvent FromITimeEvent(ITimedEvent<IMessageEvent> source) |
| 107 | + { |
| 108 | + return new Span.Types.TimeEvent |
| 109 | + { |
| 110 | + Time = new Timestamp |
| 111 | + { |
| 112 | + Nanos = source.Timestamp.Nanos, |
| 113 | + Seconds = source.Timestamp.Seconds, |
| 114 | + }, |
| 115 | + MessageEvent = new Span.Types.TimeEvent.Types.MessageEvent |
| 116 | + { |
| 117 | + Type = source.Event.Type == MessageEventType.SENT ? Span.Types.TimeEvent.Types.MessageEvent.Types.Type.Sent : Span.Types.TimeEvent.Types.MessageEvent.Types.Type.Received, |
| 118 | + CompressedSize = (ulong)source.Event.CompressedMessageSize, |
| 119 | + UncompressedSize = (ulong)source.Event.UncompressedMessageSize, |
| 120 | + Id = (ulong)source.Event.MessageId, |
| 121 | + }, |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + private static Span.Types.TimeEvents FromITimeEvents(ITimedEvents<IMessageEvent> messages, ITimedEvents<IAnnotation> annotations) |
| 126 | + { |
| 127 | + var timedEvents = new Span.Types.TimeEvents |
| 128 | + { |
| 129 | + DroppedMessageEventsCount = messages.DroppedEventsCount, |
| 130 | + DroppedAnnotationsCount = annotations.DroppedEventsCount, |
| 131 | + TimeEvent = { messages.Events.Select(FromITimeEvent), }, |
| 132 | + }; |
| 133 | + |
| 134 | + timedEvents.TimeEvent.AddRange(annotations.Events.Select(FromITimeEvent)); |
| 135 | + |
| 136 | + return timedEvents; |
| 137 | + } |
| 138 | + |
| 139 | + private static Span.Types.Link FromILink(ILink source) |
| 140 | + { |
| 141 | + return new Span.Types.Link |
| 142 | + { |
| 143 | + TraceId = ByteString.CopyFrom(source.TraceId.Bytes), |
| 144 | + SpanId = ByteString.CopyFrom(source.SpanId.Bytes), |
| 145 | + Type = source.Type == LinkType.CHILD_LINKED_SPAN ? Span.Types.Link.Types.Type.ChildLinkedSpan : Span.Types.Link.Types.Type.ParentLinkedSpan, |
| 146 | + Attributes = FromIAttributeMap(source.Attributes), |
| 147 | + }; |
| 148 | + } |
| 149 | + |
| 150 | + private static Span.Types.TimeEvent FromITimeEvent(ITimedEvent<IAnnotation> source) |
| 151 | + { |
| 152 | + return new Span.Types.TimeEvent |
| 153 | + { |
| 154 | + Time = new Timestamp |
| 155 | + { |
| 156 | + Nanos = source.Timestamp.Nanos, |
| 157 | + Seconds = source.Timestamp.Seconds, |
| 158 | + }, |
| 159 | + Annotation = new Span.Types.TimeEvent.Types.Annotation |
| 160 | + { |
| 161 | + Description = new TruncatableString { Value = source.Event.Description }, |
| 162 | + Attributes = FromIAttributeMap(source.Event.Attributes), |
| 163 | + }, |
| 164 | + }; |
| 165 | + } |
| 166 | + |
| 167 | + private static Span.Types.Attributes FromIAttributeMap(IDictionary<string, IAttributeValue> source) |
| 168 | + { |
| 169 | + var attributes = new Span.Types.Attributes(); |
| 170 | + |
| 171 | + attributes.AttributeMap.Add(source.ToDictionary( |
| 172 | + kvp => kvp.Key, |
| 173 | + kvp => new Opencensus.Proto.Trace.V1.AttributeValue |
| 174 | + { |
| 175 | + StringValue = new TruncatableString |
| 176 | + { |
| 177 | + Value = kvp.Value.Match(s => s, b => b.ToString(), l => l.ToString(), d => d.ToString(), o => o?.ToString()), |
| 178 | + }, |
| 179 | + |
| 180 | + // todo: how to determine AttributeValue type? |
| 181 | + })); |
| 182 | + |
| 183 | + return attributes; |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments