Skip to content
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

Update memcached_exporter to v0.13.0 #4645

Merged
merged 9 commits into from
Aug 16, 2023
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Main (unreleased)

- Clustering: Enable nodes to periodically rediscover and rejoin peers. (@tpaschalis)

- Update `memcached_exporter` to `v0.13.0`, which includes bugfixes, new metrics,
and the option to connect with TLS. (@spartan0x117)

- New Grafana Agent Flow components:

- `prometheus.exporter.gcp` - scrape GCP metrics. (@tburgessdev)
Expand Down
13 changes: 13 additions & 0 deletions component/prometheus/exporter/memcached/memcached.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/grafana/agent/component"
"github.com/grafana/agent/component/common/config"
"github.com/grafana/agent/component/discovery"
"github.com/grafana/agent/component/prometheus/exporter"
"github.com/grafana/agent/pkg/integrations"
Expand Down Expand Up @@ -47,16 +48,28 @@ type Arguments struct {
// Timeout is the timeout for the memcached exporter to use when connecting to the
// memcached server.
Timeout time.Duration `river:"timeout,attr,optional"`

// TLSConfig is used to configure TLS for connection to memcached.
TLSConfig *config.TLSConfig `river:"tls_config,block,optional"`
}

// SetToDefault implements river.Defaulter.
func (a *Arguments) SetToDefault() {
*a = DefaultArguments
}

// Validate implements river.Validator.
func (a Arguments) Validate() error {
if a.TLSConfig == nil {
return nil
}
return a.TLSConfig.Validate()
}

