Skip to content

Commit 4fc4202

Browse files
committed
prometheusreceiver: use gopsutil to gather collector start time on component initialization
1 parent 9f9aec2 commit 4fc4202

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

receiver/prometheusreceiver/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/prometheus/client_golang v1.20.5
1414
github.com/prometheus/common v0.61.0
1515
github.com/prometheus/prometheus v0.54.1
16+
github.com/shirou/gopsutil/v4 v4.24.12
1617
github.com/stretchr/testify v1.10.0
1718
go.opentelemetry.io/collector/component v0.117.1-0.20250119231113-f07ebc3afb51
1819
go.opentelemetry.io/collector/component/componentstatus v0.117.1-0.20250119231113-f07ebc3afb51
@@ -156,7 +157,6 @@ require (
156157
github.com/prometheus/procfs v0.15.1 // indirect
157158
github.com/rs/cors v1.11.1 // indirect
158159
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.29 // indirect
159-
github.com/shirou/gopsutil/v4 v4.24.12 // indirect
160160
github.com/spf13/cobra v1.8.1 // indirect
161161
github.com/spf13/pflag v1.0.5 // indirect
162162
github.com/tidwall/gjson v1.10.2 // indirect

receiver/prometheusreceiver/internal/starttimemetricadjuster.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ package internal // import "github.com/open-telemetry/opentelemetry-collector-co
55

66
import (
77
"errors"
8+
"os"
89
"regexp"
910
"time"
1011

12+
"github.com/shirou/gopsutil/v4/process"
1113
"go.opentelemetry.io/collector/pdata/pmetric"
1214
"go.uber.org/zap"
1315
)
@@ -17,15 +19,25 @@ var (
1719
errNoDataPointsStartTimeMetric = errors.New("start time metric with no data points")
1820
errUnsupportedTypeStartTimeMetric = errors.New("unsupported data type for start time metric")
1921

20-
// approximateCollectorStartTime is the approximate start time of the collector. Used
22+
// collectorStartTime is the approximate start time of the collector. Used
2123
// as a fallback start time for metrics that don't have a start time set.
2224
// Set when the component is initialized.
23-
approximateCollectorStartTime *time.Time
25+
collectorStartTime *time.Time
2426
)
2527

2628
func init() {
27-
now := time.Now()
28-
approximateCollectorStartTime = &now
29+
p, err := process.NewProcess(int32(os.Getpid()))
30+
if err != nil {
31+
panic("Unable to find current process" + err.Error())
32+
}
33+
34+
ct, err := p.CreateTime()
35+
if err != nil {
36+
panic("Unable to find process start time" + err.Error())
37+
}
38+
39+
startTime := time.Unix(ct/1000, 0) // Convert milliseconds to seconds
40+
collectorStartTime = &startTime
2941
}
3042

3143
type startTimeMetricAdjuster struct {
@@ -38,7 +50,7 @@ type startTimeMetricAdjuster struct {
3850
func NewStartTimeMetricAdjuster(logger *zap.Logger, startTimeMetricRegex *regexp.Regexp, useCollectorStartTimeFallback bool) MetricsAdjuster {
3951
var fallbackStartTime *time.Time
4052
if useCollectorStartTimeFallback {
41-
fallbackStartTime = approximateCollectorStartTime
53+
fallbackStartTime = collectorStartTime
4254
}
4355
return &startTimeMetricAdjuster{
4456
startTimeMetricRegex: startTimeMetricRegex,

0 commit comments

Comments
 (0)