Skip to content

Commit

Permalink
Merge pull request #5 from catkins/catkins/add-dd-trace-support
Browse files Browse the repository at this point in the history
Add support for datadog tracing
  • Loading branch information
catkins authored Dec 4, 2018
2 parents c346d20 + ca56578 commit d2ab011
Show file tree
Hide file tree
Showing 16 changed files with 388 additions and 69 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
language: go

go:
- 1.8
- 1.9
- 1.11

cache:
- vendor
Expand All @@ -16,4 +15,4 @@ install:
- dep ensure -v

script:
- go test -v -race
- go test -v -race ./...
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM golang:1.9 as binary-builder
FROM golang:1.11 as binary-builder

WORKDIR /go/src/github.com/catkins/statsd-logger

# install dep
RUN curl -L https://github.com/golang/dep/releases/download/v0.3.2/dep-linux-amd64 > $GOPATH/bin/dep \
RUN curl -L https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 > $GOPATH/bin/dep \
&& chmod +x $GOPATH/bin/dep

# install runtime dependencies
Expand Down
73 changes: 71 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
name = "github.com/stretchr/testify"
revision = "890a5c3458b43e6104ff5da8dfa139d013d77544"


[[constraint]]
name = "github.com/vmihailenco/msgpack"
version = "4.0.0"
37 changes: 0 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,6 @@ statsd-logger
echo -n "my.awesome_counter:1|c#cool:tags,another_tag:with_value" | nc -u -w0 localhost 8125
```

### Library

```bash
go get -u github.com/catkins/statsd-logger
```

Embed it into an existing application

```go
package main

import (
"os"
"os/signal"
"syscall"

"github.com/catkins/statsd-logger"
)

func main() {
shutdownChan := make(chan os.Signal)
signal.Notify(shutdownChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)

server, err := statsdLogger.New("0.0.0.0:8125")
if err != nil {
panic(err)
}

go func() {
server.Listen()
}()

<-shutdownChan
server.Close()
}
```

## Docker

`statsd-logger` can also log StatsD metrics being being sent to udp:8125 on running `docker` containers without modification.
Expand Down
16 changes: 12 additions & 4 deletions cmd/statsd-logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,30 @@ import (
"os/signal"
"syscall"

"github.com/catkins/statsd-logger"
"github.com/catkins/statsd-logger/metrics"
"github.com/catkins/statsd-logger/trace"
)

func main() {
shutdownChan := make(chan os.Signal)
signal.Notify(shutdownChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)

server, err := statsdLogger.New("0.0.0.0:8125")
metricsServer, err := metrics.NewServer(metrics.DefaultAddress)
if err != nil {
panic(err)
}

go func() {
server.Listen()
metricsServer.Listen()
}()

traceServer := trace.NewServer(trace.DefaultAddress)

go func() {
traceServer.Listen()
}()

<-shutdownChan
server.Close()
metricsServer.Close()
traceServer.Close()
}
2 changes: 1 addition & 1 deletion formatter.go → metrics/formatter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package statsdLogger
package metrics

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion formatter_test.go → metrics/formatter_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package statsdLogger
package metrics

import "testing"
import "github.com/fatih/color"
Expand Down
17 changes: 13 additions & 4 deletions metric.go → metrics/metric.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package statsdLogger
package metrics

import "strings"
import (
"fmt"
"strings"
)

// Metric is an intermediate representation of a raw statsd metric for easier presentation
type Metric struct {
Expand All @@ -9,8 +12,14 @@ type Metric struct {
Tags string
}

// ParseMetric takes a raw statsd metric and returns a populated Metric
func ParseMetric(rawMetric string) Metric {
// Parse takes a raw statsd metric and returns a populated Metric
func Parse(rawMetric string) (metric Metric) {
defer func() {
if r := recover(); r != nil {
fmt.Printf("error parsing metric: %+v\n", r)
}
}()

metricNameAndRest := strings.SplitN(rawMetric, ":", 2)
name := metricNameAndRest[0]
valueAndTags := strings.SplitN(metricNameAndRest[1], "#", 2)
Expand Down
6 changes: 3 additions & 3 deletions metric_test.go → metrics/metric_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package statsdLogger
package metrics

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseMetric(t *testing.T) {
func TestParse(t *testing.T) {
cases := []struct {
input string
expected Metric
Expand All @@ -22,6 +22,6 @@ func TestParseMetric(t *testing.T) {
}

for _, tc := range cases {
assert.Equal(t, tc.expected, ParseMetric(tc.input))
assert.Equal(t, tc.expected, Parse(tc.input))
}
}
12 changes: 6 additions & 6 deletions server.go → metrics/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package statsdLogger provides a simple dummy StatsD logging server for local development
// Package metrics provides a simple dummy StatsD logging server for local development
//
// Adapted from http://lee.hambley.name/2013/01/26/dirt-simple-statsd-server-for-local-development.html
package statsdLogger
package metrics

import (
"errors"
Expand All @@ -28,8 +28,8 @@ type Server interface {
Close() error
}

// New returns a local statsd logging server which logs to statsdLogger.DefaultOutput and is formatted with statsdLogger.DefaultFormatter
func New(address string, options ...func(*server)) (Server, error) {
// NewServer returns a local statsd logging server which logs to statsdLogger.DefaultOutput and is formatted with statsdLogger.DefaultFormatter
func NewServer(address string, options ...func(*server)) (Server, error) {
addr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
return nil, errors.New("[StatsD] Invalid address")
Expand Down Expand Up @@ -90,7 +90,7 @@ func (s *server) Listen() error {

fmt.Fprintf(s.output, "[StatsD] Listening on port %d\n", s.port)

buffer := make([]byte, 1024)
buffer := make([]byte, 10000)

for !s.isClosed() {
numBytes, err := s.connection.Read(buffer)
Expand All @@ -106,7 +106,7 @@ func (s *server) Listen() error {
}

rawMetric := buffer[0:numBytes]
metric := ParseMetric(string(rawMetric))
metric := Parse(string(rawMetric))

s.logMetric(metric)
}
Expand Down
12 changes: 6 additions & 6 deletions server_test.go → metrics/server_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package statsdLogger
package metrics

import (
"bytes"
Expand All @@ -16,7 +16,7 @@ import (

const (
testServerAddress = ":5678"
testSErverPort = 5678
testServerPort = 5678
)

func TestListen(t *testing.T) {
Expand All @@ -27,7 +27,7 @@ func TestListen(t *testing.T) {
sync.Mutex{},
}

server, err := New(testServerAddress, WithWriter(output))
server, err := NewServer(testServerAddress, WithWriter(output))

go func() { server.Listen() }()

Expand All @@ -48,7 +48,7 @@ func TestListen(t *testing.T) {
}

func TestInvalidAddress(t *testing.T) {
server, err := New("abcd", WithWriter(ioutil.Discard))
server, err := NewServer("abcd", WithWriter(ioutil.Discard))

assert.Error(t, err, "[StatsD] Invalid address")

Expand All @@ -60,7 +60,7 @@ func TestInvalidAddress(t *testing.T) {
func TestClose(t *testing.T) {
// TODO: for some reason, having this test use the `testServerAddress` seems to cause panics
// probably should figure out why
server, err := New(":0", WithWriter(ioutil.Discard))
server, err := NewServer(":0", WithWriter(ioutil.Discard))
assert.Nil(t, err)
assert.NotNil(t, server)

Expand All @@ -77,7 +77,7 @@ func TestWithFormatter(t *testing.T) {
metric := Metric{Name: "cool_metric"}
formatter.On("Format", metric).Return("ate 2 burgers")

server, err := New(testServerAddress, WithFormatter(formatter), WithWriter(ioutil.Discard))
server, err := NewServer(testServerAddress, WithFormatter(formatter), WithWriter(ioutil.Discard))
defer server.Close()
assert.Nil(t, err)

Expand Down
Loading

0 comments on commit d2ab011

Please sign in to comment.