Introduction

Asynchronous monitoring metrics execute the metric calculation logic only when metrics reader starts using that metric. Asynchronous metrics require a callback function, which is used to generate metric values and is triggered only when the metrics reader reads the metric. For example, metrics for machine CPU, memory, and disk usage, if not pulled or used by the target end, calculating metric values in advance is meaningless and wasteful of computational resources, making them suitable to manage as asynchronous metrics.

The asynchronous metrics provided by gmetric include: ObservableCounter, ObservableUpDownCounter, OvservableGauge. Asynchronous metric types are named starting with Observable, and operations for the three asynchronous metrics are quite similar, differing only in usage across different business scenarios.

Let’s demonstrate the basic usage of asynchronous metrics with a simple example.

  1. package main
  2. import (
  3. "context"
  4. "go.opentelemetry.io/otel/exporters/prometheus"
  5. "github.com/gogf/gf/contrib/metric/otelmetric/v2"
  6. "github.com/gogf/gf/v2/frame/g"
  7. "github.com/gogf/gf/v2/os/gctx"
  8. "github.com/gogf/gf/v2/os/gmetric"
  9. )
  10. var (
  11. meter = gmetric.GetGlobalProvider().Meter(gmetric.MeterOption{
  12. Instrument: "github.com/gogf/gf/example/metric/basic",
  13. InstrumentVersion: "v1.0",
  14. })
  15. observableCounter = meter.MustObservableCounter(
  16. "goframe.metric.demo.observable_counter",
  17. gmetric.MetricOption{
  18. Help: "This is a simple demo for ObservableCounter usage",
  19. Unit: "%",
  20. },
  21. )
  22. observableUpDownCounter = meter.MustObservableUpDownCounter(
  23. "goframe.metric.demo.observable_updown_counter",
  24. gmetric.MetricOption{
  25. Help: "This is a simple demo for ObservableUpDownCounter usage",
  26. Unit: "%",
  27. },
  28. )
  29. observableGauge = meter.MustObservableGauge(
  30. "goframe.metric.demo.observable_gauge",
  31. gmetric.MetricOption{
  32. Help: "This is a simple demo for ObservableGauge usage",
  33. Unit: "%",
  34. },
  35. )
  36. )
  37. func main() {
  38. var ctx = gctx.New()
  39. // Callback for observable metrics.
  40. meter.MustRegisterCallback(func(ctx context.Context, obs gmetric.Observer) error {
  41. obs.Observe(observableCounter, 10)
  42. obs.Observe(observableUpDownCounter, 20)
  43. obs.Observe(observableGauge, 30)
  44. return nil
  45. }, observableCounter, observableUpDownCounter, observableGauge)
  46. // Prometheus exporter to export metrics as Prometheus format.
  47. exporter, err := prometheus.New(
  48. prometheus.WithoutCounterSuffixes(),
  49. prometheus.WithoutUnits(),
  50. )
  51. if err != nil {
  52. g.Log().Fatal(ctx, err)
  53. }
  54. // OpenTelemetry provider.
  55. provider := otelmetric.MustProvider(otelmetric.WithReader(exporter))
  56. provider.SetAsGlobal()
  57. defer provider.Shutdown(ctx)
  58. // HTTP Server for metrics exporting.
  59. otelmetric.StartPrometheusMetricsServer(8000, "/metrics")
  60. }

Meter Callback

Asynchronous metrics require the definition of a Callback function to manage metric value changes, and this Callback function will only execute upon request or use of the metric. The Observe function is used within the Callback function to update the metric’s value, producing different results for different types of asynchronous metrics.

  • For ObservableCounter/ObservableUpDownCounter metric types, using the Observe function will increment or decrement the existing metric value.
  • For ObservableGauge metric types, using the Observe function will update the metric to the value given by Observe.

Metric Callback

In addition to updating the value of asynchronous metrics using Meter Callback, you can also specify the Callback function by using MetricOption when creating metrics. For example:

  1. observableCounter = meter.MustObservableCounter(
  2. "goframe.metric.demo.observable_counter",
  3. gmetric.MetricOption{
  4. Help: "This is a simple demo for ObservableCounter usage",
  5. Unit: "%",
  6. Callback: func(ctx context.Context, obs gmetric.MetricObserver) error {
  7. obs.Observe(10)
  8. return nil
  9. },
  10. },
  11. )

Prometheus Exporter

Expose metrics using the Prometheus protocol through the following route:

  1. otelmetric.StartPrometheusMetricsServer(8000, "/metrics")

After execution, visit http://127.0.0.1:8000/metrics to view exposed metrics:

Metrics - Asynchronous Metrics - 图1

Here we only focus on the metrics from this example; other automatically exposed metrics will be introduced in subsequent sections.