Introduction

The framework includes built-in Go basic metrics, which are disabled by default and need to be manually enabled. This can be done by enabling otelmetric.WithBuiltInMetrics() when creating the Provider.

  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. const (
  10. instrument = "github.com/gogf/gf/example/metric/basic"
  11. instrumentVersion = "v1.0"
  12. )
  13. var (
  14. meter = gmetric.GetGlobalProvider().Meter(gmetric.MeterOption{
  15. Instrument: instrument,
  16. InstrumentVersion: instrumentVersion,
  17. })
  18. counter = meter.MustCounter(
  19. "goframe.metric.demo.counter",
  20. gmetric.MetricOption{
  21. Help: "This is a simple demo for Counter usage",
  22. Unit: "bytes",
  23. },
  24. )
  25. )
  26. func main() {
  27. var ctx = gctx.New()
  28. // Prometheus exporter to export metrics as Prometheus format.
  29. exporter, err := prometheus.New(
  30. prometheus.WithoutCounterSuffixes(),
  31. prometheus.WithoutUnits(),
  32. )
  33. if err != nil {
  34. g.Log().Fatal(ctx, err)
  35. }
  36. // OpenTelemetry provider.
  37. provider := otelmetric.MustProvider(
  38. otelmetric.WithReader(exporter),
  39. otelmetric.WithBuiltInMetrics(),
  40. )
  41. provider.SetAsGlobal()
  42. defer provider.Shutdown(ctx)
  43. // Counter.
  44. counter.Inc(ctx)
  45. counter.Add(ctx, 10)
  46. // HTTP Server for metrics exporting.
  47. otelmetric.StartPrometheusMetricsServer(8000, "/metrics")
  48. }

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

Metrics - Built-in Metrics - 图1

Built-in Metrics Description

Metric NameMetric TypeMetric UnitMetric Description
process.runtime.go.cgo.callsCounterNumber of cgo calls in the current process.
process.runtime.go.gc.countCounterNumber of completed gc cycles.
process.runtime.go.gc.pause_nsHistogramnsNumber of nanoseconds paused in GC stop-the-world .
process.runtime.go.gc.pause_total_nsCounternsCumulative microseconds count of GC stop-the-world since the program started.
process.runtime.go.goroutinesGaugeNumber of currently running goroutines.
process.runtime.go.lookupsCounterNumber of pointer lookups executed at runtime.
process.runtime.go.mem.heap_allocGaugebytesNumber of bytes allocated to heap objects.
process.runtime.go.mem.heap_idleGaugebytesIdle (unused) heap memory.
process.runtime.go.mem.heap_inuseGaugebytesHeap memory in use.
process.runtime.go.mem.heap_objectsGaugeNumber of heap objects allocated.
process.runtime.go.mem.live_objectsGaugeNumber of live objects (Mallocs - Frees).
process.runtime.go.mem.heap_releasedGaugebytesHeap memory returned to the operating system.
process.runtime.go.mem.heap_sysGaugebytesHeap memory obtained from the operating system.
process.runtime.uptimeCountermsNumber of milliseconds since the application was initialized.