-
Notifications
You must be signed in to change notification settings - Fork 479
/
prometheus.go
140 lines (119 loc) · 3.72 KB
/
prometheus.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
// Package prometheus provides an otelcol.receiver.prometheus component.
package prometheus
import (
"context"
"os"
"regexp"
"sync"
"time"
"github.com/go-kit/log"
"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/receiver/prometheus/internal"
"github.com/grafana/agent/pkg/build"
"github.com/grafana/agent/pkg/util/zapadapter"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/storage"
otelcomponent "go.opentelemetry.io/collector/component"
otelreceiver "go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/trace"
)
func init() {
component.Register(component.Registration{
Name: "otelcol.receiver.prometheus",
Args: Arguments{},
Exports: Exports{},
Build: func(o component.Options, a component.Arguments) (component.Component, error) {
return New(o, a.(Arguments))
},
})
}
// Arguments configures the otelcol.receiver.prometheus component.
type Arguments struct {
// Output configures where to send received data. Required.
Output *otelcol.ConsumerArguments `river:"output,block"`
}
// Exports are the set of fields exposed by the otelcol.receiver.prometheus
// component.
type Exports struct {
Receiver storage.Appendable `river:"receiver,attr"`
}
// Component is the otelcol.receiver.prometheus component.
type Component struct {
log log.Logger
opts component.Options
mut sync.RWMutex
cfg Arguments
appendable storage.Appendable
}
var _ component.Component = (*Component)(nil)
// New creates a new otelcol.receiver.prometheus component.
func New(o component.Options, c Arguments) (*Component, error) {
res := &Component{
log: o.Logger,
opts: o,
}
if err := res.Update(c); err != nil {
return nil, err
}
return res, nil
}
// Run implements Component.
func (c *Component) Run(ctx context.Context) error {
<-ctx.Done()
return nil
}
// Update implements Component.
func (c *Component) Update(newConfig component.Arguments) error {
c.mut.Lock()
defer c.mut.Unlock()
cfg := newConfig.(Arguments)
c.cfg = cfg
// useStartTimeMetric is used to configure the 'metrics adjuster' in the
// prometheusreceiver which is applied whenever a Commit is called.
// If set to true, the receiver will utilize a `startTimeMetricAdjuster`
// to adjust metric start times based on a start time metric. The start
// time metric defaults to `process_start_time_seconds`, but can be
// overridden by using this regex.
//
// gcInterval should be at least as long as the longest scrape interval
// used by the upstream scrape configs, plus a delta to avoid race
// conditions so that jobs are not getting GC'ed between scrapes.
var (
useStartTimeMetric = false
startTimeMetricRegex *regexp.Regexp
gcInterval = 5 * time.Minute
)
settings := otelreceiver.CreateSettings{
TelemetrySettings: otelcomponent.TelemetrySettings{
Logger: zapadapter.New(c.opts.Logger),
// TODO(tpaschalis): expose tracing and logging statistics.
TracerProvider: trace.NewNoopTracerProvider(),
MeterProvider: noop.NewMeterProvider(),
},
BuildInfo: otelcomponent.BuildInfo{
Command: os.Args[0],
Description: "Grafana Agent",
Version: build.Version,
},
}
metricsSink := fanoutconsumer.Metrics(cfg.Output.Metrics)
appendable, err := internal.NewAppendable(
metricsSink,
settings,
gcInterval,
useStartTimeMetric,
startTimeMetricRegex,
otelcomponent.NewID(otelcomponent.Type(c.opts.ID)),
labels.Labels{},
)
if err != nil {
return err
}
c.appendable = appendable
// Export the receiver.
c.opts.OnStateChange(Exports{Receiver: c.appendable})
return nil
}