otelmetric

package module
v2.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 22 Imported by: 7

README

GoFrame Metric In OpenTelemetry

Installation

go get -u -v github.com/gogf/gf/contrib/metric/otelmetric/v2

suggested using go.mod:

require github.com/gogf/gf/contrib/metric/otelmetric/v2 latest

Example

basic
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",
	})
	counter = meter.MustCounter(
		"goframe.metric.demo.counter",
		gmetric.MetricOption{
			Help: "This is a simple demo for Counter usage",
			Unit: "bytes",
			Attributes: gmetric.Attributes{
				gmetric.NewAttribute("const_label_1", 1),
			},
		},
	)
	upDownCounter = meter.MustUpDownCounter(
		"goframe.metric.demo.updown_counter",
		gmetric.MetricOption{
			Help: "This is a simple demo for UpDownCounter usage",
			Unit: "%",
			Attributes: gmetric.Attributes{
				gmetric.NewAttribute("const_label_2", 2),
			},
		},
	)
	histogram = meter.MustHistogram(
		"goframe.metric.demo.histogram",
		gmetric.MetricOption{
			Help: "This is a simple demo for histogram usage",
			Unit: "ms",
			Attributes: gmetric.Attributes{
				gmetric.NewAttribute("const_label_3", 3),
			},
			Buckets: []float64{0, 10, 20, 50, 100, 500, 1000, 2000, 5000, 10000},
		},
	)
	observableCounter = meter.MustObservableCounter(
		"goframe.metric.demo.observable_counter",
		gmetric.MetricOption{
			Help: "This is a simple demo for ObservableCounter usage",
			Unit: "%",
			Attributes: gmetric.Attributes{
				gmetric.NewAttribute("const_label_4", 4),
			},
		},
	)
	observableUpDownCounter = meter.MustObservableUpDownCounter(
		"goframe.metric.demo.observable_updown_counter",
		gmetric.MetricOption{
			Help: "This is a simple demo for ObservableUpDownCounter usage",
			Unit: "%",
			Attributes: gmetric.Attributes{
				gmetric.NewAttribute("const_label_5", 5),
			},
		},
	)
	observableGauge = meter.MustObservableGauge(
		"goframe.metric.demo.observable_gauge",
		gmetric.MetricOption{
			Help: "This is a simple demo for ObservableGauge usage",
			Unit: "%",
			Attributes: gmetric.Attributes{
				gmetric.NewAttribute("const_label_6", 6),
			},
		},
	)
)

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

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

	// UpDownCounter.
	upDownCounter.Inc(ctx)
	upDownCounter.Add(ctx, 10)
	upDownCounter.Dec(ctx)

	// Record values for histogram.
	histogram.Record(1)
	histogram.Record(20)
	histogram.Record(30)
	histogram.Record(101)
	histogram.Record(2000)
	histogram.Record(9000)
	histogram.Record(20000)

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

License

GoFrame Polaris is licensed under the MIT License, 100% free and open-source, forever.

Documentation

Overview

Package otelmetric provides metric functionalities using OpenTelemetry metric.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustProvider

func MustProvider(option ...Option) gmetric.Provider

MustProvider creates and returns a metrics provider. It panics if any error occurs.

func NewProvider

func NewProvider(option ...Option) (gmetric.Provider, error)

NewProvider creates and returns a metrics provider.

func PrometheusHandler

func PrometheusHandler(r *ghttp.Request)

PrometheusHandler returns the http handler for prometheus metrics exporting.

func StartPrometheusMetricsServer

func StartPrometheusMetricsServer(port int, path string)

StartPrometheusMetricsServer starts running a http server for metrics exporting.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option applies a configuration option value to a MeterProvider.

func WithBuiltInMetrics

func WithBuiltInMetrics() Option

WithBuiltInMetrics enables builtin metrics.

func WithReader

func WithReader(reader metric.Reader) Option

WithReader associates Reader r with a MeterProvider.

By default, if this option is not used, the MeterProvider will perform no operations; no data will be exported without a Reader.

func WithResource

func WithResource(res *resource.Resource) Option

WithResource associates a Resource with a MeterProvider. This Resource represents the entity producing telemetry and is associated with all Meters the MeterProvider will create.

func WithView

func WithView(views ...metric.View) Option

WithView associates views a MeterProvider.

Views are appended to existing ones in a MeterProvider if this option is used multiple times.

By default, if this option is not used, the MeterProvider will use the default view.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL