promexporter

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

README

Small framework to write Prometheus exporters

Usage

package main
import (
    "github.com/relex/gotils/promexporter"
)
func main() {
	return promexporter.Serve(updateMetrics, 1111, nil)
}

// This function will be called on each ":1111/metrics" request
func updateMetrics() {
	// Collect and report metrics code
}

If you want to have some kind of caching that updates periodically, you can use have it like this:

// ...
func main() {
	ticker := promexporter.CreateTimerFromCron("*/10 * * * *")
	return promexporter.Serve(updateMetrics, 1111, &ticker)
}
// ...

So, now your updateMetrics will be called on exporter start and then on every */10 minute

Documentation

Index

Constants

This section is empty.

Variables

Gatherer points to the current gatherer

Functions

func AddMetrics

func AddMetrics(url string, job string)

AddMetrics pushes all metrics in the default registry to the target URL without overriding previously pushed metrics for this job

The URL should contain no path for the official pushgateway

func GetHandler

func GetHandler(getMetricsFn func(), timer *Timer) http.Handler

GetHandler returns "/metrics" handler. Use this if you want to set up more handlers If the timer is nil, `getMetricsFn` will be called on each request

func GetLabelNames

func GetLabelNames(labelStruct interface{}) []string

GetLabelNames creates a sorted list of label names out of struct fields to use in Prometheus metric The label can be specified by a `label` tag, e.g.: ```go

type ProcessLabels struct {
	ProcessName   string `label:"process_name"`
	ProcessStatus string `label:"process_status"`
}

``` If no `label` tag is specified, a field name converted to snake_case will be used instead

func GetLabelValues

func GetLabelValues(labelStruct interface{}) []string

GetLabelValues creates a sorted list of label values out of struct field values to use in Prometheus metric See GetLabelNames function for the context If a field type is not string it will be converted to string automatically, see https://golang.org/pkg/reflect/#Value.String

func GetMetricText

func GetMetricText() string

GetMetricText returns collected metrics. Usefull for tests.

func PushMetrics

func PushMetrics(url string, job string)

PushMetrics pushes all metrics in the default registry to the target URL

The URL should contain no path for the official pushgateway

func Serve

func Serve(getMetricsFn func(), port uint16, timer *Timer) error

Serve starts an http server

Types

type Target

type Target[L comparable] struct {
	Target string // address, e.g. "host.org:12340"
	Labels L
}

Target is a prometheus scrape target.

Labels must be a comparable struct, see `GetLabelNames` function description.

type TargetGroup

type TargetGroup[L comparable] struct {
	Targets []string `json:"targets"`
	Labels  L        `json:"labels"`
}

TargetGroup is a group of scrape targets sharing the same labels.

Each group normally represent the deployment one cluster on one server.

func GroupTargets

func GroupTargets[L comparable](targetList []Target[L]) []TargetGroup[L]

GroupTargets groups targets by distinct label values

The resulting target groups and the target addresses inside each group are ordered by their string representations to ensure stable ordering.

type Timer

type Timer = chan bool

Timer defines how often metrics will be updated. Should return true if there are more ticks left or false if we must shutdown

func CreateTimerFromCron

func CreateTimerFromCron(cron string) Timer

CreateTimerFromCron creates a timer from a cron exptession, e.g "* * * * *"

func CreateTimerFromTicker

func CreateTimerFromTicker(ticker *time.Ticker) Timer

CreateTimerFromTicker creates a timer from Ticker

Directories

Path Synopsis
Package promext contains custom metric types and pure utility functions
Package promext contains custom metric types and pure utility functions
Package promreg provides a front to Prometheus metric registry to help with creation of metrics in components as well as querying.
Package promreg provides a front to Prometheus metric registry to help with creation of metrics in components as well as querying.

Jump to

Keyboard shortcuts

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