forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector_test.go
113 lines (99 loc) · 3.07 KB
/
connector_test.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package exceptionsconnector
import (
"time"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
)
const (
stringAttrName = "stringAttrName"
intAttrName = "intAttrName"
doubleAttrName = "doubleAttrName"
boolAttrName = "boolAttrName"
nullAttrName = "nullAttrName"
mapAttrName = "mapAttrName"
arrayAttrName = "arrayAttrName"
notInSpanAttrName0 = "shouldBeInMetric"
notInSpanAttrName1 = "shouldNotBeInMetric"
sampleLatency = float64(11)
sampleLatencyDuration = time.Duration(sampleLatency) * time.Millisecond
)
type serviceSpans struct {
serviceName string
spans []span
}
type span struct {
name string
kind ptrace.SpanKind
statusCode ptrace.StatusCode
}
// buildSampleTrace builds the following trace:
//
// service-a (server) ->
// service-a (client) ->
// service-b (server)
func buildSampleTrace() ptrace.Traces {
traces := ptrace.NewTraces()
initServiceSpans(
serviceSpans{
serviceName: "service-a",
spans: []span{
{
name: "svc-a-ep1",
kind: ptrace.SpanKindServer,
statusCode: ptrace.StatusCodeError,
},
{
name: "svc-a-ep2",
kind: ptrace.SpanKindClient,
statusCode: ptrace.StatusCodeError,
},
},
}, traces.ResourceSpans().AppendEmpty())
initServiceSpans(
serviceSpans{
serviceName: "service-b",
spans: []span{
{
name: "svc-b-ep1",
kind: ptrace.SpanKindServer,
statusCode: ptrace.StatusCodeError,
},
},
}, traces.ResourceSpans().AppendEmpty())
initServiceSpans(serviceSpans{}, traces.ResourceSpans().AppendEmpty())
return traces
}
func initServiceSpans(serviceSpans serviceSpans, spans ptrace.ResourceSpans) {
if serviceSpans.serviceName != "" {
spans.Resource().Attributes().PutStr(conventions.AttributeServiceName, serviceSpans.serviceName)
}
ils := spans.ScopeSpans().AppendEmpty()
for _, span := range serviceSpans.spans {
initSpan(span, ils.Spans().AppendEmpty())
}
}
func initSpan(span span, s ptrace.Span) {
s.SetKind(span.kind)
s.SetName(span.name)
s.Status().SetCode(span.statusCode)
now := time.Now()
s.SetStartTimestamp(pcommon.NewTimestampFromTime(now))
s.SetEndTimestamp(pcommon.NewTimestampFromTime(now.Add(sampleLatencyDuration)))
s.Attributes().PutStr(stringAttrName, "stringAttrValue")
s.Attributes().PutInt(intAttrName, 99)
s.Attributes().PutDouble(doubleAttrName, 99.99)
s.Attributes().PutBool(boolAttrName, true)
s.Attributes().PutEmpty(nullAttrName)
s.Attributes().PutEmptyMap(mapAttrName)
s.Attributes().PutEmptySlice(arrayAttrName)
s.SetTraceID(pcommon.TraceID([16]byte{byte(42)}))
s.SetSpanID(pcommon.SpanID([8]byte{byte(42)}))
e := s.Events().AppendEmpty()
e.SetName("exception")
e.Attributes().PutStr(exceptionTypeKey, "Exception")
e.Attributes().PutStr(exceptionMessageKey, "Exception message")
e.Attributes().PutStr(exceptionStacktraceKey, "Exception stacktrace")
}