Skip to content

Commit

Permalink
Update Prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
ptodev committed Sep 24, 2024
1 parent c3afc73 commit d201694
Show file tree
Hide file tree
Showing 106 changed files with 1,098 additions and 800 deletions.
178 changes: 91 additions & 87 deletions go.mod

Large diffs are not rendered by default.

436 changes: 196 additions & 240 deletions go.sum

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions internal/component/common/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ func (b *BasicAuth) Validate() error {
}

type ProxyConfig struct {
ProxyURL URL `river:"proxy_url,attr,optional"`
NoProxy string `river:"no_proxy,attr,optional"`
ProxyFromEnvironment bool `river:"proxy_from_environment,attr,optional"`
ProxyConnectHeader Header `river:",squash"`
ProxyURL URL `river:"proxy_url,attr,optional"`
NoProxy string `river:"no_proxy,attr,optional"`
ProxyFromEnvironment bool `river:"proxy_from_environment,attr,optional"`
ProxyConnectHeader ProxyHeader `river:",squash"`
}

func (p *ProxyConfig) Convert() config.ProxyConfig {
Expand Down Expand Up @@ -214,15 +214,15 @@ func (u *URL) Convert() config.URL {
return config.URL{URL: u.URL}
}

type Header struct {
type ProxyHeader struct {
Header map[string][]rivertypes.Secret `river:"proxy_connect_header,attr,optional"`
}

func (h *Header) Convert() config.Header {
func (h *ProxyHeader) Convert() config.ProxyHeader {
if h == nil {
return nil
}
header := make(config.Header)
header := make(config.ProxyHeader)
for name, values := range h.Header {
var s []config.Secret
if values != nil {
Expand Down
12 changes: 2 additions & 10 deletions internal/component/discovery/aws/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {
Args: EC2Arguments{},
Exports: discovery.Exports{},
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return NewEC2(opts, args.(EC2Arguments))
return discovery.NewFromConvertibleConfig(opts, args.(EC2Arguments))
},
})
}
Expand All @@ -50,7 +50,7 @@ type EC2Arguments struct {
HTTPClientConfig config.HTTPClientConfig `river:",squash"`
}

func (args EC2Arguments) Convert() *promaws.EC2SDConfig {
func (args EC2Arguments) Convert() discovery.DiscovererConfig {
cfg := &promaws.EC2SDConfig{
Endpoint: args.Endpoint,
Region: args.Region,
Expand Down Expand Up @@ -105,11 +105,3 @@ func (args *EC2Arguments) Validate() error {
}
return nil
}

// New creates a new discovery.ec2 component.
func NewEC2(opts component.Options, args EC2Arguments) (component.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
conf := args.(EC2Arguments).Convert()
return promaws.NewEC2Discovery(conf, opts.Logger), nil
})
}
5 changes: 4 additions & 1 deletion internal/component/discovery/aws/ec2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/grafana/agent/internal/component/common/config"
promaws "github.com/prometheus/prometheus/discovery/aws"
"github.com/stretchr/testify/require"
"gotest.tools/assert"
)
Expand All @@ -27,7 +28,9 @@ func TestConvert(t *testing.T) {
}

// ensure values are set
promArgs := riverArgs.Convert()
converted := riverArgs.Convert()
promArgs, ok := converted.(*promaws.EC2SDConfig)
require.True(t, ok)
assert.Equal(t, "us-east-1", promArgs.Region)
assert.Equal(t, "http://example:8080", promArgs.HTTPClientConfig.ProxyURL.String())
}
12 changes: 2 additions & 10 deletions internal/component/discovery/aws/lightsail.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {
Args: LightsailArguments{},
Exports: discovery.Exports{},
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return NewLightsail(opts, args.(LightsailArguments))
return discovery.NewFromConvertibleConfig(opts, args.(LightsailArguments))
},
})
}
Expand All @@ -42,7 +42,7 @@ type LightsailArguments struct {
HTTPClientConfig config.HTTPClientConfig `river:",squash"`
}

