Skip to content

Commit

Permalink
go_metrics: adds new runtime metrics (#58)
Browse files Browse the repository at this point in the history
* go_metrics: adds new runtime metrics
go_sched_latency_seconds - Distribution of the time goroutines have spent in the scheduler in a runnable state before actually running
go_mutex_wait_total_seconds - Approximate cumulative time goroutines have spent blocked on a sync.Mutex or sync.RWMutex
#54

* fixes data race

* add tests

* wip

* wip

---------

Co-authored-by: Aliaksandr Valialkin <valyala@victoriametrics.com>
  • Loading branch information
f41gh7 and valyala committed Nov 29, 2023
1 parent ae1e9d8 commit 8870cd3
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
52 changes: 52 additions & 0 deletions go_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@ package metrics
import (
"fmt"
"io"
"math"
"runtime"
runtimemetrics "runtime/metrics"

"github.com/valyala/histogram"
)

// See https://pkg.go.dev/runtime/metrics#hdr-Supported_metrics
var runtimeMetrics = [][2]string{
{"/sched/latencies:seconds", "go_sched_latencies_seconds"},
{"/sync/mutex/wait/total:seconds", "go_mutex_wait_seconds_total"},
{"/cpu/classes/gc/mark/assist:cpu-seconds", "go_gc_mark_assist_cpu_seconds_total"},
{"/cpu/classes/gc/total:cpu-seconds", "go_gc_cpu_seconds_total"},
{"/cpu/classes/scavenge/total:cpu-seconds", "go_scavenge_cpu_seconds_total"},
{"/gc/gomemlimit:bytes", "go_memlimit_bytes"},
}

func writeGoMetrics(w io.Writer) {
writeRuntimeMetrics(w)

var ms runtime.MemStats
runtime.ReadMemStats(&ms)
fmt.Fprintf(w, "go_memstats_alloc_bytes %d\n", ms.Alloc)
Expand All @@ -17,6 +31,7 @@ func writeGoMetrics(w io.Writer) {
fmt.Fprintf(w, "go_memstats_frees_total %d\n", ms.Frees)
fmt.Fprintf(w, "go_memstats_gc_cpu_fraction %g\n", ms.GCCPUFraction)
fmt.Fprintf(w, "go_memstats_gc_sys_bytes %d\n", ms.GCSys)

fmt.Fprintf(w, "go_memstats_heap_alloc_bytes %d\n", ms.HeapAlloc)
fmt.Fprintf(w, "go_memstats_heap_idle_bytes %d\n", ms.HeapIdle)
fmt.Fprintf(w, "go_memstats_heap_inuse_bytes %d\n", ms.HeapInuse)
Expand Down Expand Up @@ -62,3 +77,40 @@ func writeGoMetrics(w io.Writer) {
fmt.Fprintf(w, "go_info_ext{compiler=%q, GOARCH=%q, GOOS=%q, GOROOT=%q} 1\n",
runtime.Compiler, runtime.GOARCH, runtime.GOOS, runtime.GOROOT())
}

func writeRuntimeMetrics(w io.Writer) {
samples := make([]runtimemetrics.Sample, len(runtimeMetrics))
for i, rm := range runtimeMetrics {
samples[i].Name = rm[0]
}
runtimemetrics.Read(samples)
for i, rm := range runtimeMetrics {
writeRuntimeMetric(w, rm[1], &samples[i])
}
}

func writeRuntimeMetric(w io.Writer, name string, sample *runtimemetrics.Sample) {
switch sample.Value.Kind() {
case runtimemetrics.KindBad:
panic(fmt.Errorf("BUG: unexpected runtimemetrics.KindBad for sample.Name=%q", sample.Name))
case runtimemetrics.KindUint64:
fmt.Fprintf(w, "%s %d\n", name, sample.Value.Uint64())
case runtimemetrics.KindFloat64:
fmt.Fprintf(w, "%s %g\n", name, sample.Value.Float64())
case runtimemetrics.KindFloat64Histogram:
writeRuntimeHistogramMetric(w, name, sample.Value.Float64Histogram())
}
}

func writeRuntimeHistogramMetric(w io.Writer, name string, h *runtimemetrics.Float64Histogram) {
runningCount := uint64(0)
buckets := h.Buckets
for i, count := range h.Counts {
fmt.Fprintf(w, `%s_bucket{le="%g"} %d`+"\n", name, buckets[i], runningCount)
runningCount += count
}
fmt.Fprintf(w, `%s_bucket{le="%g"} %d`+"\n", name, buckets[len(buckets)-1], runningCount)
if !math.IsInf(buckets[len(buckets)-1], 1) {
fmt.Fprintf(w, `%s_bucket{le="+Inf"} %d`+"\n", name, runningCount)
}
}
65 changes: 65 additions & 0 deletions go_metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package metrics

import (
"math"
runtimemetrics "runtime/metrics"
"strings"
"testing"
)

func TestWriteRuntimeHistogramMetricOk(t *testing.T) {
f := func(h *runtimemetrics.Float64Histogram, resultExpected string) {
t.Helper()
var wOut strings.Builder
writeRuntimeHistogramMetric(&wOut, "foo", h)
result := wOut.String()
if result != resultExpected {
t.Fatalf("unexpected result; got\n%s\nwant\n%s", result, resultExpected)
}

}

f(&runtimemetrics.Float64Histogram{
Counts: []uint64{1, 2, 3},
Buckets: []float64{1, 2, 3, 4},
}, `foo_bucket{le="1"} 0
foo_bucket{le="2"} 1
foo_bucket{le="3"} 3
foo_bucket{le="4"} 6
foo_bucket{le="+Inf"} 6
`)

f(&runtimemetrics.Float64Histogram{
Counts: []uint64{0, 25, 1, 3},
Buckets: []float64{1, 2, 3, 4, math.Inf(1)},
}, `foo_bucket{le="1"} 0
foo_bucket{le="2"} 0
foo_bucket{le="3"} 25
foo_bucket{le="4"} 26
foo_bucket{le="+Inf"} 29
`)

f(&runtimemetrics.Float64Histogram{
Counts: []uint64{0, 25, 1, 3, 0, 44, 15, 132, 10, 11},
Buckets: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, math.Inf(1)},
}, `foo_bucket{le="1"} 0
foo_bucket{le="2"} 0
foo_bucket{le="3"} 25
foo_bucket{le="4"} 26
foo_bucket{le="5"} 29
foo_bucket{le="6"} 29
foo_bucket{le="7"} 73
foo_bucket{le="8"} 88
foo_bucket{le="9"} 220
foo_bucket{le="10"} 230
foo_bucket{le="+Inf"} 241
`)

f(&runtimemetrics.Float64Histogram{
Counts: []uint64{1, 5},
Buckets: []float64{math.Inf(-1), 4, math.Inf(1)},
}, `foo_bucket{le="-Inf"} 0
foo_bucket{le="4"} 1
foo_bucket{le="+Inf"} 6
`)
}

0 comments on commit 8870cd3

Please sign in to comment.