Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-osman3 committed Jan 31, 2023
1 parent 6a3b6e3 commit f9d294f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .chloggen/proctelemetry-processmetrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ component: proctelemetry
note: "Instrument `proctelemetry.ProcessMetrics` metrics with otel-go"

# One or more tracking issues or pull requests related to the change
issues: [6886]
issues: [6886]
102 changes: 40 additions & 62 deletions service/internal/proctelemetry/process_telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ type processMetrics struct {
otelCPUSeconds instrument.Float64ObservableCounter
otelRSSMemory instrument.Int64ObservableGauge

meter otelmetric.Meter
useOtelForMetrics bool

// mu protects everything bellow.
mu sync.Mutex
lastMsRead time.Time
Expand All @@ -72,17 +69,16 @@ func RegisterProcessMetrics(ocRegistry *metric.Registry, mp otelmetric.MeterProv
startTimeUnixNano: time.Now().UnixNano(),
ballastSizeBytes: ballastSizeBytes,
ms: &runtime.MemStats{},
useOtelForMetrics: useOtel,
}

pm.proc, err = process.NewProcess(int32(os.Getpid()))
if err != nil {
return err
}

if pm.useOtelForMetrics {
pm.meter = mp.Meter(scopeName)
return pm.recordWithOtel()
if useOtel {
meter := mp.Meter(scopeName)
return pm.recordWithOtel(meter)
}
return pm.recordWithOC(ocRegistry)
}
Expand Down Expand Up @@ -159,95 +155,77 @@ func (pm *processMetrics) recordWithOC(ocRegistry *metric.Registry) error {
return nil
}

func (pm *processMetrics) recordWithOtel() error {
func (pm *processMetrics) recordWithOtel(meter otelmetric.Meter) error {
var err error

pm.otelProcessUptime, err = pm.meter.Float64ObservableCounter(
pm.otelProcessUptime, err = meter.Float64ObservableCounter(
"process_uptime",
instrument.WithDescription("Uptime of the process"),
instrument.WithUnit(unit.Unit("s")))
if err != nil {
return err
}
_, err = pm.meter.RegisterCallback(func(_ context.Context, o otelmetric.Observer) error {
o.ObserveFloat64(pm.otelProcessUptime, pm.updateProcessUptime())
return nil
}, pm.otelProcessUptime)
instrument.WithUnit(unit.Unit("s")),
instrument.WithFloat64Callback(func(_ context.Context, o instrument.Float64Observer) error {
o.Observe(pm.updateProcessUptime())
return nil
}))
if err != nil {
return err
}

pm.otelAllocMem, err = pm.meter.Int64ObservableGauge(
pm.otelAllocMem, err = meter.Int64ObservableGauge(
"process_runtime_heap_alloc_bytes",
instrument.WithDescription("Bytes of allocated heap objects (see 'go doc runtime.MemStats.HeapAlloc')"),
instrument.WithUnit(unit.Bytes))
if err != nil {
return err
}
_, err = pm.meter.RegisterCallback(func(_ context.Context, o otelmetric.Observer) error {
o.ObserveInt64(pm.otelAllocMem, pm.updateAllocMem())
return nil
}, pm.otelAllocMem)
instrument.WithUnit(unit.Bytes),
instrument.WithInt64Callback(func(_ context.Context, o instrument.Int64Observer) error {
o.Observe(pm.updateAllocMem())
return nil
}))
if err != nil {
return err
}

pm.otelTotalAllocMem, err = pm.meter.Int64ObservableCounter(
pm.otelTotalAllocMem, err = meter.Int64ObservableCounter(
"process_runtime_total_alloc_bytes",
instrument.WithDescription("Cumulative bytes allocated for heap objects (see 'go doc runtime.MemStats.TotalAlloc')"),
instrument.WithUnit(unit.Bytes))
if err != nil {
return err
}
_, err = pm.meter.RegisterCallback(func(_ context.Context, o otelmetric.Observer) error {
o.ObserveInt64(pm.otelTotalAllocMem, pm.updateTotalAllocMem())
return nil
}, pm.otelTotalAllocMem)
instrument.WithUnit(unit.Bytes),
instrument.WithInt64Callback(func(_ context.Context, o instrument.Int64Observer) error {
o.Observe(pm.updateTotalAllocMem())
return nil
}))
if err != nil {
return err
}

pm.otelSysMem, err = pm.meter.Int64ObservableGauge(
pm.otelSysMem, err = meter.Int64ObservableGauge(
"process_runtime_total_sys_memory_bytes",
instrument.WithDescription("Total bytes of memory obtained from the OS (see 'go doc runtime.MemStats.Sys')"),
instrument.WithUnit(unit.Bytes))
if err != nil {
return err
}
_, err = pm.meter.RegisterCallback(func(_ context.Context, o otelmetric.Observer) error {
o.ObserveInt64(pm.otelSysMem, pm.updateSysMem())
return nil
}, pm.otelSysMem)
instrument.WithUnit(unit.Bytes),
instrument.WithInt64Callback(func(_ context.Context, o instrument.Int64Observer) error {
o.Observe(pm.updateSysMem())
return nil
}))
if err != nil {
return err
}

pm.otelCPUSeconds, err = pm.meter.Float64ObservableCounter(
pm.otelCPUSeconds, err = meter.Float64ObservableCounter(
"process_cpu_seconds",
instrument.WithDescription("Total CPU user and system time in seconds"),
instrument.WithUnit(unit.Unit("s")))
if err != nil {
return err
}
_, err = pm.meter.RegisterCallback(func(_ context.Context, o otelmetric.Observer) error {
o.ObserveFloat64(pm.otelCPUSeconds, pm.updateCPUSeconds())
return nil
}, pm.otelCPUSeconds)
instrument.WithUnit(unit.Unit("s")),
instrument.WithFloat64Callback(func(_ context.Context, o instrument.Float64Observer) error {
o.Observe(pm.updateCPUSeconds())
return nil
}))
if err != nil {
return err
}

pm.otelRSSMemory, err = pm.meter.Int64ObservableGauge(
pm.otelRSSMemory, err = meter.Int64ObservableGauge(
"process_memory_rss",
instrument.WithDescription("Total physical memory (resident set size)"),
instrument.WithUnit(unit.Bytes))
if err != nil {
return err
}
_, err = pm.meter.RegisterCallback(func(_ context.Context, o otelmetric.Observer) error {
o.ObserveInt64(pm.otelRSSMemory, pm.updateRSSMemory())
return nil
}, pm.otelRSSMemory)
instrument.WithUnit(unit.Bytes),
instrument.WithInt64Callback(func(_ context.Context, o instrument.Int64Observer) error {
o.Observe(pm.updateRSSMemory())
return nil
}))
if err != nil {
return err
}
Expand Down

0 comments on commit f9d294f

Please sign in to comment.