vimebu

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2024 License: MIT Imports: 8 Imported by: ? modules, 0 packages

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.

README

vimebu

CI Go Reference Go Report Card MIT License

vimebu is a small library that provides a builder to create VictoriaMetrics compatible metrics.

Installation

go get -u github.com/wazazaby/vimebu

Usage

import (
    "github.com/VictoriaMetrics/metrics"
    "github.com/wazazaby/vimebu"
)

// Only using the builder.
var requestsTotalCounter = metrics.NewCounter(
    vimebu.
        Metric("request_total").
        Label("path", "/foo/bar").
        String(), // request_total{path="/foo/bar"}
)

var responseSizeHistogram = metrics.NewHistogram(
    vimebu.Metric("response_size").String(), // response_size
)

// Registering the metric using the provided helpers.
var updateTotalCounterV3 = vimebu.
    Metric("update_total").
    LabelInt("version", 3).
    NewCounter() // update_total{version="3"}
Create metrics with variable label values

It's even more useful when you want to build metrics with variable label values.

import (
    "net"

    "github.com/VictoriaMetrics/metrics"
    "github.com/wazazaby/vimebu"
)

func getCassandraQueryCounter(name string, host net.IP) *metrics.Counter {
    var b vimebu.Builder
    b.Metric("cassandra_query_total")
    b.Label("name", name)
    b.LabelStringer("host", host)
    metric := b.String() // cassandra_query_total{name="beep",host="1.2.3.4"}
    return metrics.GetOrCreateCounter(metric)
}
Create metrics with conditional labels
import (
    "github.com/VictoriaMetrics/metrics"
    "github.com/wazazaby/vimebu"
)

func getHTTPRequestCounter(host string) *metrics.Counter {
    var b vimebu.Builder
    b.Metric("api_http_requests_total")
    if host != "" {
        b.Label("host", host)
    }
    metric := b.String() // api_http_requests_total or api_http_requests_total{host="api.app.com"}
    return metrics.GetOrCreateCounter(metric)
}
Create metrics with label values that need to be escaped

vimebu also exposes a way to escape quotes on label values you don't control using Builder.LabelQuote.

import (
    "github.com/VictoriaMetrics/metrics"
    "github.com/wazazaby/vimebu"
)

func getHTTPRequestCounter(path string) *metrics.Counter {
    var b vimebu.Builder
    b.Metric("api_http_requests_total")
    b.LabelQuote("path", path)
    counter := b.GetOrCreateCounter() // api_http_requests_total{path="some/bro\"ken/path"}
    return counter
}
Create metrics with label values that aren't string

You can use these methods to append specific value types to the builder :

  • Builder.LabelBool for booleans
  • Builder.LabelInt and variations for signed integers
  • Builder.LabelUint and variations for unsigned integers
  • Builder.LabelFloat and variations for floats
  • Builder.LabelStringer for values implementing the fmt.Stringer interface

Gotchas

When a metric is invalid (empty name, empty label name or value etc), vimebu will skip and log to os.Stderr.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MetricNameMaxLen = 256  // MetricNameMaxLen is the maximum len in bytes allowed for the metric name.
	LabelNameMaxLen  = 128  // LabelNameMaxLen is the maximum len in bytes allowed for a label name.
	LabelValueLen    = 1024 // LabelValueLen is the maximum len in bytes allowed for a label value.
)

Functions

This section is empty.

Types

type Builder

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

Builder is used to efficiently build a VictoriaMetrics metric. It's backed by a bytes.Buffer to minimize memory copying.

The zero value is ready to use.

func Metric

func Metric(name string) *Builder

Metric creates a new Builder. It can be useful if you want to create a metric in a single line.

func (*Builder) GetOrCreateCounter added in v1.0.0

func (b *Builder) GetOrCreateCounter() *metrics.Counter

GetOrCreateCounter calls metrics.GetOrCreateCounter using the Builder's accumulated string as argument.

func (*Builder) GetOrCreateCounterInSet added in v1.1.0

func (b *Builder) GetOrCreateCounterInSet(set *metrics.Set) *metrics.Counter

GetOrCreateCounterInSet calls metrics.Set.GetOrCreateCounter using the Builder's accumulated string as argument.

func (*Builder) GetOrCreateFloatCounter added in v1.0.0

func (b *Builder) GetOrCreateFloatCounter() *metrics.FloatCounter

GetOrCreateFloatCounter calls metrics.GetOrCreateFloatCounter using the Builder's accumulated string as argument.

func (*Builder) GetOrCreateFloatCounterInSet added in v1.1.0

func (b *Builder) GetOrCreateFloatCounterInSet(set *metrics.Set) *metrics.FloatCounter

GetOrCreateFloatCounterInSet calls metrics.Set.GetOrCreateFloatCounter using the Builder's accumulated string as argument.

func (*Builder) GetOrCreateGauge added in v1.0.0

func (b *Builder) GetOrCreateGauge(f func() float64) *metrics.Gauge

GetOrCreateGauge calls metrics.GetOrCreateGauge using the Builder's accumulated string as argument.

