Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for custom headers #161

Merged
merged 2 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Help on flags:
--host_override="" Override for HTTP Host header; empty string for no
override.
--insecure Ignore server certificate if using https.
--custom_headers Adds custom headers to the collector.
--web.config="" Path to config yaml file that can enable TLS or
authentication.
--log.level=info Only log messages with the given severity or above.
Expand Down
8 changes: 5 additions & 3 deletions apache_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
scrapeURI = kingpin.Flag("scrape_uri", "URI to apache stub status page.").Default("http://localhost/server-status/?auto").String()
hostOverride = kingpin.Flag("host_override", "Override for HTTP Host header; empty string for no override.").Default("").String()
insecure = kingpin.Flag("insecure", "Ignore server certificate if using https.").Bool()
customHeaders = kingpin.Flag("custom_headers", "Adds custom headers to the collector.").StringMap()
configFile = kingpin.Flag("web.config", "Path to config yaml file that can enable TLS or authentication.").Default("").String()
gracefulStop = make(chan os.Signal, 1)
)
Expand All @@ -53,9 +54,10 @@ func main() {
signal.Notify(gracefulStop, syscall.SIGQUIT)

config := &collector.Config{
ScrapeURI: *scrapeURI,
HostOverride: *hostOverride,
Insecure: *insecure,
ScrapeURI: *scrapeURI,
HostOverride: *hostOverride,
Insecure: *insecure,
CustomHeaders: *customHeaders,
}

webSystemdSocket := false
Expand Down
7 changes: 4 additions & 3 deletions apache_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,10 @@ func checkApacheStatus(t *testing.T, status string, metricCount int) {
promlogConfig := &promlog.Config{}
logger := promlog.New(promlogConfig)
config := &collector.Config{
ScrapeURI: server.URL,
HostOverride: "",
Insecure: false,
ScrapeURI: server.URL,
HostOverride: "",
Insecure: false,
CustomHeaders: map[string]string{"Cookie": "A test cookie"},
}
e := collector.NewExporter(logger, config)
ch := make(chan prometheus.Metric)
Expand Down
26 changes: 16 additions & 10 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ const (
)

type Exporter struct {
URI string
hostOverride string
mutex sync.Mutex
client *http.Client
URI string
hostOverride string
customHeaders map[string]string
mutex sync.Mutex
client *http.Client

up *prometheus.Desc
scrapeFailures prometheus.Counter
Expand All @@ -56,16 +57,18 @@ type Exporter struct {
}

type Config struct {
ScrapeURI string
HostOverride string
Insecure bool
ScrapeURI string
HostOverride string
Insecure bool
CustomHeaders map[string]string
}

func NewExporter(logger log.Logger, config *Config) *Exporter {
return &Exporter{
URI: config.ScrapeURI,
hostOverride: config.HostOverride,
logger: logger,
URI: config.ScrapeURI,
hostOverride: config.HostOverride,
customHeaders: config.CustomHeaders,
logger: logger,
up: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Could the apache server be reached",
Expand Down Expand Up @@ -276,6 +279,9 @@ func (e *Exporter) collect(ch chan<- prometheus.Metric) error {
if err != nil {
return fmt.Errorf("error building scraping request: %v", err)
}
for k, v := range e.customHeaders {
req.Header.Add(k, v)
}
resp, err := e.client.Do(req)
if err != nil {
ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 0)
Expand Down