Skip to content

Commit

Permalink
prometheusreceiver: use gopsutil to gather collector start time on co…
Browse files Browse the repository at this point in the history
…mponent initialization
  • Loading branch information
ridwanmsharif committed Jan 20, 2025
1 parent 9f9aec2 commit 4fc4202
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion receiver/prometheusreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/prometheus/client_golang v1.20.5
github.com/prometheus/common v0.61.0
github.com/prometheus/prometheus v0.54.1
github.com/shirou/gopsutil/v4 v4.24.12
github.com/stretchr/testify v1.10.0
go.opentelemetry.io/collector/component v0.117.1-0.20250119231113-f07ebc3afb51
go.opentelemetry.io/collector/component/componentstatus v0.117.1-0.20250119231113-f07ebc3afb51
Expand Down Expand Up @@ -156,7 +157,6 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rs/cors v1.11.1 // indirect
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.29 // indirect
github.com/shirou/gopsutil/v4 v4.24.12 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/gjson v1.10.2 // indirect
Expand Down
22 changes: 17 additions & 5 deletions receiver/prometheusreceiver/internal/starttimemetricadjuster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ package internal // import "github.com/open-telemetry/opentelemetry-collector-co

import (
"errors"
"os"
"regexp"
"time"

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

// approximateCollectorStartTime is the approximate start time of the collector. Used
// collectorStartTime is the approximate start time of the collector. Used
// as a fallback start time for metrics that don't have a start time set.
// Set when the component is initialized.
approximateCollectorStartTime *time.Time
collectorStartTime *time.Time
)

func init() {
now := time.Now()
approximateCollectorStartTime = &now
p, err := process.NewProcess(int32(os.Getpid()))
if err != nil {
panic("Unable to find current process" + err.Error())
}

ct, err := p.CreateTime()
if err != nil {
panic("Unable to find process start time" + err.Error())
}

startTime := time.Unix(ct/1000, 0) // Convert milliseconds to seconds
collectorStartTime = &startTime
}

type startTimeMetricAdjuster struct {
Expand All @@ -38,7 +50,7 @@ type startTimeMetricAdjuster struct {
func NewStartTimeMetricAdjuster(logger *zap.Logger, startTimeMetricRegex *regexp.Regexp, useCollectorStartTimeFallback bool) MetricsAdjuster {
var fallbackStartTime *time.Time
if useCollectorStartTimeFallback {
fallbackStartTime = approximateCollectorStartTime
fallbackStartTime = collectorStartTime
}
return &startTimeMetricAdjuster{
startTimeMetricRegex: startTimeMetricRegex,
Expand Down

0 comments on commit 4fc4202

Please sign in to comment.