This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
span.go
197 lines (181 loc) · 5.6 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
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadog.com/).
// Copyright 2018 Datadog, Inc.
package datadog
import (
"encoding/binary"
"fmt"
"net/http"
"strconv"
"go.opencensus.io/trace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
)
// statusCodes maps (*trace.SpanData).Status.Code to their message and http status code. See:
// https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto.
var statusCodes = map[int32]codeDetails{
trace.StatusCodeOK: {message: "OK", status: http.StatusOK},
trace.StatusCodeCancelled: {message: "CANCELLED", status: 499},
trace.StatusCodeUnknown: {message: "UNKNOWN", status: http.StatusInternalServerError},
trace.StatusCodeInvalidArgument: {message: "INVALID_ARGUMENT", status: http.StatusBadRequest},
trace.StatusCodeDeadlineExceeded: {message: "DEADLINE_EXCEEDED", status: http.StatusGatewayTimeout},
trace.StatusCodeNotFound: {message: "NOT_FOUND", status: http.StatusNotFound},
trace.StatusCodeAlreadyExists: {message: "ALREADY_EXISTS", status: http.StatusConflict},
trace.StatusCodePermissionDenied: {message: "PERMISSION_DENIED", status: http.StatusForbidden},
trace.StatusCodeResourceExhausted: {message: "RESOURCE_EXHAUSTED", status: http.StatusTooManyRequests},
trace.StatusCodeFailedPrecondition: {message: "FAILED_PRECONDITION", status: http.StatusBadRequest},
trace.StatusCodeAborted: {message: "ABORTED", status: http.StatusConflict},
trace.StatusCodeOutOfRange: {message: "OUT_OF_RANGE", status: http.StatusBadRequest},
trace.StatusCodeUnimplemented: {message: "UNIMPLEMENTED", status: http.StatusNotImplemented},
trace.StatusCodeInternal: {message: "INTERNAL", status: http.StatusInternalServerError},
trace.StatusCodeUnavailable: {message: "UNAVAILABLE", status: http.StatusServiceUnavailable},
trace.StatusCodeDataLoss: {message: "DATA_LOSS", status: http.StatusNotImplemented},
trace.StatusCodeUnauthenticated: {message: "UNAUTHENTICATED", status: http.StatusUnauthorized},
}
// codeDetails specifies information about a trace status code.
type codeDetails struct {
message string // status message
status int // corresponding HTTP status code
}
// convertSpan takes an OpenCensus span and returns a Datadog span.
func (e *traceExporter) convertSpan(s *trace.SpanData) *ddSpan {
startNano := s.StartTime.UnixNano()
span := &ddSpan{
TraceID: binary.BigEndian.Uint64(s.SpanContext.TraceID[8:]),
SpanID: binary.BigEndian.Uint64(s.SpanContext.SpanID[:]),
Name: "opencensus",
Resource: s.Name,
Service: e.opts.Service,
Start: startNano,
Duration: s.EndTime.UnixNano() - startNano,
Metrics: map[string]float64{},
Meta: map[string]string{},
}
if s.ParentSpanID != (trace.SpanID{}) {
span.ParentID = binary.BigEndian.Uint64(s.ParentSpanID[:])
}
code, ok := statusCodes[s.Status.Code]
if !ok {
code = codeDetails{
message: "ERR_CODE_" + strconv.FormatInt(int64(s.Status.Code), 10),
status: http.StatusInternalServerError,
}
}
switch s.SpanKind {
case trace.SpanKindClient:
span.Type = "client"
if code.status/100 == 4 {
span.Error = 1
}
case trace.SpanKindServer:
span.Type = "server"
if code.status/100 == 5 {
span.Error = 1
}
default:
if code.status/100 == 4 || code.status/100 == 5 {
span.Error = 1
}
}
if span.Error == 1 {
span.Meta[ext.ErrorType] = code.message
if msg := s.Status.Message; msg != "" {
span.Meta[ext.ErrorMsg] = msg
}
}
span.Meta[keyStatusCode] = strconv.Itoa(int(s.Status.Code))
span.Meta[keyStatus] = code.message
if msg := s.Status.Message; msg != "" {
span.Meta[keyStatusDescription] = msg
}
for key, val := range e.opts.GlobalTags {
setTag(span, key, val)
}
for key, val := range s.Attributes {
setTag(span, key, val)
}
return span
}
const (
keySamplingPriority = "_sampling_priority_v1"
keyStatusDescription = "opencensus.status_description"
keyStatusCode = "opencensus.status_code"
keyStatus = "opencensus.status"
keySpanName = "span.name"
keySamplingPriorityRate = "_sampling_priority_rate_v1"
)
func setTag(s *ddSpan, key string, val interface{}) {
if key == ext.Error {
setError(s, val)
return
}
switch v := val.(type) {
case string:
setStringTag(s, key, v)
case bool:
if v {
setStringTag(s, key, "true")
} else {
setStringTag(s, key, "false")
}
case float64:
setMetric(s, key, v)
case int64:
setMetric(s, key, float64(v))
default:
// should never happen according to docs, nevertheless
// we should account for this to avoid exceptions
setStringTag(s, key, fmt.Sprintf("%v", v))
}
}
func setMetric(s *ddSpan, key string, v float64) {
switch key {
case ext.SamplingPriority:
s.Metrics[keySamplingPriority] = v
default:
s.Metrics[key] = v
}
}
func setStringTag(s *ddSpan, key, v string) {
switch key {
case ext.ServiceName:
s.Service = v
case ext.ResourceName:
s.Resource = v
case ext.SpanType:
s.Type = v
case ext.AnalyticsEvent:
if v != "false" {
setMetric(s, ext.EventSampleRate, 1)
} else {
setMetric(s, ext.EventSampleRate, 0)
}
case keySpanName:
s.Name = v
default:
s.Meta[key] = v
}
}
func setError(s *ddSpan, val interface{}) {
switch v := val.(type) {
case string:
s.Error = 1
s.Meta[ext.ErrorMsg] = v
case bool:
if v {
s.Error = 1
} else {
s.Error = 0
}
case int64:
if v > 0 {
s.Error = 1
} else {
s.Error = 0
}
case nil:
s.Error = 0
default:
s.Error = 1
}
}