Skip to content

Commit

Permalink
Move runtime metrics monitoring code to a separate place
Browse files Browse the repository at this point in the history
Move runtime metrics monitoring code to a separate place
  • Loading branch information
liyanwei93 committed Nov 18, 2024
1 parent 77d1500 commit 44057a0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
39 changes: 0 additions & 39 deletions src/checkoutservice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import (
"net"
"net/http"
"os"
goruntime "runtime"
"strconv"
"sync"
"time"

"go.opentelemetry.io/otel/metric"

"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -617,39 +614,3 @@ func (cs *checkoutService) getIntFeatureFlag(ctx context.Context, featureFlagNam

return int(featureFlagValue)
}

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 goruntime.MemStats
goruntime.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
}
46 changes: 46 additions & 0 deletions src/checkoutservice/runtimeMetrics.go
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
}

0 comments on commit 44057a0

Please sign in to comment.