stats

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

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

Go to latest
Published: Oct 12, 2020 License: MIT Imports: 6 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

stats

Track and public custom statistics and metrics inside of your go application

Supports tracking:

  • int64 counters
  • int64 counters (moving average)

Supports publishing to:

  • influxdb

Usage

import (
    "sync/atomic"
    "time"

    "github.com/purehyperbole/stats"
)

func main() {
    var counter, movingAverage int64

    // create a monitor that will aggregate and publish metrics every second
    monitor := stats.NewMonitor(time.Second)

    // add a new destination to publish metrics to
    monitor.AddPublisher(
        stats.NewInfluxDBPublisher("host", "token", "org", "bucket")
    )

    // track a basic counter
    monitor.Track(stats.Metric{
        Type: stats.Counter,
        Name: "simple-counter,environment=production value=",
        Value: &counter,
    })

    // track a moving average. Any polled value will be averaged over a minute
    // (interval of 1 second, with 60 samples stored)
    monitor.Track(stats.Metric{
        Type:    stats.MovingAverage,
        Name:    "moving-average,environment=production avg-reqs=",
        Samples: 60,
        Value:   &movingAverages,
    })

    go monitor.Start()

    // use the values in your app
    for i := 0; i < 1000; i++ {
        atomic.AddInt64(&counter, 1)
        atomic.AddInt64(&movingAverage, 1)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InfluxDBPublisher

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

InfluxDBPublisher ships metrics to influxdb

func NewInfluxDBPublisher

func NewInfluxDBPublisher(host, token, org, bucket string) *InfluxDBPublisher

NewInfluxDBPublisher creates a new influx db publisher

func (*InfluxDBPublisher) Publish

func (p *InfluxDBPublisher) Publish(name string, metric int64)

Publish publishes a given metric to influxdb

type Metric

type Metric struct {
	Type     MetricType
	Name     string
	Interval time.Duration
	Samples  int
	Value    *int64
	// contains filtered or unexported fields
}

Metric

type MetricType

type MetricType int

MetricType denotes the type of metric to track

const (
	Counter MetricType = iota
	MovingAverage
)

type Monitor

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

Monitor monitors given values at different timeframes

func NewMonitor

func NewMonitor(interval time.Duration) *Monitor

NewMonitor creates a new monitor

func (*Monitor) AddPubisher

func (m *Monitor) AddPubisher(publisher Publisher)

AddPublisher adds a target to publish metrics to

func (*Monitor) Start

func (m *Monitor) Start()

Start starts the monitor

func (*Monitor) Stop

func (m *Monitor) Stop()

Stop stops the monitor

func (*Monitor) Track

func (m *Monitor) Track(metric Metric)

Tracks tracks a metric

type Publisher

type Publisher interface {
	Publish(name string, metric int64)
}

Publisher

type RingBuffer

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

RingBuffer that

func NewRingBuffer

func NewRingBuffer(samples int) *RingBuffer

NewRingBuffer creates a new ring buffer of int64's

func (*RingBuffer) Dequeue

func (b *RingBuffer) Dequeue() int64

Dequeue dequeues a value off the buffer

func (*RingBuffer) Iterate

func (b *RingBuffer) Iterate(fn func(v int64))

Iterate iterates through the ring buffer

func (*RingBuffer) Queue

func (b *RingBuffer) Queue(v int64)

Queue queues a value on the buffer

Jump to

Keyboard shortcuts

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