From 766dd0a3145dd42206ab862224e20bf1bf34a7b1 Mon Sep 17 00:00:00 2001 From: Joonsoo Park Date: Thu, 6 Jun 2024 14:50:24 +0900 Subject: [PATCH 1/5] add example for synchronous gauge --- metric/example_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/metric/example_test.go b/metric/example_test.go index b01a3646788..015f87cb1cf 100644 --- a/metric/example_test.go +++ b/metric/example_test.go @@ -142,6 +142,23 @@ func ExampleMeter_upDownCounter() { } } +// Gauges can be used to record non-additive values when changes occur. +// +// Here's how you might report the speed of 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) + } + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + speedGauge.Record(r.Context(), 1500) + }) +} + // 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. From 91500353fe81f9a02e07dd6eea59b2b1b7c212c5 Mon Sep 17 00:00:00 2001 From: Joonsoo Park Date: Sat, 8 Jun 2024 20:48:32 +0900 Subject: [PATCH 2/5] fix comments. - add hard-code comment for 1500 RPM --- metric/example_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/metric/example_test.go b/metric/example_test.go index c99f931c4ea..4014954e96f 100644 --- a/metric/example_test.go +++ b/metric/example_test.go @@ -144,7 +144,7 @@ func ExampleMeter_upDownCounter() { // Gauges can be used to record non-additive values when changes occur. // -// Here's how you might report the speed of cpu fan. +// Here's how you might report the current speed of a cpu fan. func ExampleMeter_gauge() { speedGauge, err := meter.Int64Gauge( "cpu.fan.speed", @@ -155,6 +155,7 @@ func ExampleMeter_gauge() { panic(err) } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + // hard-code 1500 RPM for demonstrative purposes speedGauge.Record(r.Context(), 1500) }) } From e70884151b5d8928b3e0347a7a30558e3361388b Mon Sep 17 00:00:00 2001 From: Joonsoo Park Date: Wed, 12 Jun 2024 22:17:28 +0900 Subject: [PATCH 3/5] update sync gauge example to use subscribe cpu fan speed --- metric/example_test.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/metric/example_test.go b/metric/example_test.go index 4014954e96f..922ba227548 100644 --- a/metric/example_test.go +++ b/metric/example_test.go @@ -154,10 +154,21 @@ func ExampleMeter_gauge() { if err != nil { panic(err) } - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + + getCPUFanSpeed := func() int64 { // hard-code 1500 RPM for demonstrative purposes - speedGauge.Record(r.Context(), 1500) - }) + // 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()) + } + }() + } } // Histograms are used to measure a distribution of values over time. From 65cc50a57a8534e4c8fe5d2f4badb57902e9f8d8 Mon Sep 17 00:00:00 2001 From: joonsoo park Date: Wed, 19 Jun 2024 14:26:28 +0000 Subject: [PATCH 4/5] update sync gauge example --- metric/example_test.go | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/metric/example_test.go b/metric/example_test.go index 922ba227548..173713e7387 100644 --- a/metric/example_test.go +++ b/metric/example_test.go @@ -7,6 +7,7 @@ import ( "context" "database/sql" "fmt" + "math/rand" "net/http" "runtime" "time" @@ -155,19 +156,29 @@ func ExampleMeter_gauge() { panic(err) } - getCPUFanSpeed := func() int64 { - // hard-code 1500 RPM for demonstrative purposes - // do the real work to get the cpu fan speed - return 1500 + getCpuFanSpeed := func() int64 { + // Generates a random fan speed for demonstration purpose. + // In real wordl applications, replace this to get the actual fan speed. + return int64(1500 * rand.Intn(1000)) } - _ = func() { - go func() { - for { - time.Sleep(time.Second * 3) - speedGauge.Record(context.Background(), getCPUFanSpeed()) - } - }() + fanSpeedSubscription := make(chan int64, 1) + go func() { + defer close(fanSpeedSubscription) + + for idx := 0; idx < 5; idx++ { + // Synchronous gauges are used when the measurement cycle is + // synchronous to an external change. + // Simulate that external cycle here. + time.Sleep(time.Duration(rand.Intn(3)) * time.Second) + fanSpeed := getCpuFanSpeed() + fanSpeedSubscription <- fanSpeed + } + }() + + ctx := context.Background() + for fanSpeed := range fanSpeedSubscription { + speedGauge.Record(ctx, fanSpeed) } } From 1633a4e1e36eee5b4da9ac0a4562e2a75438a65a Mon Sep 17 00:00:00 2001 From: joonsoo park Date: Fri, 21 Jun 2024 00:38:16 +0000 Subject: [PATCH 5/5] fix typo and var-naming --- metric/example_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/metric/example_test.go b/metric/example_test.go index 173713e7387..acb5c70f5f9 100644 --- a/metric/example_test.go +++ b/metric/example_test.go @@ -156,10 +156,10 @@ func ExampleMeter_gauge() { panic(err) } - getCpuFanSpeed := func() int64 { + getCPUFanSpeed := func() int64 { // Generates a random fan speed for demonstration purpose. - // In real wordl applications, replace this to get the actual fan speed. - return int64(1500 * rand.Intn(1000)) + // In real world applications, replace this to get the actual fan speed. + return int64(1500 + rand.Intn(1000)) } fanSpeedSubscription := make(chan int64, 1) @@ -171,7 +171,7 @@ func ExampleMeter_gauge() { // synchronous to an external change. // Simulate that external cycle here. time.Sleep(time.Duration(rand.Intn(3)) * time.Second) - fanSpeed := getCpuFanSpeed() + fanSpeed := getCPUFanSpeed() fanSpeedSubscription <- fanSpeed } }()