-
Notifications
You must be signed in to change notification settings - Fork 485
/
apache_http.go
104 lines (88 loc) · 3.11 KB
/
apache_http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Package apache_http embeds https://github.com/Lusitaniae/apache_exporter
package apache_http //nolint:golint
import (
"net/http"
"net/url"
ae "github.com/Lusitaniae/apache_exporter/collector"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"
"github.com/grafana/agent/pkg/integrations/v2/common"
"github.com/grafana/agent/pkg/integrations/v2/metricsutils"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// DefaultConfig holds the default settings for the apache_http integration
var DefaultConfig = Config{
ApacheAddr: "http://localhost/server-status?auto",
ApacheHostOverride: "",
ApacheInsecure: false,
}
// Config controls the apache_http integration.
type Config struct {
ApacheAddr string `yaml:"scrape_uri,omitempty"`
ApacheHostOverride string `yaml:"host_override,omitempty"`
ApacheInsecure bool `yaml:"insecure,omitempty"`
Common common.MetricsConfig `yaml:",inline"`
}
// ApplyDefaults applies the integration's default configuration.
func (c *Config) ApplyDefaults(globals integrations_v2.Globals) error {
c.Common.ApplyDefaults(globals.SubsystemOpts.Metrics.Autoscrape)
return nil
}
// Identifier returns a string that identifies the integration.
func (c *Config) Identifier(globals integrations_v2.Globals) (string, error) {
if c.Common.InstanceKey != nil {
return *c.Common.InstanceKey, nil
}
u, err := url.Parse(c.ApacheAddr)
if err != nil {
return "", err
}
return u.Host, nil
}
// UnmarshalYAML implements yaml.Unmarshaler for Config
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultConfig
type plain Config
return unmarshal((*plain)(c))
}
// Name returns the name of the integration this config is for.
func (c *Config) Name() string {
return "apache_http"
}
type apacheHandler struct {
cfg *Config
log log.Logger
}
// NewIntegration instantiates a new integrations.MetricsIntegration
// which will handle requests to the apache http integration.
func (c *Config) NewIntegration(logger log.Logger, globals integrations_v2.Globals) (integrations_v2.Integration, error) {
ah := &apacheHandler{cfg: c, log: logger}
h, err := ah.createHandler()
if err != nil {
return nil, err
}
return metricsutils.NewMetricsHandlerIntegration(logger, c, c.Common, globals, h)
}
func (ah *apacheHandler) createHandler() (http.HandlerFunc, error) {
_, err := url.ParseRequestURI(ah.cfg.ApacheAddr)
if err != nil {
level.Error(ah.log).Log("msg", "scrape_uri is invalid", "err", err)
return nil, err
}
aeExporter := ae.NewExporter(ah.log, &ae.Config{
ScrapeURI: ah.cfg.ApacheAddr,
HostOverride: ah.cfg.ApacheHostOverride,
Insecure: ah.cfg.ApacheInsecure,
})
return func(w http.ResponseWriter, r *http.Request) {
registry := prometheus.NewRegistry()
registry.MustRegister(aeExporter)
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}, nil
}
func init() {
integrations_v2.Register(&Config{}, integrations_v2.TypeMultiplex)
}