metrics/summary_example_test.go
Vasiliy Tolstov 6aa41f4081 change import path
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-11-09 19:32:47 +03:00

32 lines
700 B
Go

package metrics_test
import (
"fmt"
"time"
"go.unistack.org/metrics"
)
func ExampleSummary() {
// Define a summary in global scope.
var s = metrics.NewSummary(`request_duration_seconds{path="/foo/bar"}`)
// Update the summary with the duration of processRequest call.
startTime := time.Now()
processRequest()
s.UpdateDuration(startTime)
}
func ExampleSummary_vec() {
for i := 0; i < 3; i++ {
// Dynamically construct metric name and pass it to GetOrCreateSummary.
name := fmt.Sprintf(`response_size_bytes{path=%q}`, "/foo/bar")
response := processRequest()
metrics.GetOrCreateSummary(name).Update(float64(len(response)))
}
}
func processRequest() string {
return "foobar"
}