Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

基本介绍

异步类型的监控指标只有在metrics reader开始使用该监控指标时才会执行指标计算逻辑。异步类型的监控指标需要设置一个回调函数,该回调函数用于生成指标数值,并在metrics reader读取指标时才会触发回调函数。例如,机器CPU、内存、磁盘使用量的指标,如果没有目标端拉取或者使用该指标时,提前计算指标值毫无意义且浪费计算资源,适合作为异步指标来管理。

gmetric提供的异步类型指标包含:ObservableCounter, ObservableUpDownCounter, OvservableGauge。异步指标类型均是使用Observable开头命名,三种异步指标的操作都差不多,均是在不同的业务场景下有不同的使用差异。

我们用一个简单的示例来演示异步指标的基本使用。

Code Block
languagego
package main

import (
	"context"

	"github.com/prometheus/client_golang/prometheus/promhttp"
	"go.opentelemetry.io/otel/exporters/prometheus"
	"go.opentelemetry.io/otel/sdk/metric"

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

var (
	meter = gmetric.GetGlobalProvider().Meter(gmetric.MeterOption{
		Instrument:        "github.com/gogf/gf/example/metric/basic",
		InstrumentVersion: "v1.0",
	})
	observableCounter = meter.MustObservableCounter(
		"goframe.metric.demo.observable_counter",
		gmetric.MetricOption{
			Help: "This is a simple demo for ObservableCounter usage",
			Unit: "%",
		},
	)
	observableUpDownCounter = meter.MustObservableUpDownCounter(
		"goframe.metric.demo.observable_updown_counter",
		gmetric.MetricOption{
			Help: "This is a simple demo for ObservableUpDownCounter usage",
			Unit: "%",
		},
	)
	observableGauge = meter.MustObservableGauge(
		"goframe.metric.demo.observable_gauge",
		gmetric.MetricOption{
			Help: "This is a simple demo for ObservableGauge usage",
			Unit: "%",
		},
	)
)

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

	// Callback for observable metrics.
	meter.MustRegisterCallback(func(ctx context.Context, obs gmetric.Observer) error {
		obs.Observe(observableCounter, 10)
		obs.Observe(observableUpDownCounter, 20)
		obs.Observe(observableGauge, 30)
		return nil
	}, observableCounter, observableUpDownCounter, observableGauge)

	// 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(metricotelmetric.WithReader(exporter))
	provider.SetAsGlobal()
	defer provider.Shutdown(ctx)

	// HTTP Server for metrics exporting.
	s := g.Server()
	s.BindHandler(otelmetric.StartPrometheusMetricsServer(8000, "/metrics", ghttp.WrapH(promhttp.Handler()))
	s.SetPort(8000)
	s.Run()
}

Meter Callback

异步指标需要定义Callback函数来管理指标数值变化,只有在请求或使用该指标时才会执行该Callback函数。Callback函数中使用Observe函数来更新指标的数值,针对不同异步指标类型的Observe会产生不同的结果。

  • 针对ObservableCounter/ObservableUpDownCounter指标类型,使用Observe函数后将会在原有指标数值上进行增减。
  • 针对ObservableGauge指标类型,使用Observe函数后,该指标的值更会更新为Observe给定的数值。

Metric Callback

除了通过Meter CallBack来实现异步指标的数值更新,也可以在创建指标时通过MetricOption指定Callback函数。例如:

Code Block
languagego
observableCounter = meter.MustObservableCounter(
	"goframe.metric.demo.observable_counter",
	gmetric.MetricOption{
		Help: "This is a simple demo for ObservableCounter usage",
		Unit: "%",
		Callback: func(ctx context.Context, obs gmetric.MetricObserver) error {
			obs.Observe(10)
			return nil
		},
	},
)

Primetheus Exporter

通过以下路由将指标通过Prometheus协议暴露:

Code Block
languagego
sotelmetric.BindHandlerStartPrometheusMetricsServer(8000, "/metrics", ghttp.WrapH(promhttp.Handler()))

执行后,访问 http://127.0.0.1:8000/metrics 查看暴露的指标:

Image RemovedImage Added

我们这里只关注本次示例中的指标,其他自动暴露的指标在后续章节介绍。



Panel
titleContent Menu

Table of Contents