-
Notifications
You must be signed in to change notification settings - Fork 8
/
opentelemetry_test.go
272 lines (237 loc) · 6.85 KB
/
opentelemetry_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package gocbopentelemetry
import (
"context"
"flag"
"fmt"
"os"
"sort"
"testing"
"time"
"github.com/couchbase/gocb/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)
func envFlagString(envName, name, value, usage string) *string {
envValue := os.Getenv(envName)
if envValue != "" {
value = envValue
}
return flag.String(name, value, usage)
}
var server, user, password, bucket string
func TestMain(m *testing.M) {
serverFlag := envFlagString("GOCBSERVER", "server", "localhost",
"The connection string to connect to for a real server")
userFlag := envFlagString("GOCBUSER", "user", "Administrator",
"The username to use to authenticate when using a real server")
passwordFlag := envFlagString("GOCBPASS", "pass", "password",
"The password to use to authenticate when using a real server")
bucketFlag := envFlagString("GOCBBUCKET", "bucket", "default",
"The bucket to use to test against")
flag.Parse()
server = *serverFlag
user = *userFlag
password = *passwordFlag
bucket = *bucketFlag
result := m.Run()
os.Exit(result)
}
func TestOpenTelemetryTracer(t *testing.T) {
gocb.SetLogger(gocb.VerboseStdioLogger())
ctx := context.Background()
exporter := tracetest.NewInMemoryExporter()
defer exporter.Shutdown(ctx)
bsp := sdktrace.NewSimpleSpanProcessor(exporter)
defer bsp.Shutdown(ctx)
tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(bsp))
defer tp.Shutdown(ctx)
otel.SetTracerProvider(tp)
tracer := tp.Tracer("test-demo")
cluster, err := gocb.Connect(server, gocb.ClusterOptions{
Authenticator: gocb.PasswordAuthenticator{
Username: user,
Password: password,
},
Tracer: NewOpenTelemetryRequestTracer(tp),
})
require.Nil(t, err)
defer cluster.Close(nil)
b := cluster.Bucket(bucket)
err = b.WaitUntilReady(5*time.Second, nil)
require.Nil(t, err, err)
col := b.DefaultCollection()
// First operation to ensure that cid fetches have already happened and that the connections are good to go.
_, err = col.Upsert("someid", "someval", nil)
require.Nil(t, err)
// Force flush the processor and then reset the exporter so that we only get spans that we want.
assert.NoError(t, bsp.ForceFlush(ctx))
exporter.Reset()
ctx, span := tracer.Start(ctx, "myparentoperation")
_, err = col.Upsert("someid", "someval", &gocb.UpsertOptions{
ParentSpan: NewOpenTelemetryRequestSpan(ctx, span),
})
require.Nil(t, err)
span.End()
assert.NoError(t, bsp.ForceFlush(ctx))
spans := exporter.GetSpans()
if len(spans) != 5 {
t.Fatalf("Expected 5 spans but got %d", len(spans))
} // myparentoperation, upsert, encoding, CMD_SET, dispatch
sort.SliceStable(spans, func(i, j int) bool {
return spans[i].StartTime.Before(spans[j].StartTime)
})
assertOTSpan(t, spans[0], "myparentoperation", []attribute.KeyValue{})
assertOTSpan(t, spans[1], "upsert", []attribute.KeyValue{
{
Key: "db.system",
Value: attribute.StringValue("couchbase"),
},
{
Key: "db.couchbase.service",
Value: attribute.StringValue("kv"),
},
{
Key: "db.name",
Value: attribute.StringValue(b.Name()),
},
{
Key: "db.couchbase.scope",
Value: attribute.StringValue("_default"),
},
{
Key: "db.couchbase.collection",
Value: attribute.StringValue("_default"),
},
{
Key: "db.operation",
Value: attribute.StringValue("upsert"),
},
})
assertOTSpan(t, spans[2], "request_encoding", []attribute.KeyValue{
{
Key: "db.system",
Value: attribute.StringValue("couchbase"),
},
})
assertOTSpan(t, spans[3], "CMD_SET", []attribute.KeyValue{
{
Key: "db.system",
Value: attribute.StringValue("couchbase"),
},
{
Key: "db.couchbase.retries",
Value: attribute.StringValue(""),
},
})
assertOTSpan(t, spans[4], "dispatch_to_server", []attribute.KeyValue{
{
Key: "db.system",
Value: attribute.StringValue("couchbase"),
},
{
Key: "net.transport",
Value: attribute.StringValue("IP.TCP"),
},
{
Key: "db.couchbase.operation_id",
Value: attribute.StringValue(""),
},
{
Key: "db.couchbase.local_id",
Value: attribute.StringValue(""),
},
{
Key: "net.host.name",
Value: attribute.StringValue(""),
},
{
Key: "net.host.port",
Value: attribute.StringValue(""),
},
{
Key: "net.peer.name",
Value: attribute.StringValue(""),
},
{
Key: "net.peer.port",
Value: attribute.StringValue(""),
},
{
Key: "db.couchbase.server_duration",
Value: attribute.StringValue(""),
},
})
}
func TestOpenTelemetryMeter(t *testing.T) {
gocb.SetLogger(gocb.VerboseStdioLogger())
rdr := metric.NewManualReader()
provider := metric.NewMeterProvider(
metric.WithReader(rdr),
)
cluster, err := gocb.Connect(server, gocb.ClusterOptions{
Authenticator: gocb.PasswordAuthenticator{
Username: user,
Password: password,
},
Meter: NewOpenTelemetryMeter(provider),
})
require.Nil(t, err)
defer cluster.Close(nil)
b := cluster.Bucket(bucket)
err = b.WaitUntilReady(5*time.Second, nil)
require.Nil(t, err, err)
col := b.DefaultCollection()
_, err = col.Upsert("someid", "someval", nil)
require.Nil(t, err)
_, err = col.Get("someid", nil)
require.Nil(t, err)
var data metricdata.ResourceMetrics
_ = rdr.Collect(context.Background(), &data)
require.Len(t, data.ScopeMetrics, 1)
require.Len(t, data.ScopeMetrics[0].Metrics, 1)
histogram, ok := data.ScopeMetrics[0].Metrics[0].Data.(metricdata.Histogram[int64])
require.True(t, ok)
assertOTMetric(t, histogram.DataPoints[0], "upsert")
assertOTMetric(t, histogram.DataPoints[1], "get")
}
func assertOTSpan(t *testing.T, span tracetest.SpanStub, name string, attribs []attribute.KeyValue) {
assert.NotZero(t, span.StartTime)
assert.NotZero(t, span.EndTime)
assert.Equal(t, name, span.Name)
require.Len(t, span.Attributes, len(attribs))
for _, attrib := range attribs {
var found bool
for _, a := range span.Attributes {
if attrib.Key == a.Key {
// otel doesn't have a nil value type so we have to use empty string.
if attrib.Value.AsString() == "" {
assert.NotEmpty(t, a.Value)
} else {
assert.Equal(t, attrib.Value, a.Value)
}
found = true
break
}
}
assert.True(t, found, fmt.Sprintf("key not found: %s", attrib.Key))
}
}
func assertOTMetric(t *testing.T, metric metricdata.HistogramDataPoint[int64], name string) {
require.EqualValues(t, metric.Attributes.Len(), 2)
expectedKeys := []attribute.KeyValue{
attribute.String("db.couchbase.service", "kv"),
attribute.String("db.operation", name),
}
for _, val := range expectedKeys {
v, found := metric.Attributes.Value(val.Key)
assert.True(t, found)
assert.Equal(t, val.Value, v)
}
require.EqualValues(t, metric.Count, 1)
}