Documentation
¶
Overview ¶
Package prometheusadapter counts authentication outcomes as Prometheus metrics, on a registry the caller passes in. It is the only package of the library that imports client_golang: the providers report outcomes through an auth.Observer, and OutcomeCounter.Observe is one.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Options ¶
type Options struct {
Registerer prometheus.Registerer // required; the caller's registry, never the default one
Namespace string // required, e.g. "forwardauth"; the metric is <Namespace>_auth_outcomes_total
}
Options configure an OutcomeCounter.
type OutcomeCounter ¶
type OutcomeCounter struct {
// contains filtered or unexported fields
}
OutcomeCounter counts authentication outcomes in <namespace>_auth_outcomes_total{provider,action,result}. The label values are closed sets: provider instance names, auth.Action and auth.Result. It is safe for concurrent use.
func New ¶
func New(options *Options) (*OutcomeCounter, error)
New registers <namespace>_auth_outcomes_total on options.Registerer and returns the counter. A failed registration (e.g. a duplicate) is returned as an error, never panicked.
Example ¶
The counter's Observe method is an auth.Observer: pass it (or a closure calling it next to ginadapter.Observe) as the Observer option of a provider, a session.Guard or an apikey.Authenticator.
package main
import (
"context"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/govengers/auth"
"github.com/govengers/auth/prometheusadapter"
)
func main() {
registry := prometheus.NewRegistry() // built in main and served on the metrics port
counter, err := prometheusadapter.New(&prometheusadapter.Options{Registerer: registry, Namespace: "billing"})
if err != nil {
fmt.Println(err)
return
}
var observer auth.Observer = counter.Observe
observer(context.Background(), auth.Outcome{Provider: "session", Action: auth.ActionAuthenticate, Result: auth.ResultSuccess})
count, err := testutil.GatherAndCount(registry, "billing_auth_outcomes_total")
fmt.Println(count, err)
}
Output: 1 <nil>
func (*OutcomeCounter) Observe ¶
func (c *OutcomeCounter) Observe(_ context.Context, outcome auth.Outcome)
Observe has the auth.Observer shape: it counts outcome under its provider, action and result. An outcome with a label value Prometheus refuses (invalid UTF-8) is not counted: an Observer runs inside the request and must not panic.