Categorygithub.com/rs/xstats
modulepackage
0.0.0-20170813190920-c67367528e16
Repository: https://github.com/rs/xstats.git
Documentation: pkg.go.dev

# README

XStats

godoc license Build Status Coverage

Package xstats is a generic client for service instrumentation.

xstats is inspired from Go-kit's metrics package but it takes a slightly different path. Instead of having to create an instance for each metric, xstats use a single instance to log every metrics you want. This reduces the boiler plate when you have a lot a metrics in your app. It's also easier in term of dependency injection.

Talking about dependency injection, xstats comes with a xhandler.Handler integration so it can automatically inject the xstats client within the net/context of each request. Each request's xstats instance have its own tags storage ; This let you inject some per request contextual tags to be included with all observations sent within the lifespan of the request.

xstats is pluggable and comes with integration for expvar, StatsD and DogStatsD, the Datadog augmented version of StatsD with support for tags. More integration may come later (PR welcome).

Supported Clients

Install

go get github.com/rs/xstats

Usage

// Defines interval between flushes to statsd server
flushInterval := 5 * time.Second

// Connection to the statsd server
statsdWriter, err := net.Dial("udp", "127.0.0.1:8126")
if err != nil {
    log.Fatal(err)
}

// Create the stats client
s := xstats.New(dogstatsd.New(statsdWriter, flushInterval))

// Global tags sent with all metrics (only with supported clients like datadog's)
s.AddTags("role:my-service", "dc:sv6")

// Send some observations
s.Count("requests", 1, "tag")
s.Timing("something", 5*time.Millisecond, "tag")

Integration with github.com/rs/xhandler:

var xh xhandler.HandlerC

// Here is your handler
xh = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    // Get the xstats request's instance from the context. You can safely assume it will
    // be always there, if the handler is removed, xstats.FromContext will return a nop
    // instance.
    m := xstats.FromRequest(r)

    // Count something
    m.Count("requests", 1, "route:index")
})

// Install the metric handler with dogstatsd backend client and some env tags
flushInterval := 5 * time.Second
tags := []string{"role:my-service"}
statsdWriter, err := net.Dial("udp", "127.0.0.1:8126")
if err != nil {
    log.Fatal(err)
}
xh = xstats.NewHandler(dogstatsd.New(statsdWriter, flushInterval), tags, xh)

// Root context
ctx := context.Background()
h := xhandler.New(ctx, xh)
http.Handle("/", h)

if err := http.ListenAndServe(":8080", nil); err != nil {
    log.Fatal(err)
}

Testing

func TestFunc(t *testing.T) {
    m := mock.New()
    s := xstats.New(m)
    m.On("Timing", "something", 5*time.Millisecond, "tag")
    s.Timing("something", 5*time.Millisecond, "tag")
    s.AssertExpectations(t)
}

Licenses

All source code is licensed under the MIT License.

# Packages

Package dogstatsd implement Datadog extended StatsD protocol for github.com/rs/xstats.
No description provided by the author
Package mock implements mock object for xstats Sender interface based on github.com/stretchr/testify/mock package mock implementation.
No description provided by the author
Package statsd implement the StatsD protocol for github.com/rs/xstats.
Package telegrafstatsd implement telegraf extended StatsD protocol for github.com/rs/xstats.

# Functions

Close will call Close() on any xstats.XStater that implements io.Closer.
CloseSender will call Close() on any xstats.Sender that implements io.Closer.
Copy makes a copy of the given XStater if it implements the Copier interface.
FromContext retreives the request's xstats client from a given context if any.
FromRequest gets the xstats client in the request's context.
New returns a new xstats client with the provided backend sender.
NewContext returns a copy of the parent context and associates it with passed stats.
NewHandler creates a new handler with the provided metric client.
NewHandlerPrefix creates a new handler with the provided metric client.
NewPrefix returns a new xstats client with the provided backend sender.
NewScoping returns a new xstats client with the provided backend sender.
Scope makes a scoped copy of the given XStater if it implements the Scoper interface.

# Variables

DisablePooling will disable the use of sync.Pool fo resource management when true.

# Structs

Handler injects a per request metrics client in the net/context which can be retrived using xstats.FromContext(ctx).

# Interfaces

Copier is an interface to an XStater that supports coping.
Scoper is an interface to an XStater, that supports scoping.
Sender define an interface to a stats system like statsd or datadog to send service's metrics.
XStater is a wrapper around a Sender to inject env tags within all observations.

# Type aliases

MultiSender lets you assign more than one sender to xstats in order to multicast observeration to different systems.