Skip to content

Commit 28579d1

Browse files
committed
prometheusreceiver: change start time fallback from a config knob to a featuregate
This change creates an Alpha feature gate for this functionality since this can be replaced when open-telemetry#37186 is implemented.
1 parent c783a95 commit 28579d1

File tree

14 files changed

+63
-44
lines changed

14 files changed

+63
-44
lines changed

.chloggen/starttime-fallback.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ change_type: enhancement
77
component: prometheusreceiver
88

99
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10-
note: Add `UseCollectorStartTimeFallback` option for the start time metric adjuster to use the collector start time as an approximation of process start time as a fallback.
10+
note: Add `UseCollectorStartTimeFallback` featuregate for the start time metric adjuster to use the collector start time as an approximation of process start time as a fallback.
1111

1212
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
1313
issues: [36364]

receiver/prometheusreceiver/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,17 @@ prometheus --config.file=prom.yaml
6767
```shell
6868
"--feature-gates=receiver.prometheusreceiver.UseCreatedMetric"
6969
```
70+
- `receiver.prometheusreceiver.UseCollectorStartTimeFallback`: enables using
71+
the collector start time as the metric start time if the
72+
process_start_time_seconds metric yields no result (for example if targets
73+
expose no process_start_time_seconds metric). This is useful when the collector
74+
start time is a good approximation of the process start time - for example in
75+
serverless workloads when the collector is deployed as a sidecar. To enable it,
76+
use the following feature gate option:
7077