func (a Arguments) Convert() *memcached_exporter.Config {
return &memcached_exporter.Config{
MemcachedAddress: a.Address,
Timeout: a.Timeout,
TLSConfig: a.TLSConfig.Convert(),
}
}
43 changes: 43 additions & 0 deletions component/prometheus/exporter/memcached/memcached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/grafana/agent/component/common/config"
"github.com/grafana/agent/component/discovery"
"github.com/grafana/agent/pkg/integrations/memcached_exporter"
"github.com/grafana/agent/pkg/river"
Expand All @@ -28,6 +29,48 @@ timeout = "5s"`
assert.Equal(t, expected, args)
}

func TestRiverUnmarshalTLS(t *testing.T) {
var exampleRiverConfig = `
address = "localhost:99"
timeout = "5s"
tls_config {
ca_file = "/path/to/ca_file"
cert_file = "/path/to/cert_file"
key_file = "/path/to/key_file"
}`
var args Arguments
err := river.Unmarshal([]byte(exampleRiverConfig), &args)
assert.NoError(t, err)

expected := Arguments{
Address: "localhost:99",
Timeout: 5 * time.Second,
TLSConfig: &config.TLSConfig{
CAFile: "/path/to/ca_file",
CertFile: "/path/to/cert_file",
KeyFile: "/path/to/key_file",
},
}
assert.Equal(t, expected, args)

var invalidRiverConfig = `
address = "localhost:99"
timeout = "5s"
tls_config {
ca_pem = "ca"
ca_file = "/path/to/ca_file"
}`
err = river.Unmarshal([]byte(invalidRiverConfig), &args)
assert.Error(t, err)
assert.ErrorContains(t, err, "at most one of")
}

func TestValidateNilTLSConfig(t *testing.T) {
var args Arguments = Arguments{}
err := args.Validate()
assert.NoError(t, err)
}

func TestRiverUnmarshalDefaults(t *testing.T) {
var exampleRiverConfig = ``

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,18 @@ Name | Type | Description
`timeout` | `duration` | The timeout for connecting to the Memcached server. | `"1s"` | no |

## Blocks
The `prometheus.exporter.memcached` component does not support any blocks, and is configured
fully through arguments.
The following blocks are supported inside the definition of `prometheus.exporter.memcached`:

Hierarchy | Block | Description | Required
--------- | ----- | ----------- | --------
tls_config | [tls_config][] | TLS configuration for requests to the Memcached server. | no

[tls_config]: #tls_config-block

### tls_config block

{{< docs/shared lookup="flow/reference/components/tls-config-block.md" source="agent" >}}


## Exported fields
The following fields are exported and can be referenced by other components:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,31 @@ Full reference of options:

# Timeout for connecting to memcached.
[timeout: <duration> | default = "1s"]

# TLS configuration for requests to the memcached server.
tls_config:
# The CA cert to use.
[ca: <string>]
# The client cert to use.
[cert: <string>]
# The client key to use.
[key: <string>]

# Path to the CA cert file to use.
[ca_file: <string>]
# Path to the client cert file to use.
[cert_file: <string>]
# Path to the client key file to use.
[key_file: <string>]

# Used to verify the hostname for the memcached server.
[server_name: <string>]

# Disable memcached server certificate validation.
[insecure_skip_verify: <boolean> | default = false]

# Minimum TLS version.
[min_version: <tls_version>]
# Maximum TLS version.
[max_version: <tls_version>]
```
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ require (
github.com/prometheus/client_model v0.4.0
github.com/prometheus/common v0.44.0
github.com/prometheus/consul_exporter v0.8.0
github.com/prometheus/memcached_exporter v0.10.0
github.com/prometheus/memcached_exporter v0.13.0
github.com/prometheus/mysqld_exporter v0.14.0
github.com/prometheus/node_exporter v1.6.0
github.com/prometheus/procfs v0.11.0
Expand Down Expand Up @@ -396,7 +396,7 @@ require (
github.com/gosnmp/gosnmp v1.35.0 // indirect
github.com/grafana/gomemcache v0.0.0-20230316202710-a081dae0aba9 // indirect
github.com/grafana/loki/pkg/push v0.0.0-20230730074919-e455a11b49dd // k161 branch
github.com/grobie/gomemcache v0.0.0-20201204163352-08d7c80fcac6 // indirect
github.com/grobie/gomemcache v0.0.0-20230213081705-239240bbc445 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/hashicorp/cronexpr v1.1.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,8 @@ github.com/grafana/windows_exporter v0.15.1-0.20230612134738-fdb3ba7accd8/go.mod
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/grobie/gomemcache v0.0.0-20201204163352-08d7c80fcac6 h1:vqwzYov9hrRoGAmy61um8mVteOCD0bEbKgXr1mmkTlI=
github.com/grobie/gomemcache v0.0.0-20201204163352-08d7c80fcac6/go.mod h1:L69/dBlPQlWkcnU76WgcppK5e4rrxzQdi6LhLnK/ytA=
github.com/grobie/gomemcache v0.0.0-20230213081705-239240bbc445 h1:FlKQKUYPZ5yDCN248M3R7x8yu2E3yEZ0H7aLomE4EoE=
github.com/grobie/gomemcache v0.0.0-20230213081705-239240bbc445/go.mod h1:L69/dBlPQlWkcnU76WgcppK5e4rrxzQdi6LhLnK/ytA=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0/go.mod h1:f5nM7jw/oeRSadq3xCzHAvxcr8HZnzsqU6ILg/0NiiE=
Expand Down Expand Up @@ -2991,6 +2993,8 @@ github.com/prometheus/exporter-toolkit v0.10.0 h1:yOAzZTi4M22ZzVxD+fhy1URTuNRj/3
github.com/prometheus/exporter-toolkit v0.10.0/go.mod h1:+sVFzuvV5JDyw+Ih6p3zFxZNVnKQa3x5qPmDSiPu4ZY=
github.com/prometheus/memcached_exporter v0.10.0 h1:TrPnwRaOg4CbRCsLJHXfA/NxbbGjn9X3jZ4zxvLCTCk=
github.com/prometheus/memcached_exporter v0.10.0/go.mod h1:qHvTkwVTllvShLCs1CSnX8gB+9D3Zayp8g4tCAM+chQ=
github.com/prometheus/memcached_exporter v0.13.0 h1:d246RYODFCXy39XA8S2PBrqp5jLCSvl9b4KsYspDCHk=
github.com/prometheus/memcached_exporter v0.13.0/go.mod h1:fp7Wk6v0RFijeP3Syvd1TShBSJoCG5iFfvPdi5dCMEU=
github.com/prometheus/node_exporter v1.0.0-rc.0.0.20200428091818-01054558c289/go.mod h1:FGbBv5OPKjch+jNUJmEQpMZytIdyW0NdBtWFcfSKusc=
github.com/prometheus/node_exporter v1.6.0 h1:TKPvENRy8/yhKQWf862ssecaT9kQ1jnW9mQDtTzTIAU=
github.com/prometheus/node_exporter v1.6.0/go.mod h1:+zK+m9vwxu19JHl/kVVmixdCT6fWWHlmcOUHDFpkt0Y=
Expand Down
24 changes: 23 additions & 1 deletion pkg/integrations/memcached_exporter/memcached_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ package memcached_exporter //nolint:golint
import (
"time"

"crypto/tls"

config_util "github.com/prometheus/common/config"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/agent/pkg/integrations"
integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"
"github.com/grafana/agent/pkg/integrations/v2/metricsutils"
Expand All @@ -24,6 +29,9 @@ type Config struct {

// Timeout is the connection timeout for memcached.
Timeout time.Duration `yaml:"timeout,omitempty"`

// TLSConfig is used to configure TLS for connection to memcached.
TLSConfig *config_util.TLSConfig `yaml:"tls_config,omitempty"`
}

// UnmarshalYAML implements yaml.Unmarshaler for Config.
Expand Down Expand Up @@ -57,10 +65,24 @@ func init() {
// New creates a new memcached_exporter integration. The integration scrapes metrics
// from a memcached server.
func New(log log.Logger, c *Config) (integrations.Integration, error) {
var tlsConfig *tls.Config
var err error
// NewTLSConfig uses Validate, which does not have a check if the config is nil,
// so we need to check it
if c.TLSConfig != nil {
tlsConfig, err = config_util.NewTLSConfig(c.TLSConfig)
if err != nil {
level.Error(log).Log("msg", "invalid tls_config", "err", err)
return nil, err
}
}

return integrations.NewCollectorIntegration(
c.Name(),
integrations.WithCollectors(
exporter.New(c.MemcachedAddress, c.Timeout, log),
// The memcached client does check if the tlsConfig is nil, so passing
// nil here is fine.
exporter.New(c.MemcachedAddress, c.Timeout, log, tlsConfig),
),
), nil
}