Skip to content

Commit

Permalink
use program name as prefix in default engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Achille Roussel committed Nov 28, 2016
1 parent c560eb1 commit e15f6b9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 48 deletions.
18 changes: 9 additions & 9 deletions datadog/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func TestServer(t *testing.T) {

addr, closer := startTestServer(t, HandlerFunc(func(m stats.Metric, _ net.Addr) {
switch m.Name {
case "test.A":
case "datadog.test.A":
atomic.AddUint32(&a, uint32(m.Value))

case "test.B":
case "datadog.test.B":
atomic.AddUint32(&b, uint32(m.Value))

case "test.C":
case "datadog.test.C":
atomic.AddUint32(&c, uint32(m.Value))
}
}))
Expand All @@ -38,30 +38,30 @@ func TestServer(t *testing.T) {
})
defer client.Close()

ma := stats.MakeCounter(engine, "test.A")
ma := stats.MakeCounter(engine, "A")
ma.Incr()

mb := stats.MakeCounter(engine, "test.B")
mb := stats.MakeCounter(engine, "B")
mb.Incr()
mb.Incr()

mc := stats.MakeCounter(engine, "test.C")
mc := stats.MakeCounter(engine, "C")
mc.Incr()
mc.Incr()
mc.Incr()

time.Sleep(10 * time.Millisecond)

if atomic.LoadUint32(&a) != 1 {
t.Error("test.A not reported")
t.Error("datadog.test.A not reported")
}

if atomic.LoadUint32(&b) != 2 {
t.Error("test.B not reported")
t.Error("datadog.test.B not reported")
}

if atomic.LoadUint32(&c) != 3 {
t.Error("test.C not reported")
t.Error("datadog.test.C not reported")
}
}

Expand Down
11 changes: 10 additions & 1 deletion engine.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package stats

import (
"os"
"path/filepath"
"runtime"
"sync"
"time"
Expand Down Expand Up @@ -84,7 +86,7 @@ func Time(name string, start time.Time, tags ...Tag) *Clock {

// NewDefaultEngine creates and returns an engine configured with default settings.
func NewDefaultEngine() *Engine {
return NewEngine(EngineConfig{})
return NewEngine(EngineConfig{Prefix: progname()})
}

// NewEngine creates and returns an engine configured with config.
Expand Down Expand Up @@ -229,3 +231,10 @@ func runEngine(e engine) {
}
}
}

