metrics

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

README

go-metrics

Health and Metrics controller and web server library for Go

Usage with example

package example

import (
	"math/rand"

	"github.com/randlabs/go-metrics"
)

func main() {
	// Create a new health & metrics controller with a web server
	srvOpts := metrics.Options{
		Address:             "127.0.0.1",
		Port:                3000,
		HealthCallback:      healthCallback, // Setup our health check callback
		EnableDebugProfiles: true,
		IncludeCORS:         true,
		DisableClientCache:  true,
	}
	mc, err := metrics.CreateController(srvOpts)
	if err != nil {
		// handle error
	}

	// Create a custom prometheus counter
	err = mc.CreateCounterWithCallback(
		"random_counter", "A random counter",
		func() float64 {
			// Return the counter value.
			// The common scenario is to have a shared set of variables you regularly update with the current
			// state of your application.
			return rand.Float64()
		},
	)

	// Start health & metrics web server
	err = mc.Start()
	if err != nil {
		// handle error
	}

	// your app code may go here

	// Stop health & metrics web server before quitting
	mc.Destroy()
}

// Health output is in JSON format. Don't forget to add json tags.
type exampleHealthOutput struct {
	Status  string `json:"status"`
}

// Our health callback routine.
func healthCallback() interface{} {
	return exampleHealthOutput{
		Status: "ok",
	}
}

License

See LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Controller added in v1.1.0

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

Controller holds details about a metrics monitor instance.

func CreateController added in v1.1.0

func CreateController(opts Options) (*Controller, error)

CreateController initializes and creates a new controller

func (*Controller) CreateCounterVecWithCallback added in v1.1.0

func (mws *Controller) CreateCounterVecWithCallback(
	name string, help string, variableLabels []string, subItems VectorMetric,
) error

func (*Controller) CreateCounterWithCallback added in v1.1.0

func (mws *Controller) CreateCounterWithCallback(name string, help string, handler ValueHandler) error

func (*Controller) CreateGaugeVecWithCallback added in v1.1.0

func (mws *Controller) CreateGaugeVecWithCallback(
	name string, help string, variableLabels []string, subItems VectorMetric,
) error

func (*Controller) CreateGaugeWithCallback added in v1.1.0

func (mws *Controller) CreateGaugeWithCallback(name string, help string, handler ValueHandler) error

func (*Controller) Destroy added in v1.1.0

func (mws *Controller) Destroy()

Destroy destroys the monitor and stops the internal web server

func (*Controller) Registry added in v1.1.0

func (mws *Controller) Registry() *prometheus.Registry

Registry returns the prometheus registry object

func (*Controller) Start added in v1.1.0

func (mws *Controller) Start() error

Start starts the monitor's internal web server

type HealthCallback

type HealthCallback func() interface{}

HealthCallback indicates a function that returns an object that will be returned as a JSON output.

type Options

type Options struct {
	// If Server is provided, use this server instead of creating a new one.
	Server *webserver.Server

	// Address is the bind address to attach the internal web server.
	Address string

	// Port is the port number the internal web server will use.
	Port uint16

	// TLSConfig optionally provides a TLS configuration for use.
	TLSConfig *tls.Config

	// AccessToken is an optional access token required to access the status endpoints.
	AccessToken string

	// If RequestAccessTokenInHealth is enabled, access token checked also in '/health' endpoint.
	RequestAccessTokenInHealth bool

	// HealthCallback is a function that returns an object which, in turn, will be converted to JSON format.
	HealthCallback HealthCallback

	// Expose debugging profiles /debug/pprof endpoint.
	EnableDebugProfiles bool

	// Include Cache-Control headers in response to disable client-side caching.
	DisableClientCache bool

	// Include CORS headers in response.
	IncludeCORS bool

	// Middlewares additional set of middlewares for the endpoints.
	Middlewares []webserver.MiddlewareFunc
}

Options specifies metrics controller initialization options.

type ValueHandler

type ValueHandler func() float64

type VectorMetric

type VectorMetric []struct {
	Values  []string
	Handler ValueHandler
}

Jump to

Keyboard shortcuts

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