From 178dec583491f36c11916cd91df49727cf03d13c Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Mon, 15 Aug 2016 19:50:51 -0400 Subject: [PATCH] work on docs/etc --- doc.go | 5 +++++ handler.go | 6 ++---- setup.go | 32 ++++++++++++++++---------------- 3 files changed, 23 insertions(+), 20 deletions(-) create mode 100644 doc.go diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..5da2312 --- /dev/null +++ b/doc.go @@ -0,0 +1,5 @@ +// Package caddydogstatsd provides a Datadog middleware for caddy. +// +// caddydogstatsd is a plugin for https://caddyserver.com which will emit Datadog dogstatsd metrics +// after each request is handled. +package caddydogstatsd diff --git a/handler.go b/handler.go index 4c45dc0..d606d53 100644 --- a/handler.go +++ b/handler.go @@ -9,15 +9,13 @@ import ( "github.com/mholt/caddy/caddyhttp/httpserver" ) -// DogstatsdHandler is a middleware handler for reporting dogstatsd metrics on requests -type DogstatsdHandler struct { +type dogstatsdHandler struct { Client *statsd.Client SampleRate float64 Next httpserver.Handler } -// ServeHTTP is the middleware handler which will emit dogstatsd metrics after handling a request -func (h DogstatsdHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { +func (h dogstatsdHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { // If we do not have a statsd.Client configured, then skip any processing if h.Client == nil { return h.Next.ServeHTTP(w, r) diff --git a/setup.go b/setup.go index f38bc7c..c777f16 100644 --- a/setup.go +++ b/setup.go @@ -11,7 +11,7 @@ import ( ) func init() { - // register our plugin with Caddy + // register our plugin with Caddy caddy.RegisterPlugin("dogstatsd", caddy.Plugin{ ServerType: "http", Action: setup, @@ -20,35 +20,35 @@ func init() { func setup(c *caddy.Controller) error { for c.Next() { - // only parse if the initial directive is "dogstatsd" + // only parse if the initial directive is "dogstatsd" if c.Val() != "dogstatsd" { continue } - // default config values + // default config values var namespace = "" var host = "127.0.0.1:8125" var globalTags = []string{} var sampleRate = 1.0 - // if we have a block, then parse that - // e.g. - // dogstatsd { - // host 127.0.0.1:8125 - // } + // if we have a block, then parse that + // e.g. + // dogstatsd { + // host 127.0.0.1:8125 + // } for c.NextBlock() { - // each line if of the format `{key} {arg} [{arg}...]` + // each line if of the format `{key} {arg} [{arg}...]` var key string key = c.Val() var args []string args = c.RemainingArgs() - // we expect every directive to have at least 1 argument + // we expect every directive to have at least 1 argument if len(args) == 0 { return c.ArgErr() } - // parse directives + // parse directives switch key { case "host": host = args[0] @@ -70,9 +70,9 @@ func setup(c *caddy.Controller) error { } } - // handle non-block configuration - // e.g. - // dogstatsd [{host:port} [{samplerate}]] + // handle non-block configuration + // e.g. + // dogstatsd [{host:port} [{samplerate}]] if c.NextArg() { var args []string args = c.RemainingArgs() @@ -88,7 +88,7 @@ func setup(c *caddy.Controller) error { } } - // add our middleware + // add our middleware var cfg *httpserver.SiteConfig cfg = httpserver.GetConfig(c) cfg.AddMiddleware(func(next httpserver.Handler) httpserver.Handler { @@ -100,7 +100,7 @@ func setup(c *caddy.Controller) error { client.Tags = globalTags } - return DogstatsdHandler{ + return dogstatsdHandler{ Client: client, SampleRate: sampleRate, Next: next,