| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package proctelemetry // import "go.opentelemetry.io/collector/service/internal/proctelemetry" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"os" |
| 10 |
"runtime" |
| 11 |
"sync" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/shirou/gopsutil/v4/common" |
| 15 |
"github.com/shirou/gopsutil/v4/process" |
| 16 |
"go.opentelemetry.io/otel/metric" |
| 17 |
|
| 18 |
"go.opentelemetry.io/collector/component" |
| 19 |
"go.opentelemetry.io/collector/service/internal/metadata" |
| 20 |
) |
| 21 |
|
| 22 |
// processMetrics is a struct that contains views related to process metrics (cpu, mem, etc) |
| 23 |
type processMetrics struct { |
| 24 |
startTimeUnixNano int64 |
| 25 |
proc *process.Process |
| 26 |
context context.Context |
| 27 |
|
| 28 |
// mu protects everything bellow. |
| 29 |
mu sync.Mutex |
| 30 |
lastMsRead time.Time |
| 31 |
ms *runtime.MemStats |
| 32 |
} |
| 33 |
|
| 34 |
type RegisterOption interface { |
| 35 |
apply(*registerOption) |
| 36 |
} |
| 37 |
|
| 38 |
type registerOption struct { |
| 39 |
hostProc string |
| 40 |
} |
| 41 |
|
| 42 |
type registerOptionFunc func(*registerOption) |
| 43 |
|
| 44 |
func (fn registerOptionFunc) apply(set *registerOption) { |
| 45 |
fn(set) |
| 46 |
} |
| 47 |
|
| 48 |
// WithHostProc overrides the /proc folder on Linux used by process telemetry. |
| 49 |
func WithHostProc(hostProc string) RegisterOption { |
| 50 |
return registerOptionFunc(func(uo *registerOption) { |
| 51 |
uo.hostProc = hostProc |
| 52 |
}) |
| 53 |
} |
| 54 |
|
| 55 |
// RegisterProcessMetrics creates a new set of processMetrics (mem, cpu) that can be used to measure |
| 56 |
// basic information about this process. |
| 57 |
func RegisterProcessMetrics(cfg component.TelemetrySettings, opts ...RegisterOption) error { |
| 58 |
set := registerOption{} |
| 59 |
for _, opt := range opts { |
| 60 |
opt.apply(&set) |
| 61 |
} |
| 62 |
var err error |
| 63 |
pm := &processMetrics{ |
| 64 |
startTimeUnixNano: time.Now().UnixNano(), |
| 65 |
ms: &runtime.MemStats{}, |
| 66 |
} |
| 67 |
|
| 68 |
ctx := context.Background() |
| 69 |
if set.hostProc != "" { |
| 70 |
ctx = context.WithValue(ctx, common.EnvKey, common.EnvMap{common.HostProcEnvKey: set.hostProc}) |
| 71 |
} |
| 72 |
pm.context = ctx |
| 73 |
pm.proc, err = process.NewProcessWithContext(pm.context, int32(os.Getpid())) |
| 74 |
if err != nil { |
| 75 |
return err |
| 76 |
} |
| 77 |
|
| 78 |
tb, err := metadata.NewTelemetryBuilder(cfg) |
| 79 |
if err != nil { |
| 80 |
return err |
| 81 |
} |
| 82 |
return errors.Join( |
| 83 |
tb.RegisterProcessUptimeCallback(pm.updateProcessUptime), |
| 84 |
tb.RegisterProcessRuntimeHeapAllocBytesCallback(pm.updateAllocMem), |
| 85 |
tb.RegisterProcessRuntimeTotalAllocBytesCallback(pm.updateTotalAllocMem), |
| 86 |
tb.RegisterProcessRuntimeTotalSysMemoryBytesCallback(pm.updateSysMem), |
| 87 |
tb.RegisterProcessCPUSecondsCallback(pm.updateCPUSeconds), |
| 88 |
tb.RegisterProcessMemoryRssCallback(pm.updateRSSMemory), |
| 89 |
) |
| 90 |
} |
| 91 |
|
| 92 |
func (pm *processMetrics) updateProcessUptime(_ context.Context, obs metric.Float64Observer) error { |
| 93 |
now := time.Now().UnixNano() |
| 94 |
obs.Observe(float64(now-pm.startTimeUnixNano) / 1e9) |
| 95 |
return nil |
| 96 |
} |
| 97 |
|
| 98 |
func (pm *processMetrics) updateAllocMem(_ context.Context, obs metric.Int64Observer) error { |
| 99 |
pm.mu.Lock() |
| 100 |
defer pm.mu.Unlock() |
| 101 |
pm.readMemStatsIfNeeded() |
| 102 |
obs.Observe(int64(pm.ms.Alloc)) |
| 103 |
return nil |
| 104 |
} |
| 105 |
|
| 106 |
func (pm *processMetrics) updateTotalAllocMem(_ context.Context, obs metric.Int64Observer) error { |
| 107 |
pm.mu.Lock() |
| 108 |
defer pm.mu.Unlock() |
| 109 |
pm.readMemStatsIfNeeded() |
| 110 |
obs.Observe(int64(pm.ms.TotalAlloc)) |
| 111 |
return nil |
| 112 |
} |
| 113 |
|
| 114 |
func (pm *processMetrics) updateSysMem(_ context.Context, obs metric.Int64Observer) error { |
| 115 |
pm.mu.Lock() |
| 116 |
defer pm.mu.Unlock() |
| 117 |
pm.readMemStatsIfNeeded() |
| 118 |
obs.Observe(int64(pm.ms.Sys)) |
| 119 |
return nil |
| 120 |
} |
| 121 |
|
| 122 |
func (pm *processMetrics) updateCPUSeconds(_ context.Context, obs metric.Float64Observer) error { |
| 123 |
times, err := pm.proc.TimesWithContext(pm.context) //nolint:contextcheck |
| 124 |
if err != nil { |
| 125 |
return err |
| 126 |
} |
| 127 |
|
| 128 |
obs.Observe(times.User + times.System + times.Idle + times.Nice + |
| 129 |
times.Iowait + times.Irq + times.Softirq + times.Steal) |
| 130 |
return nil |
| 131 |
} |
| 132 |
|
| 133 |
func (pm *processMetrics) updateRSSMemory(_ context.Context, obs metric.Int64Observer) error { |
| 134 |
mem, err := pm.proc.MemoryInfoWithContext(pm.context) //nolint:contextcheck |
| 135 |
if err != nil { |
| 136 |
return err |
| 137 |
} |
| 138 |
obs.Observe(int64(mem.RSS)) |
| 139 |
return nil |
| 140 |
} |
| 141 |
|
| 142 |
func (pm *processMetrics) readMemStatsIfNeeded() { |
| 143 |
now := time.Now() |
| 144 |
// If last time we read was less than one second ago just reuse the values |
| 145 |
if now.Sub(pm.lastMsRead) < time.Second { |
| 146 |
return |
| 147 |
} |
| 148 |
pm.lastMsRead = now |
| 149 |
runtime.ReadMemStats(pm.ms) |
| 150 |
} |