1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
package transform
import (
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/resource"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
v11 "go.opentelemetry.io/proto/otlp/common/v1"
v1 "go.opentelemetry.io/proto/otlp/resource/v1"
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
)
const (
maxMessageEventsPerSpan = 128
)
// Spans transforms slice of OTLP ResourceSpan into a slice of SpanSnapshots.
func Spans(sdl []*tracepb.ResourceSpans) []tracesdk.ReadOnlySpan {
if len(sdl) == 0 {
return nil
}
var out []tracesdk.ReadOnlySpan
for _, sd := range sdl {
if sd == nil {
continue
}
for _, sdi := range sd.ScopeSpans {
if sdi == nil {
continue
}
sda := make([]tracesdk.ReadOnlySpan, 0, len(sdi.Spans))
for _, s := range sdi.Spans {
if s == nil {
continue
}
sda = append(sda, &readOnlySpan{
pb: s,
is: sdi.Scope,
resource: sd.Resource,
schemaURL: sd.SchemaUrl,
})
}
out = append(out, sda...)
}
}
return out
}
type readOnlySpan struct {
// Embed the interface to implement the private method.
tracesdk.ReadOnlySpan
pb *tracepb.Span
is *v11.InstrumentationScope
resource *v1.Resource
schemaURL string
}
func (s *readOnlySpan) Name() string {
return s.pb.Name
}
func (s *readOnlySpan) SpanContext() trace.SpanContext {
var tid trace.TraceID
copy(tid[:], s.pb.TraceId)
var sid trace.SpanID
copy(sid[:], s.pb.SpanId)
st, _ := trace.ParseTraceState(s.pb.TraceState)
return trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
SpanID: sid,
TraceState: st,
})
}
func (s *readOnlySpan) Parent() trace.SpanContext {
if len(s.pb.ParentSpanId) == 0 {
return trace.SpanContext{}
}
var tid trace.TraceID
copy(tid[:], s.pb.TraceId)
var psid trace.SpanID
copy(psid[:], s.pb.ParentSpanId)
return trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
SpanID: psid,
})
}
func (s *readOnlySpan) SpanKind() trace.SpanKind {
return spanKind(s.pb.Kind)
}
func (s *readOnlySpan) StartTime() time.Time {
return time.Unix(0, int64(s.pb.StartTimeUnixNano))
}
func (s *readOnlySpan) EndTime() time.Time {
return time.Unix(0, int64(s.pb.EndTimeUnixNano))
}
func (s *readOnlySpan) Attributes() []attribute.KeyValue {
return Attributes(s.pb.Attributes)
}
func (s *readOnlySpan) Links() []tracesdk.Link {
return links(s.pb.Links)
}
func (s *readOnlySpan) Events() []tracesdk.Event {
return spanEvents(s.pb.Events)
}
func (s *readOnlySpan) Status() tracesdk.Status {
return tracesdk.Status{
Code: statusCode(s.pb.Status),
Description: s.pb.Status.GetMessage(),
}
}
func (s *readOnlySpan) InstrumentationScope() instrumentation.Scope {
return instrumentationScope(s.is)
}
// Deprecated: use InstrumentationScope.
func (s *readOnlySpan) InstrumentationLibrary() instrumentation.Library {
return s.InstrumentationScope()
}
// Resource returns information about the entity that produced the span.
func (s *readOnlySpan) Resource() *resource.Resource {
if s.resource == nil {
return nil
}
if s.schemaURL != "" {
return resource.NewWithAttributes(s.schemaURL, Attributes(s.resource.Attributes)...)
}
return resource.NewSchemaless(Attributes(s.resource.Attributes)...)
}
// DroppedAttributes returns the number of attributes dropped by the span
// due to limits being reached.
func (s *readOnlySpan) DroppedAttributes() int {
return int(s.pb.DroppedAttributesCount)
}
// DroppedLinks returns the number of links dropped by the span due to
// limits being reached.
func (s *readOnlySpan) DroppedLinks() int {
return int(s.pb.DroppedLinksCount)
}
// DroppedEvents returns the number of events dropped by the span due to
// limits being reached.
func (s *readOnlySpan) DroppedEvents() int {
return int(s.pb.DroppedEventsCount)
}
// ChildSpanCount returns the count of spans that consider the span a
// direct parent.
func (s *readOnlySpan) ChildSpanCount() int {
return 0
}
var _ tracesdk.ReadOnlySpan = &readOnlySpan{}
// status transform a OTLP span status into span code.
func statusCode(st *tracepb.Status) codes.Code {
if st == nil {
return codes.Unset
}
switch st.Code {
case tracepb.Status_STATUS_CODE_ERROR:
return codes.Error
default:
return codes.Ok
}
}
// links transforms OTLP span links to span Links.
func links(links []*tracepb.Span_Link) []tracesdk.Link {
if len(links) == 0 {
return nil
}
sl := make([]tracesdk.Link, 0, len(links))
for _, otLink := range links {
if otLink == nil {
continue
}
// This redefinition is necessary to prevent otLink.*ID[:] copies
// being reused -- in short we need a new otLink per iteration.
otLink := otLink
var tid trace.TraceID
copy(tid[:], otLink.TraceId)
var sid trace.SpanID
copy(sid[:], otLink.SpanId)
sctx := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
SpanID: sid,
})
sl = append(sl, tracesdk.Link{
SpanContext: sctx,
Attributes: Attributes(otLink.Attributes),
})
}
return sl
}
// spanEvents transforms OTLP span events to span Events.
func spanEvents(es []*tracepb.Span_Event) []tracesdk.Event {
if len(es) == 0 {
return nil
}
evCount := len(es)
if evCount > maxMessageEventsPerSpan {
evCount = maxMessageEventsPerSpan
}
events := make([]tracesdk.Event, 0, evCount)
messageEvents := 0
// Transform message events
for _, e := range es {
if messageEvents >= maxMessageEventsPerSpan {
break
}
if e == nil {
continue
}
messageEvents++
events = append(events,
tracesdk.Event{
Name: e.Name,
Time: time.Unix(0, int64(e.TimeUnixNano)),
Attributes: Attributes(e.Attributes),
DroppedAttributeCount: int(e.DroppedAttributesCount),
},
)
}
return events
}
// spanKind transforms a an OTLP span kind to SpanKind.
func spanKind(kind tracepb.Span_SpanKind) trace.SpanKind {
switch kind {
case tracepb.Span_SPAN_KIND_INTERNAL:
return trace.SpanKindInternal
case tracepb.Span_SPAN_KIND_CLIENT:
return trace.SpanKindClient
case tracepb.Span_SPAN_KIND_SERVER:
return trace.SpanKindServer
case tracepb.Span_SPAN_KIND_PRODUCER:
return trace.SpanKindProducer
case tracepb.Span_SPAN_KIND_CONSUMER:
return trace.SpanKindConsumer
default:
return trace.SpanKindUnspecified
}
}
|