forked from krak3n/opentelemetry-go-datadog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspan.go
57 lines (49 loc) · 1.62 KB
/
span.go
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
package datadog
//go:generate go run github.com/tinylib/msgp -marshal=false -o=msgp.go -tests=false
import (
"encoding/binary"
traceAPI "go.opentelemetry.io/otel/api/trace"
traceSDK "go.opentelemetry.io/otel/sdk/export/trace"
)
// Span is a DataDog Span
type Span struct {
SpanID uint64 `msg:"span_id"`
TraceID uint64 `msg:"trace_id"`
ParentID uint64 `msg:"parent_id"`
Name string `msg:"name"`
Service string `msg:"service"`
Resource string `msg:"resource"`
Type string `msg:"type"`
Start int64 `msg:"start"`
Duration int64 `msg:"duration"`
Meta map[string]string `msg:"meta,omitempty"`
Metrics map[string]float64 `msg:"metrics,omitempty"`
Error int32 `msg:"error"`
}
// ConvertSpan converts an OpenTelemetry span to a DataDog span.
func ConvertSpan(s *traceSDK.SpanData) *Span {
span := &Span{
TraceID: binary.BigEndian.Uint64(s.SpanContext.TraceID[8:]),
SpanID: binary.BigEndian.Uint64(s.SpanContext.SpanID[:]),
Name: "opentelemetry",
Resource: s.Name,
Start: s.StartTime.UnixNano(),
Duration: s.EndTime.Sub(s.StartTime).Nanoseconds(),
Metrics: map[string]float64{},
Meta: map[string]string{},
}
if s.ParentSpanID.IsValid() {
span.ParentID = binary.BigEndian.Uint64(s.ParentSpanID[:])
}
switch s.SpanKind {
case traceAPI.SpanKindClient:
span.Type = "client"
case traceAPI.SpanKindServer:
span.Type = "server"
case traceAPI.SpanKindProducer:
span.Type = "producer"
case traceAPI.SpanKindConsumer:
span.Type = "consumer"
}
return span
}