-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
traces.go
154 lines (131 loc) · 4.27 KB
/
traces.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
// Copyright The OpenTelemetry Authors
//
// Licensed 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:https://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 traces
import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.uber.org/zap"
"golang.org/x/time/rate"
"google.golang.org/grpc"
"github.com/open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/internal/common"
)
func Start(cfg *Config) error {
logger, err := common.CreateLogger()
if err != nil {
return err
}
grpcExpOpt := []otlptracegrpc.Option{
otlptracegrpc.WithEndpoint(cfg.Endpoint),
otlptracegrpc.WithDialOption(
grpc.WithBlock(),
),
}
httpExpOpt := []otlptracehttp.Option{
otlptracehttp.WithEndpoint(cfg.Endpoint),
}
if cfg.Insecure {
grpcExpOpt = append(grpcExpOpt, otlptracegrpc.WithInsecure())
httpExpOpt = append(httpExpOpt, otlptracehttp.WithInsecure())
}
if len(cfg.Headers) > 0 {
grpcExpOpt = append(grpcExpOpt, otlptracegrpc.WithHeaders(cfg.Headers))
httpExpOpt = append(httpExpOpt, otlptracehttp.WithHeaders(cfg.Headers))
}
var exp *otlptrace.Exporter
if cfg.UseHTTP {
logger.Info("starting HTTP exporter")
exp, err = otlptracehttp.New(context.Background(), httpExpOpt...)
} else {
logger.Info("starting gRPC exporter")
exp, err = otlptracegrpc.New(context.Background(), grpcExpOpt...)
}
if err != nil {
return fmt.Errorf("failed to obtain OTLP exporter: %w", err)
}
defer func() {
logger.Info("stopping the exporter")
if tempError := exp.Shutdown(context.Background()); tempError != nil {
logger.Error("failed to stop the exporter", zap.Error(tempError))
}
}()
ssp := sdktrace.NewBatchSpanProcessor(exp, sdktrace.WithBatchTimeout(time.Second))
defer func() {
logger.Info("stop the batch span processor")
if tempError := ssp.Shutdown(context.Background()); tempError != nil {
logger.Error("failed to stop the batch span processor", zap.Error(err))
}
}()
var attributes []attribute.KeyValue
// may be overridden by `-otlp-attributes service.name="foo"`
attributes = append(attributes, semconv.ServiceNameKey.String(cfg.ServiceName))
attributes = append(attributes, cfg.GetAttributes()...)
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithResource(resource.NewWithAttributes(semconv.SchemaURL, attributes...)),
)
tracerProvider.RegisterSpanProcessor(ssp)
otel.SetTracerProvider(tracerProvider)
if err = Run(cfg, logger); err != nil {
logger.Error("failed to execute the test scenario.", zap.Error(err))
return err
}
return nil
}
// Run executes the test scenario.
func Run(c *Config, logger *zap.Logger) error {
if c.TotalDuration > 0 {
c.NumTraces = 0
} else if c.NumTraces <= 0 {
return fmt.Errorf("either `traces` or `duration` must be greater than 0")
}
limit := rate.Limit(c.Rate)
if c.Rate == 0 {
limit = rate.Inf
logger.Info("generation of traces isn't being throttled")
} else {
logger.Info("generation of traces is limited", zap.Float64("per-second", float64(limit)))
}
wg := sync.WaitGroup{}
running := &atomic.Bool{}
running.Store(true)
for i := 0; i < c.WorkerCount; i++ {
wg.Add(1)
w := worker{
numTraces: c.NumTraces,
propagateContext: c.PropagateContext,
limitPerSecond: limit,
totalDuration: c.TotalDuration,
running: running,
wg: &wg,
logger: logger.With(zap.Int("worker", i)),
}
go w.simulateTraces()
}
if c.TotalDuration > 0 {
time.Sleep(c.TotalDuration)
running.Store(false)
}
wg.Wait()
return nil
}