stats

package module
v0.0.0-...-3ea002e Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2018 License: BSD-2-Clause Imports: 9 Imported by: 0

README

Statistics library for Go

Documentation on https://godoc.org/github.com/Brickchain/go-stats.v1.

Based on go-metrics.

Simplifies setup of metrics sinks and collecting statistics.

Usage

package main

import (
    "os"
    "time"
    "path"

    stats "github.com/Brickchain/go-stats.v1"
)

func main() {
    // start an inmem metrics sink that will print metrics once per minute.
    // set the instance name to the name of our binary.
    stats.Setup("inmem", path.Base(os.Args[0]))

    someFunc()

    // Wait a bit more than a minute in order to see the metrics being printed
    time.Sleep(time.Second * 62)
}

func someFunc() {
    t := stats.StartTimer("someFunc.total")
    defer t.Stop()
}

Documentation

Overview

Package stats is a helper lib for collecting statistics.

Example:

package main

import (
	"os"
	"time"
	"path"

	stats "github.com/Brickchain/go-stats.v1"
)

func main() {
	// start an inmem metrics sink that will print metrics once per minute.
	// set the instance name to the name of our binary.
	stats.Setup("inmem", path.Base(os.Args[0]))

	someFunc()

	// Wait a bit more than a minute in order to see the metrics being printed
	time.Sleep(time.Second * 62)
}

func someFunc() {
	t := stats.StartTimer("someFunc.total")
	defer t.Stop()
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Gauge

func Gauge(path string, val float32)

Gauge sends a value for a metric.

Example
package main

import (
	stats "github.com/Brickchain/go-stats.v1"
)

func main() {
	response := []byte("Hello, World")
	stats.Gauge("api.get.bytes", float32(len(response)))
}
Output:

func Increment

func Increment(path string, val float32)

Increment a metric value.

Example
package main

import (
	stats "github.com/Brickchain/go-stats.v1"
)

func main() {
	stats.Increment("api.get.calls", 1)
}
Output:

func Setup

func Setup(sinkType, name string) error

Setup the metrics sink. Takes sink type (datadog, prometheus or inmem) and a name to prepend the metrics keys with.

Example (Datadog)
package main

import (
	stats "github.com/Brickchain/go-stats.v1"
)

func main() {
	// Requires that the DOGSTATSD environment variable is set and pointing to the statsd port of your dd-agent.
	// export DOGSTATSD="localhost:8125"
	stats.Setup("datadog", "example")
}
Output:

Example (Inmem)
package main

import (
	stats "github.com/Brickchain/go-stats.v1"
)

func main() {
	// The inmem sink prints the collected metrics to stdout once per minute.
	stats.Setup("inmem", "example")
}
Output:

Example (Prometheus)
package main

import (
	stats "github.com/Brickchain/go-stats.v1"
)

func main() {
	// The prometheus sink adds the /metrics handler to your HTTP stack.
	// You need to setup a HTTP listener on some port that you point Prometheus to.
	stats.Setup("prometheus", "example")
}
Output:

Types

type Timer

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

Timer is the struct that holds the information about an ongoing timer.

func StartTimer

func StartTimer(path string) *Timer

StartTimer starts a new timer and returns a Timer object instance.

Example
package main

import (
	stats "github.com/Brickchain/go-stats.v1"
)

func main() {
	t := stats.StartTimer("api.get.total")
	defer t.Stop()
}
Output:

func (*Timer) Stop

func (t *Timer) Stop()

Stop an ongoing timer.

Jump to

Keyboard shortcuts

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