Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for synchronous gauge #5492

Merged
merged 12 commits into from
Jun 24, 2024
29 changes: 29 additions & 0 deletions metric/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,35 @@ func ExampleMeter_upDownCounter() {
}
}

// Gauges can be used to record non-additive values when changes occur.
//
// Here's how you might report the current speed of a cpu fan.
func ExampleMeter_gauge() {
speedGauge, err := meter.Int64Gauge(
"cpu.fan.speed",
metric.WithDescription("Speed of CPU fan"),
metric.WithUnit("RPM"),
)
if err != nil {
panic(err)
}

getCPUFanSpeed := func() int64 {
// hard-code 1500 RPM for demonstrative purposes
// do the real work to get the cpu fan speed
return 1500
}

_ = func() {
go func() {
for {
time.Sleep(time.Second * 3)
speedGauge.Record(context.Background(), getCPUFanSpeed())
}
}()
}
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
}

// Histograms are used to measure a distribution of values over time.
//
// Here's how you might report a distribution of response times for an HTTP handler.
Expand Down