Path: blob/main/pkg/integrations/process_exporter/process-exporter_linux.go
5302 views
// Package process_exporter embeds https://github.com/ncabatoff/process-exporter1package process_exporter //nolint:golint23import (4"context"5"fmt"6"net/http"78"github.com/go-kit/log"9"github.com/grafana/agent/pkg/build"10"github.com/grafana/agent/pkg/integrations/config"11"github.com/prometheus/client_golang/prometheus"12"github.com/prometheus/client_golang/prometheus/promhttp"1314"github.com/ncabatoff/process-exporter/collector"15)1617// Integration is the process_exporter integration. The integration scrapes18// metrics based on information in the /proc filesystem for Linux.19// Agent's own metrics.20type Integration struct {21c *Config22collector *collector.NamedProcessCollector23}2425// New creates a new instance of the process_exporter integration.26func New(logger log.Logger, c *Config) (*Integration, error) {27cfg, err := c.ProcessExporter.ToConfig()28if err != nil {29return nil, fmt.Errorf("process_names is invalid: %w", err)30}3132pc, err := collector.NewProcessCollector(collector.ProcessCollectorOption{33ProcFSPath: c.ProcFSPath,34Children: c.Children,35Threads: c.Threads,36GatherSMaps: c.SMaps,37Namer: cfg.MatchNamers,38Recheck: c.Recheck,39Debug: false,40})41if err != nil {42return nil, err43}4445return &Integration{c: c, collector: pc}, nil46}4748// MetricsHandler satisfies Integration.RegisterRoutes.49func (i *Integration) MetricsHandler() (http.Handler, error) {50r := prometheus.NewRegistry()51if err := r.Register(i.collector); err != nil {52return nil, fmt.Errorf("couldn't register process_exporter collector: %w", err)53}5455// Register process_exporter_build_info metrics, generally useful for56// dashboards that depend on them for discovering targets.57if err := r.Register(build.NewCollector("process_exporter")); err != nil {58return nil, fmt.Errorf("couldn't register process_exporter: %w", err)59}6061return promhttp.HandlerFor(62prometheus.Gatherers{r},63promhttp.HandlerOpts{64ErrorHandling: promhttp.ContinueOnError,65MaxRequestsInFlight: 0,66},67), nil68}6970// ScrapeConfigs satisfies Integration.ScrapeConfigs.71func (i *Integration) ScrapeConfigs() []config.ScrapeConfig {72return []config.ScrapeConfig{{73JobName: i.c.Name(),74MetricsPath: "/metrics",75}}76}7778// Run satisfies Integration.Run.79func (i *Integration) Run(ctx context.Context) error {80// We don't need to do anything here, so we can just wait for the context to81// finish.82<-ctx.Done()83return ctx.Err()84}858687