Help us shape the future of Go! Take the 2026 Go Developer Survey

prometheusadapter

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package prometheusadapter exports the live engine's outcomes as Prometheus collectors (D3): New registers them on a Registerer the application passes in, never on the default registry, and Metrics.Observe is the livewire.Observer that feeds them (D49). It is the only package of the module that imports client_golang.

Example
package main

import (
	"context"
	"fmt"
	"net/http"

	"github.com/govengers/auth"
	"github.com/govengers/auth/authorization"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/testutil"

	"github.com/govengers/livewire"
	"github.com/govengers/livewire/prometheusadapter"
)

type greeting struct {
	ID string `json:"id"`
}

// noTransactor begins transactions that do nothing; a real application passes
// postgresadapter's Transactor.
type noTransactor struct{}

func (noTransactor) Begin(context.Context, livewire.TransactionDetails) (livewire.Transaction, error) {
	return noTransaction{}, nil
}

type noTransaction struct{}

func (noTransaction) Commit(context.Context) error   { return nil }
func (noTransaction) Rollback(context.Context) error { return nil }
func (noTransaction) Handle() any                    { return nil }

// anyone is the IdentitySource of the example; a real application passes session.Guard.
type anyone struct{}

func (anyone) Identify(context.Context, *http.Request) (*auth.Identity, error) {
	return &auth.Identity{Provider: "example", Subject: "ada", Active: true}, nil
}

func main() {
	metricsRegistry := prometheus.NewRegistry() // the application's own, built in main (MET-002)
	metrics, err := prometheusadapter.New(&prometheusadapter.Options{Registerer: metricsRegistry, Namespace: "shop"})
	if err != nil {
		fmt.Println(err)
		return
	}
	registry, err := livewire.NewRegistry(&livewire.RegistryOptions{Observer: metrics.Observe}) // the one Observer (D49)
	if err != nil {
		fmt.Println(err)
		return
	}
	err = registry.Add(livewire.NewList("greetings", "/greetings", &livewire.ListOptions[greeting, livewire.NoParameters]{
		Read: authorization.IsUser(),
		Load: func(context.Context, *livewire.NoParameters) ([]greeting, error) { return []greeting{{ID: "1"}}, nil },
	}))
	if err != nil {
		fmt.Println(err)
		return
	}
	engine, err := livewire.NewEngine(&livewire.EngineOptions{Registry: registry, IdentitySource: anyone{}, Transactor: noTransactor{}, SameHostOnly: true})
	if err != nil {
		fmt.Println(err)
		return
	}
	ctx := auth.WithIdentity(context.Background(), &auth.Identity{Provider: "example", Subject: "ada", Active: true})
	_, _ = engine.Read(ctx, livewire.ReadRequest{Target: "greetings"})
	_, _ = engine.Read(context.Background(), livewire.ReadRequest{Target: "greetings"})

	fmt.Println(testutil.CollectAndCount(metricsRegistry, "shop_livewire_units_total"))
}
Output:
2

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Metrics

type Metrics struct {
	// contains filtered or unexported fields
}

Metrics turns the engine's outcomes into Prometheus collectors (design section 2): the units of work by kind, socket operation, target and status class with their duration, the subscription re-runs, the open sockets and subscriptions, the socket closes by code and the queue drops among them, the wait for the engine's query slots, and the failures of the background work. Every label value is from a closed set: the outcome kinds and operations, the registry's names (the engine reports nothing else, D59), status classes and the known close codes. It is safe for concurrent use.

func New

func New(options *Options) (*Metrics, error)

New builds the collectors and registers them on options.Registerer; a registration error (a collector registered twice) is returned, with the collectors registered before it unregistered again, so a corrected retry starts from a clean registry.

func (*Metrics) Observe

func (m *Metrics) Observe(_ context.Context, outcome livewire.Outcome)

Observe is a livewire.Observer: pass it as RegistryOptions.Observer, or call it from the application's own Observer next to its other reporting (D49).

type Options

type Options struct {
	Registerer prometheus.Registerer
	Namespace  string
}

Options configures New. Registerer is required: the collectors are registered on it, the application's own registry, never the default one (LIB-011, MET-002). Namespace is the first part of every metric name, the application's name (MET-005); the subsystem is "livewire".

Jump to

Keyboard shortcuts

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