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