func progname() (name string) {
if args := os.Args; len(args) != 0 {
name = filepath.Base(args[0])
}
return
}
60 changes: 30 additions & 30 deletions netstats/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,50 @@ func TestConn(t *testing.T) {
expects := []stats.Metric{
stats.Metric{
Type: stats.CounterType,
Key: "conn.bytes.count?operation=read&protocol=tcp",
Name: "conn.bytes.count",
Key: "netstats.test.conn.bytes.count?operation=read&protocol=tcp",
Name: "netstats.test.conn.bytes.count",
Tags: []stats.Tag{{"operation", "read"}, {"protocol", "tcp"}},
Value: 12,
Sample: 1,
},
stats.Metric{
Type: stats.CounterType,
Key: "conn.bytes.count?operation=write&protocol=tcp",
Name: "conn.bytes.count",
Key: "netstats.test.conn.bytes.count?operation=write&protocol=tcp",
Name: "netstats.test.conn.bytes.count",
Tags: []stats.Tag{{"operation", "write"}, {"protocol", "tcp"}},
Value: 12,
Sample: 1,
},
stats.Metric{
Type: stats.CounterType,
Key: "conn.close.count?protocol=tcp",
Name: "conn.close.count",
Key: "netstats.test.conn.close.count?protocol=tcp",
Name: "netstats.test.conn.close.count",
Tags: []stats.Tag{{"protocol", "tcp"}},
Value: 1,
Sample: 1,
},
stats.Metric{
Type: stats.HistogramType,
Group: "conn.iops?operation=read&protocol=tcp",
Key: "conn.iops?operation=read&protocol=tcp#0",
Name: "conn.iops",
Group: "netstats.test.conn.iops?operation=read&protocol=tcp",
Key: "netstats.test.conn.iops?operation=read&protocol=tcp#0",
Name: "netstats.test.conn.iops",
Tags: []stats.Tag{{"operation", "read"}, {"protocol", "tcp"}},
Value: 12,
Sample: 1,
},
stats.Metric{
Type: stats.HistogramType,
Group: "conn.iops?operation=write&protocol=tcp",
Key: "conn.iops?operation=write&protocol=tcp#0",
Name: "conn.iops",
Group: "netstats.test.conn.iops?operation=write&protocol=tcp",
Key: "netstats.test.conn.iops?operation=write&protocol=tcp#0",
Name: "netstats.test.conn.iops",
Tags: []stats.Tag{{"operation", "write"}, {"protocol", "tcp"}},
Value: 12,
Sample: 1,
},
stats.Metric{
Type: stats.CounterType,
Key: "conn.open.count?protocol=tcp",
Name: "conn.open.count",
Key: "netstats.test.conn.open.count?protocol=tcp",
Name: "netstats.test.conn.open.count",
Tags: []stats.Tag{{"protocol", "tcp"}},
Value: 1,
Sample: 1,
Expand Down Expand Up @@ -128,64 +128,64 @@ func TestConnError(t *testing.T) {
expects := []stats.Metric{
stats.Metric{
Type: stats.CounterType,
Key: "conn.close.count?protocol=tcp",
Name: "conn.close.count",
Key: "netstats.test.conn.close.count?protocol=tcp",
Name: "netstats.test.conn.close.count",
Tags: []stats.Tag{{"protocol", "tcp"}},
Value: 1,
Sample: 1,
},
stats.Metric{
Type: stats.CounterType,
Key: "conn.errors.count?operation=close&protocol=tcp",
Name: "conn.errors.count",
Key: "netstats.test.conn.errors.count?operation=close&protocol=tcp",
Name: "netstats.test.conn.errors.count",
Tags: []stats.Tag{{"operation", "close"}, {"protocol", "tcp"}},
Value: 1,
Sample: 1,
},
stats.Metric{
Type: stats.CounterType,
Key: "conn.errors.count?operation=read&protocol=tcp",
Name: "conn.errors.count",
Key: "netstats.test.conn.errors.count?operation=read&protocol=tcp",
Name: "netstats.test.conn.errors.count",
Tags: []stats.Tag{{"operation", "read"}, {"protocol", "tcp"}},
Value: 3,
Sample: 3,
},
stats.Metric{
Type: stats.CounterType,
Key: "conn.errors.count?operation=set-read-timeout&protocol=tcp",
Name: "conn.errors.count",
Key: "netstats.test.conn.errors.count?operation=set-read-timeout&protocol=tcp",
Name: "netstats.test.conn.errors.count",
Tags: []stats.Tag{{"operation", "set-read-timeout"}, {"protocol", "tcp"}},
Value: 1,
Sample: 1,
},
stats.Metric{
Type: stats.CounterType,
Key: "conn.errors.count?operation=set-timeout&protocol=tcp",
Name: "conn.errors.count",
Key: "netstats.test.conn.errors.count?operation=set-timeout&protocol=tcp",
Name: "netstats.test.conn.errors.count",
Tags: []stats.Tag{{"operation", "set-timeout"}, {"protocol", "tcp"}},
Value: 1,
Sample: 1,
},
stats.Metric{
Type: stats.CounterType,
Key: "conn.errors.count?operation=set-write-timeout&protocol=tcp",
Name: "conn.errors.count",
Key: "netstats.test.conn.errors.count?operation=set-write-timeout&protocol=tcp",
Name: "netstats.test.conn.errors.count",
Tags: []stats.Tag{{"operation", "set-write-timeout"}, {"protocol", "tcp"}},
Value: 1,
Sample: 1,
},
stats.Metric{
Type: stats.CounterType,
Key: "conn.errors.count?operation=write&protocol=tcp",
Name: "conn.errors.count",
Key: "netstats.test.conn.errors.count?operation=write&protocol=tcp",
Name: "netstats.test.conn.errors.count",
Tags: []stats.Tag{{"operation", "write"}, {"protocol", "tcp"}},
Value: 1,
Sample: 1,
},
stats.Metric{
Type: stats.CounterType,
Key: "conn.open.count?protocol=tcp",
Name: "conn.open.count",
Key: "netstats.test.conn.open.count?protocol=tcp",
Name: "netstats.test.conn.open.count",
Tags: []stats.Tag{{"protocol", "tcp"}},
Value: 1,
Sample: 1,
Expand Down
8 changes: 4 additions & 4 deletions procstats/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func TestGoMetrics(t *testing.T) {

for _, m := range metrics {
switch {
case strings.HasPrefix(m.Name, "go.runtime."):
case strings.HasPrefix(m.Name, "go.memstats."):
case strings.HasPrefix(m.Name, "procstats.test.go.runtime."):
case strings.HasPrefix(m.Name, "procstats.test.go.memstats."):
default:
t.Error("invalid metric name:", m.Name)
}
Expand Down Expand Up @@ -59,8 +59,8 @@ func TestGoMetricsMock(t *testing.T) {

for _, m := range metrics {
switch {
case strings.HasPrefix(m.Name, "go.runtime."):
case strings.HasPrefix(m.Name, "go.memstats."):
case strings.HasPrefix(m.Name, "procstats.test.go.runtime."):
case strings.HasPrefix(m.Name, "procstats.test.go.memstats."):
default:
t.Error("invalid metric name:", m.Name)
}
Expand Down
8 changes: 4 additions & 4 deletions procstats/proc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func TestProcMetrics(t *testing.T) {

for _, m := range metrics {
switch {
case strings.HasPrefix(m.Name, "cpu."):
case strings.HasPrefix(m.Name, "memory."):
case strings.HasPrefix(m.Name, "files."):
case strings.HasPrefix(m.Name, "thread."):
case strings.HasPrefix(m.Name, "procstats.test.cpu."):
case strings.HasPrefix(m.Name, "procstats.test.memory."):
case strings.HasPrefix(m.Name, "procstats.test.files."):
case strings.HasPrefix(m.Name, "procstats.test.thread."):
default:
t.Error("invalid metric name:", m.Name)
}
Expand Down

0 comments on commit e15f6b9

Please sign in to comment.