-
Notifications
You must be signed in to change notification settings - Fork 197
/
spancontext_test.go
173 lines (160 loc) · 4.5 KB
/
spancontext_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
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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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.
package apm_test
import (
"context"
"fmt"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.elastic.co/apm/v2"
"go.elastic.co/apm/v2/apmtest"
"go.elastic.co/apm/v2/model"
)
func TestSpanContextSetLabel(t *testing.T) {
_, spans, _ := apmtest.WithTransaction(func(ctx context.Context) {
span, _ := apm.StartSpan(ctx, "name", "type")
span.Context.SetLabel("foo", "bar")
span.Context.SetLabel("foo", "bar!") // Last instance wins
span.Context.SetLabel("bar", "baz")
span.Context.SetLabel("baz", 123.456)
span.Context.SetLabel("qux", true)
span.End()
})
require.Len(t, spans, 1)
assert.Equal(t, model.IfaceMap{
{Key: "bar", Value: "baz"},
{Key: "baz", Value: 123.456},
{Key: "foo", Value: "bar!"},
{Key: "qux", Value: true},
}, spans[0].Context.Tags)
}
func TestSpanContextSetHTTPRequest(t *testing.T) {
type testcase struct {
url string
addr string
port int
name string
resource string
}
testcases := []testcase{{
url: "http://localhost/foo/bar",
addr: "localhost",
port: 80,
name: "http://localhost",
resource: "localhost:80",
}, {
url: "http://localhost:80/foo/bar",
addr: "localhost",
port: 80,
name: "http://localhost",
resource: "localhost:80",
}, {
url: "https://[::1]/foo/bar",
addr: "::1",
port: 443,
name: "https://[::1]",
resource: "[::1]:443",
}, {
url: "https://[::1]:8443/foo/bar",
addr: "::1",
port: 8443,
name: "https://[::1]:8443",
resource: "[::1]:8443",
}, {
url: "gopher://gopher.invalid:70",
addr: "gopher.invalid",
port: 70,
name: "gopher://gopher.invalid:70",
resource: "gopher.invalid:70",
}, {
url: "gopher://gopher.invalid",
addr: "gopher.invalid",
port: 0,
name: "gopher://gopher.invalid",
resource: "gopher.invalid",
}}
for _, tc := range testcases {
t.Run(tc.url, func(t *testing.T) {
url, err := url.Parse(tc.url)
require.NoError(t, err)
_, spans, _ := apmtest.WithTransaction(func(ctx context.Context) {
span, _ := apm.StartSpanOptions(ctx, "name", "http", apm.SpanOptions{
ExitSpan: true,
})
span.Context.SetHTTPRequest(&http.Request{URL: url})
span.End()
})
require.Len(t, spans, 1)
assert.Equal(t, &model.DestinationSpanContext{
Address: tc.addr,
Port: tc.port,
Service: &model.DestinationServiceSpanContext{
Type: spans[0].Type,
Name: tc.name,
Resource: tc.resource,
},
}, spans[0].Context.Destination)
assert.Equal(t, &model.ServiceTargetSpanContext{
Type: "http",
Name: tc.resource,
}, spans[0].Context.Service.Target)
})
}
}
func TestSetDestinationService(t *testing.T) {
type testcase struct {
name string
resource string
expectEmpty bool
}
testcases := []testcase{{
resource: "",
expectEmpty: true,
}, {
resource: "nonempty",
expectEmpty: false,
}, {
resource: "",
expectEmpty: true,
}, {
resource: "nonempty",
}}
for _, tc := range testcases {
t.Run(fmt.Sprintf("%s_%s", tc.name, tc.resource), func(t *testing.T) {
_, spans, _ := apmtest.WithTransaction(func(ctx context.Context) {
span, _ := apm.StartSpan(ctx, "name", "span_type")
span.Context.SetDestinationAddress("testing.invalid", 123)
span.Context.SetDestinationService(apm.DestinationServiceSpanContext{
Resource: tc.resource,
})
span.End()
})
require.Len(t, spans, 1)
if tc.expectEmpty {
assert.Nil(t, spans[0].Context.Destination.Service)
} else {
assert.Equal(t, &model.DestinationServiceSpanContext{
Resource: tc.resource,
Type: "span_type",
}, spans[0].Context.Destination.Service)
}
})
}
}