Introduction

Synchronous Type is used to quickly expose monitoring metrics. Whether or not the metrics reader uses the monitoring metrics, the metric calculation results are completed and await reading. For example, total HTTP request count and request size must be recorded in the corresponding monitoring metrics during the request execution process, making them suitable for management as synchronous metrics.

The synchronous metrics provided by gmetric include: Counter, UpDownCounter, Histogram.

We will demonstrate the basic usage of synchronous metrics with a simple example.

  1. package main
  2. import (
  3. "go.opentelemetry.io/otel/exporters/prometheus"
  4. "github.com/gogf/gf/contrib/metric/otelmetric/v2"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. "github.com/gogf/gf/v2/os/gmetric"
  8. )
  9. var (
  10. meter = gmetric.GetGlobalProvider().Meter(gmetric.MeterOption{
  11. Instrument: "github.com/gogf/gf/example/metric/basic",
  12. InstrumentVersion: "v1.0",
  13. })
  14. counter = meter.MustCounter(
  15. "goframe.metric.demo.counter",
  16. gmetric.MetricOption{
  17. Help: "This is a simple demo for Counter usage",
  18. Unit: "bytes",
  19. },
  20. )
  21. upDownCounter = meter.MustUpDownCounter(
  22. "goframe.metric.demo.updown_counter",
  23. gmetric.MetricOption{
  24. Help: "This is a simple demo for UpDownCounter usage",
  25. Unit: "%",
  26. },
  27. )
  28. histogram = meter.MustHistogram(
  29. "goframe.metric.demo.histogram",
  30. gmetric.MetricOption{
  31. Help: "This is a simple demo for histogram usage",
  32. Unit: "ms",
  33. Buckets: []float64{0, 10, 20, 50, 100, 500, 1000, 2000, 5000, 10000},
  34. },
  35. )
  36. )
  37. func main() {
  38. var ctx = gctx.New()
  39. // Prometheus exporter to export metrics as Prometheus format.
  40. exporter, err := prometheus.New(
  41. prometheus.WithoutCounterSuffixes(),
  42. prometheus.WithoutUnits(),
  43. )
  44. if err != nil {
  45. g.Log().Fatal(ctx, err)
  46. }
  47. // OpenTelemetry provider.
  48. provider := otelmetric.MustProvider(otelmetric.WithReader(exporter))
  49. provider.SetAsGlobal()
  50. defer provider.Shutdown(ctx)
  51. // Counter.
  52. counter.Inc(ctx)
  53. counter.Add(ctx, 10)
  54. // UpDownCounter.
  55. upDownCounter.Inc(ctx)
  56. upDownCounter.Add(ctx, 10)
  57. upDownCounter.Dec(ctx)
  58. // Record values for histogram.
  59. histogram.Record(1)
  60. histogram.Record(20)
  61. histogram.Record(30)
  62. histogram.Record(101)
  63. histogram.Record(2000)
  64. histogram.Record(9000)
  65. histogram.Record(20000)
  66. // HTTP Server for metrics exporting.
  67. otelmetric.StartPrometheusMetricsServer(8000, "/metrics")
  68. }

Counter & UpDownCounter

Counter and UpDownCounter are relatively simple, so they won’t be elaborated here. However, it is important to note that although Counter and UpDownCounter appear similar, they serve to more accurately and finely distinguish usage scenarios. Mapping these data types to classic Prometheus metric types, Counter corresponds to the Counter metric type of Prometheus, whereas UpDownCounter corresponds to the Gauge metric type of Prometheus.

Histogram

Histogram is a statistical type that allows for the quick computation of percentile statistics such as p95, p99, resulting in a histogram of metrics like time consumption and success/failure rates. Note that this metric uses considerable memory and space, so it’s inadvisable to add too many dynamic attributes, as different attributes may derive different storage values for the same Histogram metric.

Prometheus Exporter

In this example, we have used the commonly used Prometheus protocol to output metric contents, typically to expose metrics for external components to capture. The metrics are exposed using the following route with the Prometheus protocol:

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

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

Metrics - Synchronous Metrics - 图1

We focus only on the metrics in this example, while other automatically exposed metrics will be introduced in subsequent sections.