Skip to content

Commit 86ee36f

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 7091862 commit 86ee36f

12 files changed

+51
-44
lines changed

.chloggen/starttime-fallback.yaml

+1-1
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

+11-2
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

+4-24
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

+2-2
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

+8
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

+1-1
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

+4-4
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

+2-2
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

+3-5
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

+3-3
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

+8
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/collector/client v1.23.1-0.20250119231113-f07ebc3afb51 // indirect
143151
go.opentelemetry.io/collector/component/componentstatus v0.117.1-0.20250119231113-f07ebc3afb51 // indirect

receiver/purefbreceiver/go.sum

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)