Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from igorcmoura/master
Browse files Browse the repository at this point in the history
Fix to follow other libs patterns
  • Loading branch information
jonathansp authored Jul 4, 2018
2 parents 7574f87 + f7af88e commit 831d5ba
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 121 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
language: go
script: go test -v ./...
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# go-metrics logstash
# go-metrics logstash [![Build Status](https://travis-ci.com/jonathansp/go-metrics-logstash.svg?branch=master)](https://travis-ci.com/jonathansp/go-metrics-logstash) [![GoDoc](https://godoc.org/github.com/jonathansp/go-metrics-logstash?status.svg)](http://godoc.org/github.com/jonathansp/go-metrics-logstash)

This package provides a reporter for the [go-metrics](https://github.com/rcrowley/go-metrics) library that will post the metrics to logstash. This library is based on [go-metrics-datadog](https://github.com/syntaqx/go-metrics-datadog).

Expand Down Expand Up @@ -29,9 +29,11 @@ func main() {
metrics.RegisterRuntimeMemStats(registry)

reporter, err := logstash.NewReporter(
registry, // go-metrics registry, or nil
"127.0.0.1:1984", // logstash UDP address,
"my-app", // reporter's name
registry, // go-metrics registry, or nil
"127.0.0.1:1984", // logstash UDP address,
map[string]interface{}{ // default values to be sent at each flush
"client": "my-app",
}
)
if err != nil {
log.Fatal(err)
Expand Down
175 changes: 175 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package logstash

import (
"net"
"strconv"
"strings"
"testing"

metrics "github.com/rcrowley/go-metrics"
"github.com/stretchr/testify/assert"
)

type UDPServer struct {
conn *net.UDPConn
}

func newUDPServer(port int) (*UDPServer, error) {
serverAddr, err := net.ResolveUDPAddr("udp", ":"+strconv.Itoa(port))
if err != nil {
return nil, err
}
conn, err := net.ListenUDP("udp", serverAddr)
if err != nil {
return nil, err
}
return &UDPServer{conn}, nil
}

func (us *UDPServer) Read() (string, error) {
buffer := make([]byte, 4096)
_, _, err := us.conn.ReadFromUDP(buffer)
if err != nil {
return "", err
}
resizedStr := strings.Trim(string(buffer), "\x00") // Remove the empty chars at the end of the buffer
return resizedStr, nil
}

func (us *UDPServer) Close() {
us.conn.Close()
}

func TestFlushOnce(t *testing.T) {
serverAddr := "localhost:1984"
server, err := newUDPServer(1984)
if err != nil {
t.Fatal(err)
}
defer server.Close()

registry := metrics.NewRegistry()
reporter, err := NewReporter(registry, serverAddr, nil)

// Insert metrics
metrics.GetOrRegisterCounter("test_counter", registry).Inc(6)
metrics.GetOrRegisterCounter("test_counter", registry).Inc(2)
metrics.GetOrRegisterGauge("test_gauge", registry).Update(2)
metrics.GetOrRegisterGauge("test_gauge", registry).Update(3)
metrics.GetOrRegisterGaugeFloat64("test_gaugeFloat64", registry).Update(4)
metrics.GetOrRegisterGaugeFloat64("test_gaugeFloat64", registry).Update(5)
sample := metrics.NewUniformSample(2)
metrics.GetOrRegisterHistogram("test_histogram", registry, sample).Update(9)
metrics.GetOrRegisterHistogram("test_histogram", registry, sample).Update(10)
// TODO test meter and timer
reporter.FlushOnce()

received, err := server.Read()
if err != nil {
t.Fatal(err)
}

expected := `{
"test_counter.count": 8,
"test_gauge": 3,
"test_gaugeFloat64": 5,
"test_histogram.count": 2,
"test_histogram.min": 9,
"test_histogram.max": 10,
"test_histogram.mean": 9.5,
"test_histogram.stddev": 0.5,
"test_histogram.var": 0.25,
"test_histogram.p50": 9.5,
"test_histogram.p75": 10,
"test_histogram.p95": 10,
"test_histogram.p99": 10,
"test_histogram.p99_9": 10
}`
assert.JSONEq(t, expected, received)
}

func TestFlushOnceKeepsPreviousValues(t *testing.T) {
serverAddr := "localhost:1984"
server, err := newUDPServer(1984)
if err != nil {
t.Fatal(err)
}
defer server.Close()

registry := metrics.NewRegistry()
reporter, err := NewReporter(registry, serverAddr, nil)

// Insert metrics
sample := metrics.NewUniformSample(3)
metrics.GetOrRegisterCounter("test_counter", registry).Inc(6)
metrics.GetOrRegisterCounter("test_counter", registry).Inc(2)
metrics.GetOrRegisterGauge("test_gauge", registry).Update(2)
metrics.GetOrRegisterGauge("test_gauge", registry).Update(3)
metrics.GetOrRegisterGaugeFloat64("test_gaugeFloat64", registry).Update(4)
metrics.GetOrRegisterGaugeFloat64("test_gaugeFloat64", registry).Update(5)
metrics.GetOrRegisterHistogram("test_histogram", registry, sample).Update(9)
metrics.GetOrRegisterHistogram("test_histogram", registry, sample).Update(10)
reporter.FlushOnce()
server.Read() // Ignore current values

metrics.GetOrRegisterCounter("test_counter", registry).Inc(4)
metrics.GetOrRegisterGauge("test_gauge", registry).Update(8)
metrics.GetOrRegisterGaugeFloat64("test_gaugeFloat64", registry).Update(9)
metrics.GetOrRegisterHistogram("test_histogram", registry, sample).Update(12)
// TODO test meter and timer
reporter.FlushOnce()

received, err := server.Read()
if err != nil {
t.Fatal(err)
}

expected := `{
"test_counter.count": 12,
"test_gauge": 8,
"test_gaugeFloat64": 9,
"test_histogram.count": 3,
"test_histogram.min": 9,
"test_histogram.max": 12,
"test_histogram.mean": 10.333333333333334,
"test_histogram.stddev": 1.247219128924647,
"test_histogram.var": 1.5555555555555556,
"test_histogram.p50": 10,
"test_histogram.p75": 12,
"test_histogram.p95": 12,
"test_histogram.p99": 12,
"test_histogram.p99_9": 12
}`
assert.JSONEq(t, expected, received)
}

func TestFlushOnceWithDefaultValues(t *testing.T) {
serverAddr := "localhost:1984"
server, err := newUDPServer(1984)
if err != nil {
t.Fatal(err)
}
defer server.Close()

registry := metrics.NewRegistry()
reporter, err := NewReporter(registry, serverAddr, map[string]interface{}{
"client": "dummy-client",
"metric": "doc",
})

// Insert metrics
metrics.GetOrRegisterCounter("test_counter", registry).Inc(6)
reporter.FlushOnce()

received, err := server.Read()
if err != nil {
t.Fatal(err)
}

expected := `{
"client":"dummy-client",
"metric": "doc",
"test_counter.count": 6
}`
assert.JSONEq(t, expected, received)
}
61 changes: 0 additions & 61 deletions metrics.go

This file was deleted.

Loading

0 comments on commit 831d5ba

Please sign in to comment.