Browse Source

work on docs/etc

pull/1/head
Brett Langdon 9 years ago
parent
commit
178dec5834
No known key found for this signature in database GPG Key ID: A2ECAB73CE12147F
3 changed files with 23 additions and 20 deletions
  1. +5
    -0
      doc.go
  2. +2
    -4
      handler.go
  3. +16
    -16
      setup.go

+ 5
- 0
doc.go View File

@ -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

+ 2
- 4
handler.go View File

@ -9,15 +9,13 @@ import (
"github.com/mholt/caddy/caddyhttp/httpserver" "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 Client *statsd.Client
SampleRate float64 SampleRate float64
Next httpserver.Handler 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 we do not have a statsd.Client configured, then skip any processing
if h.Client == nil { if h.Client == nil {
return h.Next.ServeHTTP(w, r) return h.Next.ServeHTTP(w, r)


+ 16
- 16
setup.go View File

@ -11,7 +11,7 @@ import (
) )
func init() { func init() {
// register our plugin with Caddy
// register our plugin with Caddy
caddy.RegisterPlugin("dogstatsd", caddy.Plugin{ caddy.RegisterPlugin("dogstatsd", caddy.Plugin{
ServerType: "http", ServerType: "http",
Action: setup, Action: setup,
@ -20,35 +20,35 @@ func init() {
func setup(c *caddy.Controller) error { func setup(c *caddy.Controller) error {
for c.Next() { for c.Next() {
// only parse if the initial directive is "dogstatsd"
// only parse if the initial directive is "dogstatsd"
if c.Val() != "dogstatsd" { if c.Val() != "dogstatsd" {
continue continue
} }
// default config values
// default config values
var namespace = "" var namespace = ""
var host = "127.0.0.1:8125" var host = "127.0.0.1:8125"
var globalTags = []string{} var globalTags = []string{}
var sampleRate = 1.0 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() { for c.NextBlock() {
// each line if of the format `{key} {arg} [{arg}...]`
// each line if of the format `{key} {arg} [{arg}...]`
var key string var key string
key = c.Val() key = c.Val()
var args []string var args []string
args = c.RemainingArgs() 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 { if len(args) == 0 {
return c.ArgErr() return c.ArgErr()
} }
// parse directives
// parse directives
switch key { switch key {
case "host": case "host":
host = args[0] 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() { if c.NextArg() {
var args []string var args []string
args = c.RemainingArgs() args = c.RemainingArgs()
@ -88,7 +88,7 @@ func setup(c *caddy.Controller) error {
} }
} }
// add our middleware
// add our middleware
var cfg *httpserver.SiteConfig var cfg *httpserver.SiteConfig
cfg = httpserver.GetConfig(c) cfg = httpserver.GetConfig(c)
cfg.AddMiddleware(func(next httpserver.Handler) httpserver.Handler { cfg.AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
@ -100,7 +100,7 @@ func setup(c *caddy.Controller) error {
client.Tags = globalTags client.Tags = globalTags
} }
return DogstatsdHandler{
return dogstatsdHandler{
Client: client, Client: client,
SampleRate: sampleRate, SampleRate: sampleRate,
Next: next, Next: next,


Loading…
Cancel
Save