-
Notifications
You must be signed in to change notification settings - Fork 486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade agent to prometheus 2.51 #6754
Changes from 15 commits
48a3651
ecbb843
ba5de16
294e749
94237b5
d1bfe3c
8452b25
6971508
6614ad6
cad5a1f
2a29169
e287704
7a71ad7
e33008b
2f5e452
4b2f888
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// This code is copied from Promtail (https://https://github.com/grafana/loki/tree/3b5ed8c3ec88/clients/pkg/promtail/discovery/consulagent). | ||
// Some changes have been made to improve the component: | ||
// - "Namespace: namespace" has been removed | ||
|
||
// This code was adapted from the consul service discovery | ||
// package in prometheus: https://github.com/prometheus/prometheus/blob/main/discovery/consul/metrics.go | ||
// which is copyrighted: 2015 The Prometheus Authors | ||
// and licensed under the Apache License, Version 2.0 (the "License"); | ||
|
||
package consulagent | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"github.com/prometheus/prometheus/discovery" | ||
) | ||
|
||
var _ discovery.DiscovererMetrics = (*consulMetrics)(nil) | ||
|
||
type consulMetrics struct { | ||
rpcFailuresCount prometheus.Counter | ||
rpcDuration *prometheus.SummaryVec | ||
|
||
servicesRPCDuration prometheus.Observer | ||
serviceRPCDuration prometheus.Observer | ||
|
||
metricRegisterer discovery.MetricRegisterer | ||
} | ||
|
||
func newDiscovererMetrics(reg prometheus.Registerer, _ discovery.RefreshMetricsInstantiator) discovery.DiscovererMetrics { | ||
m := &consulMetrics{ | ||
rpcFailuresCount: prometheus.NewCounter( | ||
prometheus.CounterOpts{ | ||
Name: "sd_consulagent_rpc_failures_total", | ||
Help: "The number of Consul Agent RPC call failures.", | ||
}), | ||
rpcDuration: prometheus.NewSummaryVec( | ||
prometheus.SummaryOpts{ | ||
Name: "sd_consulagent_rpc_duration_seconds", | ||
Help: "The duration of a Consul Agent RPC call in seconds.", | ||
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, | ||
}, | ||
[]string{"endpoint", "call"}, | ||
), | ||
} | ||
|
||
m.metricRegisterer = discovery.NewMetricRegisterer(reg, []prometheus.Collector{ | ||
m.rpcFailuresCount, | ||
m.rpcDuration, | ||
}) | ||
|
||
// Initialize metric vectors. | ||
m.servicesRPCDuration = m.rpcDuration.WithLabelValues("agent", "services") | ||
m.serviceRPCDuration = m.rpcDuration.WithLabelValues("agent", "service") | ||
|
||
return m | ||
} | ||
|
||
// Register implements discovery.DiscovererMetrics. | ||
func (m *consulMetrics) Register() error { | ||
return m.metricRegisterer.RegisterMetrics() | ||
} | ||
|
||
// Unregister implements discovery.DiscovererMetrics. | ||
func (m *consulMetrics) Unregister() { | ||
m.metricRegisterer.UnregisterMetrics() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,12 +93,12 @@ type Exports struct { | |
Targets []Target `river:"targets,attr"` | ||
} | ||
|
||
// Discoverer is an alias for Prometheus' Discoverer interface, so users of this package don't need | ||
// DiscovererConfig is an alias for Prometheus' discovery.Config interface, so users of this package don't need | ||
// to import github.com/prometheus/prometheus/discover as well. | ||
type Discoverer discovery.Discoverer | ||
type DiscovererConfig discovery.Config | ||
|
||
// Creator is a function provided by an implementation to create a concrete Discoverer instance. | ||
type Creator func(component.Arguments) (Discoverer, error) | ||
// Creator is a function provided by an implementation to create a concrete DiscovererConfig instance. | ||
type Creator func(component.Arguments) DiscovererConfig | ||
|
||
// Component is a reusable component for any discovery implementation. | ||
// it will handle dynamic updates and exporting targets appropriately for a scrape implementation. | ||
|
@@ -110,13 +110,36 @@ type Component struct { | |
newDiscoverer chan struct{} | ||
|
||
creator Creator | ||
|
||
sdMetrics discovery.DiscovererMetrics | ||
refreshMetrics discovery.DiscovererMetrics | ||
} | ||
|
||
// New creates a discovery component given arguments and a concrete Discovery implementation function. | ||
func New(o component.Options, args component.Arguments, creator Creator) (*Component, error) { | ||
// The metrics stay the same, even if the config of the SD mechanism changes. | ||
// This means it's ok to do the registration now, with the initial config. | ||
// This is similar ot how Prometheus does it. | ||
discConfig := creator(args) | ||
//TODO(ptodev): Change upstream Prometheus to have NewDiscovererMetrics outside of discovery.Config, | ||
// to show that it's independent of the config. | ||
|
||
//TODO(ptodev): It looks messy to have a refresh metrics separate from the other SD metrics. | ||
// Can we make this cleaner? The reason it was made like this currently, | ||
// is so that metrics exposed by Prometheus executables don't change. | ||
refreshMetrics := discovery.NewRefreshMetrics(o.Registerer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels ugly, so we dont have to Register the the refreshMetrics but we do need to unregister them? And then since we can get a newdiscover on the channel we have to unregister there. The fact they aren't balanced from looking at the code makes it rough. |
||
sdMetrics := discConfig.NewDiscovererMetrics(o.Registerer, refreshMetrics) | ||
|
||
err := sdMetrics.Register() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c := &Component{ | ||
opts: o, | ||
creator: creator, | ||
opts: o, | ||
creator: creator, | ||
sdMetrics: sdMetrics, | ||
refreshMetrics: refreshMetrics, | ||
// buffered to avoid deadlock from the first immediate update | ||
newDiscoverer: make(chan struct{}, 1), | ||
} | ||
|
@@ -125,6 +148,9 @@ func New(o component.Options, args component.Arguments, creator Creator) (*Compo | |
|
||
// Run implements component.Component. | ||
func (c *Component) Run(ctx context.Context) error { | ||
var wg sync.WaitGroup | ||
defer wg.Wait() | ||
|
||
var cancel context.CancelFunc | ||
for { | ||
select { | ||
|
@@ -145,14 +171,25 @@ func (c *Component) Run(ctx context.Context) error { | |
c.discMut.Lock() | ||
disc := c.latestDisc | ||
c.discMut.Unlock() | ||
go c.runDiscovery(newCtx, disc) | ||
wg.Add(1) | ||
go func() { | ||
c.runDiscovery(newCtx, disc) | ||
c.sdMetrics.Unregister() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like it should break if we create the discovering multiple time in the Update. |
||
c.refreshMetrics.Unregister() | ||
wg.Done() | ||
}() | ||
} | ||
} | ||
} | ||
|
||
// Update implements component.Component. | ||
func (c *Component) Update(args component.Arguments) error { | ||
disc, err := c.creator(args) | ||
discConfig := c.creator(args) | ||
|
||
disc, err := discConfig.NewDiscoverer(discovery.DiscovererOptions{ | ||
Logger: c.opts.Logger, | ||
Metrics: c.sdMetrics, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -174,7 +211,7 @@ var MaxUpdateFrequency = 5 * time.Second | |
|
||
// runDiscovery is a utility for consuming and forwarding target groups from a discoverer. | ||
// It will handle collating targets (and clearing), as well as time based throttling of updates. | ||
func (c *Component) runDiscovery(ctx context.Context, d Discoverer) { | ||
func (c *Component) runDiscovery(ctx context.Context, d discovery.Discoverer) { | ||
// all targets we have seen so far | ||
cache := map[string]*targetgroup.Group{} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this go into breaking changes documentation?