Skip to content

Commit

Permalink
[prometheusexporter] Make metrics path configurable
Browse files Browse the repository at this point in the history
Fixes open-telemetry#8810

Signed-off-by: Goutham Veeramachaneni <gouthamve@gmail.com>
  • Loading branch information
gouthamve committed May 3, 2022
1 parent 92ab551 commit 7a8948a
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 🧰

Expand Down
4 changes: 3 additions & 1 deletion exporter/prometheusexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +27,7 @@ Example:
exporters:
prometheus:
endpoint: "1.2.3.4:1234"
metrics_path: "/metrics"
namespace: test-space
const_labels:
label1: value1
Expand All @@ -34,4 +36,4 @@ exporters:
metric_expiration: 180m
resource_to_telemetry_conversion:
enabled: true
```
```
3 changes: 3 additions & 0 deletions exporter/prometheusexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
26 changes: 13 additions & 13 deletions exporter/prometheusexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions exporter/prometheusexporter/end_to_end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
1 change: 1 addition & 0 deletions exporter/prometheusexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion exporter/prometheusexporter/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
type prometheusExporter struct {
name string
endpoint string
metricsPath string
shutdownFunc func() error
handler http.Handler
collector *collector
Expand All @@ -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 },
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions exporter/prometheusexporter/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestPrometheusExporter(t *testing.T) {
"code0": "one0",
},
Endpoint: ":8999",
MetricsPath: "/metrics",
SendTimestamps: false,
MetricExpiration: 60 * time.Second,
},
Expand Down Expand Up @@ -107,6 +108,7 @@ func TestPrometheusExporter_endToEnd(t *testing.T) {
"code1": "one1",
},
Endpoint: ":7777",
MetricsPath: "/metrics",
MetricExpiration: 120 * time.Minute,
}

Expand Down Expand Up @@ -182,6 +184,7 @@ func TestPrometheusExporter_endToEndWithTimestamps(t *testing.T) {
"code2": "one2",
},
Endpoint: ":7777",
MetricsPath: "/metrics",
SendTimestamps: true,
MetricExpiration: 120 * time.Minute,
}
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 7a8948a

Please sign in to comment.