metrics

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: Apache-2.0 Imports: 9 Imported by: 9

README

gin-go-metrics Go Report Card

gin-go-metrics is gin-gonic/gin middleware to gather and store metrics using rcrowley/go-metrics

How to use

gin middleware
package main

import (
	"fmt"
	"os"
	"time"

	metrics "github.com/bmc-toolbox/gin-go-metrics"
	"github.com/bmc-toolbox/gin-go-metrics/middleware"
	"github.com/gin-gonic/gin"
)

func main() {
	// Optional part to send metrics to Graphite,
	// as alternative you can send metrics from
	// rcrowley/go-metrics.DefaultRegistry yourself
	err := metrics.Setup(
		"graphite",  // clientType
		"localhost", // graphite host
		2003,        // graphite port
		"server",    // metrics prefix
		time.Minute, // graphite flushInterval
	)
	if err != nil {
		fmt.Printf("Failed to set up monitoring: %s\n", err)
		os.Exit(1)
	}

	r := gin.New()

	// argument to NewMetrics tells which variables need to be
	// expanded in metrics, more on that by link:
	// https://banzaicloud.com/blog/monitoring-gin-with-prometheus/
	p := middleware.NewMetrics([]string{"expanded_parameter"})
	r.Use(p.HandlerFunc(
		[]string{"/ping", "/api/ping"}, // ignore given URLs from stats
		true,                           // replace "/" with "_" in URLs to prevent splitting Graphite namespace
	))

	r.GET("/", func(c *gin.Context) {
		c.JSON(200, "Hello world!")
	})

	r.Run(":8000")
}
standalone metrics sending
package main

import (
	"fmt"
	"os"
	"time"

	metrics "github.com/bmc-toolbox/gin-go-metrics"
)

func main() {
	err := metrics.Setup(
		"graphite",  // clientType
		"localhost", // graphite host
		2003,        // graphite port
		"server",    // metrics prefix
		time.Minute, // graphite flushInterval
	)
	if err != nil {
		fmt.Printf("Failed to set up monitoring: %s\n", err)
		os.Exit(1)
	}
	// collect data using provided functions with provided arguments once a minute
	go metrics.Scheduler(time.Minute, metrics.GoRuntimeStats, []string{})
	go metrics.Scheduler(time.Minute, metrics.MeasureRuntime, []string{"uptime"}, time.Now())

	//<...>
	metrics.IncrCounter([]string{"happy_routine", "happy_runs_counter"}, 1)
	metrics.UpdateGauge([]string{"happy_routine", "happiness_level"}, 9000)
	metrics.UpdateHistogram([]string{"happy_routine", "happiness_hit"}, 35)
	metrics.UpdateTimer([]string{"happy_time"}, time.Minute)
}

Provided metrics

Request processing time and count of requests stored in go-metrics.Timer

Request and response size stored in go-metrics.Histogram

Data storage

Currently only helper function for sending data to Graphite with cyberdelia/go-metrics-graphite is present, however, you can send data using go-metrics.DefaultRegistry anywhere you want.

Acknowledgment

This library was originally developed for Booking.com. With approval from Booking.com, the code and specification was generalized and published as Open Source on GitHub, for which the authors would like to express their gratitude.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close(printStats bool)

Close runs cleanup actions

func GoRuntimeStats

func GoRuntimeStats(prefix []string)

GoRuntimeStats collects go runtime stats. prefix is a slice of metric namespace nodes.

func IncrCounter

func IncrCounter(key []string, value int64)

IncrCounter sets up metric attributes and passes them to the metricsChan. key = slice of strings that will be joined with "." to be used as the metric namespace val = float64 metric value

func MeasureRuntime

func MeasureRuntime(key []string, start time.Time)

MeasureRuntime measures time elapsed since invocation

func Scheduler

func Scheduler(interval time.Duration, fn interface{}, args ...interface{})

Scheduler starts passed function at start and then every "interval" value

func Setup

func Setup(clientType string, host string, port int, prefix string, flushInterval time.Duration) (err error)

Setup sets up external and internal metric sinks.

func UpdateGauge

func UpdateGauge(key []string, value int64)

UpdateGauge sets up the Gauge metric and passes them to the metricsChan. key = slice of strings that will be joined with "." to be used as the metric namespace val = float64 metric value

func UpdateHistogram

func UpdateHistogram(key []string, value int64)

UpdateHistogram sets up the Histogram metric and passes them to the metricsChan. key = slice of strings that will be joined with "." to be used as the metric namespace val = int64 metric value

func UpdateTimer

func UpdateTimer(key []string, value time.Duration)

UpdateTimer sets up the Timer metric and passes them to the metricsChan. key = slice of strings that will be joined with "." to be used as the metric namespace val = time.Time metric value

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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