touchkit

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 5 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

Jump to

Keyboard shortcuts

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