Skip to content

Commit

Permalink
Add Dogstatsd support. Closes #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
darron committed Nov 15, 2015
1 parent b3474ba commit ed277fa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ all: build
deps:
go get github.com/spf13/cobra
go get github.com/hashicorp/consul/api
go get github.com/PagerDuty/godspeed

format:
gofmt -w .
Expand Down
28 changes: 28 additions & 0 deletions commands/dogstatsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package commands

import (
"fmt"
"github.com/PagerDuty/godspeed"
"time"
)

func makeTags(event, exec, ltime string) []string {
tags := make([]string, 3)
eventTag := fmt.Sprintf("event:%s", event)
execTag := fmt.Sprintf("exec:%s", exec)
lTimeTag := fmt.Sprintf("ltime:%s", ltime)
tags = append(tags, eventTag)
tags = append(tags, execTag)
tags = append(tags, lTimeTag)
return tags
}

func StatsdRunTime(start time.Time, event string, exec string, ltime int64) {
elapsed := time.Since(start)
milliseconds := int64(elapsed / time.Millisecond)
Log(fmt.Sprintf("dogstatsd='true' event='%s' exec='%s' ltime='%d' elapsed='%s'", event, exec, ltime, elapsed), "info")
statsd, _ := godspeed.NewDefault()
defer statsd.Conn.Close()
tags := makeTags(event, exec, string(ltime))
statsd.Gauge("sifter.time", float64(milliseconds), tags)
}
6 changes: 4 additions & 2 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ var RootCmd = &cobra.Command{
}

var (
Exec string
Token string
Exec string
Token string
DogStatsd bool
)

func init() {
RootCmd.PersistentFlags().StringVarP(&Exec, "exec", "e", "", "Execute this command if a payload is present.")
RootCmd.PersistentFlags().StringVarP(&Token, "token", "t", "anonymous", "Token for Consul access")
RootCmd.PersistentFlags().BoolVarP(&DogStatsd, "dogstatsd", "d", false, "send metrics to dogstatsd")
}
3 changes: 3 additions & 0 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func startRun(cmd *cobra.Command, args []string) {
Set(c, ConsulKey, lTimeString)
runCommand(Exec)
RunTime(start, "complete", fmt.Sprintf("exec='%s' ltime='%d'", Exec, lTime))
if DogStatsd {
StatsdRunTime(start, EventName, Exec, lTime)
}
} else {
RunTime(start, "duplicate", fmt.Sprintf("exec='%s' ltime='%d'", Exec, lTime))
}
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
SIFTER_VERSION="0.5"
SIFTER_VERSION="0.6"
echo $SIFTER_VERSION

0 comments on commit ed277fa

Please sign in to comment.