github.com/go-srvc/mods/metermod
go get github.com/go-srvc/mods/metermod@v1.17.0
README
metermod
metermod exports metrics to the otel endpoint and flushes buffers before the application exits. By default it also collects Go runtime metrics.
package main
import (
"fmt"
"net/http"
"os"
"github.com/go-srvc/mods/httpmod"
"github.com/go-srvc/mods/metermod"
"github.com/go-srvc/mods/sigmod"
"github.com/go-srvc/srvc"
)
func main() {
srvc.RunAndExit(
metermod.New(),
sigmod.New(os.Interrupt),
httpmod.New(
httpmod.WithAddr(":8080"),
httpmod.WithHandler(http.HandlerFunc(hello)),
),
)
}
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "ok")
}
Overview
Package metermod provides OpenTelemetry meter provider as a module.
Constants
const (
ErrMissingProvider = errStr("meter provider not set")
// ErrFlushFailed is no longer returned by Stop, which relies on
// Shutdown to flush remaining metrics.
//
// Deprecated: kept for API compatibility and will be removed in a future
// major version.
ErrFlushFailed = errStr("failed to flush remaining metrics")
)const ID = "metermod"Types
type Opt
type Opt func(*Provider) errorfunc WithEnv
func WithEnv() OptWithEnv uses OTEL_EXPORTER_OTLP_METRICS_PROTOCOL and OTEL_EXPORTER_OTLP_PROTOCOL environment variable to set exporter. Accepted values are:
- http/protobuf
- grpc
- stdout
If no value is provided, stdout is used.
func WithGRPC
func WithGRPC() OptWithGRPC creates meter provider with periodic reader using grpc exporter from OTEL_* env configs. Env variables: https://pkg.go.dev/go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc
func WithHTTP
func WithHTTP() OptWithHTTP creates meter provider with periodic reader using http exporter from OTEL_* env configs. Env variables: https://pkg.go.dev/go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp
func WithProvider
func WithProvider(exp *metric.MeterProvider) OptWithProvider sets the underlying meter provider for module.
func WithProviderFn
func WithProviderFn(fn func() (*metric.MeterProvider, error)) OptWithProviderFn sets the underlying meter provider for module using given function.
func WithRuntimeMetrics
func WithRuntimeMetrics(opts ...runtime.Option) OptWithRuntimeMetrics starts runtime metrics collection.
func WithStdout
func WithStdout(opt ...stdoutmetric.Option) OptWithStdout creates meter provider with stdout exporter.
type Provider
type Provider struct {
// contains filtered or unexported fields
}func New
func New(opts ...Opt) *ProviderNew creates meter provider module with sane defaults. Default options are: WithEnv and WithRuntimeMetrics.
func (*Provider) ID
func (p *Provider) ID() stringfunc (*Provider) Init
func (p *Provider) Init() errorfunc (*Provider) Run
func (p *Provider) Run() errorfunc (*Provider) Stop
func (p *Provider) Stop() errorExamples
ExampleNew
package main
import (
"github.com/go-srvc/mods/metermod"
"github.com/go-srvc/srvc"
)
func main() {
srvc.RunAndExit(
metermod.New(),
)
}