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 Name | Metric Type | Metric Unit | Metric Description |
---|---|---|---|
process.runtime.go.cgo.calls | Counter | Number of cgo calls in the current process. | |
process.runtime.go.gc.count | Counter | Number of completed gc cycles. | |
process.runtime.go.gc.pause_ns | Histogram | ns | Number of nanoseconds paused in GC stop-the-world . |
process.runtime.go.gc.pause_total_ns | Counter | ns | Cumulative microseconds count of GC stop-the-world since the program started. |
process.runtime.go.goroutines | Gauge | Number of currently running goroutines. | |
process.runtime.go.lookups | Counter | Number of pointer lookups executed at runtime. | |
process.runtime.go.mem.heap_alloc | Gauge | bytes | Number of bytes allocated to heap objects. |
process.runtime.go.mem.heap_idle | Gauge | bytes | Idle (unused) heap memory. |
process.runtime.go.mem.heap_inuse | Gauge | bytes | Heap memory in use. |
process.runtime.go.mem.heap_objects | Gauge | Number of heap objects allocated. | |
process.runtime.go.mem.live_objects | Gauge | Number of live objects (Mallocs - Frees ). | |
process.runtime.go.mem.heap_released | Gauge | bytes | Heap memory returned to the operating system. |
process.runtime.go.mem.heap_sys | Gauge | bytes | Heap memory obtained from the operating system. |
process.runtime.uptime | Counter | ms | Number of milliseconds since the application was initialized. |