Skip to main content
Version: 2.8.x(Latest)

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.

package main

import (
"go.opentelemetry.io/otel/exporters/prometheus"

"github.com/gogf/gf/contrib/metric/otelmetric/v2"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gmetric"
)

const (
instrument = "github.com/gogf/gf/example/metric/basic"
instrumentVersion = "v1.0"
)

var (
meter = gmetric.GetGlobalProvider().Meter(gmetric.MeterOption{
Instrument: instrument,
InstrumentVersion: instrumentVersion,
})
counter = meter.MustCounter(
"goframe.metric.demo.counter",
gmetric.MetricOption{
Help: "This is a simple demo for Counter usage",
Unit: "bytes",
},
)
)

func main() {
var ctx = gctx.New()

// Prometheus exporter to export metrics as Prometheus format.
exporter, err := prometheus.New(
prometheus.WithoutCounterSuffixes(),
prometheus.WithoutUnits(),
)
if err != nil {
g.Log().Fatal(ctx, err)
}

// OpenTelemetry provider.
provider := otelmetric.MustProvider(
otelmetric.WithReader(exporter),
otelmetric.WithBuiltInMetrics(),
)
provider.SetAsGlobal()
defer provider.Shutdown(ctx)

// Counter.
counter.Inc(ctx)
counter.Add(ctx, 10)

// HTTP Server for metrics exporting.
otelmetric.StartPrometheusMetricsServer(8000, "/metrics")
}

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

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.