Skip to content
Open
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = 1.5.0
VERSION = 1.5.1
TAG = $(VERSION)
PREFIX = nginx/nginx-prometheus-exporter
# renovate: datasource=github-tags depName=golangci/golangci-lint
Expand Down
9 changes: 8 additions & 1 deletion client/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Reading: %d Writing: %d Waiting: %d
type NginxClient struct {
httpClient *http.Client
apiEndpoint string
hostHeader string
}

// StubStats represents NGINX stub_status metrics.
Expand All @@ -37,10 +38,11 @@ type StubConnections struct {
}

// NewNginxClient creates an NginxClient.
func NewNginxClient(httpClient *http.Client, apiEndpoint string) *NginxClient {
func NewNginxClient(httpClient *http.Client, apiEndpoint string, hostHeader string) *NginxClient {
client := &NginxClient{
apiEndpoint: apiEndpoint,
httpClient: httpClient,
hostHeader: hostHeader,
}

return client
Expand All @@ -55,6 +57,11 @@ func (client *NginxClient) GetStubStats() (*StubStats, error) {
if err != nil {
return nil, fmt.Errorf("failed to create a get request: %w", err)
}

if len(client.hostHeader) > 0 {
req.Host = client.hostHeader
}

resp, err := client.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to get %v: %w", client.apiEndpoint, err)
Expand Down
10 changes: 5 additions & 5 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ var (
sslClientCert = kingpin.Flag("nginx.ssl-client-cert", "Path to the PEM encoded client certificate file to use when connecting to the server.").Default("").Envar("SSL_CLIENT_CERT").String()
sslClientKey = kingpin.Flag("nginx.ssl-client-key", "Path to the PEM encoded client certificate key file to use when connecting to the server.").Default("").Envar("SSL_CLIENT_KEY").String()
useProxyProto = kingpin.Flag("nginx.proxy-protocol", "Pass proxy protocol payload to nginx listeners.").Default("false").Envar("PROXY_PROTOCOL").Bool()
HostHeader = kingpin.Flag("nginx.hostheader", "Host header to add to HTTP request.").Default("").Envar("HOST_HEADER").String()

// Custom command-line flags.
timeout = createPositiveDurationFlag(kingpin.Flag("nginx.timeout", "A timeout for scraping metrics from NGINX or NGINX Plus.").Default("5s").Envar("TIMEOUT").HintOptions("5s", "10s", "30s", "1m", "5m"))
Expand Down Expand Up @@ -164,14 +165,14 @@ func main() {
}

if len(*scrapeURIs) == 1 {
registerCollector(logger, transport, (*scrapeURIs)[0], constLabels)
registerCollector(logger, transport, (*scrapeURIs)[0], constLabels, *HostHeader)
} else {
for _, addr := range *scrapeURIs {
// add scrape URI to const labels
labels := maps.Clone(constLabels)
labels["addr"] = addr

registerCollector(logger, transport, addr, labels)
registerCollector(logger, transport, addr, labels, *HostHeader)
}
}

Expand Down Expand Up @@ -224,8 +225,7 @@ func main() {
}

func registerCollector(logger *slog.Logger, transport *http.Transport,
addr string, labels map[string]string,
) {
addr string, labels map[string]string, hostheader string) {
var socketPath string

if strings.HasPrefix(addr, "unix:") {
Expand Down Expand Up @@ -309,7 +309,7 @@ func registerCollector(logger *slog.Logger, transport *http.Transport,
variableLabelNames := collector.NewVariableLabelNames(nil, nil, nil, nil, nil, nil, nil)
prometheus.MustRegister(collector.NewNginxPlusCollector(plusClient, "nginxplus", variableLabelNames, labels, logger))
} else {
ossClient := client.NewNginxClient(httpClient, addr)
ossClient := client.NewNginxClient(httpClient, addr, hostheader)
prometheus.MustRegister(collector.NewNginxCollector(ossClient, "nginx", labels, logger))
}
}
Expand Down