metrics

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

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

Go to latest
Published: Mar 22, 2018 License: Apache-2.0 Imports: 11 Imported by: 1

README

Golang application metrics

NATS application metrics package

Build Status codecov GoDoc Go Report Card

Installation

go get github.com/cryptopay-dev/go-metrics

Default metrics tags

hostname - application host
app - application name

Metrics

    "alloc":         8810230, // memory allocated in bytes
    "alloc_objects": 123, // total heap objects allocated
    "gorotines":     10, // number of goroutines
    "gc":            1495532586, // timestamp of last GC
    "next_gc":       9000000, // heap size when GC will be run next time
    "pause_ns":      100 // pause time of GC

Usage

Basic

package main

import (
    "log"

    "github.com/cryptopay.dev/go-metrics"
)

func main() {
    err := metrics.Setup("nats://localhost:4222", "application_name", "hostname")
    if err != nil {
        log.Fatal(err)
    }

    for i:=0; i<10; i++ {
        // You metrics will be reported at application_name:metric
        // You metrics will be send to: mymetric,user=test@example.com counter=1,gauge=true,string=name
        // E.t.c.
        err = metrics.SendAndWait("metric", metrics.M{
            "counter": i,
            "gauge": true,
            "string": "name",
        }, metrics.T{
            "user": "test@example.com"
        }, "mymetric")

        if err != nil {
            log.Fatal(err)
        }
    }
}

More stuff

You can find more examples in /examples folder

Documentation

Index

Constants

View Source
const DefaultQueue = "telegraf"

DefaultQueue is queue where we puts event into NATS

Variables

View Source
var (
	// ErrAppNameNotSet not set
	ErrAppNameNotSet = errors.New("application name not set")
	// ErrHostNameNotSet not set
	ErrHostNameNotSet = errors.New("hostname not set")
)
View Source
var DefaultErrorHandler = func(err error) {
	fmt.Fprintf(os.Stderr, "Error while sending metrics: %v", err)
}

DefaultErrorHandler just printing all errors comes from async writes to Stderr

Functions

func Disable

func Disable()

Disable disables watcher and disconnects

func Send

func Send(metrics M, path ...string)

Send metrics to NATS queue

Example:

m.Send(metrics.M{
		"counter": i,
})

func SendAndWait

func SendAndWait(metrics M, path ...string) error

SendAndWait metrics to NATS queue waiting for response

Example:

err = SendAndWait(metrics.M{
		"counter": i,
}, "metricname")

func SendWithTags

func SendWithTags(metrics M, tags T, path ...string)

SendWithTags metrics to NATS queue waiting for response

Example:

err = SendWithTags(metrics.M{
		"counter": i,
}, metrics.T{
	    "tag": "sometag",
}, "metricname")

func SendWithTagsAndWait

func SendWithTagsAndWait(metrics M, tags T, path ...string) error

SendWithTagsAndWait metrics to NATS queue waiting for response

Example:

err = SendWithTags(metrics.M{
		"counter": i,
}, metrics.T{
	    "tag": "sometag",
}, "metricname")

func SetErrorHandler

func SetErrorHandler(fn ErrorHandler)

SetErrorHandler changes error handler to provided one

func Setup

func Setup(url string, application, hostname string, options ...nats.Option) error

Setup rewrites default metrics configuration

Params: - url (in e.g. "nats://localhost:4222") - options nats.Option array

Example: import (

"log"

"github.com/cryptopay.dev/go-metrics"

)

func main() {
    err := metrics.Setup("nats://localhost:4222")
    if err != nil {
        log.Fatal(err)
    }

    for i:=0; i<10; i++ {
        err = metrics.SendAndWait(metrics.M{
            "counter": i,
        })

        if err != nil {
            log.Fatal(err)
        }
    }
}

func Watch

func Watch(interval time.Duration) error

Watch watches memory, goroutine counter

Types

type Conn

type Conn interface {
	SetErrorHandler(fn ErrorHandler)
	Send(metrics M, path ...string)
	SendWithTags(metrics M, tags T, path ...string)
	SendWithTagsAndWait(metrics M, tags T, path ...string) error
	SendAndWait(metrics M, path ...string) error
	Disable()
	Watch(interval time.Duration) error
}

Conn - connection interface

var DefaultConn Conn

DefaultConn shared default metric connection

func New

func New(url string, application, hostname string, options ...nats.Option) (Conn, error)

New creates new metrics connection

Params: - url (in e.g. "nats://localhost:4222") - options nats.Option array

Example: import (

"log"

"github.com/cryptopay.dev/go-metrics"

)

func main() {
    m, err := metrics.New("nats://localhost:4222")
    if err != nil {
        log.Fatal(err)
    }

    for i:=0; i<10; i++ {
        err = m.SendAndWait(metrics.M{
            "counter": i,
        })

        if err != nil {
            log.Fatal(err)
        }
    }
}

type ErrorHandler

type ErrorHandler func(err error)

ErrorHandler type

type M

type M map[string]interface{}

M metrics storage Example:

m := metrics.M{
	"metric": 1000,
	"gauge": 1,
}

type T

type T map[string]string

T tags storage Example:

m := metrics.T{
	"tag": "some_default_tag"
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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