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

Optimize PrometheusCollector scrape efficiency. #2974

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions cmd/internal/http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/google/cadvisor/cmd/internal/pages"
"github.com/google/cadvisor/cmd/internal/pages/static"
"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/validate"
Expand Down Expand Up @@ -92,31 +93,42 @@ func RegisterHandlers(mux httpmux.Mux, containerManager manager.Manager, httpAut
return nil
}

// RegisterPrometheusHandler creates a new PrometheusCollector and configures
// RegisterPrometheusHandler creates a new ContainerCollector and configures
// the provided HTTP mux to handle the given Prometheus endpoint.
func RegisterPrometheusHandler(mux httpmux.Mux, resourceManager manager.Manager, prometheusEndpoint string,
f metrics.ContainerLabelsFunc, includedMetrics container.MetricSet) {
goCollector := prometheus.NewGoCollector()
processCollector := prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{})
machineCollector := metrics.NewPrometheusMachineCollector(resourceManager, includedMetrics)

r := prometheus.NewRegistry()
r.MustRegister(
prometheus.NewGoCollector(),
prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
)
container := metrics.NewContainerCollector(resourceManager, f, includedMetrics, clock.RealClock{})
machine := metrics.NewMachineCollector(resourceManager, includedMetrics)

nameTypeCache := metrics.NewCachedGatherer(container.Collect, machine.Collect)
nameDockerCache := metrics.NewCachedGatherer(container.Collect, machine.Collect)
nameTypeGatherer := prometheus.NewMultiTRegistry(prometheus.ToTransactionalGatherer(r), nameTypeCache)
nameDockerGatherer := prometheus.NewMultiTRegistry(prometheus.ToTransactionalGatherer(r), nameDockerCache)

mux.Handle(prometheusEndpoint, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// TODO(bwplotka): Why we ask for full object if we override half of fields?
opts, err := api.GetRequestOptions(req)
if err != nil {
http.Error(w, "No metrics gathered, last error:\n\n"+err.Error(), http.StatusInternalServerError)
return
}
opts.Count = 1 // we only want the latest datapoint
opts.Recursive = true // get all child containers

r := prometheus.NewRegistry()
r.MustRegister(
metrics.NewPrometheusCollector(resourceManager, f, includedMetrics, clock.RealClock{}, opts),
machineCollector,
goCollector,
processCollector,
)
promhttp.HandlerFor(r, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}).ServeHTTP(w, req)
opts.Count = 1 // We only want the latest datapoint.
opts.Recursive = true // Get all child containers.
bwplotka marked this conversation as resolved.
Show resolved Hide resolved

// Present different metrics depending on option.
if opts.IdType == v2.TypeDocker {
nameDockerCache.UpdateOnMaxAge(opts)
promhttp.HandlerForTransactional(nameDockerGatherer, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}).ServeHTTP(w, req)
return
}
nameTypeCache.UpdateOnMaxAge(opts)
promhttp.HandlerForTransactional(nameTypeGatherer, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}).ServeHTTP(w, req)
}))
}

Expand Down
2 changes: 1 addition & 1 deletion container/libcontainer/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewHandler(cgroupManager cgroups.Manager, rootFs string, pid int, includedM
}
}

// Get cgroup and networking stats of the specified container
// GetStats gets cgroup and networking stats of the specified container.
func (h *Handler) GetStats() (*info.ContainerStats, error) {
ignoreStatsError := false
if cgroups.IsCgroup2UnifiedMode() {
Expand Down
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ module github.com/google/cadvisor
go 1.16

require (
cloud.google.com/go v0.54.0
cloud.google.com/go v0.65.0
github.com/Microsoft/go-winio v0.4.15
github.com/aws/aws-sdk-go v1.35.24
github.com/blang/semver v3.5.1+incompatible
github.com/cespare/xxhash/v2 v2.1.2
github.com/containerd/containerd v1.4.12
github.com/containerd/ttrpc v1.0.2
github.com/containerd/typeurl v1.0.2
Expand All @@ -16,6 +17,7 @@ require (
github.com/docker/go-units v0.4.0
github.com/euank/go-kmsg-parser v2.0.0+incompatible
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.5.2
github.com/gorilla/mux v1.8.0 // indirect
github.com/karrick/godirwalk v1.16.1
github.com/mindprince/gonvml v0.0.0-20190828220739-9ebdce4bb989
Expand All @@ -28,12 +30,12 @@ require (
github.com/opencontainers/runc v1.1.0
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.7.1
github.com/prometheus/client_golang v1.12.2-0.20220223112252-1f81b3e9130f
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.10.0
github.com/prometheus/common v0.32.1
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c
golang.org/x/net v0.0.0-20210525063256-abc453219eb5
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9
google.golang.org/grpc v1.33.2
k8s.io/klog/v2 v2.4.0
k8s.io/utils v0.0.0-20211116205334-6203023598ed
Expand Down
Loading