-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
flow: add a
prometheus.exporter.cadvisor
component (#5307)
Signed-off-by: Paschalis Tsilias <[email protected]> Signed-off-by: erikbaranowski <[email protected]> Co-authored-by: Mischa Thompson <[email protected]> Co-authored-by: Clayton Cornell <[email protected]> Co-authored-by: erikbaranowski <[email protected]>
- Loading branch information
1 parent
04c013d
commit 2fd6eb6
Showing
16 changed files
with
453 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package cadvisor | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/grafana/agent/component" | ||
"github.com/grafana/agent/component/prometheus/exporter" | ||
"github.com/grafana/agent/pkg/integrations" | ||
"github.com/grafana/agent/pkg/integrations/cadvisor" | ||
) | ||
|
||
func init() { | ||
component.Register(component.Registration{ | ||
Name: "prometheus.exporter.cadvisor", | ||
Args: Arguments{}, | ||
Exports: exporter.Exports{}, | ||
NeedsServices: exporter.RequiredServices(), | ||
Build: exporter.New(createExporter, "cadvisor"), | ||
}) | ||
} | ||
|
||
func createExporter(opts component.Options, args component.Arguments, defaultInstanceKey string) (integrations.Integration, string, error) { | ||
a := args.(Arguments) | ||
return integrations.NewIntegrationWithInstanceKey(opts.Logger, a.Convert(), defaultInstanceKey) | ||
} | ||
|
||
// DefaultArguments holds non-zero default options for Arguments when it is | ||
// unmarshaled from river. | ||
var DefaultArguments = Arguments{ | ||
StoreContainerLabels: true, | ||
AllowlistedContainerLabels: []string{""}, | ||
EnvMetadataAllowlist: []string{""}, | ||
RawCgroupPrefixAllowlist: []string{""}, | ||
ResctrlInterval: 0, | ||
StorageDuration: 2 * time.Minute, | ||
|
||
ContainerdHost: "/run/containerd/containerd.sock", | ||
ContainerdNamespace: "k8s.io", | ||
|
||
// TODO(@tpaschalis) Do we need the default cert/key/ca since tls is disabled by default? | ||
DockerHost: "unix:///var/run/docker.sock", | ||
UseDockerTLS: false, | ||
DockerTLSCert: "cert.pem", | ||
DockerTLSKey: "key.pem", | ||
DockerTLSCA: "ca.pem", | ||
|
||
DockerOnly: false, | ||
} | ||
|
||
// Arguments configures the prometheus.exporter.cadvisor component. | ||
type Arguments struct { | ||
StoreContainerLabels bool `river:"store_container_labels,attr,optional"` | ||
AllowlistedContainerLabels []string `river:"allowlisted_container_labels,attr,optional"` | ||
EnvMetadataAllowlist []string `river:"env_metadata_allowlist,attr,optional"` | ||
RawCgroupPrefixAllowlist []string `river:"raw_cgroup_prefix_allowlist,attr,optional"` | ||
PerfEventsConfig string `river:"perf_events_config,attr,optional"` | ||
ResctrlInterval time.Duration `river:"resctrl_interval,attr,optional"` | ||
DisabledMetrics []string `river:"disabled_metrics,attr,optional"` | ||
EnabledMetrics []string `river:"enabled_metrics,attr,optional"` | ||
StorageDuration time.Duration `river:"storage_duration,attr,optional"` | ||
ContainerdHost string `river:"containerd_host,attr,optional"` | ||
ContainerdNamespace string `river:"containerd_namespace,attr,optional"` | ||
DockerHost string `river:"docker_host,attr,optional"` | ||
UseDockerTLS bool `river:"use_docker_tls,attr,optional"` | ||
DockerTLSCert string `river:"docker_tls_cert,attr,optional"` | ||
DockerTLSKey string `river:"docker_tls_key,attr,optional"` | ||
DockerTLSCA string `river:"docker_tls_ca,attr,optional"` | ||
DockerOnly bool `river:"docker_only,attr,optional"` | ||
} | ||
|
||
// SetToDefault implements river.Defaulter. | ||
func (a *Arguments) SetToDefault() { | ||
*a = DefaultArguments | ||
} | ||
|
||
// Convert returns the upstream-compatible configuration struct. | ||
func (a *Arguments) Convert() *cadvisor.Config { | ||
if len(a.AllowlistedContainerLabels) == 0 { | ||
a.AllowlistedContainerLabels = []string{""} | ||
} | ||
if len(a.RawCgroupPrefixAllowlist) == 0 { | ||
a.RawCgroupPrefixAllowlist = []string{""} | ||
} | ||
if len(a.EnvMetadataAllowlist) == 0 { | ||
a.EnvMetadataAllowlist = []string{""} | ||
} | ||
|
||
cfg := &cadvisor.Config{ | ||
StoreContainerLabels: a.StoreContainerLabels, | ||
AllowlistedContainerLabels: a.AllowlistedContainerLabels, | ||
EnvMetadataAllowlist: a.EnvMetadataAllowlist, | ||
RawCgroupPrefixAllowlist: a.RawCgroupPrefixAllowlist, | ||
PerfEventsConfig: a.PerfEventsConfig, | ||
ResctrlInterval: int64(a.ResctrlInterval), // TODO(@tpaschalis) This is so that the cadvisor package can re-cast back to time.Duration. Can we make it use time.Duration directly instead? | ||
DisabledMetrics: a.DisabledMetrics, | ||
EnabledMetrics: a.EnabledMetrics, | ||
StorageDuration: a.StorageDuration, | ||
Containerd: a.ContainerdHost, | ||
ContainerdNamespace: a.ContainerdNamespace, | ||
Docker: a.DockerHost, | ||
DockerTLS: a.UseDockerTLS, | ||
DockerTLSCert: a.DockerTLSCert, | ||
DockerTLSKey: a.DockerTLSKey, | ||
DockerTLSCA: a.DockerTLSCA, | ||
DockerOnly: a.DockerOnly, | ||
} | ||
|
||
return cfg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cadvisor | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/grafana/agent/pkg/integrations/cadvisor" | ||
"github.com/grafana/river" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUnmarshalRiver(t *testing.T) { | ||
riverCfg := ` | ||
store_container_labels = true | ||
allowlisted_container_labels = ["label1", "label2"] | ||
env_metadata_allowlist = ["env1", "env2"] | ||
raw_cgroup_prefix_allowlist = ["prefix1", "prefix2"] | ||
perf_events_config = "perf_events_config" | ||
resctrl_interval = "1s" | ||
disabled_metrics = ["metric1", "metric2"] | ||
enabled_metrics = ["metric3", "metric4"] | ||
storage_duration = "2s" | ||
containerd_host = "containerd_host" | ||
containerd_namespace = "containerd_namespace" | ||
docker_host = "docker_host" | ||
use_docker_tls = true | ||
docker_tls_cert = "docker_tls_cert" | ||
docker_tls_key = "docker_tls_key" | ||
docker_tls_ca = "docker_tls_ca" | ||
` | ||
var args Arguments | ||
err := river.Unmarshal([]byte(riverCfg), &args) | ||
require.NoError(t, err) | ||
expected := Arguments{ | ||
StoreContainerLabels: true, | ||
AllowlistedContainerLabels: []string{"label1", "label2"}, | ||
EnvMetadataAllowlist: []string{"env1", "env2"}, | ||
RawCgroupPrefixAllowlist: []string{"prefix1", "prefix2"}, | ||
PerfEventsConfig: "perf_events_config", | ||
ResctrlInterval: 1 * time.Second, | ||
DisabledMetrics: []string{"metric1", "metric2"}, | ||
EnabledMetrics: []string{"metric3", "metric4"}, | ||
StorageDuration: 2 * time.Second, | ||
ContainerdHost: "containerd_host", | ||
ContainerdNamespace: "containerd_namespace", | ||
DockerHost: "docker_host", | ||
UseDockerTLS: true, | ||
DockerTLSCert: "docker_tls_cert", | ||
DockerTLSKey: "docker_tls_key", | ||
DockerTLSCA: "docker_tls_ca", | ||
} | ||
require.Equal(t, expected, args) | ||
} | ||
|
||
func TestConvert(t *testing.T) { | ||
args := Arguments{ | ||
StoreContainerLabels: true, | ||
AllowlistedContainerLabels: []string{"label1", "label2"}, | ||
EnvMetadataAllowlist: []string{"env1", "env2"}, | ||
RawCgroupPrefixAllowlist: []string{"prefix1", "prefix2"}, | ||
PerfEventsConfig: "perf_events_config", | ||
ResctrlInterval: 1 * time.Second, | ||
DisabledMetrics: []string{"metric1", "metric2"}, | ||
EnabledMetrics: []string{"metric3", "metric4"}, | ||
StorageDuration: 2 * time.Second, | ||
ContainerdHost: "containerd_host", | ||
ContainerdNamespace: "containerd_namespace", | ||
DockerHost: "docker_host", | ||
UseDockerTLS: true, | ||
DockerTLSCert: "docker_tls_cert", | ||
DockerTLSKey: "docker_tls_key", | ||
DockerTLSCA: "docker_tls_ca", | ||
} | ||
|
||
res := args.Convert() | ||
expected := &cadvisor.Config{ | ||
StoreContainerLabels: true, | ||
AllowlistedContainerLabels: []string{"label1", "label2"}, | ||
EnvMetadataAllowlist: []string{"env1", "env2"}, | ||
RawCgroupPrefixAllowlist: []string{"prefix1", "prefix2"}, | ||
PerfEventsConfig: "perf_events_config", | ||
ResctrlInterval: int64(1 * time.Second), | ||
DisabledMetrics: []string{"metric1", "metric2"}, | ||
EnabledMetrics: []string{"metric3", "metric4"}, | ||
StorageDuration: 2 * time.Second, | ||
Containerd: "containerd_host", | ||
ContainerdNamespace: "containerd_namespace", | ||
Docker: "docker_host", | ||
DockerTLS: true, | ||
DockerTLSCert: "docker_tls_cert", | ||
DockerTLSKey: "docker_tls_key", | ||
DockerTLSCA: "docker_tls_ca", | ||
} | ||
require.Equal(t, expected, res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
converter/internal/staticconvert/internal/build/cadvisor_exporter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package build | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/grafana/agent/component/discovery" | ||
"github.com/grafana/agent/component/prometheus/exporter/cadvisor" | ||
"github.com/grafana/agent/converter/internal/common" | ||
"github.com/grafana/agent/converter/internal/prometheusconvert" | ||
cadvisor_integration "github.com/grafana/agent/pkg/integrations/cadvisor" | ||
) | ||
|
||
func (b *IntegrationsV1ConfigBuilder) appendCadvisorExporter(config *cadvisor_integration.Config) discovery.Exports { | ||
args := toCadvisorExporter(config) | ||
compLabel := common.LabelForParts(b.globalCtx.LabelPrefix, config.Name()) | ||
b.f.Body().AppendBlock(common.NewBlockWithOverride( | ||
[]string{"prometheus", "exporter", "cadvisor"}, | ||
compLabel, | ||
args, | ||
)) | ||
|
||
return prometheusconvert.NewDiscoveryExports(fmt.Sprintf("prometheus.exporter.cadvisor.%s.targets", compLabel)) | ||
} | ||
|
||
func toCadvisorExporter(config *cadvisor_integration.Config) *cadvisor.Arguments { | ||
return &cadvisor.Arguments{ | ||
|
||
StoreContainerLabels: config.StoreContainerLabels, | ||
AllowlistedContainerLabels: config.AllowlistedContainerLabels, | ||
EnvMetadataAllowlist: config.EnvMetadataAllowlist, | ||
RawCgroupPrefixAllowlist: config.RawCgroupPrefixAllowlist, | ||
PerfEventsConfig: config.PerfEventsConfig, | ||
ResctrlInterval: time.Duration(config.ResctrlInterval), | ||
DisabledMetrics: config.DisabledMetrics, | ||
EnabledMetrics: config.EnabledMetrics, | ||
StorageDuration: config.StorageDuration, | ||
ContainerdHost: config.Containerd, | ||
ContainerdNamespace: config.ContainerdNamespace, | ||
DockerHost: config.Docker, | ||
UseDockerTLS: config.DockerTLS, | ||
DockerTLSCert: config.DockerTLSCert, | ||
DockerTLSKey: config.DockerTLSKey, | ||
DockerTLSCA: config.DockerTLSCA, | ||
DockerOnly: config.DockerOnly, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.