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.
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".