78+
```shell
79+
"--feature-gates=receiver.prometheusreceiver.UseCollectorStartTimeFallback"
80+
```
7181
- `receiver.prometheusreceiver.EnableNativeHistograms`: process and turn native histogram metrics into OpenTelemetry exponential histograms. For more details consult the [Prometheus native histograms](#prometheus-native-histograms) section.
7282

7383
```shell
@@ -119,7 +129,7 @@ The prometheus receiver also supports additional top-level options:
119129
- **trim_metric_suffixes**: [**Experimental**] When set to true, this enables trimming unit and some counter type suffixes from metric names. For example, it would cause `singing_duration_seconds_total` to be trimmed to `singing_duration`. This can be useful when trying to restore the original metric names used in OpenTelemetry instrumentation. Defaults to false.
120130
- **use_start_time_metric**: When set to true, this enables retrieving the start time of all counter metrics from the process_start_time_seconds metric. This is only correct if all counters on that endpoint started after the process start time, and the process is the only actor exporting the metric after the process started. It should not be used in "exporters" which export counters that may have started before the process itself. Use only if you know what you are doing, as this may result in incorrect rate calculations. Defaults to false.
121131
- **start_time_metric_regex**: The regular expression for the start time metric, and is only applied when use_start_time_metric is enabled. Defaults to process_start_time_seconds.
122-
- **use_collector_start_time_fallback**: When set to true, this option enables using the collector start time as the metric start time if the process_start_time_seconds metric yields no result (for example if targets expose no process_start_time_seconds metric). This is useful when the collector start time is a good approximation of the process start time - for example in serverless workloads when the collector is deployed as a sidecar. This is only applied when use_start_time_metric is enabled. Defaults to false.
132+
123133
For example,
124134

125135
```yaml
@@ -128,7 +138,6 @@ receivers:
128138
trim_metric_suffixes: true
129139
use_start_time_metric: true
130140
start_time_metric_regex: foo_bar_.*
131-
use_collector_start_time_fallback: true
132141
config:
133142
scrape_configs:
134143
- job_name: 'otel-collector'

receiver/prometheusreceiver/config.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@ type Config struct {
2424
PrometheusConfig *PromConfig `mapstructure:"config"`
2525
TrimMetricSuffixes bool `mapstructure:"trim_metric_suffixes"`
2626

27-
// Settings for adjusting metrics. Will default to using an InitialPointAdjuster
28-
// which will use the first scraped point to define the start time for the timeseries.
29-
AdjustOpts MetricAdjusterOpts `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
30-
31-
// ReportExtraScrapeMetrics - enables reporting of additional metrics for Prometheus client like scrape_body_size_bytes
32-
ReportExtraScrapeMetrics bool `mapstructure:"report_extra_scrape_metrics"`
33-
34-
TargetAllocator *targetallocator.Config `mapstructure:"target_allocator"`
35-
}
36-
37-
type MetricAdjusterOpts struct {
3827
// UseStartTimeMetric enables retrieving the start time of all counter
3928
// metrics from the process_start_time_seconds metric. This is only correct
4029
// if all counters on that endpoint started after the process start time,
@@ -45,19 +34,10 @@ type MetricAdjusterOpts struct {
4534
UseStartTimeMetric bool `mapstructure:"use_start_time_metric"`
4635
StartTimeMetricRegex string `mapstructure:"start_time_metric_regex"`
4736

48-
// UseCollectorStartTimeFallback enables using a fallback start time if a
49-
// start time is otherwise unavailable when adjusting metrics. This would
50-
// happen if the UseStartTimeMetric is used but the application doesn't emit
51-
// a process_start_time_seconds metric or a metric that matches the
52-
// StartTimeMetricRegex provided.
53-
//
54-
// If enabled, the fallback start time used for adjusted metrics is an
55-
// approximation of the collector start time.
56-
//
57-
// This option should be used when the collector start time is a good
58-
// approximation of the process start time - for example in serverless
59-
// workloads when the collector is deployed as a sidecar.
60-
UseCollectorStartTimeFallback bool `mapstructure:"use_collector_start_time_fallback"`
37+
// ReportExtraScrapeMetrics - enables reporting of additional metrics for Prometheus client like scrape_body_size_bytes
38+
ReportExtraScrapeMetrics bool `mapstructure:"report_extra_scrape_metrics"`
39+
40+
TargetAllocator *targetallocator.Config `mapstructure:"target_allocator"`
6141
}
6242

6343
// Validate checks the receiver configuration is valid.

receiver/prometheusreceiver/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ func TestLoadConfig(t *testing.T) {
4343
r1 := cfg.(*Config)
4444
assert.Equal(t, "demo", r1.PrometheusConfig.ScrapeConfigs[0].JobName)
4545
assert.Equal(t, 5*time.Second, time.Duration(r1.PrometheusConfig.ScrapeConfigs[0].ScrapeInterval))
46-
assert.True(t, r1.AdjustOpts.UseStartTimeMetric)
46+
assert.True(t, r1.UseStartTimeMetric)
4747
assert.True(t, r1.TrimMetricSuffixes)
48-
assert.Equal(t, "^(.+_)*process_start_time_seconds$", r1.AdjustOpts.StartTimeMetricRegex)
48+
assert.Equal(t, "^(.+_)*process_start_time_seconds$", r1.StartTimeMetricRegex)
4949
assert.True(t, r1.ReportExtraScrapeMetrics)
5050

5151
assert.Equal(t, "http://my-targetallocator-service", r1.TargetAllocator.Endpoint)

receiver/prometheusreceiver/factory.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ var enableNativeHistogramsGate = featuregate.GlobalRegistry().MustRegister(
3333
" those Prometheus classic histograms that have a native histogram alternative"),
3434
)
3535

36+
var useCollectorStartTimeFallbackGate = featuregate.GlobalRegistry().MustRegister(
37+
"receiver.prometheusreceiver.UseCollectorStartTimeFallback",
38+
featuregate.StageAlpha,
39+
featuregate.WithRegisterDescription("When enabled, the Prometheus receiver's"+
40+
" start time metric adjuster will fallback to using the collector start time"+
41+
" when a start time is not available"),
42+
)
43+
3644
// NewFactory creates a new Prometheus receiver factory.
3745
func NewFactory() receiver.Factory {
3846
return receiver.NewFactory(

receiver/prometheusreceiver/internal/starttimemetricadjuster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (stma *startTimeMetricAdjuster) AdjustMetrics(metrics pmetric.Metrics) erro
6565
if stma.fallbackStartTime == nil {
6666
return err
6767
}
68-
stma.logger.Info("Couldn't get start time for metrics. Using fallback start time.", zap.Error(err))
68+
stma.logger.Info("Couldn't get start time for metrics. Using fallback start time.", zap.Error(err), zap.Time("fallback_start_time", *stma.fallbackStartTime))
6969
startTime = float64(stma.fallbackStartTime.Unix())
7070
}
7171

receiver/prometheusreceiver/metrics_receiver.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ func (r *pReceiver) initPrometheusComponents(ctx context.Context, logger log.Log
123123
}()
124124

125125
var startTimeMetricRegex *regexp.Regexp
126-
if r.cfg.AdjustOpts.StartTimeMetricRegex != "" {
127-
startTimeMetricRegex, err = regexp.Compile(r.cfg.AdjustOpts.StartTimeMetricRegex)
126+
if r.cfg.StartTimeMetricRegex != "" {
127+
startTimeMetricRegex, err = regexp.Compile(r.cfg.StartTimeMetricRegex)
128128
if err != nil {
129129
return err
130130
}
@@ -134,10 +134,10 @@ func (r *pReceiver) initPrometheusComponents(ctx context.Context, logger log.Log
134134
r.consumer,
135135
r.settings,
136136
gcInterval(r.cfg.PrometheusConfig),
137-
r.cfg.AdjustOpts.UseStartTimeMetric,
137+
r.cfg.UseStartTimeMetric,
138138
startTimeMetricRegex,
139139
useCreatedMetricGate.IsEnabled(),
140-
r.cfg.AdjustOpts.UseCollectorStartTimeFallback,
140+
useCollectorStartTimeFallbackGate.IsEnabled(),
141141
enableNativeHistogramsGate.IsEnabled(),
142142
r.cfg.PrometheusConfig.GlobalConfig.ExternalLabels,
143143
r.cfg.TrimMetricSuffixes,

receiver/prometheusreceiver/metrics_receiver_helper_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,8 +687,8 @@ func testComponent(t *testing.T, targets []*testData, alterConfig func(*Config),
687687
defer mp.Close()
688688

689689
config := &Config{
690-
PrometheusConfig: cfg,
691-
AdjustOpts: MetricAdjusterOpts{StartTimeMetricRegex: ""},
690+
PrometheusConfig: cfg,
691+
StartTimeMetricRegex: "",
692692
}
693693
if alterConfig != nil {
694694
alterConfig(config)

receiver/prometheusreceiver/metrics_receiver_report_extra_scrape_metrics_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@ func testScraperMetrics(t *testing.T, targets []*testData, reportExtraScrapeMetr
5252

5353
cms := new(consumertest.MetricsSink)
5454
receiver := newPrometheusReceiver(receivertest.NewNopSettings(), &Config{
55-
PrometheusConfig: cfg,
56-
AdjustOpts: MetricAdjusterOpts{
57-
UseStartTimeMetric: false,
58-
StartTimeMetricRegex: "",
59-
},
55+
PrometheusConfig: cfg,
56+
UseStartTimeMetric: false,
57+
StartTimeMetricRegex: "",
6058
ReportExtraScrapeMetrics: reportExtraScrapeMetrics,
6159
}, cms)
6260

receiver/prometheusreceiver/metrics_receiver_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ func TestStartTimeMetric(t *testing.T) {
14241424
},
14251425
}
14261426
testComponent(t, targets, func(c *Config) {
1427-
c.AdjustOpts.UseStartTimeMetric = true
1427+
c.UseStartTimeMetric = true
14281428
})
14291429
}
14301430

@@ -1475,8 +1475,8 @@ func TestStartTimeMetricRegex(t *testing.T) {
14751475
},
14761476
}
14771477
testComponent(t, targets, func(c *Config) {
1478-
c.AdjustOpts.StartTimeMetricRegex = "^(.+_)*process_start_time_seconds$"
1479-
c.AdjustOpts.UseStartTimeMetric = true
1478+
c.StartTimeMetricRegex = "^(.+_)*process_start_time_seconds$"
1479+
c.UseStartTimeMetric = true
14801480
})
14811481
}
14821482

receiver/purefbreceiver/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ require (
4848
github.com/docker/docker v27.3.1+incompatible // indirect
4949
github.com/docker/go-connections v0.4.0 // indirect
5050
github.com/docker/go-units v0.5.0 // indirect
51+
github.com/ebitengine/purego v0.8.1 // indirect
5152
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
5253
github.com/envoyproxy/go-control-plane v0.13.1 // indirect
5354
github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect
@@ -58,6 +59,7 @@ require (
5859
github.com/go-logfmt/logfmt v0.6.0 // indirect
5960
github.com/go-logr/logr v1.4.2 // indirect
6061
github.com/go-logr/stdr v1.2.2 // indirect
62+
github.com/go-ole/go-ole v1.2.6 // indirect
6163
github.com/go-openapi/jsonpointer v0.20.2 // indirect
6264
github.com/go-openapi/jsonreference v0.20.4 // indirect
6365
github.com/go-openapi/swag v0.22.9 // indirect
@@ -107,6 +109,7 @@ require (
107109
github.com/kolo/xmlrpc v0.0.0-20220921171641-a4b6fa1dd06b // indirect
108110
github.com/kylelemons/godebug v1.1.0 // indirect
109111
github.com/linode/linodego v1.37.0 // indirect
112+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
110113
github.com/mailru/easyjson v0.7.7 // indirect
111114
github.com/mattn/go-colorable v0.1.13 // indirect
112115
github.com/mattn/go-isatty v0.0.20 // indirect
@@ -130,14 +133,19 @@ require (
130133
github.com/pkg/errors v0.9.1 // indirect
131134
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
132135
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
136+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
133137
github.com/prometheus/client_golang v1.20.5 // indirect
134138
github.com/prometheus/client_model v0.6.1 // indirect
135139
github.com/prometheus/common/sigv4 v0.1.0 // indirect
136140
github.com/prometheus/procfs v0.15.1 // indirect
137141
github.com/rs/cors v1.11.1 // indirect
138142
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.29 // indirect
143+
github.com/shirou/gopsutil/v4 v4.24.12 // indirect
139144
github.com/spf13/pflag v1.0.5 // indirect
145+
github.com/tklauser/go-sysconf v0.3.12 // indirect
146+
github.com/tklauser/numcpus v0.6.1 // indirect
140147
github.com/vultr/govultr/v2 v2.17.2 // indirect
148+
github.com/yusufpapurcu/wmi v1.2.4 // indirect
141149
go.opencensus.io v0.24.0 // indirect
142150
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
143151
go.opentelemetry.io/collector/client v1.24.1-0.20250121185328-fbefb22cc2b3 // indirect

receiver/purefbreceiver/go.sum

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/simpleprometheusreceiver/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ require (
4545
github.com/docker/docker v27.3.1+incompatible // indirect
4646
github.com/docker/go-connections v0.4.0 // indirect
4747
github.com/docker/go-units v0.5.0 // indirect
48+
github.com/ebitengine/purego v0.8.1 // indirect
4849
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
4950
github.com/envoyproxy/go-control-plane v0.13.1 // indirect
5051
github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect
@@ -56,6 +57,7 @@ require (
5657
github.com/go-logfmt/logfmt v0.6.0 // indirect
5758
github.com/go-logr/logr v1.4.2 // indirect
5859
github.com/go-logr/stdr v1.2.2 // indirect
60+
github.com/go-ole/go-ole v1.2.6 // indirect
5961
github.com/go-openapi/jsonpointer v0.20.2 // indirect
6062
github.com/go-openapi/jsonreference v0.20.4 // indirect
6163
github.com/go-openapi/swag v0.22.9 // indirect
@@ -105,6 +107,7 @@ require (
105107
github.com/kolo/xmlrpc v0.0.0-20220921171641-a4b6fa1dd06b // indirect
106108
github.com/kylelemons/godebug v1.1.0 // indirect
107109
github.com/linode/linodego v1.37.0 // indirect
110+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
108111
github.com/mailru/easyjson v0.7.7 // indirect
109112
github.com/mattn/go-colorable v0.1.13 // indirect
110113
github.com/mattn/go-isatty v0.0.20 // indirect
@@ -128,15 +131,20 @@ require (
128131
github.com/pkg/errors v0.9.1 // indirect
129132
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
130133
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
134+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
131135
github.com/prometheus/client_golang v1.20.5 // indirect
132136
github.com/prometheus/client_model v0.6.1 // indirect
133137
github.com/prometheus/common/sigv4 v0.1.0 // indirect
134138
github.com/prometheus/procfs v0.15.1 // indirect
135139
github.com/rs/cors v1.11.1 // indirect
136140
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.29 // indirect
141+
github.com/shirou/gopsutil/v4 v4.24.12 // indirect
137142
github.com/spf13/pflag v1.0.5 // indirect
143+
github.com/tklauser/go-sysconf v0.3.12 // indirect
144+
github.com/tklauser/numcpus v0.6.1 // indirect
138145
github.com/vultr/govultr/v2 v2.17.2 // indirect
139146
github.com/x448/float16 v0.8.4 // indirect
147+
github.com/yusufpapurcu/wmi v1.2.4 // indirect
140148
go.opencensus.io v0.24.0 // indirect
141149
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
142150
go.opentelemetry.io/collector/client v1.24.1-0.20250121185328-fbefb22cc2b3 // indirect

0 commit comments

Comments
 (0)