func (args LightsailArguments) Convert() *promaws.LightsailSDConfig {
func (args LightsailArguments) Convert() discovery.DiscovererConfig {
cfg := &promaws.LightsailSDConfig{
Endpoint: args.Endpoint,
Region: args.Region,
Expand Down Expand Up @@ -87,11 +87,3 @@ func (args *LightsailArguments) Validate() error {
}
return nil
}

// New creates a new discovery.lightsail component.
func NewLightsail(opts component.Options, args LightsailArguments) (component.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
conf := args.(LightsailArguments).Convert()
return promaws.NewLightsailDiscovery(conf, opts.Logger), nil
})
}
12 changes: 2 additions & 10 deletions internal/component/discovery/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func init() {
Exports: discovery.Exports{},

Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return New(opts, args.(Arguments))
return discovery.NewFromConvertibleConfig(opts, args.(Arguments))
},
})
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func (a *Arguments) Validate() error {
return a.ProxyConfig.Validate()
}

func (a *Arguments) Convert() *prom_discovery.SDConfig {
func (a Arguments) Convert() discovery.DiscovererConfig {
var (
authMethod string
clientID string
Expand Down Expand Up @@ -115,11 +115,3 @@ func (a *Arguments) Convert() *prom_discovery.SDConfig {
HTTPClientConfig: *httpClientConfig.Convert(),
}
}

// New returns a new instance of a discovery.azure component.
func New(opts component.Options, args Arguments) (*discovery.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
newArgs := args.(Arguments)
return prom_discovery.NewDiscovery(newArgs.Convert(), opts.Logger), nil
})
}
9 changes: 7 additions & 2 deletions internal/component/discovery/azure/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/grafana/agent/internal/component/common/config"
"github.com/grafana/river"
"github.com/prometheus/common/model"
promdiscovery "github.com/prometheus/prometheus/discovery/azure"
"github.com/stretchr/testify/require"
"gotest.tools/assert"
)
Expand Down Expand Up @@ -130,7 +131,9 @@ func TestConvert(t *testing.T) {
},
}

promArgs := riverArgsOAuth.Convert()
args := riverArgsOAuth.Convert()
promArgs, ok := args.(*promdiscovery.SDConfig)
require.True(t, ok)
assert.Equal(t, "AzureTestCloud", promArgs.Environment)
assert.Equal(t, 8080, promArgs.Port)
assert.Equal(t, "subid", promArgs.SubscriptionID)
Expand Down Expand Up @@ -161,7 +164,9 @@ func TestConvert(t *testing.T) {
},
}

promArgs = riverArgsManagedIdentity.Convert()
args = riverArgsManagedIdentity.Convert()
promArgs, ok = args.(*promdiscovery.SDConfig)
require.True(t, ok)
assert.Equal(t, "AzureTestCloud", promArgs.Environment)
assert.Equal(t, 8080, promArgs.Port)
assert.Equal(t, "subid", promArgs.SubscriptionID)
Expand Down
12 changes: 2 additions & 10 deletions internal/component/discovery/consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {
Exports: discovery.Exports{},

Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return New(opts, args.(Arguments))
return discovery.NewFromConvertibleConfig(opts, args.(Arguments))
},
})
}
Expand Down Expand Up @@ -69,7 +69,7 @@ func (args *Arguments) Validate() error {
return args.HTTPClientConfig.Validate()
}

func (args *Arguments) Convert() *prom_discovery.SDConfig {
func (args Arguments) Convert() discovery.DiscovererConfig {
httpClient := &args.HTTPClientConfig

return &prom_discovery.SDConfig{
Expand All @@ -90,11 +90,3 @@ func (args *Arguments) Convert() *prom_discovery.SDConfig {
NodeMeta: args.NodeMeta,
}
}

// New returns a new instance of a discovery.consul component.
func New(opts component.Options, args Arguments) (*discovery.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
newArgs := args.(Arguments)
return prom_discovery.NewDiscovery(newArgs.Convert(), opts.Logger)
})
}
12 changes: 2 additions & 10 deletions internal/component/discovery/consulagent/consulagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func init() {
Exports: discovery.Exports{},

Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return New(opts, args.(Arguments))
return discovery.NewFromConvertibleConfig(opts, args.(Arguments))
},
})
}
Expand Down Expand Up @@ -62,7 +62,7 @@ func (args *Arguments) Validate() error {
}

