diff --git a/CHANGELOG.md b/CHANGELOG.md index a6045cccc31a..fae519f1730c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - `k8sclusterreceiver`: Validate that k8s API supports a resource before setting up a watcher for it (#9523) - `internal/stanza`: Add support for `remove` operator (#9524) +- `prometheusexporter`: Add option to configure metrics path (#9239) ### 🧰 Bug fixes 🧰 diff --git a/exporter/prometheusexporter/README.md b/exporter/prometheusexporter/README.md index 68be92fb4710..0657627f8593 100644 --- a/exporter/prometheusexporter/README.md +++ b/exporter/prometheusexporter/README.md @@ -12,6 +12,7 @@ The following settings are required: The following settings can be optionally configured: +- `metrics_path` (default=`/metrics`): The URL under which the metrics are exposed. - `const_labels` (no default): key/values that are applied for every exported metric. - `namespace` (no default): if set, exports metrics under the provided value. - `send_timestamps` (default = `false`): if true, sends the timestamp of the underlying @@ -26,6 +27,7 @@ Example: exporters: prometheus: endpoint: "1.2.3.4:1234" + metrics_path: "/metrics" namespace: test-space const_labels: label1: value1 @@ -34,4 +36,4 @@ exporters: metric_expiration: 180m resource_to_telemetry_conversion: enabled: true -``` +``` \ No newline at end of file diff --git a/exporter/prometheusexporter/config.go b/exporter/prometheusexporter/config.go index 41ab87ad0f4f..824970980025 100644 --- a/exporter/prometheusexporter/config.go +++ b/exporter/prometheusexporter/config.go @@ -31,6 +31,9 @@ type Config struct { // Endpoint is the address on which the Prometheus scrape handler will be run on. Endpoint string `mapstructure:"endpoint"` + // MetricsPath is the path under which the Prometheus handler serves metrics. /metrics by default. + MetricsPath string `mapstructure:"metrics_path"` + // Namespace if set, exports metrics under the provided value. Namespace string `mapstructure:"namespace"` diff --git a/exporter/prometheusexporter/config_test.go b/exporter/prometheusexporter/config_test.go index 8ea29c71882b..bbecb6ca2737 100644 --- a/exporter/prometheusexporter/config_test.go +++ b/exporter/prometheusexporter/config_test.go @@ -41,17 +41,17 @@ func TestLoadConfig(t *testing.T) { assert.Equal(t, e0, factory.CreateDefaultConfig()) e1 := cfg.Exporters[config.NewComponentIDWithName(typeStr, "2")] - assert.Equal(t, e1, - &Config{ - ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "2")), - Endpoint: "1.2.3.4:1234", - Namespace: "test-space", - ConstLabels: map[string]string{ - "label1": "value1", - "another label": "spaced value", - }, - SendTimestamps: true, - MetricExpiration: 60 * time.Minute, - skipSanitizeLabel: false, - }) + assert.Equal(t, &Config{ + ExporterSettings: config.NewExporterSettings(config.NewComponentIDWithName(typeStr, "2")), + Endpoint: "1.2.3.4:1234", + MetricsPath: "/metrics", + Namespace: "test-space", + ConstLabels: map[string]string{ + "label1": "value1", + "another label": "spaced value", + }, + SendTimestamps: true, + MetricExpiration: 60 * time.Minute, + skipSanitizeLabel: false, + }, e1) } diff --git a/exporter/prometheusexporter/end_to_end_test.go b/exporter/prometheusexporter/end_to_end_test.go index 1302fb77e12f..a466cef5ffe3 100644 --- a/exporter/prometheusexporter/end_to_end_test.go +++ b/exporter/prometheusexporter/end_to_end_test.go @@ -70,6 +70,7 @@ func TestEndToEndSummarySupport(t *testing.T) { ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)), Namespace: "test", Endpoint: ":8787", + MetricsPath: "/metrics", SendTimestamps: true, MetricExpiration: 2 * time.Hour, } diff --git a/exporter/prometheusexporter/factory.go b/exporter/prometheusexporter/factory.go index 05af3cdde06f..d6242c99f0de 100644 --- a/exporter/prometheusexporter/factory.go +++ b/exporter/prometheusexporter/factory.go @@ -43,6 +43,7 @@ func NewFactory() component.ExporterFactory { func createDefaultConfig() config.Exporter { return &Config{ ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)), + MetricsPath: "/metrics", ConstLabels: map[string]string{}, SendTimestamps: false, MetricExpiration: time.Minute * 5, diff --git a/exporter/prometheusexporter/prometheus.go b/exporter/prometheusexporter/prometheus.go index 15887e49d54b..6c09b4f31643 100644 --- a/exporter/prometheusexporter/prometheus.go +++ b/exporter/prometheusexporter/prometheus.go @@ -30,6 +30,7 @@ import ( type prometheusExporter struct { name string endpoint string + metricsPath string shutdownFunc func() error handler http.Handler collector *collector @@ -50,6 +51,7 @@ func newPrometheusExporter(config *Config, set component.ExporterCreateSettings) return &prometheusExporter{ name: config.ID().String(), endpoint: addr, + metricsPath: config.MetricsPath, collector: collector, registry: registry, shutdownFunc: func() error { return nil }, @@ -72,7 +74,7 @@ func (pe *prometheusExporter) Start(_ context.Context, _ component.Host) error { pe.shutdownFunc = ln.Close mux := http.NewServeMux() - mux.Handle("/metrics", pe.handler) + mux.Handle(pe.metricsPath, pe.handler) srv := &http.Server{Handler: mux} go func() { _ = srv.Serve(ln) diff --git a/exporter/prometheusexporter/prometheus_test.go b/exporter/prometheusexporter/prometheus_test.go index 5769ec009341..938e6c1c5717 100644 --- a/exporter/prometheusexporter/prometheus_test.go +++ b/exporter/prometheusexporter/prometheus_test.go @@ -49,6 +49,7 @@ func TestPrometheusExporter(t *testing.T) { "code0": "one0", }, Endpoint: ":8999", + MetricsPath: "/metrics", SendTimestamps: false, MetricExpiration: 60 * time.Second, }, @@ -107,6 +108,7 @@ func TestPrometheusExporter_endToEnd(t *testing.T) { "code1": "one1", }, Endpoint: ":7777", + MetricsPath: "/metrics", MetricExpiration: 120 * time.Minute, } @@ -182,6 +184,7 @@ func TestPrometheusExporter_endToEndWithTimestamps(t *testing.T) { "code2": "one2", }, Endpoint: ":7777", + MetricsPath: "/metrics", SendTimestamps: true, MetricExpiration: 120 * time.Minute, } @@ -258,6 +261,7 @@ func TestPrometheusExporter_endToEndWithResource(t *testing.T) { "code2": "one2", }, Endpoint: ":7777", + MetricsPath: "/metrics", SendTimestamps: true, MetricExpiration: 120 * time.Minute, ResourceToTelemetrySettings: resourcetotelemetry.Settings{