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"
)
// 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)


+ 16
- 16
setup.go View File

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


Loading…
Cancel
Save