| | |
| | |
| | |
| |
|
| | package metrics_test |
| |
|
| | import ( |
| | "fmt" |
| | "runtime/metrics" |
| | ) |
| |
|
| | func ExampleRead_readingOneMetric() { |
| | |
| | const myMetric = "/memory/classes/heap/free:bytes" |
| |
|
| | |
| | sample := make([]metrics.Sample, 1) |
| | sample[0].Name = myMetric |
| |
|
| | |
| | metrics.Read(sample) |
| |
|
| | |
| | |
| | |
| | if sample[0].Value.Kind() == metrics.KindBad { |
| | panic(fmt.Sprintf("metric %q no longer supported", myMetric)) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | freeBytes := sample[0].Value.Uint64() |
| |
|
| | fmt.Printf("free but not released memory: %d\n", freeBytes) |
| | } |
| |
|
| | func ExampleRead_readingAllMetrics() { |
| | |
| | descs := metrics.All() |
| |
|
| | |
| | samples := make([]metrics.Sample, len(descs)) |
| | for i := range samples { |
| | samples[i].Name = descs[i].Name |
| | } |
| |
|
| | |
| | metrics.Read(samples) |
| |
|
| | |
| | for _, sample := range samples { |
| | |
| | name, value := sample.Name, sample.Value |
| |
|
| | |
| | switch value.Kind() { |
| | case metrics.KindUint64: |
| | fmt.Printf("%s: %d\n", name, value.Uint64()) |
| | case metrics.KindFloat64: |
| | fmt.Printf("%s: %f\n", name, value.Float64()) |
| | case metrics.KindFloat64Histogram: |
| | |
| | |
| | fmt.Printf("%s: %f\n", name, medianBucket(value.Float64Histogram())) |
| | case metrics.KindBad: |
| | |
| | |
| | panic("bug in runtime/metrics package!") |
| | default: |
| | |
| | |
| | |
| | |
| | |
| | fmt.Printf("%s: unexpected metric Kind: %v\n", name, value.Kind()) |
| | } |
| | } |
| | } |
| |
|
| | func medianBucket(h *metrics.Float64Histogram) float64 { |
| | total := uint64(0) |
| | for _, count := range h.Counts { |
| | total += count |
| | } |
| | thresh := total / 2 |
| | total = 0 |
| | for i, count := range h.Counts { |
| | total += count |
| | if total >= thresh { |
| | return h.Buckets[i] |
| | } |
| | } |
| | panic("should not happen") |
| | } |
| |
|