forked from open-telemetry/opentelemetry-demo
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move runtime metrics monitoring code to a separate place
Move runtime metrics monitoring code to a separate place
- Loading branch information
1 parent
77d1500
commit 44057a0
Showing
2 changed files
with
46 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package main | ||
|
||
import ( | ||
"context" | ||
"runtime" | ||
|
||
"go.opentelemetry.io/otel/metric" | ||
) | ||
|
||
func recordRuntimeMetrics(meter metric.Meter) error { | ||
// Create metric instruments | ||
gcPause, err := meter.Float64ObservableGauge("go_gc_pause") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
allocatedMemory, err := meter.Float64ObservableGauge("go_allocated_memory") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
memoryObtainedFromSystem, err := meter.Float64ObservableGauge("go_memory_obtained_from_system") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Record the runtime stats periodically | ||
if _, err := meter.RegisterCallback( | ||
func(ctx context.Context, o metric.Observer) error { | ||
var memStats runtime.MemStats | ||
runtime.ReadMemStats(&memStats) | ||
|
||
o.ObserveFloat64(gcPause, float64(memStats.PauseTotalNs)/1e6) // GC Pause in ms | ||
o.ObserveFloat64(allocatedMemory, float64(memStats.Alloc)) // Allocated Memory in B | ||
o.ObserveFloat64(memoryObtainedFromSystem, float64(memStats.Sys)) // Memory Obtained From System in B | ||
return nil | ||
}, | ||
gcPause, allocatedMemory, memoryObtainedFromSystem, | ||
); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |