Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
add dogstatsd utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Achille Roussel committed Jan 6, 2017
1 parent a0055de commit 185df39
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ _testmain.go

# Emacs
*~

# Commands
/dogstatsd
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:alpine

COPY . /go/src/github.com/segmentio/stats

ENV CGO_ENABLED=0
RUN apk add --no-cache git && \
cd /go/src/github.com/segmentio/stats && \
go build -v -o /dogstatsd ./cmd/dogstatsd && \
apk del git && \
rm -rf /go/*

ENTRYPOINT ["/dogstatsd"]
21 changes: 21 additions & 0 deletions cmd/dogstatsd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"flag"
"log"
"net"

"github.com/segmentio/stats/datadog"
)

func main() {
var bind string

flag.StringVar(&bind, "bind", ":8125", "The network address to listen on for incoming UDP datagrams")
flag.Parse()
log.Printf("listening for incoming UDP datagram on %s", bind)

datadog.ListenAndServe(bind, datadog.HandlerFunc(func(metric datadog.Metric, from net.Addr) {
log.Print(metric)
}))
}
28 changes: 27 additions & 1 deletion datadog/metric.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package datadog

import "github.com/segmentio/stats"
import (
"fmt"
"sync"

"github.com/segmentio/stats"
)

// MetricType is an enumeration providing symbols to represent the different
// metric types upported by datadog.
Expand All @@ -21,3 +26,24 @@ type Metric struct {
Tags []stats.Tag // the list of tags set on the metric
Namespace stats.Namespace // the metric namespace (never populated by parsing operations)
}

// String satisfies the fmt.Stringer interface.
func (m Metric) String() string {
return fmt.Sprint(m)
}

// Format satisfies the fmt.Formatter interface.
func (m Metric) Format(f fmt.State, _ rune) {
buf := bufferPool.Get().(*buffer)
buf.b = appendMetric(buf.b[:0], m)
f.Write(buf.b)
bufferPool.Put(buf)
}

type buffer struct {
b []byte
}

var bufferPool = sync.Pool{
New: func() interface{} { return &buffer{make([]byte, 0, 512)} },
}
16 changes: 15 additions & 1 deletion datadog/metric_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package datadog

import "github.com/segmentio/stats"
import (
"testing"

"github.com/segmentio/stats"
)

var testMetrics = []struct {
s string
Expand Down Expand Up @@ -116,3 +120,13 @@ var testMetrics = []struct {
},
},
}

func TestMetricString(t *testing.T) {
for _, test := range testMetrics {
t.Run(test.s, func(t *testing.T) {
if s := test.m.String(); s != test.s {
t.Error(s)
}
})
}
}

0 comments on commit 185df39

Please sign in to comment.