Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/agent/agent.go
5304 views
1
// Package agent is an "example" integration that has very little functionality,
2
// but is still useful in practice. The Agent integration re-exposes the Agent's
3
// own metrics endpoint and allows the Agent to scrape itself.
4
package agent
5
6
import (
7
"context"
8
"net/http"
9
10
"github.com/go-kit/log"
11
"github.com/grafana/agent/pkg/integrations"
12
"github.com/grafana/agent/pkg/integrations/config"
13
"github.com/prometheus/client_golang/prometheus/promhttp"
14
)
15
16
// Config controls the Agent integration.
17
type Config struct{}
18
19
// Name returns the name of the integration that this config represents.
20
func (c *Config) Name() string {
21
return "agent"
22
}
23
24
// InstanceKey returns the hostname of the machine.
25
func (c *Config) InstanceKey(agentKey string) (string, error) {
26
return agentKey, nil
27
}
28
29
// NewIntegration converts this config into an instance of an integration.
30
func (c *Config) NewIntegration(_ log.Logger) (integrations.Integration, error) {
31
return New(c), nil
32
}
33
34
func init() {
35
integrations.RegisterIntegration(&Config{})
36
}
37
38
// Integration is the Agent integration. The Agent integration scrapes the
39
// Agent's own metrics.
40
type Integration struct {
41
c *Config
42
}
43
44
// New creates a new Agent integration.
45
func New(c *Config) *Integration {
46
return &Integration{c: c}
47
}
48
49
// MetricsHandler satisfies Integration.RegisterRoutes.
50
func (i *Integration) MetricsHandler() (http.Handler, error) {
51
return promhttp.Handler(), nil
52
}
53
54
// ScrapeConfigs satisfies Integration.ScrapeConfigs.
55
func (i *Integration) ScrapeConfigs() []config.ScrapeConfig {
56
return []config.ScrapeConfig{{
57
JobName: i.c.Name(),
58
MetricsPath: "/metrics",
59
}}
60
}
61
62
// Run satisfies Integration.Run.
63
func (i *Integration) Run(ctx context.Context) error {
64
// We don't need to do anything here, so we can just wait for the context to
65
// finish.
66
<-ctx.Done()
67
return ctx.Err()
68
}
69
70