From 99fbbdbc5d0f96eb67234435f46a0a4c9b14aed8 Mon Sep 17 00:00:00 2001 From: Ryan Geyer Date: Wed, 15 Dec 2021 15:37:33 -0800 Subject: [PATCH] cAdvisor Integration (#1081) * Add cadvisor module * Begin creating common config for cadvisor * Don't export internal state * Finish config options for cadvisor * Set config options, and implement cAdvisor collectors * Linting * Buildflags for cadvisor only in linux * I R LEArN Build Tags * Don't zero value the zero value * Offload sketchy global var manipulation to the integrations Run func * Remove unused collectors * Lint * Create generic stub integration and use it for cadvisor * Lint * Final refactor of cAdvisor config for unsupported platforms. Pared down stub integrations. * Lint * Docs for cadvisor config * Update changelog * Update pkg/integrations/stub_integration.go Co-authored-by: Robert Fratto * Reorder changelog * Instance key clarity * Inclusive naming * Finish name changes Keep default disable metric list in sync with upstream Idiomatic golang * Hardcode disabled metrics for cadvisor Co-authored-by: Robert Fratto --- CHANGELOG.md | 2 + .../integrations/cadvisor-config.md | 112 ++++++++++++ .../agent/config/agent-local.yaml | 8 +- go.mod | 4 + go.sum | 38 ++++ pkg/integrations/cadvisor/cadvisor.go | 168 ++++++++++++++++++ pkg/integrations/cadvisor/cadvisor_stub.go | 17 ++ pkg/integrations/cadvisor/common.go | 122 +++++++++++++ pkg/integrations/install/install.go | 1 + pkg/integrations/stub_integration.go | 27 +++ 10 files changed, 498 insertions(+), 1 deletion(-) create mode 100644 docs/configuration/integrations/cadvisor-config.md create mode 100644 pkg/integrations/cadvisor/cadvisor.go create mode 100644 pkg/integrations/cadvisor/cadvisor_stub.go create mode 100644 pkg/integrations/cadvisor/common.go create mode 100644 pkg/integrations/stub_integration.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 8af6dd80b245..ed9c4642a46f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ - [FEATURE] (beta) Enable experimental config urls for fetching remote configs. Currently, only HTTP/S is supported. Use `-experiment.config-urls.enable` flag to turn this on. (@rlankfo) +- [FEATURE] Added [cAdvisor](https://github.com/google/cadvisor) integration. (@rgeyer) + - [ENHANCEMENT] Traces: Improved pod association in PromSD processor (@mapno) - [BUGFIX] Fix usage of POSTGRES_EXPORTER_DATA_SOURCE_NAME when using postgres_exporter integration (@f11r) diff --git a/docs/configuration/integrations/cadvisor-config.md b/docs/configuration/integrations/cadvisor-config.md new file mode 100644 index 000000000000..fa8e5330db0d --- /dev/null +++ b/docs/configuration/integrations/cadvisor-config.md @@ -0,0 +1,112 @@ ++++ +title = "cadvisor_config" ++++ + +# cadvisor_config + +The `cadvisor_config` block configures the `cadvisor` integration, +which is an embedded version of +[`cadvisor`](https://github.com/google/cadvisor). This allows for the collection of container utilization metrics. + +The cAdvisor integration requires some broad privileged permissions to the host. Without these permissions the metrics will not be accessible. This means that the agent must *also* have those elevated permissions. + +A good example of the required file, and system permissions can be found in the docker run command published in the [cAdvisor docs](https://github.com/google/cadvisor#quick-start-running-cadvisor-in-a-docker-container). + +Full reference of options: + +```yaml + # Enables the cadvisor integration, allowing the Agent to automatically + # collect metrics for the specified github objects. + [enabled: | default = false] + + # Sets an explicit value for the instance label when the integration is + # self-scraped. Overrides inferred values. + [instance: | default = ] + + # Automatically collect metrics from this integration. If disabled, + # the cadvisor integration will be run but not scraped and thus not + # remote-written. Metrics for the integration will be exposed at + # /integrations/cadvisor/metrics and can be scraped by an external + # process. + [scrape_integration: | default = ] + + # How often should the metrics be collected? Defaults to + # prometheus.global.scrape_interval. + [scrape_interval: | default = ] + + # The timeout before considering the scrape a failure. Defaults to + # prometheus.global.scrape_timeout. + [scrape_timeout: | default = ] + + # Allows for relabeling labels on the target. + relabel_configs: + [- ... ] + + # Relabel metrics coming from the integration, allowing to drop series + # from the integration that you don't care about. + metric_relabel_configs: + [ - ... ] + + # How frequent to truncate the WAL for this integration. + [wal_truncate_frequency: | default = "60m"] + + # + # cAdvisor-specific configuration options + # + + # Convert container labels and environment variables into labels on prometheus metrics for each container. If false, then only metrics exported are container name, first alias, and image name. + [store_container_labels: | default = true] + + # List of container labels to be converted to labels on prometheus metrics for each container. store_container_labels must be set to false for this to take effect. + allowlisted_container_labels: + [ - ] + + # List of environment variable keys matched with specified prefix that needs to be collected for containers, only support containerd and docker runtime for now. + env_metadata_allowlist: + [ - ] + + # List of cgroup path prefix that needs to be collected even when docker_only is specified. + raw_cgroup_prefix_allowlist: + [ - ] + + # Path to a JSON file containing configuration of perf events to measure. Empty value disabled perf events measuring. + [perf_events_config: ] + + # resctrl mon groups updating interval. Zero value disables updating mon groups. + [resctrl_interval: | default = 0] + + # List of `metrics` to be disabled. + disabled_metrics: + [ - ] + + # List of `metrics` to be enabled. If set, overrides disabled_metrics + enabled_metrics: + [ - ] + + # Length of time to keep data stored in memory + [storage_duration: | default = "2m"] + + # Containerd endpoint + [containerd: | default = "/run/containerd/containerd.sock"] + + # Containerd namespace + [containerd_namespace: | default = "k8s.io"] + + # Docker endpoint + [docker: | default = "unix:///var/run/docker.sock"] + + # Use TLS to connect to docker + [docker_tls: | default = false] + + # Path to client certificate for TLS connection to docker + [docker_tls_cert: | default = "cert.pem"] + + # Path to private key for TLS connection to docker + [docker_tls_key: | default = "key.pem"] + + # Path to a trusted CA for TLS connection to docker + [docker_tls_ca: | default = "ca.pem"] + + # Only report docker containers in addition to root stats + [docker_only: | default = false] +``` diff --git a/example/docker-compose/agent/config/agent-local.yaml b/example/docker-compose/agent/config/agent-local.yaml index fcf0ee22c8a5..77bf6fc6e7e9 100644 --- a/example/docker-compose/agent/config/agent-local.yaml +++ b/example/docker-compose/agent/config/agent-local.yaml @@ -85,4 +85,10 @@ integrations: replacement: 'mongodb' - source_labels: [__address__] target_label: mongodb_cluster - replacement: 'mongodb-cluster' \ No newline at end of file + replacement: 'mongodb-cluster' + cadvisor: + enabled: true + disabled_metrics: + - disk + enabled_metrics: + - percpu \ No newline at end of file diff --git a/go.mod b/go.mod index b25b53256d08..320238015422 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/go-sql-driver/mysql v1.6.0 github.com/gogo/protobuf v1.3.2 github.com/golang/protobuf v1.5.2 + github.com/google/cadvisor v0.43.0 // indirect github.com/google/dnsmasq_exporter v0.0.0-00010101000000-000000000000 github.com/google/go-jsonnet v0.17.0 github.com/gorilla/mux v1.8.0 @@ -84,6 +85,9 @@ require ( k8s.io/api v0.22.3 k8s.io/apimachinery v0.22.3 k8s.io/client-go v12.0.0+incompatible + k8s.io/klog v1.0.0 // indirect + k8s.io/klog/v2 v2.20.0 // indirect + k8s.io/utils v0.0.0-20211116205334-6203023598ed // indirect sigs.k8s.io/controller-runtime v0.9.0-beta.5 sigs.k8s.io/yaml v1.2.0 ) diff --git a/go.sum b/go.sum index a9c619707b9b..bdfec149ab3c 100644 --- a/go.sum +++ b/go.sum @@ -171,6 +171,7 @@ github.com/Microsoft/go-winio v0.4.9/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyv github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA= github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= github.com/Microsoft/go-winio v0.4.15-0.20190919025122-fc70bd9a86b5/go.mod h1:tTuCMEN+UleMWgg9dVx4Hu52b1bJo+59jBh3ajtinzw= +github.com/Microsoft/go-winio v0.4.15/go.mod h1:tTuCMEN+UleMWgg9dVx4Hu52b1bJo+59jBh3ajtinzw= github.com/Microsoft/go-winio v0.4.16-0.20201130162521-d1ffc52c7331/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0= github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0= github.com/Microsoft/go-winio v0.4.17-0.20210211115548-6eac466e5fa3/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84= @@ -290,6 +291,7 @@ github.com/aws/aws-sdk-go v1.33.12/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZve github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48= github.com/aws/aws-sdk-go v1.34.34/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48= github.com/aws/aws-sdk-go v1.35.5/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k= +github.com/aws/aws-sdk-go v1.35.24/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k= github.com/aws/aws-sdk-go v1.35.31/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= github.com/aws/aws-sdk-go v1.37.8/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= github.com/aws/aws-sdk-go v1.38.3/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= @@ -327,6 +329,8 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k= github.com/bitly/go-hostpool v0.1.0/go.mod h1:4gOCgp6+NZnVqlKyZ/iBZFTAJKembaVENUpMkpg42fw= github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA= +github.com/bits-and-blooms/bitset v1.2.0 h1:Kn4yilvwNtMACtf1eYDlG8H77R07mZSPbMjLyS07ChA= +github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA= github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84= github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM= github.com/blang/semver v3.1.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk= @@ -380,6 +384,8 @@ github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/checkpoint-restore/go-criu/v4 v4.1.0/go.mod h1:xUQBLp4RLc5zJtWY++yjOoMoB5lihDt7fai+75m+rGw= +github.com/checkpoint-restore/go-criu/v5 v5.0.0 h1:TW8f/UvntYoVDMN1K2HlT82qH1rb0sOjpGw3m6Ym+i4= +github.com/checkpoint-restore/go-criu/v5 v5.0.0/go.mod h1:cfwC0EG7HMUenopBsUf9d89JlCLQIfgVcNsNN0t6T2M= github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s= github.com/chromedp/cdproto v0.0.0-20200116234248-4da64dd111ac/go.mod h1:PfAWWKJqjlGFYJEidUM6aVIWPr0EpobeyVWEEmplX7g= github.com/chromedp/cdproto v0.0.0-20200424080200-0de008e41fa0/go.mod h1:PfAWWKJqjlGFYJEidUM6aVIWPr0EpobeyVWEEmplX7g= @@ -391,6 +397,8 @@ github.com/cilium/ebpf v0.0.0-20200110133405-4032b1d8aae3/go.mod h1:MA5e5Lr8slmE github.com/cilium/ebpf v0.0.0-20200702112145-1c8d4c9ef775/go.mod h1:7cR51M8ViRLIdUjrmSXlK9pkrsDlLHbO8jiB8X8JnOc= github.com/cilium/ebpf v0.2.0/go.mod h1:To2CFviqOWL/M0gIMsvSMlqe7em/l1ALkX1PyjrX2Qs= github.com/cilium/ebpf v0.4.0/go.mod h1:4tRaxcgiL706VnOzHOdBlY8IEAIdxINsQBcU4xJJXRs= +github.com/cilium/ebpf v0.6.2 h1:iHsfF/t4aW4heW2YKfeHrVPGdtYTL4C4KocpM8KTSnI= +github.com/cilium/ebpf v0.6.2/go.mod h1:4tRaxcgiL706VnOzHOdBlY8IEAIdxINsQBcU4xJJXRs= github.com/circonus-labs/circonus-gometrics v0.0.0-20161109192337-d17a8420c36e/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag= github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag= github.com/circonus-labs/circonusllhist v0.0.0-20161110002650-365d370cc145/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I= @@ -432,6 +440,7 @@ github.com/containerd/console v0.0.0-20180822173158-c12b1e7919c1/go.mod h1:Tj/on github.com/containerd/console v0.0.0-20181022165439-0650fd9eeb50/go.mod h1:Tj/on1eG8kiEhd0+fhSDzsPAFESxzBBvdyEgyryXffw= github.com/containerd/console v0.0.0-20191206165004-02ecf6a7291e/go.mod h1:8Pf4gM6VEbTNRIT26AyyU7hxdQU3MvAvxVI0sc00XBE= github.com/containerd/console v1.0.1/go.mod h1:XUsP6YE/mKtz6bxc+I8UiKKTP04qjQL4qcS3XoQ5xkw= +github.com/containerd/console v1.0.2 h1:Pi6D+aZXM+oUw1czuKgH5IJ+y0jhYcwBJfx5/Ghn9dE= github.com/containerd/console v1.0.2/go.mod h1:ytZPjGgY2oeTkAONYafi2kSj0aYggsf8acV1PGKCbzQ= github.com/containerd/containerd v1.2.7/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA= github.com/containerd/containerd v1.2.10/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA= @@ -442,6 +451,7 @@ github.com/containerd/containerd v1.3.2/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMX github.com/containerd/containerd v1.4.0-beta.2.0.20200729163537-40b22ef07410/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA= github.com/containerd/containerd v1.4.1/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA= github.com/containerd/containerd v1.4.3/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA= +github.com/containerd/containerd v1.4.11/go.mod h1:bC6axHOhabU15QhwfG7w5PipXdVtMXFTttgp+kVtyUA= github.com/containerd/containerd v1.5.0-beta.1/go.mod h1:5HfvG1V2FsKesEGQ17k5/T7V960Tmcumvqn8Mc+pCYQ= github.com/containerd/containerd v1.5.0-beta.3/go.mod h1:/wr9AVtEM7x9c+n0+stptlo/uBBoBORwEx6ardVcmKU= github.com/containerd/containerd v1.5.0-beta.4/go.mod h1:GmdgZd2zA2GYIBZ0w09ZvgqEq8EfBp/m3lcVZIvPHhI= @@ -481,6 +491,7 @@ github.com/containerd/ttrpc v0.0.0-20190828154514-0e0f228740de/go.mod h1:PvCDdDG github.com/containerd/ttrpc v0.0.0-20190828172938-92c8520ef9f8/go.mod h1:PvCDdDGpgqzQIzDW1TphrGLssLDZp2GuS+X5DkEJB8o= github.com/containerd/ttrpc v0.0.0-20191028202541-4f1b8fe65a5c/go.mod h1:LPm1u0xBw8r8NOKoOdNMeVHSawSsltak+Ihv+etqsE8= github.com/containerd/ttrpc v1.0.1/go.mod h1:UAxOpgT9ziI0gJrmKvgcZivgxOp8iFPSk8httJEt98Y= +github.com/containerd/ttrpc v1.0.2 h1:2/O3oTZN36q2xRolk0a2WWGgh7/Vf/liElg5hFYLX9U= github.com/containerd/ttrpc v1.0.2/go.mod h1:UAxOpgT9ziI0gJrmKvgcZivgxOp8iFPSk8httJEt98Y= github.com/containerd/typeurl v0.0.0-20180627222232-a93fcdb778cd/go.mod h1:Cm3kwCdlkCfMSHURc+r6fwoGH6/F1hH3S4sg0rLFWPc= github.com/containerd/typeurl v0.0.0-20190911142611-5eb25027c9fd/go.mod h1:GeKYzf2pQcqv7tJ0AoCuuhtnqhva5LNU3U+OyKxxJpk= @@ -550,6 +561,7 @@ github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ github.com/crossdock/crossdock-go v0.0.0-20160816171116-049aabb0122b/go.mod h1:v9FBN7gdVTpiD/+LZ7Po0UKvROyT87uLVxTHVky/dlQ= github.com/cucumber/godog v0.8.1/go.mod h1:vSh3r/lM+psC1BPXvdkSEuNjmXfpVqrMGYAElF6hxnA= github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4= +github.com/cyphar/filepath-securejoin v0.2.2 h1:jCwT2GTP+PY5nBz3c/YL5PAIbusElVrPujOBSCj8xRg= github.com/cyphar/filepath-securejoin v0.2.2/go.mod h1:FpkQEhXnPnOthhzymB7CGsFk2G9VLXONKD9G7QGMM+4= github.com/cznic/b v0.0.0-20180115125044-35e9bbe41f07/go.mod h1:URriBxXwVq5ijiJ12C7iIZqlA69nTlI+LgI6/pwftG8= github.com/cznic/fileutil v0.0.0-20180108211300-6a051e75936f/go.mod h1:8S58EK26zhXSxzv7NQFpnliaOQsmDUxvoQO3rt154Vg= @@ -680,6 +692,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7 github.com/envoyproxy/protoc-gen-validate v0.6.1 h1:4CF52PCseTFt4bE+Yk3dIpdVi7XWuPVMhPtm4FaIJPM= github.com/envoyproxy/protoc-gen-validate v0.6.1/go.mod h1:txg5va2Qkip90uYoSKH+nkAAmXrb2j3iq4FLwdrCbXQ= github.com/ericchiang/k8s v1.2.0/go.mod h1:/OmBgSq2cd9IANnsGHGlEz27nwMZV2YxlpXuQtU3Bz4= +github.com/euank/go-kmsg-parser v2.0.0+incompatible h1:cHD53+PLQuuQyLZeriD1V/esuG4MuU0Pjs5y6iknohY= +github.com/euank/go-kmsg-parser v2.0.0+incompatible/go.mod h1:MhmAMZ8V4CYH4ybgdRwPr2TU5ThnS43puaKEMpja1uw= github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/evanphx/json-patch v4.5.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/evanphx/json-patch v4.9.0+incompatible h1:kLcOMZeuLAJvL2BPWLMIj5oaZQobrkAqrL+WFZwQses= @@ -917,6 +931,7 @@ github.com/godbus/dbus v0.0.0-20190402143921-271e53dc4968/go.mod h1:/YcGZj5zSblf github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e h1:BWhy2j3IXJhjCbC68FptL43tDKIq8FladmaTs3Xs7Z8= github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gofrs/flock v0.7.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU= github.com/gofrs/uuid v2.1.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= @@ -1004,6 +1019,10 @@ github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Z github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4= github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA= +github.com/google/cadvisor v0.42.0 h1:cOksMcz7nkbUET0vx2tcg7HNbUOHu8/m7fDf/QfMTaQ= +github.com/google/cadvisor v0.42.0/go.mod h1:Q0HacoI0IqP/Rpw9jAK6ahfV6P0bvXB45smJiyEzXXw= +github.com/google/cadvisor v0.43.0 h1:z0ULgYPKZ7L/c7Zjq+ZD6ltklWwYdCSvBMgSjNC/hGo= +github.com/google/cadvisor v0.43.0/go.mod h1:+RdMSbc3FVr5NYCD2dOEJy/LI0jYJ/0xJXkzWXEyiFQ= github.com/google/flatbuffers v1.11.0/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= github.com/google/flatbuffers v1.12.0/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= github.com/google/flatbuffers v2.0.0+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= @@ -1403,6 +1422,7 @@ github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALr github.com/kardianos/service v1.0.0/go.mod h1:8CzDhVuCuugtsHyZoTvsOBuvonN/UDBvl0kH+BUxvbo= github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4= github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA= +github.com/karrick/godirwalk v1.16.1 h1:DynhcF+bztK8gooS0+NDJFrdNZjJ3gzVzC545UNA9iw= github.com/karrick/godirwalk v1.16.1/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/keybase/go-crypto v0.0.0-20180614160407-5114a9a81e1b/go.mod h1:ghbZscTyKdM07+Fw3KSi0hcJm+AlEUWj8QLlPtijN/M= @@ -1569,6 +1589,8 @@ github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0 github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs= github.com/mikioh/ipaddr v0.0.0-20190404000644-d465c8ab6721/go.mod h1:Ickgr2WtCLZ2MDGd4Gr0geeCH5HybhRJbonOgQpvSxc= github.com/mileusna/useragent v0.0.0-20190129205925-3e331f0949a5/go.mod h1:JWhYAp2EXqUtsxTKdeGlY8Wp44M7VxThC9FEoNGi2IE= +github.com/mindprince/gonvml v0.0.0-20190828220739-9ebdce4bb989 h1:PS1dLCGtD8bb9RPKJrc8bS7qHL6JnW1CZvwzH9dPoUs= +github.com/mindprince/gonvml v0.0.0-20190828220739-9ebdce4bb989/go.mod h1:2eu9pRWp8mo84xCg6KswZ+USQHjwgRhNp06sozOdsTY= github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= github.com/minio/md5-simd v1.1.0/go.mod h1:XpBqgZULrMYD3R+M28PcmP0CkI7PEMzB3U77ZrKZ0Gw= @@ -1577,6 +1599,7 @@ github.com/minio/minio-go/v6 v6.0.56/go.mod h1:KQMM+/44DSlSGSQWSfRrAZ12FVMmpWNuX github.com/minio/minio-go/v7 v7.0.2/go.mod h1:dJ80Mv2HeGkYLH1sqS/ksz07ON6csH3S6JUMSQ2zAns= github.com/minio/minio-go/v7 v7.0.10/go.mod h1:td4gW1ldOsj1PbSNS+WYK43j+P1XVhX/8W8awaYlBFo= github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= +github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible h1:aKW/4cBs+yK6gpqU3K/oIwk9Q/XICqd3zOX/UFuvqmk= github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible/go.mod h1:8AuVvqP/mXw1px98n46wfvcGfQ4ci2FwoAjKYxuo3Z4= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI= @@ -1612,6 +1635,7 @@ github.com/mjibson/esc v0.2.0/go.mod h1:9Hw9gxxfHulMF5OJKCyhYD7PzlSdhzXyaGEBRPH1 github.com/moby/locker v1.0.1/go.mod h1:S7SDdo5zpBK84bzzVlKr2V0hz+7x9hWbYC/kq7oQppc= github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c= github.com/moby/sys/mountinfo v0.4.0/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A= +github.com/moby/sys/mountinfo v0.4.1 h1:1O+1cHA1aujwEwwVMa2Xm2l+gIpUHyd3+D+d7LZh1kM= github.com/moby/sys/mountinfo v0.4.1/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A= github.com/moby/sys/symlink v0.1.0/go.mod h1:GGDODQmbFOjFsXvfLVn3+ZRxkch54RkSiGqsZeMYowQ= github.com/moby/term v0.0.0-20200312100748-672ec06f55cd/go.mod h1:DdlQx2hp0Ss5/fLikoLlEeIYiATotOjgB//nb973jeo= @@ -1632,6 +1656,7 @@ github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7P github.com/mozilla/tls-observatory v0.0.0-20190404164649-a3c1b6cfecfd/go.mod h1:SrKMQvPiws7F7iqYp8/TX+IhxCYhzr6N/1yb8cwHsGk= github.com/mozillazg/go-cos v0.13.0/go.mod h1:Zp6DvvXn0RUOXGJ2chmWt2bLEqRAnJnS3DnAZsJsoaE= github.com/mozillazg/go-httpheader v0.2.1/go.mod h1:jJ8xECTlalr6ValeXYdOF8fFUISeBAdw6E61aqQma60= +github.com/mrunalp/fileutils v0.5.0 h1:NKzVxiH7eSk+OQ4M+ZYW1K6h27RUV3MI6NUTsHhU6Z4= github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ= github.com/multiplay/go-ts3 v1.0.0/go.mod h1:14S6cS3fLNT3xOytrA/DkRyAFNuQLMLEqOYAsf87IbQ= github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= @@ -1771,14 +1796,20 @@ github.com/opencontainers/runc v0.1.1/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59P github.com/opencontainers/runc v1.0.0-rc8.0.20190926000215-3e425f80a8c9/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U= github.com/opencontainers/runc v1.0.0-rc9/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U= github.com/opencontainers/runc v1.0.0-rc93/go.mod h1:3NOsor4w32B2tC0Zbl8Knk4Wg84SM2ImC1fxBuqJ/H0= +github.com/opencontainers/runc v1.0.2 h1:opHZMaswlyxz1OuGpBE53Dwe4/xF7EZTY0A2L/FpCOg= +github.com/opencontainers/runc v1.0.2/go.mod h1:aTaHFFwQXuA71CiyxOdFFIorAoemI04suvGRQFzWTD0= github.com/opencontainers/runtime-spec v0.1.2-0.20190507144316-5b71a03e2700/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/opencontainers/runtime-spec v1.0.1/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/opencontainers/runtime-spec v1.0.2-0.20190207185410-29686dbc5559/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/opencontainers/runtime-spec v1.0.3-0.20200929063507-e6143ca7d51d/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= +github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 h1:3snG66yBm59tKhhSPQrQ/0bCrv1LQbKt40LnUPiUxdc= +github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/opencontainers/runtime-tools v0.0.0-20181011054405-1d69bd0f9c39/go.mod h1:r3f7wjNzSs2extwzU3Y+6pKfobzPh+kKFJ3ofN+3nfs= github.com/opencontainers/selinux v1.6.0/go.mod h1:VVGKuOLlE7v4PJyT6h7mNWvq1rzqiriPsEqVhc+svHE= github.com/opencontainers/selinux v1.8.0/go.mod h1:RScLhm78qiWa2gbVCcGkC7tCGdgk3ogry1nUQF8Evvo= +github.com/opencontainers/selinux v1.8.2 h1:c4ca10UMgRcvZ6h0K4HtS15UaVSBEaE+iln2LVpAuGc= +github.com/opencontainers/selinux v1.8.2/go.mod h1:MUIHuUEvKB1wtJjQdOyYRgOnLD2xAPP8dBsCoU0KuF8= github.com/opentracing-contrib/go-grpc v0.0.0-20180928155321-4b5a12d3ff02/go.mod h1:JNdpVEzCpXBgIiv4ds+TzhN1hrtxq6ClLrTlT9OQRSc= github.com/opentracing-contrib/go-grpc v0.0.0-20191001143057-db30781987df/go.mod h1:DYR5Eij8rJl8h7gblRrOZ8g0kW1umSpKqYIBTgeDtLo= github.com/opentracing-contrib/go-grpc v0.0.0-20210225150812-73cb765af46e h1:4cPxUYdgaGzZIT5/j0IfqOrrXmq6bG8AwvwisMXpdrg= @@ -2008,6 +2039,7 @@ github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210223165440-c65ae3540d44 github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210223165440-c65ae3540d44/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= +github.com/seccomp/libseccomp-golang v0.9.1 h1:NJjM5DNFOs0s3kYE1WUOr6G8V97sdt46rlXTMfXGWBo= github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo= github.com/securego/gosec v0.0.0-20200203094520-d13bb6d2420c/go.mod h1:gp0gaHj0WlmPh9BdsTmo1aq6C27yIPWdxCKGFGdVKBE= github.com/segmentio/fasthash v0.0.0-20180216231524-a72b379d632e/go.mod h1:tm/wZFQ8e24NYaBGIlnO2WGCAi67re4HHuOm0sftE/M= @@ -2138,6 +2170,7 @@ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= +github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtseVEc4yCz2qF8ZrQvIDBJLl4S1c3GCXmoI= github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/tbrandon/mbserver v0.0.0-20170611213546-993e1772cc62/go.mod h1:qUzPVlSj2UgxJkVbH0ZwuuiR46U8RBMDT5KLY78Ifpw= github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I= @@ -2216,9 +2249,11 @@ github.com/vektra/mockery v0.0.0-20181123154057-e78b021dcbb5/go.mod h1:ppEjwdhyy github.com/vishvananda/netlink v0.0.0-20171020171820-b2de5d10e38e/go.mod h1:+SR5DhBJrl6ZM7CoCKvpw5BKroDKQ+PJqOg65H/2ktk= github.com/vishvananda/netlink v0.0.0-20181108222139-023a6dafdcdf/go.mod h1:+SR5DhBJrl6ZM7CoCKvpw5BKroDKQ+PJqOg65H/2ktk= github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE= +github.com/vishvananda/netlink v1.1.1-0.20201029203352-d40f9887b852 h1:cPXZWzzG0NllBLdjWoD1nDfaqu98YMv+OneaKc8sPOA= github.com/vishvananda/netlink v1.1.1-0.20201029203352-d40f9887b852/go.mod h1:twkDnbuQxJYemMlGd4JFIcuhgX83tXhKS2B/PRMpOho= github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc/go.mod h1:ZjcWmFBXmLKZu9Nxj3WKYEafiSqer2rnvPr0en9UNpI= github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU= +github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae h1:4hwBBUfQCFe3Cym0ZtKyq7L16eZUtYKs+BaHDN6mAns= github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= github.com/vjeantet/grok v1.0.0/go.mod h1:/FWYEVYekkm+2VjcFmO9PufDU5FgXHUz9oy2EGqmQBo= github.com/vmware/govmomi v0.18.0/go.mod h1:URlwyTFZX72RmxtxuaFL2Uj3fD1JTvZdx59bHWk6aFU= @@ -2738,6 +2773,7 @@ golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210412220455-f1c623a9e750/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210426230700-d19ff857e887/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210503080704-8803ae5d1324/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -3214,6 +3250,8 @@ k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/ k8s.io/utils v0.0.0-20210111153108-fddb29f9d009/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= k8s.io/utils v0.0.0-20210305010621-2afb4311ab10 h1:u5rPykqiCpL+LBfjRkXvnK71gOgIdmq3eHUEkPrbeTI= k8s.io/utils v0.0.0-20210305010621-2afb4311ab10/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= +k8s.io/utils v0.0.0-20211116205334-6203023598ed h1:ck1fRPWPJWsMd8ZRFsWc6mh/zHp5fZ/shhbrgPUxDAE= +k8s.io/utils v0.0.0-20211116205334-6203023598ed/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= modernc.org/httpfs v1.0.0/go.mod h1:BSkfoMUcahSijQD5J/Vu4UMOxzmEf5SNRwyXC4PJBEw= modernc.org/libc v1.3.1/go.mod h1:f8sp9GAfEyGYh3lsRIKtBh/XwACdFvGznxm6GJmQvXk= modernc.org/mathutil v1.1.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= diff --git a/pkg/integrations/cadvisor/cadvisor.go b/pkg/integrations/cadvisor/cadvisor.go new file mode 100644 index 000000000000..7e1e899db7e0 --- /dev/null +++ b/pkg/integrations/cadvisor/cadvisor.go @@ -0,0 +1,168 @@ +//go:build linux +// +build linux + +package cadvisor //nolint:golint + +import ( + "context" + "fmt" + "net/http" + "time" + + "github.com/go-kit/kit/log" + "github.com/google/cadvisor/cache/memory" + "github.com/google/cadvisor/container" + v2 "github.com/google/cadvisor/info/v2" + "github.com/google/cadvisor/manager" + "github.com/google/cadvisor/metrics" + "github.com/google/cadvisor/storage" + "github.com/google/cadvisor/utils/sysfs" + "k8s.io/klog/v2" + "k8s.io/utils/clock" + + "github.com/grafana/agent/pkg/integrations" + + // Register container providers + + "github.com/google/cadvisor/container/containerd" + _ "github.com/google/cadvisor/container/containerd/install" // register containerd container plugin + _ "github.com/google/cadvisor/container/crio/install" // register crio container plugin + "github.com/google/cadvisor/container/docker" + _ "github.com/google/cadvisor/container/docker/install" // register docker container plugin + "github.com/google/cadvisor/container/raw" + _ "github.com/google/cadvisor/container/systemd/install" // register systemd container plugin +) + +// Matching the default disabled set from cadvisor - https://github.com/google/cadvisor/blob/3c6e3093c5ca65c57368845ddaea2b4ca6bc0da8/cmd/cadvisor.go#L78-L93 +// Note: This *could* be kept in sync with upstream by using the following. However, that would require importing the github.com/google/cadvisor/cmd package, which introduces some dependency conflicts that weren't worth the hassle IMHO. +// var disabledMetricsSet = *flag.Lookup("disable_metrics").Value.(*container.MetricSet) +var disabledMetricsSet = container.MetricSet{ + container.MemoryNumaMetrics: struct{}{}, + container.NetworkTcpUsageMetrics: struct{}{}, + container.NetworkUdpUsageMetrics: struct{}{}, + container.NetworkAdvancedTcpUsageMetrics: struct{}{}, + container.ProcessSchedulerMetrics: struct{}{}, + container.ProcessMetrics: struct{}{}, + container.HugetlbUsageMetrics: struct{}{}, + container.ReferencedMemoryMetrics: struct{}{}, + container.CPUTopologyMetrics: struct{}{}, + container.ResctrlMetrics: struct{}{}, + container.CPUSetMetrics: struct{}{}, +} + +// GetIncludedMetrics applies some logic to determine the final set of metrics to be scraped and returned by the cAdvisor integration +func (c *Config) GetIncludedMetrics() (container.MetricSet, error) { + var enabledMetrics, includeMetrics container.MetricSet + // Clear default disabled metrics if explicit disabled metrics are configured + if len(c.DisabledMetrics) > 0 { + disabledMetricsSet = container.MetricSet{} + } + for _, d := range c.DisabledMetrics { + if err := disabledMetricsSet.Set(d); err != nil { + return includeMetrics, fmt.Errorf("failed to set disabled metric: %w", err) + } + } + + for _, e := range c.EnabledMetrics { + if err := enabledMetrics.Set(e); err != nil { + return includeMetrics, fmt.Errorf("failed to set enabled metric: %w", err) + } + } + + if len(enabledMetrics) > 0 { + includeMetrics = enabledMetrics + } else { + includeMetrics = container.AllMetrics.Difference(disabledMetricsSet) + } + + return includeMetrics, nil +} + +// NewIntegration creates a new cadvisor integration +func (c *Config) NewIntegration(logger log.Logger) (integrations.Integration, error) { + return New(logger, c) +} + +// Integration implements the cadvisor integration +type Integration struct { + c *Config + i *integrations.CollectorIntegration +} + +// Run holds all the configuration logic for globals, as well as starting the resource manager and registering the collectors with the collector integration +func (i *Integration) Run(ctx context.Context) error { + // Do gross global configs. This works, so long as there is only one instance of the cAdvisor integration + // per host. + // Containerd + containerd.ArgContainerdEndpoint = &i.c.Containerd + containerd.ArgContainerdNamespace = &i.c.ContainerdNamespace + + // Docker + docker.ArgDockerEndpoint = &i.c.Docker + docker.ArgDockerTLS = &i.c.DockerTLS + docker.ArgDockerCert = &i.c.DockerTLSCert + docker.ArgDockerKey = &i.c.DockerTLSKey + docker.ArgDockerCA = &i.c.DockerTLSCA + + // Raw + raw.DockerOnly = &i.c.DockerOnly + + // Only using in-memory storage, with no backup storage for cadvisor stats + memoryStorage := memory.New(i.c.StorageDuration, []storage.StorageDriver{}) + + sysFs := sysfs.NewRealSysFs() + + var collectorHTTPClient http.Client + + includedMetrics, err := i.c.GetIncludedMetrics() + if err != nil { + return fmt.Errorf("unable to determine included metrics: %w", err) + } + + rm, err := manager.New(memoryStorage, sysFs, manager.HousekeepingConfigFlags, includedMetrics, &collectorHTTPClient, i.c.RawCgroupPrefixAllowlist, i.c.EnvMetadataAllowlist, i.c.PerfEventsConfig, time.Duration(i.c.ResctrlInterval)) + if err != nil { + return fmt.Errorf("failed to create a manager: %w", err) + } + + if err := rm.Start(); err != nil { + return fmt.Errorf("failed to start manager: %w", err) + } + + containerLabelFunc := metrics.DefaultContainerLabels + if !i.c.StoreContainerLabels { + containerLabelFunc = metrics.BaseContainerLabels(i.c.AllowlistedContainerLabels) + } + + machCol := metrics.NewPrometheusMachineCollector(rm, includedMetrics) + // This is really just a concatenation of the defaults found at; + // https://github.com/google/cadvisor/tree/f89291a53b80b2c3659fff8954c11f1fc3de8a3b/cmd/internal/api/versions.go#L536-L540 + // https://github.com/google/cadvisor/tree/f89291a53b80b2c3659fff8954c11f1fc3de8a3b/cmd/internal/http/handlers.go#L109-L110 + // AFAIK all we are ever doing is the "default" metrics request, and we don't need to support the "docker" request type. + reqOpts := v2.RequestOptions{ + IdType: v2.TypeName, + Count: 1, + Recursive: true, + } + contCol := metrics.NewPrometheusCollector(rm, containerLabelFunc, includedMetrics, clock.RealClock{}, reqOpts) + integrations.WithCollectors(machCol, contCol)(i.i) + + <-ctx.Done() + + if err := rm.Stop(); err != nil { + return fmt.Errorf("failed to stop manager: %w", err) + } + return ctx.Err() +} + +// New creates a new cadvisor integration +func New(logger log.Logger, c *Config) (integrations.Integration, error) { + klog.SetLogger(logger) + + ci := integrations.NewCollectorIntegration(c.Name()) + integration := Integration{ + c: c, + i: ci, + } + integrations.WithRunner(integration.Run)(ci) + return ci, nil +} diff --git a/pkg/integrations/cadvisor/cadvisor_stub.go b/pkg/integrations/cadvisor/cadvisor_stub.go new file mode 100644 index 000000000000..399c16ec5c6e --- /dev/null +++ b/pkg/integrations/cadvisor/cadvisor_stub.go @@ -0,0 +1,17 @@ +//go:build !linux +// +build !linux + +package cadvisor //nolint:golint + +import ( + "github.com/grafana/agent/pkg/integrations" + + "github.com/go-kit/kit/log" + "github.com/go-kit/kit/log/level" +) + +// NewIntegration creates a new cadvisor integration +func (c *Config) NewIntegration(logger log.Logger) (integrations.Integration, error) { + level.Warn(logger).Log("msg", "the cadvisor integration only works on linux; enabling it on other platforms will do nothing") + return &integrations.StubIntegration{}, nil +} diff --git a/pkg/integrations/cadvisor/common.go b/pkg/integrations/cadvisor/common.go new file mode 100644 index 000000000000..6edf9a699534 --- /dev/null +++ b/pkg/integrations/cadvisor/common.go @@ -0,0 +1,122 @@ +package cadvisor + +import ( + "time" + + "github.com/grafana/agent/pkg/integrations" + "github.com/grafana/agent/pkg/integrations/config" +) + +const name = "cadvisor" + +// DefaultConfig holds the default settings for the cadvisor integration +var DefaultConfig Config = Config{ + // Common cadvisor config defaults + StoreContainerLabels: true, + ResctrlInterval: 0, + + StorageDuration: 2 * time.Minute, + + // Containerd config defaults + Containerd: "/run/containerd/containerd.sock", + ContainerdNamespace: "k8s.io", + + // Docker config defaults + Docker: "unix:///var/run/docker.sock", + DockerTLS: false, + DockerTLSCert: "cert.pem", + DockerTLSKey: "key.pem", + DockerTLSCA: "ca.pem", + + // Raw config defaults + DockerOnly: false, +} + +// Config controls cadvisor +type Config struct { + Common config.Common `yaml:",inline"` + + // Common cadvisor config options + // StoreContainerLabels converts container labels and environment variables into labels on prometheus metrics for each container. If false, then only metrics exported are container name, first alias, and image name. + StoreContainerLabels bool `yaml:"store_container_labels,omitempty"` + + // AllowlistedContainerLabels list of container labels to be converted to labels on prometheus metrics for each container. store_container_labels must be set to false for this to take effect. + AllowlistedContainerLabels []string `yaml:"allowlisted_container_labels,omitempty"` + + // EnvMetadataAllowlist list of environment variable keys matched with specified prefix that needs to be collected for containers, only support containerd and docker runtime for now. + EnvMetadataAllowlist []string `yaml:"env_metadata_allowlist,omitempty"` + + // RawCgroupPrefixAllowlist list of cgroup path prefix that needs to be collected even when -docker_only is specified. + RawCgroupPrefixAllowlist []string `yaml:"raw_cgroup_prefix_allowlist,omitempty"` + + // PerfEventsConfig path to a JSON file containing configuration of perf events to measure. Empty value disabled perf events measuring. + PerfEventsConfig string `yaml:"perf_events_config,omitempty"` + + // ResctrlInterval resctrl mon groups updating interval. Zero value disables updating mon groups. + ResctrlInterval int `yaml:"resctrl_interval,omitempty"` + + // DisableMetrics list of `metrics` to be disabled. + DisabledMetrics []string `yaml:"disabled_metrics,omitempty"` + + // EnableMetrics list of `metrics` to be enabled. If set, overrides 'disable_metrics'. + EnabledMetrics []string `yaml:"enabled_metrics,omitempty"` + + // StorageDuration length of time to keep data stored in memory (Default: 2m) + StorageDuration time.Duration `yaml:"storage_duration,omitempty"` + + // Containerd config options + // Containerd containerd endpoint + Containerd string `yaml:"containerd,omitempty"` + + // ContainerdNamespace containerd namespace + ContainerdNamespace string `yaml:"containerd_namespace,omitempty"` + + // Docker config options + // Docker docker endpoint + Docker string `yaml:"docker,omitempty"` + + // DockerTLS use TLS to connect to docker + DockerTLS bool `yaml:"docker_tls,omitempty"` + + // DockerTLSCert path to client certificate + DockerTLSCert string `yaml:"docker_tls_cert,omitempty"` + + // DockerTLSKey path to private key + DockerTLSKey string `yaml:"docker_tls_key,omitempty"` + + // DockerTLSCA path to trusted CA + DockerTLSCA string `yaml:"docker_tls_ca,omitempty"` + + // Raw config options + // DockerOnly only report docker containers in addition to root stats + DockerOnly bool `yaml:"docker_only,omitempty"` +} + +// UnmarshalYAML implements yaml.Unmarshaler for Config +func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { + *c = DefaultConfig + + type plain Config + err := unmarshal((*plain)(c)) + return err +} + +// Name returns the name of the integration that this config represents. +func (c *Config) Name() string { + return name +} + +// CommonConfig returns the common settings shared across all configs for +// integrations. +func (c *Config) CommonConfig() config.Common { + return c.Common +} + +// InstanceKey returns the agentKey +func (c *Config) InstanceKey(agentKey string) (string, error) { + return agentKey, nil +} + +func init() { + integrations.RegisterIntegration(&Config{}) +} diff --git a/pkg/integrations/install/install.go b/pkg/integrations/install/install.go index 2138531816f4..e831427faaca 100644 --- a/pkg/integrations/install/install.go +++ b/pkg/integrations/install/install.go @@ -3,6 +3,7 @@ package install import ( _ "github.com/grafana/agent/pkg/integrations/agent" // register agent + _ "github.com/grafana/agent/pkg/integrations/cadvisor" // register cadvisor _ "github.com/grafana/agent/pkg/integrations/consul_exporter" // register consul_exporter _ "github.com/grafana/agent/pkg/integrations/dnsmasq_exporter" // register dnsmasq_exporter _ "github.com/grafana/agent/pkg/integrations/elasticsearch_exporter" // register elasticsearch_exporter diff --git a/pkg/integrations/stub_integration.go b/pkg/integrations/stub_integration.go new file mode 100644 index 000000000000..80a688e11760 --- /dev/null +++ b/pkg/integrations/stub_integration.go @@ -0,0 +1,27 @@ +package integrations + +import ( + "context" + "net/http" + + "github.com/grafana/agent/pkg/integrations/config" +) + +// StubIntegration implements a no-op integration for use on platforms not supported by an integration +type StubIntegration struct{} + +// MetricsHandler returns an http.NotFoundHandler to satisfy the Integration interface +func (i *StubIntegration) MetricsHandler() (http.Handler, error) { + return http.NotFoundHandler(), nil +} + +// ScrapeConfigs returns an empty list of scrape configs, since there is nothing to scrape +func (i *StubIntegration) ScrapeConfigs() []config.ScrapeConfig { + return []config.ScrapeConfig{} +} + +// Run just waits for the context to finish +func (i *StubIntegration) Run(ctx context.Context) error { + <-ctx.Done() + return ctx.Err() +}