statuspage

package module
v0.0.0-...-f8f6aba Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2022 License: GPL-3.0 Imports: 16 Imported by: 1

README

Build Status go report GoDoc

Description

The package allows to easily export application metrics (github.com/trafficstars/metrics) to prometheus.

Examples

Generic case

package main

import (
    "fmt"
    "math/rand"
    "net/http"

    "github.com/trafficstars/metrics"
    "github.com/trafficstars/statuspage"
)

func hello(w http.ResponseWriter, r *http.Request) {
    answerInt := rand.Intn(10)
        
    // just a metric
    metrics.Count(`hello`, metrics.Tags{`answer_int`: answerInt}).Increment()
        
    fmt.Fprintf(w, "Hello world! The answerInt == %v\n", answerInt)
}

func sendMetrics(w http.ResponseWriter, r *http.Request) {
    statuspage.WriteMetricsPrometheus(w)
}

func main() {
    http.HandleFunc("/", hello)
    http.HandleFunc("/metrics.prometheus", sendMetrics) // here we export metrics for prometheus
    http.ListenAndServe(":8000", nil)
}
$ curl http://localhost:8000/
Hello world! The answerInt == 1

$ curl http://localhost:8000/
Hello world! The answerInt == 7

$ curl http://localhost:8000/
Hello world! The answerInt == 7

$ curl -s http://localhost:8000/metrics.prometheus | grep hello
# TYPE metrics_hello counter
metrics_hello{answer_int="1"} 1
metrics_hello{answer_int="7"} 2

Framework "echo"

The same as above, but just use our handler:

// import "github.com/trafficstars/statuspage/handler/echostatuspage"

r := echo.New()
r.GET("/status.prometheus", echostatuspage.StatusPrometheus)

Documentation

Index

Constants

View Source
const (
	// PrometheusFormat is a constant that defines which prometheus format will be used
	// see also: https://github.com/prometheus/common/blob/6fb6fce6f8b75884b92e1889c150403fc0872c5e/expfmt/expfmt.go#L27
	PrometheusFormat = expfmt.FmtText
)

Variables

This section is empty.

Functions

func AddCustomMetricsHook

func AddCustomMetricsHook(hook func() map[string]interface{})

AddCustomMetricsHook adds a new hook that will be called everytime to collect additional metrics when function WriteMetricsPrometheus or WriteMetricsJSON is called.

The hook should return map of "string to interface{}" where the "string" is a metric key and "interface{}" is the value.

func SetLogger

func SetLogger(newLogger Logger)

SetLogger sets the logger to be used to report about errors

func WriteMetricsJSON

func WriteMetricsJSON(writer io.Writer, registry *metrics.Registry) error

WriteMetricsJSON write all the metrics via writer in JSON format.

func WriteMetricsPrometheus

func WriteMetricsPrometheus(writer io.Writer, registry *metrics.Registry) error

WriteMetricsPrometheus write all the metrics via writer in prometheus format.

Types

type Logger

type Logger interface {
	Error(error)
}

Logger is an interface of a logger that could be set via function `SetLogger` as the logger of this package. If there will be any errors then they will be written via the logger.

Directories

Path Synopsis
handler

Jump to

Keyboard shortcuts

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