Skip to content

Commit 3d36c25

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

File tree

11 files changed

+40
-42
lines changed

11 files changed

+40
-42
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/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/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

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

0 commit comments

Comments
 (0)