|
1
|
|
|
package otelroundtripper |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"go.opentelemetry.io/otel/metric/noop" |
|
5
|
|
|
"net/http" |
|
6
|
|
|
"testing" |
|
7
|
|
|
|
|
8
|
|
|
"go.opentelemetry.io/otel/attribute" |
|
9
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.18.0" |
|
10
|
|
|
|
|
11
|
|
|
"github.com/stretchr/testify/assert" |
|
12
|
|
|
) |
|
13
|
|
|
|
|
14
|
|
|
func TestWithParent(t *testing.T) { |
|
15
|
|
|
t.Run("parent is not set when the parent is nil", func(t *testing.T) { |
|
16
|
|
|
// Setup |
|
17
|
|
|
t.Parallel() |
|
18
|
|
|
|
|
19
|
|
|
// Arrange |
|
20
|
|
|
cfg := defaultConfig() |
|
21
|
|
|
|
|
22
|
|
|
// Act |
|
23
|
|
|
WithParent(nil).apply(cfg) |
|
24
|
|
|
|
|
25
|
|
|
// Assert |
|
26
|
|
|
assert.NotNil(t, cfg.parent) |
|
27
|
|
|
}) |
|
28
|
|
|
|
|
29
|
|
|
t.Run("parent is set when the parent is not nil", func(t *testing.T) { |
|
30
|
|
|
// Setup |
|
31
|
|
|
t.Parallel() |
|
32
|
|
|
|
|
33
|
|
|
// Arrange |
|
34
|
|
|
cfg := defaultConfig() |
|
35
|
|
|
parent := &http.Transport{} |
|
36
|
|
|
|
|
37
|
|
|
// Act |
|
38
|
|
|
WithParent(parent).apply(cfg) |
|
39
|
|
|
|
|
40
|
|
|
// Assert |
|
41
|
|
|
assert.NotNil(t, cfg.parent) |
|
42
|
|
|
assert.Equal(t, parent, cfg.parent) |
|
43
|
|
|
}) |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
func TestWithName(t *testing.T) { |
|
47
|
|
|
t.Run("name is not set when the parent is an empty string", func(t *testing.T) { |
|
48
|
|
|
// Setup |
|
49
|
|
|
t.Parallel() |
|
50
|
|
|
|
|
51
|
|
|
// Arrange |
|
52
|
|
|
cfg := defaultConfig() |
|
53
|
|
|
|
|
54
|
|
|
// Act |
|
55
|
|
|
WithName(" ").apply(cfg) |
|
56
|
|
|
|
|
57
|
|
|
// Assert |
|
58
|
|
|
assert.Equal(t, "http.client", cfg.name) |
|
59
|
|
|
}) |
|
60
|
|
|
|
|
61
|
|
|
t.Run("name is set when the name is not an empty string", func(t *testing.T) { |
|
62
|
|
|
// Setup |
|
63
|
|
|
t.Parallel() |
|
64
|
|
|
|
|
65
|
|
|
// Arrange |
|
66
|
|
|
cfg := defaultConfig() |
|
67
|
|
|
name := " name " |
|
68
|
|
|
|
|
69
|
|
|
// Act |
|
70
|
|
|
WithName(name).apply(cfg) |
|
71
|
|
|
|
|
72
|
|
|
// Assert |
|
73
|
|
|
assert.Equal(t, "name", cfg.name) |
|
74
|
|
|
}) |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
func TestWithMeter(t *testing.T) { |
|
78
|
|
|
t.Run("meter is set successfully", func(t *testing.T) { |
|
79
|
|
|
// Setup |
|
80
|
|
|
t.Parallel() |
|
81
|
|
|
|
|
82
|
|
|
// Arrange |
|
83
|
|
|
cfg := defaultConfig() |
|
84
|
|
|
meter := noop.NewMeterProvider().Meter("http.client") |
|
85
|
|
|
|
|
86
|
|
|
// Act |
|
87
|
|
|
WithMeter(meter).apply(cfg) |
|
88
|
|
|
|
|
89
|
|
|
// Assert |
|
90
|
|
|
assert.Equal(t, meter, cfg.meter) |
|
91
|
|
|
}) |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
func TestWithAttributes(t *testing.T) { |
|
95
|
|
|
t.Run("attributes are successfully", func(t *testing.T) { |
|
96
|
|
|
// Setup |
|
97
|
|
|
t.Parallel() |
|
98
|
|
|
|
|
99
|
|
|
// Arrange |
|
100
|
|
|
cfg := defaultConfig() |
|
101
|
|
|
attributes := []attribute.KeyValue{ |
|
102
|
|
|
semconv.ServiceNamespaceKey.String("namespace"), |
|
103
|
|
|
semconv.ServiceInstanceIDKey.Int(1), |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// Act |
|
107
|
|
|
WithAttributes(attributes...).apply(cfg) |
|
108
|
|
|
|
|
109
|
|
|
// Assert |
|
110
|
|
|
assert.Equal(t, attributes, cfg.attributes) |
|
111
|
|
|
}) |
|
112
|
|
|
} |
|
113
|
|
|
|