-
Notifications
You must be signed in to change notification settings - Fork 486
/
Copy pathtraces.go
111 lines (90 loc) · 2.75 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
package traces
import (
"fmt"
"sync"
"github.com/go-kit/log"
"github.com/grafana/agent/pkg/logs"
"github.com/grafana/agent/pkg/metrics/instance"
"github.com/grafana/agent/pkg/util/zapadapter"
prom_client "github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
)
// Traces wraps the OpenTelemetry collector to enable tracing pipelines
type Traces struct {
mut sync.Mutex
instances map[string]*Instance
logger *zap.Logger
reg prom_client.Registerer
promInstanceManager instance.Manager
}
// New creates and starts trace collection.
func New(logsSubsystem *logs.Logs, promInstanceManager instance.Manager, reg prom_client.Registerer, cfg Config, l log.Logger) (*Traces, error) {
traces := &Traces{
instances: make(map[string]*Instance),
logger: newLogger(l),
reg: reg,
promInstanceManager: promInstanceManager,
}
if err := traces.ApplyConfig(logsSubsystem, promInstanceManager, cfg); err != nil {
return nil, err
}
return traces, nil
}
// Instance is used to retrieve a named Traces instance
func (t *Traces) Instance(name string) *Instance {
t.mut.Lock()
defer t.mut.Unlock()
return t.instances[name]
}
// ApplyConfig updates Traces with a new Config.
func (t *Traces) ApplyConfig(logsSubsystem *logs.Logs, promInstanceManager instance.Manager, cfg Config) error {
t.mut.Lock()
defer t.mut.Unlock()
newInstances := make(map[string]*Instance, len(cfg.Configs))
for _, c := range cfg.Configs {
var (
instReg = prom_client.WrapRegistererWith(prom_client.Labels{"traces_config": c.Name}, t.reg)
)
// If an old instance exists, update it and move it to the new map.
if old, ok := t.instances[c.Name]; ok {
err := old.ApplyConfig(logsSubsystem, promInstanceManager, instReg, c)
if err != nil {
return err
}
newInstances[c.Name] = old
continue
}
var (
instLogger = t.logger.With(zap.String("traces_config", c.Name))
)
inst, err := NewInstance(logsSubsystem, instReg, c, instLogger, t.promInstanceManager)
if err != nil {
return fmt.Errorf("failed to create tracing instance %s: %w", c.Name, err)
}
newInstances[c.Name] = inst
}
// Any instance in l.instances that isn't in newInstances has been removed
// from the config. Stop them before replacing the map.
for key, i := range t.instances {
if _, exist := newInstances[key]; exist {
continue
}
i.Stop()
}
t.instances = newInstances
return nil
}
// Stop stops the OpenTelemetry collector subsystem
func (t *Traces) Stop() {
t.mut.Lock()
defer t.mut.Unlock()
for _, i := range t.instances {
i.Stop()
}
}
func newLogger(l log.Logger) *zap.Logger {
logger := zapadapter.New(l)
logger = logger.With(zap.String("component", "traces"))
logger.Info("Traces Logger Initialized")
return logger
}