Path: blob/main/pkg/integrations/v2/apache_http/apache_http.go
5295 views
// Package apache_http embeds https://github.com/Lusitaniae/apache_exporter1package apache_http //nolint:golint23import (4"net/http"5"net/url"67ae "github.com/Lusitaniae/apache_exporter/collector"8"github.com/go-kit/log"9"github.com/go-kit/log/level"10integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"11"github.com/grafana/agent/pkg/integrations/v2/common"12"github.com/grafana/agent/pkg/integrations/v2/metricsutils"13"github.com/prometheus/client_golang/prometheus"14"github.com/prometheus/client_golang/prometheus/promhttp"15)1617// DefaultConfig holds the default settings for the apache_http integration18var DefaultConfig = Config{19ApacheAddr: "http://localhost/server-status?auto",20ApacheHostOverride: "",21ApacheInsecure: false,22}2324// Config controls the apache_http integration.25type Config struct {26ApacheAddr string `yaml:"scrape_uri,omitempty"`27ApacheHostOverride string `yaml:"host_override,omitempty"`28ApacheInsecure bool `yaml:"insecure,omitempty"`29Common common.MetricsConfig `yaml:",inline"`30}3132// ApplyDefaults applies the integration's default configuration.33func (c *Config) ApplyDefaults(globals integrations_v2.Globals) error {34c.Common.ApplyDefaults(globals.SubsystemOpts.Metrics.Autoscrape)35return nil36}3738// Identifier returns a string that identifies the integration.39func (c *Config) Identifier(globals integrations_v2.Globals) (string, error) {40if c.Common.InstanceKey != nil {41return *c.Common.InstanceKey, nil42}43u, err := url.Parse(c.ApacheAddr)44if err != nil {45return "", err46}47return u.Host, nil48}4950// UnmarshalYAML implements yaml.Unmarshaler for Config51func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {52*c = DefaultConfig5354type plain Config55return unmarshal((*plain)(c))56}5758// Name returns the name of the integration this config is for.59func (c *Config) Name() string {60return "apache_http"61}6263type apacheHandler struct {64cfg *Config65log log.Logger66}6768// NewIntegration instantiates a new integrations.MetricsIntegration69// which will handle requests to the apache http integration.70func (c *Config) NewIntegration(logger log.Logger, globals integrations_v2.Globals) (integrations_v2.Integration, error) {71ah := &apacheHandler{cfg: c, log: logger}72h, err := ah.createHandler()73if err != nil {74return nil, err75}7677return metricsutils.NewMetricsHandlerIntegration(logger, c, c.Common, globals, h)78}7980func (ah *apacheHandler) createHandler() (http.HandlerFunc, error) {81_, err := url.ParseRequestURI(ah.cfg.ApacheAddr)82if err != nil {83level.Error(ah.log).Log("msg", "scrape_uri is invalid", "err", err)84return nil, err85}8687aeExporter := ae.NewExporter(ah.log, &ae.Config{88ScrapeURI: ah.cfg.ApacheAddr,89HostOverride: ah.cfg.ApacheHostOverride,90Insecure: ah.cfg.ApacheInsecure,91})9293return func(w http.ResponseWriter, r *http.Request) {94registry := prometheus.NewRegistry()95registry.MustRegister(aeExporter)96h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})97h.ServeHTTP(w, r)98}, nil99}100101func init() {102integrations_v2.Register(&Config{}, integrations_v2.TypeMultiplex)103}104105106