-
Notifications
You must be signed in to change notification settings - Fork 486
/
receiver.go
183 lines (150 loc) · 5.59 KB
/
receiver.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Package receiver utilities to create a Flow component from OpenTelemetry
// Collector receivers.
package receiver
import (
"context"
"errors"
"os"
"github.com/grafana/agent/component"
"github.com/grafana/agent/component/otelcol"
"github.com/grafana/agent/component/otelcol/internal/fanoutconsumer"
"github.com/grafana/agent/component/otelcol/internal/lazycollector"
"github.com/grafana/agent/component/otelcol/internal/scheduler"
"github.com/grafana/agent/pkg/build"
"github.com/grafana/agent/pkg/util/zapadapter"
"github.com/prometheus/client_golang/prometheus"
otelcomponent "go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/extension"
otelreceiver "go.opentelemetry.io/collector/receiver"
sdkprometheus "go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/sdk/metric"
_ "github.com/grafana/agent/component/otelcol/internal/featuregate" // Enable needed feature gates
)
// Arguments is an extension of component.Arguments which contains necessary
// settings for OpenTelemetry Collector receivers.
type Arguments interface {
component.Arguments
// Convert converts the Arguments into an OpenTelemetry Collector receiver
// configuration.
Convert() (otelcomponent.Config, error)
// Extensions returns the set of extensions that the configured component is
// allowed to use.
Extensions() map[otelcomponent.ID]extension.Extension
// Exporters returns the set of exporters that are exposed to the configured
// component.
Exporters() map[otelcomponent.DataType]map[otelcomponent.ID]otelcomponent.Component
// NextConsumers returns the set of consumers to send data to.
NextConsumers() *otelcol.ConsumerArguments
}
// Receiver is a Flow component shim which manages an OpenTelemetry Collector
// receiver component.
type Receiver struct {
ctx context.Context
cancel context.CancelFunc
opts component.Options
factory otelreceiver.Factory
sched *scheduler.Scheduler
collector *lazycollector.Collector
}
var (
_ component.Component = (*Receiver)(nil)
_ component.HealthComponent = (*Receiver)(nil)
)
// New creates a new Flow component which encapsulates an OpenTelemetry
// Collector receiver. args must hold a value of the argument type registered
// with the Flow component.
//
// If the registered Flow component registers exported fields, it is the
// responsibility of the caller to export values when needed; the Receiver
// component never exports any values.
func New(opts component.Options, f otelreceiver.Factory, args Arguments) (*Receiver, error) {
ctx, cancel := context.WithCancel(context.Background())
// Create a lazy collector where metrics from the upstream component will be
// forwarded.
collector := lazycollector.New()
opts.Registerer.MustRegister(collector)
r := &Receiver{
ctx: ctx,
cancel: cancel,
opts: opts,
factory: f,
sched: scheduler.New(opts.Logger),
collector: collector,
}
if err := r.Update(args); err != nil {
return nil, err
}
return r, nil
}
// Run starts the Receiver component.
func (r *Receiver) Run(ctx context.Context) error {
defer r.cancel()
return r.sched.Run(ctx)
}
// Update implements component.Component. It will convert the Arguments into
// configuration for OpenTelemetry Collector receiver configuration and manage
// the underlying OpenTelemetry Collector receiver.
func (r *Receiver) Update(args component.Arguments) error {
rargs := args.(Arguments)
host := scheduler.NewHost(
r.opts.Logger,
scheduler.WithHostExtensions(rargs.Extensions()),
scheduler.WithHostExporters(rargs.Exporters()),
)
reg := prometheus.NewRegistry()
r.collector.Set(reg)
promExporter, err := sdkprometheus.New(sdkprometheus.WithRegisterer(reg), sdkprometheus.WithoutTargetInfo())
if err != nil {
return err
}
settings := otelreceiver.CreateSettings{
TelemetrySettings: otelcomponent.TelemetrySettings{
Logger: zapadapter.New(r.opts.Logger),
TracerProvider: r.opts.Tracer,
MeterProvider: metric.NewMeterProvider(metric.WithReader(promExporter)),
},
BuildInfo: otelcomponent.BuildInfo{
Command: os.Args[0],
Description: "Grafana Agent",
Version: build.Version,
},
}
receiverConfig, err := rargs.Convert()
if err != nil {
return err
}
var (
next = rargs.NextConsumers()
nextTraces = fanoutconsumer.Traces(next.Traces)
nextMetrics = fanoutconsumer.Metrics(next.Metrics)
nextLogs = fanoutconsumer.Logs(next.Logs)
)
// Create instances of the receiver from our factory for each of our
// supported telemetry signals.
var components []otelcomponent.Component
tracesReceiver, err := r.factory.CreateTracesReceiver(r.ctx, settings, receiverConfig, nextTraces)
if err != nil && !errors.Is(err, otelcomponent.ErrDataTypeIsNotSupported) {
return err
} else if tracesReceiver != nil {
components = append(components, tracesReceiver)
}
metricsReceiver, err := r.factory.CreateMetricsReceiver(r.ctx, settings, receiverConfig, nextMetrics)
if err != nil && !errors.Is(err, otelcomponent.ErrDataTypeIsNotSupported) {
return err
} else if metricsReceiver != nil {
components = append(components, metricsReceiver)
}
logsReceiver, err := r.factory.CreateLogsReceiver(r.ctx, settings, receiverConfig, nextLogs)
if err != nil && !errors.Is(err, otelcomponent.ErrDataTypeIsNotSupported) {
return err
} else if logsReceiver != nil {
components = append(components, logsReceiver)
}
// Schedule the components to run once our component is running.
r.sched.Schedule(host, components...)
return nil
}
// CurrentHealth implements component.HealthComponent.
func (r *Receiver) CurrentHealth() component.Health {
return r.sched.CurrentHealth()
}