Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/process_exporter/process-exporter_linux.go
5302 views
1
// Package process_exporter embeds https://github.com/ncabatoff/process-exporter
2
package process_exporter //nolint:golint
3
4
import (
5
"context"
6
"fmt"
7
"net/http"
8
9
"github.com/go-kit/log"
10
"github.com/grafana/agent/pkg/build"
11
"github.com/grafana/agent/pkg/integrations/config"
12
"github.com/prometheus/client_golang/prometheus"
13
"github.com/prometheus/client_golang/prometheus/promhttp"
14
15
"github.com/ncabatoff/process-exporter/collector"
16
)
17
18
// Integration is the process_exporter integration. The integration scrapes
19
// metrics based on information in the /proc filesystem for Linux.
20
// Agent's own metrics.
21
type Integration struct {
22
c *Config
23
collector *collector.NamedProcessCollector
24
}
25
26
// New creates a new instance of the process_exporter integration.
27
func New(logger log.Logger, c *Config) (*Integration, error) {
28
cfg, err := c.ProcessExporter.ToConfig()
29
if err != nil {
30
return nil, fmt.Errorf("process_names is invalid: %w", err)
31
}
32
33
pc, err := collector.NewProcessCollector(collector.ProcessCollectorOption{
34
ProcFSPath: c.ProcFSPath,
35
Children: c.Children,
36
Threads: c.Threads,
37
GatherSMaps: c.SMaps,
38
Namer: cfg.MatchNamers,
39
Recheck: c.Recheck,
40
Debug: false,
41
})
42
if err != nil {
43
return nil, err
44
}
45
46
return &Integration{c: c, collector: pc}, nil
47
}
48
49
// MetricsHandler satisfies Integration.RegisterRoutes.
50
func (i *Integration) MetricsHandler() (http.Handler, error) {
51
r := prometheus.NewRegistry()
52
if err := r.Register(i.collector); err != nil {
53
return nil, fmt.Errorf("couldn't register process_exporter collector: %w", err)
54
}
55
56
// Register process_exporter_build_info metrics, generally useful for
57
// dashboards that depend on them for discovering targets.
58
if err := r.Register(build.NewCollector("process_exporter")); err != nil {
59
return nil, fmt.Errorf("couldn't register process_exporter: %w", err)
60
}
61
62
return promhttp.HandlerFor(
63
prometheus.Gatherers{r},
64
promhttp.HandlerOpts{
65
ErrorHandling: promhttp.ContinueOnError,
66
MaxRequestsInFlight: 0,
67
},
68
), nil
69
}
70
71
// ScrapeConfigs satisfies Integration.ScrapeConfigs.
72
func (i *Integration) ScrapeConfigs() []config.ScrapeConfig {
73
return []config.ScrapeConfig{{
74
JobName: i.c.Name(),
75
MetricsPath: "/metrics",
76
}}
77
}
78
79
// Run satisfies Integration.Run.
80
func (i *Integration) Run(ctx context.Context) error {
81
// We don't need to do anything here, so we can just wait for the context to
82
// finish.
83
<-ctx.Done()
84
return ctx.Err()
85
}
86
87