func (*Builder) GetOrCreateGaugeInSet added in v1.1.0

func (b *Builder) GetOrCreateGaugeInSet(set *metrics.Set, f func() float64) *metrics.Gauge

GetOrCreateGaugeInSet calls metrics.Set.GetOrCreateGauge using the Builder's accumulated string as argument.

func (*Builder) GetOrCreateHistogram added in v1.0.0

func (b *Builder) GetOrCreateHistogram() *metrics.Histogram

GetOrCreateHistogram calls metrics.GetOrCreateHistogram using the Builder's accumulated string as argument.

func (*Builder) GetOrCreateHistogramInSet added in v1.1.0

func (b *Builder) GetOrCreateHistogramInSet(set *metrics.Set) *metrics.Histogram

GetOrCreateHistogramInSet calls metrics.Set.GetOrCreateHistogram using the Builder's accumulated string as argument.

func (*Builder) GetOrCreateSummary added in v1.0.0

func (b *Builder) GetOrCreateSummary() *metrics.Summary

GetOrCreateSummary calls metrics.GetOrCreateSummary using the Builder's accumulated string as argument.

func (*Builder) GetOrCreateSummaryExt added in v1.0.0

func (b *Builder) GetOrCreateSummaryExt(window time.Duration, quantiles []float64) *metrics.Summary

GetOrCreateSummaryExt calls metrics.GetOrCreateSummaryExt using the Builder's accumulated string as argument.

func (*Builder) GetOrCreateSummaryExtInSet added in v1.1.0

func (b *Builder) GetOrCreateSummaryExtInSet(set *metrics.Set, window time.Duration, quantiles []float64) *metrics.Summary

GetOrCreateSummaryExtInSet calls metrics.Set.GetOrCreateSummaryExt using the Builder's accumulated string as argument.

func (*Builder) GetOrCreateSummaryInSet added in v1.1.0

func (b *Builder) GetOrCreateSummaryInSet(set *metrics.Set) *metrics.Summary

GetOrCreateSummaryInSet calls metrics.Set.GetOrCreateSummary using the Builder's accumulated string as argument.

func (*Builder) Label

func (b *Builder) Label(name, value string) *Builder

Label appends a pair of label name and label value to the Builder. Unlike vimebu.Builder.LabelQuote, quotes inside the label value will not be escaped. It's better suited for a label value where you control the input (either it is already sanitized, or it comes from a const or an enum for example).

NoOp if :

func (*Builder) LabelBool added in v1.0.0

func (b *Builder) LabelBool(name string, value bool) *Builder

LabelBool appends a pair of label name and boolean label value to the Builder.

NoOp if :

func (*Builder) LabelErr added in v1.4.0

func (b *Builder) LabelErr(name string, err error) *Builder

LabelErr appends a pair of label name and error label value to the Builder. Unlike vimebu.Builder.LabelErrQuote, quotes inside the error label value will not be escaped. It's better suited for a label value where you control the input (either it is already sanitized, or it comes from a const or an enum for example).

NoOp if :

func (*Builder) LabelErrQuote added in v1.4.0

func (b *Builder) LabelErrQuote(name string, err error) *Builder

LabelErrQuote appends a pair of label name and error label value to the Builder. Quotes inside the error label value will be escaped.

NoOp if :

func (*Builder) LabelFloat32 added in v1.3.1

func (b *Builder) LabelFloat32(name string, value float32) *Builder

LabelFloat32 appends a pair of label name and float32 label value to the Builder.

NoOp if :

func (*Builder) LabelFloat64 added in v1.3.1

func (b *Builder) LabelFloat64(name string, value float64) *Builder

LabelFloat64 appends a pair of label name and float64 label value to the Builder.

NoOp if :

func (*Builder) LabelInt added in v1.0.0

func (b *Builder) LabelInt(name string, value int) *Builder

LabelInt appends a pair of label name and int label value to the Builder.

NoOp if :

func (*Builder) LabelInt8 added in v1.3.1

func (b *Builder) LabelInt8(name string, value int8) *Builder

LabelInt8 appends a pair of label name and int8 label value to the Builder.

NoOp if :

func (*Builder) LabelInt16 added in v1.3.1

func (b *Builder) LabelInt16(name string, value int16) *Builder

LabelInt16 appends a pair of label name and int16 label value to the Builder.

NoOp if :

func (*Builder) LabelInt32 added in v1.3.1

func (b *Builder) LabelInt32(name string, value int32) *Builder

LabelInt32 appends a pair of label name and int32 label value to the Builder.

NoOp if :

func (*Builder) LabelInt64 added in v1.3.1

func (b *Builder) LabelInt64(name string, value int64) *Builder

LabelInt64 appends a pair of label name and int64 label value to the Builder.

NoOp if :

func (*Builder) LabelQuote added in v0.5.0

func (b *Builder) LabelQuote(name, value string) *Builder

LabelQuote appends a pair of label name and label value to the Builder. Quotes inside the label value will be escaped.

NoOp if :

func (*Builder) LabelStringer added in v1.0.1