// Convert converts Arguments into the SDConfig type.
func (args *Arguments) Convert() *SDConfig {
func (args Arguments) Convert() discovery.DiscovererConfig {
return &SDConfig{
RefreshInterval: model.Duration(args.RefreshInterval),
Server: args.Server,
Expand All @@ -77,11 +77,3 @@ func (args *Arguments) Convert() *SDConfig {
TLSConfig: *args.TLSConfig.Convert(),
}
}

// New returns a new instance of a discovery.consulagent component.
func New(opts component.Options, args Arguments) (*discovery.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
newArgs := args.(Arguments)
return NewDiscovery(newArgs.Convert(), opts.Logger)
})
}
62 changes: 62 additions & 0 deletions internal/component/discovery/consulagent/consulagent_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package consulagent

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/discovery"
)

// 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");

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
Expand Up @@ -32,7 +32,7 @@ func TestConvert(t *testing.T) {
err := river.Unmarshal([]byte(exampleRiverConfig), &args)
require.NoError(t, err)

converted := args.Convert()
converted := args.Convert().(*SDConfig)
assert.Equal(t, "localhost:8500", converted.Server)
assert.Equal(t, promcfg.Secret("token"), converted.Token)
assert.Equal(t, "dc", converted.Datacenter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ type SDConfig struct {
TLSConfig config.TLSConfig `yaml:"tls_config,omitempty"`
}

// NewDiscovererMetrics implements discovery.DiscovererConfig.
func (c *SDConfig) NewDiscovererMetrics(reg prometheus.Registerer, rmi discovery.RefreshMetricsInstantiator) discovery.DiscovererMetrics {
return newDiscovererMetrics(reg, rmi)
}

// Name returns the name of the Config.
func (*SDConfig) Name() string { return "consulagent" }

Expand Down
12 changes: 2 additions & 10 deletions internal/component/discovery/digitalocean/digitalocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func init() {
Exports: discovery.Exports{},

Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return New(opts, args.(Arguments))
return discovery.NewFromConvertibleConfig(opts, args.(Arguments))
},
})
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func (a *Arguments) Validate() error {
return a.ProxyConfig.Validate()
}

func (a *Arguments) Convert() *prom_discovery.SDConfig {
func (a Arguments) Convert() discovery.DiscovererConfig {
httpClientConfig := config.DefaultHTTPClientConfig
httpClientConfig.BearerToken = a.BearerToken
httpClientConfig.BearerTokenFile = a.BearerTokenFile
Expand All @@ -79,11 +79,3 @@ func (a *Arguments) Convert() *prom_discovery.SDConfig {
HTTPClientConfig: *httpClientConfig.Convert(),
}
}

// New returns a new instance of a discovery.digitalocean component.
func New(opts component.Options, args Arguments) (*discovery.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
newArgs := args.(Arguments)
return prom_discovery.NewDiscovery(newArgs.Convert(), opts.Logger)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/grafana/river"
prom_common_config "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
promdiscovery "github.com/prometheus/prometheus/discovery/digitalocean"
"github.com/stretchr/testify/require"
"gotest.tools/assert"
)
Expand Down Expand Up @@ -81,7 +82,7 @@ func TestConvert(t *testing.T) {
EnableHTTP2: false,
}

converted := args.Convert()
converted := args.Convert().(*promdiscovery.SDConfig)
assert.Equal(t, model.Duration(5*time.Minute), converted.RefreshInterval)
assert.Equal(t, 8181, converted.Port)
assert.Equal(t, prom_common_config.Secret("token"), converted.HTTPClientConfig.BearerToken)
Expand Down
Loading

0 comments on commit d201694

Please sign in to comment.