@@ -5,9 +5,11 @@ package internal // import "github.com/open-telemetry/opentelemetry-collector-co
5
5
6
6
import (
7
7
"errors"
8
+ "os"
8
9
"regexp"
9
10
"time"
10
11
12
+ "github.com/shirou/gopsutil/v4/process"
11
13
"go.opentelemetry.io/collector/pdata/pmetric"
12
14
"go.uber.org/zap"
13
15
)
@@ -17,15 +19,25 @@ var (
17
19
errNoDataPointsStartTimeMetric = errors .New ("start time metric with no data points" )
18
20
errUnsupportedTypeStartTimeMetric = errors .New ("unsupported data type for start time metric" )
19
21
20
- // approximateCollectorStartTime is the approximate start time of the collector. Used
22
+ // collectorStartTime is the approximate start time of the collector. Used
21
23
// as a fallback start time for metrics that don't have a start time set.
22
24
// Set when the component is initialized.
23
- approximateCollectorStartTime * time.Time
25
+ collectorStartTime * time.Time
24
26
)
25
27
26
28
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
29
41
}
30
42
31
43
type startTimeMetricAdjuster struct {
@@ -38,7 +50,7 @@ type startTimeMetricAdjuster struct {
38
50
func NewStartTimeMetricAdjuster (logger * zap.Logger , startTimeMetricRegex * regexp.Regexp , useCollectorStartTimeFallback bool ) MetricsAdjuster {
39
51
var fallbackStartTime * time.Time
40
52
if useCollectorStartTimeFallback {
41
- fallbackStartTime = approximateCollectorStartTime
53
+ fallbackStartTime = collectorStartTime
42
54
}
43
55
return & startTimeMetricAdjuster {
44
56
startTimeMetricRegex : startTimeMetricRegex ,
0 commit comments