-
Notifications
You must be signed in to change notification settings - Fork 67
/
collector.go
93 lines (75 loc) 路 2.11 KB
/
collector.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
package collector
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/kubeshop/tracetest/server/otlp"
"go.opentelemetry.io/otel/trace"
)
type Config struct {
HTTPPort int
GRPCPort int
BatchTimeout time.Duration
RemoteServerURL string
RemoteServerToken string
}
type CollectorOption func(*remoteIngesterConfig)
func WithTraceCache(traceCache TraceCache) CollectorOption {
return func(ric *remoteIngesterConfig) {
ric.traceCache = traceCache
}
}
func WithStartRemoteServer(startRemoteServer bool) CollectorOption {
return func(ric *remoteIngesterConfig) {
ric.startRemoteServer = startRemoteServer
}
}
type collector struct {
grpcServer stoppable
httpServer stoppable
}
// Stop implements stoppable.
func (c *collector) Stop() {
c.grpcServer.Stop()
c.httpServer.Stop()
}
func Start(ctx context.Context, config Config, tracer trace.Tracer, opts ...CollectorOption) (stoppable, error) {
ingesterConfig := remoteIngesterConfig{
URL: config.RemoteServerURL,
Token: config.RemoteServerToken,
}
for _, opt := range opts {
opt(&ingesterConfig)
}
ingester, err := newForwardIngester(ctx, config.BatchTimeout, ingesterConfig, ingesterConfig.startRemoteServer)
if err != nil {
return nil, fmt.Errorf("could not start local collector: %w", err)
}
grpcServer := otlp.NewGrpcServer(fmt.Sprintf("0.0.0.0:%d", config.GRPCPort), ingester, tracer)
httpServer := otlp.NewHttpServer(fmt.Sprintf("0.0.0.0:%d", config.HTTPPort), ingester)
onProcessTermination(func() {
grpcServer.Stop()
httpServer.Stop()
if stoppableIngester, ok := ingester.(stoppable); ok {
stoppableIngester.Stop()
}
})
if err = grpcServer.Start(); err != nil {
return nil, fmt.Errorf("could not start gRPC OTLP listener: %w", err)
}
if err = httpServer.Start(); err != nil {
return nil, fmt.Errorf("could not start HTTP OTLP listener: %w", err)
}
return &collector{grpcServer: grpcServer, httpServer: httpServer}, nil
}
func onProcessTermination(callback func()) {
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
callback()
}()
}