Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Implement WriteOnceScaled, from LogScaled #235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ func Write(r Registry, d time.Duration, w io.Writer) {
}
}

// WriteOnce sorts and writes metrics in the given registry to the given
// WriteOnceScaled sorts and writes metrics in the given registry to the given
// io.Writer.
func WriteOnce(r Registry, w io.Writer) {
func WriteOnceScaled(r Registry, w io.Writer, scale time.Duration) {
du := float64(scale)
duSuffix := scale.String()[1:]

var namedMetrics namedMetricSlice
r.Each(func(name string, i interface{}) {
namedMetrics = append(namedMetrics, namedMetric{name, i})
Expand Down Expand Up @@ -66,15 +69,15 @@ func WriteOnce(r Registry, w io.Writer) {
ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
fmt.Fprintf(w, "timer %s\n", namedMetric.name)
fmt.Fprintf(w, " count: %9d\n", t.Count())
fmt.Fprintf(w, " min: %9d\n", t.Min())
fmt.Fprintf(w, " max: %9d\n", t.Max())
fmt.Fprintf(w, " mean: %12.2f\n", t.Mean())
fmt.Fprintf(w, " stddev: %12.2f\n", t.StdDev())
fmt.Fprintf(w, " median: %12.2f\n", ps[0])
fmt.Fprintf(w, " 75%%: %12.2f\n", ps[1])
fmt.Fprintf(w, " 95%%: %12.2f\n", ps[2])
fmt.Fprintf(w, " 99%%: %12.2f\n", ps[3])
fmt.Fprintf(w, " 99.9%%: %12.2f\n", ps[4])
fmt.Fprintf(w, " min: %12.2f%s\n", float64(t.Min())/du, duSuffix)
fmt.Fprintf(w, " max: %12.2f%s\n", float64(t.Max())/du, duSuffix)
fmt.Fprintf(w, " mean: %12.2f%s\n", t.Mean()/du, duSuffix)
fmt.Fprintf(w, " stddev: %12.2f%s\n", t.StdDev()/du, duSuffix)
fmt.Fprintf(w, " median: %12.2f%s\n", ps[0]/du, duSuffix)
fmt.Fprintf(w, " 75%%: %12.2f%s\n", ps[1]/du, duSuffix)
fmt.Fprintf(w, " 95%%: %12.2f%s\n", ps[2]/du, duSuffix)
fmt.Fprintf(w, " 99%%: %12.2f%s\n", ps[3]/du, duSuffix)
fmt.Fprintf(w, " 99.9%%: %12.2f%s\n", ps[4]/du, duSuffix)
fmt.Fprintf(w, " 1-min rate: %12.2f\n", t.Rate1())
fmt.Fprintf(w, " 5-min rate: %12.2f\n", t.Rate5())
fmt.Fprintf(w, " 15-min rate: %12.2f\n", t.Rate15())
Expand All @@ -83,6 +86,12 @@ func WriteOnce(r Registry, w io.Writer) {
}
}

// WriteOnce sorts and writes metrics in the given registry to the given
// io.Writer.
func WriteOnce(r Registry, w io.Writer) {
WriteOnceScaled(r, w, time.Nanosecond)
}

type namedMetric struct {
name string
m interface{}
Expand Down