Skip to content

Commit 50c08e6

Browse files
committed
Add MetricName as command line flag, refactor the Dogstatsd function to be more useful.
1 parent 7551daa commit 50c08e6

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

commands/dogstatsd.go

+18-14
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@ import (
66
"time"
77
)
88

9-
func makeTags(event, exec, ltime string) []string {
10-
tags := make([]string, 3)
11-
eventTag := fmt.Sprintf("event:%s", event)
9+
func makeTags(exec, watchType, watchId, id string) []string {
10+
tags := make([]string, 4)
1211
execTag := fmt.Sprintf("exec:%s", exec)
13-
lTimeTag := fmt.Sprintf("ltime:%s", ltime)
14-
tags = append(tags, eventTag)
12+
watchTypeTag := fmt.Sprintf("watchtype:%s", watchType)
13+
watchIdTag := fmt.Sprintf("watchid:%s", watchId)
14+
idTag := fmt.Sprintf("id:%s", id)
1515
tags = append(tags, execTag)
16-
tags = append(tags, lTimeTag)
16+
tags = append(tags, watchTypeTag)
17+
tags = append(tags, watchIdTag)
18+
tags = append(tags, idTag)
1719
return tags
1820
}
1921

20-
func StatsdRunTime(start time.Time, event string, exec string, ltime int64) {
21-
elapsed := time.Since(start)
22-
milliseconds := int64(elapsed / time.Millisecond)
23-
Log(fmt.Sprintf("dogstatsd='true' event='%s' exec='%s' ltime='%d' elapsed='%s'", event, exec, ltime, elapsed), "info")
24-
statsd, _ := godspeed.NewDefault()
25-
defer statsd.Conn.Close()
26-
tags := makeTags(event, exec, string(ltime))
27-
statsd.Gauge("sifter.time", float64(milliseconds), tags)
22+
func StatsdRunTime(start time.Time, exec string, watchType string, watchId string, id string) {
23+
if DogStatsd {
24+
elapsed := time.Since(start)
25+
milliseconds := int64(elapsed / time.Millisecond)
26+
Log(fmt.Sprintf("dogstatsd='true' %s='%s' exec='%s' id='%s' elapsed='%s'", watchType, watchId, exec, id, elapsed), "info")
27+
statsd, _ := godspeed.NewDefault()
28+
defer statsd.Conn.Close()
29+
tags := makeTags(exec, watchType, watchId, id)
30+
statsd.Gauge(MetricName, float64(milliseconds), tags)
31+
}
2832
}

commands/event.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ func startEvent(cmd *cobra.Command, args []string) {
4141
Set(c, ConsulKey, lTimeString)
4242
runCommand(Exec, Payload)
4343
RunTime(start, "complete", fmt.Sprintf("watch='event' exec='%s' ltime='%d'", Exec, lTime))
44-
if DogStatsd {
45-
StatsdRunTime(start, EventName, Exec, lTime)
46-
}
44+
StatsdRunTime(start, Exec, "event", EventName, strconv.FormatInt(lTime, 10))
4745
} else {
4846
RunTime(start, "duplicate", fmt.Sprintf("watch='event' exec='%s' ltime='%d'", Exec, lTime))
4947
}

commands/key.go

+6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func startKey(cmd *cobra.Command, args []string) {
3737
Set(c, url, shaValue)
3838
runCommand(Exec, "")
3939
RunTime(start, "complete", fmt.Sprintf("watch='key' exec='%s' sha='%s'", Exec, shaValue))
40+
StatsdRunTime(start, Exec, "key", d.getKey(), shaValue)
4041
} else {
4142
RunTime(start, "duplicate", fmt.Sprintf("watch='key' exec='%s' sha='%s'", Exec, shaValue))
4243
}
@@ -92,6 +93,11 @@ func (c *ConsulKey) getSHA() string {
9293
return sha
9394
}
9495

96+
func (c *ConsulKey) getKey() string {
97+
keyName := c.Key
98+
return keyName
99+
}
100+
95101
func init() {
96102
RootCmd.AddCommand(keyCmd)
97103
}

commands/root.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ var RootCmd = &cobra.Command{
1515
}
1616

1717
var (
18-
Exec string
19-
Token string
20-
DogStatsd bool
21-
Prefix string
18+
Exec string
19+
Token string
20+
DogStatsd bool
21+
Prefix string
22+
MetricName string
2223
)
2324

2425
func init() {
2526
RootCmd.PersistentFlags().StringVarP(&Exec, "exec", "e", "", "Execute this command if a new event is present.")
2627
RootCmd.PersistentFlags().StringVarP(&Prefix, "prefix", "p", "sifter", "Consul prefix for saved state.")
2728
RootCmd.PersistentFlags().StringVarP(&Token, "token", "t", "anonymous", "Token for Consul access")
29+
RootCmd.PersistentFlags().StringVarP(&MetricName, "metric", "m", "sifter.time", "Metric name for dogstatsd.")
2830
RootCmd.PersistentFlags().BoolVarP(&DogStatsd, "dogstatsd", "d", false, "send metrics to dogstatsd")
2931
}

0 commit comments

Comments
 (0)