Documentation
¶
Overview ¶
Package touchkit adds integration with go-kit's metrics API with prometheus as the backend. This package's primary use case is to allow code written against go-kit's metrics package to participate in dependency injection via touchstone.
Example ¶
Example shows how code written against go-kit's metrics interfaces can receive prometheus-backed metrics through dependency injection. Each component is named after its metric, so a struct with fx.In can select the metric it wants by name.
package main
import (
"fmt"
"strings"
"github.com/go-kit/kit/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/xmidt-org/touchstone"
"github.com/xmidt-org/touchstone/touchkit"
"go.uber.org/fx"
)
func main() {
type In struct {
fx.In
Requests metrics.Counter `name:"requests"`
Latency metrics.Histogram `name:"latency"`
}
var g prometheus.Gatherer
app := fx.New(
fx.NopLogger,
touchstone.Provide(),
touchkit.Counter(
prometheus.CounterOpts{Name: "requests", Help: "the total number of requests"},
"code",
),
touchkit.Histogram(
prometheus.HistogramOpts{Name: "latency", Help: "latency in milliseconds", Buckets: []float64{10, 100}},
),
fx.Populate(&g),
fx.Invoke(func(in In) {
in.Requests.With("code", "200").Add(3)
in.Latency.Observe(42)
}),
)
if err := app.Err(); err != nil {
fmt.Println(err)
return
}
expected := `
# HELP requests the total number of requests
# TYPE requests counter
requests{code="200"} 3
`
fmt.Println(testutil.GatherAndCompare(g, strings.NewReader(expected), "requests"))
count, err := testutil.GatherAndCount(g, "latency")
fmt.Println(count, err)
}
Output: <nil> 1 <nil>
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Counter ¶
func Counter(o prometheus.CounterOpts, labelNames ...string) fx.Option
Counter uses an injected touchstone Factory to create a go-kit metrics.Counter backed by a prometheus CounterVec. The *touchstone.Factory from the enclosing fx.App is used to create and register the prometheus metric. The name of the returned component will be the same as the metric name.
func Gauge ¶
func Gauge(o prometheus.GaugeOpts, labelNames ...string) fx.Option
Gauge uses an injected touchstone Factory to create a go-kit metrics.Gauge backed by a prometheus GaugeVec. The *touchstone.Factory from the enclosing fx.App is used to create and register the prometheus metric. The name of the returned component will be the same as the metric name.
func Histogram ¶
func Histogram(o prometheus.HistogramOpts, labelNames ...string) fx.Option
Histogram uses an injected touchstone Factory to create a go-kit metrics.Histogram backed by a prometheus HistogramVec. The *touchstone.Factory from the enclosing fx.App is used to create and register the prometheus metric. The name of the returned component will be the same as the metric name.
func Summary ¶
func Summary(o prometheus.SummaryOpts, labelNames ...string) fx.Option
Summary uses an injected touchstone Factory to create a go-kit metrics.Histogram backed by a prometheus SummaryVec. The *touchstone.Factory from the enclosing fx.App is used to create and register the prometheus metric. The name of the returned component will be the same as the metric name.
Types ¶
This section is empty.