xstats is a generic client for service instrumentation
Go
Switch branches/tags
Nothing to show
Clone or download
kevinconway and rs Add option to opt-out of sync.Pool use (#22)
* Add option to opt-out of sync.Pool use

When the http.Handler wrapped by the xstats middleware returns, the
internal state of the stater is cleared and the instance is returned
to the pool. However, there are quite a few cases in which a reference
to the stater escapes the context of the handler and ends up in a
background task.

When passing instances between goroutines we've established the habit
of making a copy to pass along to avoid this issue. However, when
interacting with third party code we don't always have this as an
option. For cases when it's absolutely necessary, we'd like to disable
the sync.Pool optimisation in favour of persistent stater references.

This patch adds the option in a backwards compatible way. The default
behaviour is maintained and only adding a new, explicit option to
disable the optimisation will trigger the new behaviour.

* Disable pre1.7 travis jobs
Latest commit c673675 Aug 13, 2017
Permalink
Failed to load latest commit information.
dogstatsd Make maxPacketLen configurable (#20) May 26, 2017
expvar Add support for expvar and MultiSender Oct 26, 2015
mock Mock sender for xstats mocking on testing stage (#21) May 31, 2017
prometheus Fix prometheus test Oct 18, 2016
statsd Make maxPacketLen configurable (#20) May 26, 2017
telegraf Make maxPacketLen configurable (#20) May 26, 2017
.travis.yml Add option to opt-out of sync.Pool use (#22) Aug 13, 2017
LICENSE Initial Commit Oct 23, 2015
README.md Mock sender for xstats mocking on testing stage (#21) May 31, 2017
handler.go Expose sync.Pool management via New, Copy and Close (fix #12) Feb 19, 2017
handler_example_test.go Add support for go 1.7 with backward compat Oct 18, 2016
handler_pre17.go Fix Go pre 1.7 regression Feb 19, 2017
handler_pre17_test.go Add support for go 1.7 with backward compat Oct 18, 2016
handler_test.go Add support for go 1.7 with backward compat Oct 18, 2016
nop.go nop: fix a typo in the doc Feb 19, 2017
nop_test.go Add nop test so we get the 100% coverage mark Oct 26, 2015
sender.go Closing an XStater closes the underlying Sender (#19) Mar 19, 2017
sender_example_test.go Add support for expvar and MultiSender Oct 26, 2015
sender_test.go Closing an XStater closes the underlying Sender (#19) Mar 19, 2017
xstats.go Add option to opt-out of sync.Pool use (#22) Aug 13, 2017
xstats_example_test.go Make maxPacketLen configurable (#20) May 26, 2017
xstats_test.go Add option to opt-out of sync.Pool use (#22) Aug 13, 2017

README.md

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.