-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrace_test.go
More file actions
112 lines (105 loc) · 2.35 KB
/
trace_test.go
File metadata and controls
112 lines (105 loc) · 2.35 KB
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
package cmd_test
import (
"context"
"io"
"testing"
"github.com/hamba/cmd/v3"
"github.com/hamba/logger/v2"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v3"
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
)
func TestNewTracer(t *testing.T) {
log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Error)
tests := []struct {
name string
args []string
wantErr assert.ErrorAssertionFunc
}{
{
name: "no tracer",
args: []string{},
wantErr: assert.NoError,
},
{
name: "otelhttp",
args: []string{
"--tracing.exporter=otlphttp",
"--tracing.endpoint=http://localhost:1234/",
},
wantErr: assert.NoError,
},
{
name: "otelgrpc",
args: []string{
"--tracing.exporter=otlpgrpc",
"--tracing.endpoint=localhost:1234",
},
wantErr: assert.NoError,
},
{
name: "with tags",
args: []string{
"--tracing.exporter=otlphttp",
"--tracing.endpoint=http://localhost:1234/api/v2",
"--tracing.ratio=1",
"--tracing.tags=cluster=test",
"--tracing.tags=namespace=num",
},
wantErr: assert.NoError,
},
{
name: "with headers",
args: []string{
"--tracing.exporter=otlphttp",
"--tracing.endpoint=http://localhost:1234/",
"--tracing.ratio=1",
"--tracing.headers=cluster=test",
"--tracing.headers=namespace=num",
},
wantErr: assert.NoError,
},
{
name: "unknown exporter",
args: []string{
"--tracing.exporter=some-exporter",
"--tracing.endpoint=localhost:1234",
},
wantErr: assert.Error,
},
{
name: "ratio too low",
args: []string{
"--tracing.exporter=otlpgrpc",
"--tracing.endpoint=localhost:1234",
"--tracing.ratio=-1",
},
wantErr: assert.NoError,
},
{
name: "ratio too high",
args: []string{
"--tracing.exporter=otlpgrpc",
"--tracing.endpoint=localhost:1234",
"--tracing.ratio=2",
},
wantErr: assert.NoError,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
c := &cli.Command{
Flags: cmd.TracingFlags,
Action: func(ctx context.Context, c *cli.Command) error {
_, err := cmd.NewTracer(ctx, c, log,
semconv.ServiceNameKey.String("my-service"),
semconv.ServiceVersionKey.String("1.0.0"),
)
return err
},
}
err := c.Run(t.Context(), append([]string{"test"}, test.args...))
test.wantErr(t, err)
})
}
}