func (b *Builder) LabelStringer(name string, value fmt.Stringer) *Builder

LabelStringer appends a pair of label name and label value (implementing fmt.Stringer) to the Builder. Unlike vimebu.Builder.LabelStringerQuote, quotes inside the label value will not be escaped. It's better suited for a label value where you control the input (either it is already sanitized, or it comes from a const or an enum for example).

NoOp if :

func (*Builder) LabelStringerQuote added in v1.0.1

func (b *Builder) LabelStringerQuote(name string, value fmt.Stringer) *Builder

LabelStringerQuote appends a pair of label name and label value (implementing fmt.Stringer) to the Builder. Quotes inside the label value will be escaped.

NoOp if :

func (*Builder) LabelUint added in v1.3.1

func (b *Builder) LabelUint(name string, value uint) *Builder

LabelUint appends a pair of label name and uint label value to the Builder.

NoOp if :

func (*Builder) LabelUint8 added in v1.3.1

func (b *Builder) LabelUint8(name string, value uint8) *Builder

LabelUint8 appends a pair of label name and uint8 label value to the Builder.

NoOp if :

func (*Builder) LabelUint16 added in v1.3.1

func (b *Builder) LabelUint16(name string, value uint16) *Builder

LabelUint16 appends a pair of label name and uint16 label value to the Builder.

NoOp if :

func (*Builder) LabelUint32 added in v1.3.1

func (b *Builder) LabelUint32(name string, value uint32) *Builder

LabelUint32 appends a pair of label name and uint32 label value to the Builder.

NoOp if :

func (*Builder) LabelUint64 added in v1.3.1

func (b *Builder) LabelUint64(name string, value uint64) *Builder

LabelUint64 appends a pair of label name and uint64 label value to the Builder.

NoOp if :

func (*Builder) Metric

func (b *Builder) Metric(name string) *Builder

Metric sets the metric name of the Builder.

NoOp if :

  • called more than once for the same builder instance.
  • the name is empty or contains more than vimebu.MetricNameMaxLen bytes.
  • the name contains a double quote.

func (*Builder) NewCounter added in v1.0.0

func (b *Builder) NewCounter() *metrics.Counter

NewCounter calls metrics.NewCounter using the Builder's accumulated string as argument.

func (*Builder) NewCounterInSet added in v1.1.0

func (b *Builder) NewCounterInSet(set *metrics.Set) *metrics.Counter

NewCounterInSet calls metrics.Set.NewCounter using the Builder's accumulated string as argument.

func (*Builder) NewFloatCounter added in v1.0.0

func (b *Builder) NewFloatCounter() *metrics.FloatCounter

NewFloatCounter calls metrics.NewFloatCounter using the Builder's accumulated string as argument.

func (*Builder) NewFloatCounterInSet added in v1.1.0

func (b *Builder) NewFloatCounterInSet(set *metrics.Set) *metrics.FloatCounter

NewFloatCounterInSet calls metrics.Set.NewFloatCounter using the Builder's accumulated string as argument.

func (*Builder) NewGauge added in v1.0.0

func (b *Builder) NewGauge(f func() float64) *metrics.Gauge

NewGauge calls metrics.NewGauge using the Builder's accumulated string as argument.

func (*Builder) NewGaugeInSet added in v1.1.0

func (b *Builder) NewGaugeInSet(set *metrics.Set, f func() float64) *metrics.Gauge

NewGaugeInSet calls metrics.Set.NewGauge using the Builder's accumulated string as argument.

func (*Builder) NewHistogram added in v1.0.0

func (b *Builder) NewHistogram() *metrics.Histogram

NewHistogram calls metrics.NewHistogram using the Builder's accumulated string as argument.

func (*Builder) NewHistogramInSet added in v1.1.0

func (b *Builder) NewHistogramInSet(set *metrics.Set) *metrics.Histogram

NewHistogramInSet calls metrics.Set.NewHistogram using the Builder's accumulated string as argument.

func (*Builder) NewSummary added in v1.0.0

func (b *Builder) NewSummary() *metrics.Summary

NewSummary calls metrics.NewSummary using the Builder's accumulated string as argument.

func (*Builder) NewSummaryExt added in v1.0.0

func (b *Builder) NewSummaryExt(window time.Duration, quantiles []float64) *metrics.Summary

NewSummaryExt calls metrics.NewSummaryExt using the Builder's accumulated string as argument.

func (*Builder) NewSummaryExtInSet added in v1.1.0

func (b *Builder) NewSummaryExtInSet(set *metrics.Set, window time.Duration, quantiles []float64) *metrics.Summary

NewSummaryExtInSet calls metrics.Set.NewSummaryExtInSet using the Builder's accumulated string as argument.

func (*Builder) NewSummaryInSet added in v1.1.0

func (b *Builder) NewSummaryInSet(set *metrics.Set) *metrics.Summary

NewSummaryInSet calls metrics.Set.NewSummary using the Builder's accumulated string as argument.

func (*Builder) String

func (b *Builder) String() string

String builds the metric by returning the accumulated string.

Jump to

Keyboard shortcuts

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