2020-03-25 18:04:31 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2020-03-07 08:01:02 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-03-25 18:04:31 +02:00
|
|
|
package transform
|
2020-03-07 08:01:02 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
|
|
|
|
tracepb "github.com/open-telemetry/opentelemetry-proto/gen/go/trace/v1"
|
|
|
|
|
|
|
|
apitrace "go.opentelemetry.io/otel/api/trace"
|
|
|
|
export "go.opentelemetry.io/otel/sdk/export/trace"
|
2020-03-25 18:04:31 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
2020-03-07 08:01:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxMessageEventsPerSpan = 128
|
|
|
|
)
|
|
|
|
|
2020-03-25 18:04:31 +02:00
|
|
|
// SpanData transforms a slice of SpanData into a slice of OTLP ResourceSpans.
|
|
|
|
func SpanData(sdl []*export.SpanData) []*tracepb.ResourceSpans {
|
|
|
|
if len(sdl) == 0 {
|
2020-03-13 22:07:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-03-25 18:04:31 +02:00
|
|
|
rsm := make(map[*resource.Resource]*tracepb.ResourceSpans)
|
|
|
|
|
|
|
|
for _, sd := range sdl {
|
|
|
|
if sd != nil {
|
|
|
|
rs, ok := rsm[sd.Resource]
|
|
|
|
if !ok {
|
|
|
|
rs = &tracepb.ResourceSpans{
|
|
|
|
Resource: Resource(sd.Resource),
|
|
|
|
InstrumentationLibrarySpans: []*tracepb.InstrumentationLibrarySpans{
|
|
|
|
{
|
|
|
|
Spans: []*tracepb.Span{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
rsm[sd.Resource] = rs
|
|
|
|
}
|
|
|
|
rs.InstrumentationLibrarySpans[0].Spans =
|
|
|
|
append(rs.InstrumentationLibrarySpans[0].Spans, span(sd))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rss := make([]*tracepb.ResourceSpans, 0, len(rsm))
|
|
|
|
for _, rs := range rsm {
|
|
|
|
rss = append(rss, rs)
|
2020-03-13 22:07:36 +02:00
|
|
|
}
|
2020-03-25 18:04:31 +02:00
|
|
|
return rss
|
2020-03-13 22:07:36 +02:00
|
|
|
}
|
|
|
|
|
2020-03-25 18:04:31 +02:00
|
|
|
// span transforms a Span into an OTLP span.
|
|
|
|
func span(sd *export.SpanData) *tracepb.Span {
|
2020-03-07 08:01:02 +02:00
|
|
|
if sd == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &tracepb.Span{
|
|
|
|
TraceId: sd.SpanContext.TraceID[:],
|
|
|
|
SpanId: sd.SpanContext.SpanID[:],
|
|
|
|
ParentSpanId: sd.ParentSpanID[:],
|
2020-03-25 18:04:31 +02:00
|
|
|
Status: status(sd.StatusCode, sd.StatusMessage),
|
2020-03-24 17:45:07 +02:00
|
|
|
StartTimeUnixNano: uint64(sd.StartTime.Nanosecond()),
|
|
|
|
EndTimeUnixNano: uint64(sd.EndTime.Nanosecond()),
|
2020-03-25 18:04:31 +02:00
|
|
|
Links: links(sd.Links),
|
|
|
|
Kind: spanKind(sd.SpanKind),
|
2020-03-07 08:01:02 +02:00
|
|
|
Name: sd.Name,
|
2020-03-25 18:04:31 +02:00
|
|
|
Attributes: Attributes(sd.Attributes),
|
|
|
|
Events: spanEvents(sd.MessageEvents),
|
2020-03-07 08:01:02 +02:00
|
|
|
// TODO (rghetia): Add Tracestate: when supported.
|
|
|
|
DroppedAttributesCount: uint32(sd.DroppedAttributeCount),
|
|
|
|
DroppedEventsCount: uint32(sd.DroppedMessageEventCount),
|
|
|
|
DroppedLinksCount: uint32(sd.DroppedLinkCount),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 18:04:31 +02:00
|
|
|
// status transform a span code and message into an OTLP span status.
|
|
|
|
func status(status codes.Code, message string) *tracepb.Status {
|
2020-03-07 08:01:02 +02:00
|
|
|
return &tracepb.Status{
|
2020-03-07 20:38:59 +02:00
|
|
|
Code: tracepb.Status_StatusCode(status),
|
|
|
|
Message: message,
|
2020-03-07 08:01:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 18:04:31 +02:00
|
|
|
// links transforms span Links to OTLP span links.
|
|
|
|
func links(links []apitrace.Link) []*tracepb.Span_Link {
|
2020-03-07 08:01:02 +02:00
|
|
|
if len(links) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sl := make([]*tracepb.Span_Link, 0, len(links))
|
|
|
|
for _, otLink := range links {
|
|
|
|
// This redefinition is necessary to prevent otLink.*ID[:] copies
|
|
|
|
// being reused -- in short we need a new otLink per iteration.
|
|
|
|
otLink := otLink
|
|
|
|
|
|
|
|
sl = append(sl, &tracepb.Span_Link{
|
|
|
|
TraceId: otLink.TraceID[:],
|
|
|
|
SpanId: otLink.SpanID[:],
|
2020-03-25 18:04:31 +02:00
|
|
|
Attributes: Attributes(otLink.Attributes),
|
2020-03-07 08:01:02 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return sl
|
|
|
|
}
|
|
|
|
|
2020-03-25 18:04:31 +02:00
|
|
|
// spanEvents transforms span Events to an OTLP span events.
|
|
|
|
func spanEvents(es []export.Event) []*tracepb.Span_Event {
|
2020-03-07 08:01:02 +02:00
|
|
|
if len(es) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
evCount := len(es)
|
|
|
|
if evCount > maxMessageEventsPerSpan {
|
|
|
|
evCount = maxMessageEventsPerSpan
|
|
|
|
}
|
|
|
|
events := make([]*tracepb.Span_Event, 0, evCount)
|
|
|
|
messageEvents := 0
|
|
|
|
|
|
|
|
// Transform message events
|
|
|
|
for _, e := range es {
|
|
|
|
if messageEvents >= maxMessageEventsPerSpan {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
messageEvents++
|
|
|
|
events = append(events,
|
|
|
|
&tracepb.Span_Event{
|
2020-03-25 18:04:31 +02:00
|
|
|
Name: e.Name,
|
2020-03-24 17:45:07 +02:00
|
|
|
TimeUnixNano: uint64(e.Time.Nanosecond()),
|
2020-03-25 18:04:31 +02:00
|
|
|
Attributes: Attributes(e.Attributes),
|
2020-03-07 08:01:02 +02:00
|
|
|
// TODO (rghetia) : Add Drop Counts when supported.
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return events
|
|
|
|
}
|
|
|
|
|
2020-03-25 18:04:31 +02:00
|
|
|
// spanKind transforms a SpanKind to an OTLP span kind.
|
|
|
|
func spanKind(kind apitrace.SpanKind) tracepb.Span_SpanKind {
|
2020-03-07 08:01:02 +02:00
|
|
|
switch kind {
|
|
|
|
case apitrace.SpanKindInternal:
|
|
|
|
return tracepb.Span_INTERNAL
|
|
|
|
case apitrace.SpanKindClient:
|
|
|
|
return tracepb.Span_CLIENT
|
|
|
|
case apitrace.SpanKindServer:
|
|
|
|
return tracepb.Span_SERVER
|
|
|
|
case apitrace.SpanKindProducer:
|
|
|
|
return tracepb.Span_PRODUCER
|
|
|
|
case apitrace.SpanKindConsumer:
|
|
|
|
return tracepb.Span_CONSUMER
|
|
|
|
default:
|
|
|
|
return tracepb.Span_SPAN_KIND_UNSPECIFIED
|
|
|
|
}
|
|
|
|
}
|