Skip to content

Commit

Permalink
Merge pull request #12 from riemann/feat/time-refactoring
Browse files Browse the repository at this point in the history
[Breaking change] Refactoring event Time
  • Loading branch information
mcorbin authored Apr 24, 2017
2 parents 0f94ab1 + c9cf1ed commit 121dccf
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RIEMANN_VERSION = 0.2.12
RIEMANN_VERSION = 0.2.13

all: install

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Features:
* Idiomatic concurrency
* Sending events, queries.
* Support TCP, UDP, TLS clients.
* Second and Microsecond time resolution

This client is a fork of Goryman, a Riemann go client written by Christopher Gilbert. Thanks and full credit to him!

Expand Down
6 changes: 4 additions & 2 deletions event.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package riemanngo

import ()
import (
"time"
)

// Event is a wrapper for Riemann events
type Event struct {
Ttl float32
Time int64
Time time.Time
Tags []string
Host string // Defaults to os.Hostname()
State string
Expand Down
15 changes: 10 additions & 5 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"os"
"reflect"
"time"
"sort"
"time"

pb "github.com/golang/protobuf/proto"
"github.com/riemann/riemann-go-client/proto"
Expand All @@ -16,13 +16,14 @@ func EventToProtocolBuffer(event *Event) (*proto.Event, error) {
if event.Host == "" {
event.Host, _ = os.Hostname()
}
if event.Time == 0 {
event.Time = time.Now().Unix()
if event.Time.IsZero() {
event.Time = time.Now()
}

var e proto.Event
e.Host = pb.String(event.Host)
e.Time = pb.Int64(event.Time)
e.Time = pb.Int64(event.Time.Unix())
e.TimeMicros = pb.Int64(event.Time.UnixNano() / int64(time.Microsecond))
if event.Service != "" {
e.Service = pb.String(event.Service)
}
Expand Down Expand Up @@ -77,9 +78,13 @@ func ProtocolBuffersToEvents(pbEvents []*proto.Event) []Event {
Host: event.GetHost(),
Description: event.GetDescription(),
Ttl: event.GetTtl(),
Time: event.GetTime(),
Tags: event.GetTags(),
}
if event.TimeMicros != nil {
e.Time = time.Unix(0, event.GetTimeMicros()*int64(time.Microsecond))
} else if event.Time != nil {
e.Time = time.Unix(event.GetTime(), 0)
}
if event.MetricF != nil {
e.Metric = event.GetMetricF()
} else if event.MetricD != nil {
Expand Down
152 changes: 117 additions & 35 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
pb "github.com/golang/protobuf/proto"
"github.com/riemann/riemann-go-client/proto"
"testing"
"time"
)

func TestEventToProtocolBuffer(t *testing.T) {
Expand All @@ -14,7 +15,7 @@ func TestEventToProtocolBuffer(t *testing.T) {
Service: "foobar",
Metric: m,
Tags: []string{"hello"},
Time: 100,
Time: time.Unix(100, 0),
}
protoRes, error := EventToProtocolBuffer(&event)
if error != nil {
Expand All @@ -23,6 +24,7 @@ func TestEventToProtocolBuffer(t *testing.T) {
protoTest := proto.Event{
Host: pb.String("baz"),
Time: pb.Int64(100),
TimeMicros: pb.Int64(100000000),
MetricSint64: pb.Int64(100),
Service: pb.String("foobar"),
Tags: []string{"hello"},
Expand All @@ -36,7 +38,7 @@ func TestEventToProtocolBuffer(t *testing.T) {
Service: "foobar",
Metric: 100,
Tags: []string{"hello"},
Time: 100,
Time: time.Unix(100, 0),
}
protoRes, error = EventToProtocolBuffer(&event)
if error != nil {
Expand All @@ -45,6 +47,7 @@ func TestEventToProtocolBuffer(t *testing.T) {
protoTest = proto.Event{
Host: pb.String("baz"),
Time: pb.Int64(100),
TimeMicros: pb.Int64(100000000),
MetricSint64: pb.Int64(100),
Service: pb.String("foobar"),
Tags: []string{"hello"},
Expand All @@ -58,7 +61,7 @@ func TestEventToProtocolBuffer(t *testing.T) {
Service: "foobar",
Metric: 100.1,
Tags: []string{"hello"},
Time: 100,
Time: time.Unix(100, 0),
Ttl: 10,
Attributes: map[string]string{
"foo": "bar",
Expand All @@ -70,18 +73,19 @@ func TestEventToProtocolBuffer(t *testing.T) {
t.Error("Error during EventToProtocolBuffer")
}
protoTest = proto.Event{
Host: pb.String("baz"),
Time: pb.Int64(100),
MetricD: pb.Float64(100.1),
Service: pb.String("foobar"),
Tags: []string{"hello"},
Ttl: pb.Float32(10),
Host: pb.String("baz"),
Time: pb.Int64(100),
TimeMicros: pb.Int64(100000000),
MetricD: pb.Float64(100.1),
Service: pb.String("foobar"),
Tags: []string{"hello"},
Ttl: pb.Float32(10),
Attributes: []*proto.Attribute{
&proto.Attribute{
{
Key: pb.String("bar"),
Value: pb.String("baz"),
},
&proto.Attribute{
{
Key: pb.String("foo"),
Value: pb.String("bar"),
},
Expand All @@ -99,7 +103,7 @@ func TestEventToProtocolBuffer(t *testing.T) {
State: "critical",
Metric: 100,
Tags: []string{"hello"},
Time: 100,
Time: time.Unix(100, 0),
}
protoRes, error = EventToProtocolBuffer(&event)
if error != nil {
Expand All @@ -108,6 +112,7 @@ func TestEventToProtocolBuffer(t *testing.T) {
protoTest = proto.Event{
Host: pb.String("baz"),
Time: pb.Int64(100),
TimeMicros: pb.Int64(100000000),
Ttl: pb.Float32(20),
Description: pb.String("desc"),
State: pb.String("critical"),
Expand All @@ -127,7 +132,7 @@ func TestEventToProtocolBuffer(t *testing.T) {
State: "critical",
Metric: int64(100),
Tags: []string{"hello"},
Time: 100,
Time: time.Unix(100, 0),
}
protoRes, error = EventToProtocolBuffer(&event)
if error != nil {
Expand All @@ -136,6 +141,7 @@ func TestEventToProtocolBuffer(t *testing.T) {
protoTest = proto.Event{
Host: pb.String("baz"),
Time: pb.Int64(100),
TimeMicros: pb.Int64(100000000),
Ttl: pb.Float32(20),
Description: pb.String("desc"),
State: pb.String("critical"),
Expand All @@ -155,21 +161,22 @@ func TestEventToProtocolBuffer(t *testing.T) {
State: "critical",
Metric: float32(100.0),
Tags: []string{"hello"},
Time: 100,
Time: time.Unix(100, 0),
}
protoRes, error = EventToProtocolBuffer(&event)
if error != nil {
t.Error("Error during EventToProtocolBuffer")
}
protoTest = proto.Event{
Host: pb.String("baz"),
Time: pb.Int64(100),
Ttl: pb.Float32(20),
Description: pb.String("desc"),
State: pb.String("critical"),
MetricD: pb.Float64(100.0),
Service: pb.String("foobar"),
Tags: []string{"hello"},
Host: pb.String("baz"),
Time: pb.Int64(100),
TimeMicros: pb.Int64(100000000),
Ttl: pb.Float32(20),
Description: pb.String("desc"),
State: pb.String("critical"),
MetricD: pb.Float64(100.0),
Service: pb.String("foobar"),
Tags: []string{"hello"},
}
if !pb.Equal(protoRes, &protoTest) {
t.Error("Error during event to protobuf conversion")
Expand All @@ -183,7 +190,33 @@ func TestEventToProtocolBuffer(t *testing.T) {
State: "critical",
Metric: float64(100.12),
Tags: []string{"hello"},
Time: 100,
Time: time.Unix(100, 0),
}
protoRes, error = EventToProtocolBuffer(&event)
if error != nil {
t.Error("Error during EventToProtocolBuffer")
}
protoTest = proto.Event{
Host: pb.String("baz"),
Time: pb.Int64(100),
TimeMicros: pb.Int64(100000000),
Ttl: pb.Float32(20),
Description: pb.String("desc"),
State: pb.String("critical"),
MetricD: pb.Float64(100.12),
Service: pb.String("foobar"),
Tags: []string{"hello"},
}
if !pb.Equal(protoRes, &protoTest) {
t.Error("Error during event to protobuf conversion")
}
// simple event with time in nanosecond
event = Event{
Host: "baz",
Service: "foobar",
Metric: m,
Tags: []string{"hello"},
Time: time.Unix(100, 123456789),
}
protoRes, error = EventToProtocolBuffer(&event)
if error != nil {
Expand All @@ -192,10 +225,8 @@ func TestEventToProtocolBuffer(t *testing.T) {
protoTest = proto.Event{
Host: pb.String("baz"),
Time: pb.Int64(100),
Ttl: pb.Float32(20),
Description: pb.String("desc"),
State: pb.String("critical"),
MetricD: pb.Float64(100.12),
TimeMicros: pb.Int64(100123456),
MetricSint64: pb.Int64(100),
Service: pb.String("foobar"),
Tags: []string{"hello"},
}
Expand Down Expand Up @@ -249,7 +280,7 @@ func compareEvents(e1 *Event, e2 *Event, t *testing.T) {

func TestProtocolBuffersToEvents(t *testing.T) {
pbEvents := []*proto.Event{
&proto.Event{
{
Host: pb.String("baz"),
Time: pb.Int64(100),
Ttl: pb.Float32(20),
Expand All @@ -262,7 +293,7 @@ func TestProtocolBuffersToEvents(t *testing.T) {
}
event := Event{
Host: "baz",
Time: 100,
Time: time.Unix(100, 0),
Ttl: 20,
Description: "desc",
State: "critical",
Expand All @@ -273,7 +304,7 @@ func TestProtocolBuffersToEvents(t *testing.T) {
events := ProtocolBuffersToEvents(pbEvents)
compareEvents(&events[0], &event, t)
pbEvents = []*proto.Event{
&proto.Event{
{
Host: pb.String("baz"),
Time: pb.Int64(100),
Ttl: pb.Float32(20),
Expand All @@ -283,7 +314,7 @@ func TestProtocolBuffersToEvents(t *testing.T) {
Service: pb.String("foobar"),
Tags: []string{"hello"},
},
&proto.Event{
{
Host: pb.String("baz"),
Time: pb.Int64(100),
Ttl: pb.Float32(20),
Expand All @@ -293,11 +324,11 @@ func TestProtocolBuffersToEvents(t *testing.T) {
Service: pb.String("foobar"),
Tags: []string{"hello"},
Attributes: []*proto.Attribute{
&proto.Attribute{
{
Key: pb.String("foo"),
Value: pb.String("bar"),
},
&proto.Attribute{
{
Key: pb.String("bar"),
Value: pb.String("baz"),
},
Expand All @@ -306,7 +337,7 @@ func TestProtocolBuffersToEvents(t *testing.T) {
}
event1 := Event{
Host: "baz",
Time: 100,
Time: time.Unix(100, 0),
Ttl: 20,
Description: "desc",
State: "critical",
Expand All @@ -321,7 +352,7 @@ func TestProtocolBuffersToEvents(t *testing.T) {
State: "critical",
Metric: 100.1,
Tags: []string{"hello"},
Time: 100,
Time: time.Unix(100, 0),
Ttl: 20,
Attributes: map[string]string{
"foo": "bar",
Expand All @@ -331,4 +362,55 @@ func TestProtocolBuffersToEvents(t *testing.T) {
events = ProtocolBuffersToEvents(pbEvents)
compareEvents(&events[0], &event1, t)
compareEvents(&events[1], &event2, t)

pbEvents = []*proto.Event{
{
Host: pb.String("baz"),
Time: pb.Int64(100),
TimeMicros: pb.Int64(100123456),
Ttl: pb.Float32(20),
Description: pb.String("desc"),
State: pb.String("critical"),
MetricSint64: pb.Int64(100),
Service: pb.String("foobar"),
Tags: []string{"hello"},
},
}
event = Event{
Host: "baz",
Time: time.Unix(100, 123456000),
Ttl: 20,
Description: "desc",
State: "critical",
Metric: int64(100),
Service: "foobar",
Tags: []string{"hello"},
}
events = ProtocolBuffersToEvents(pbEvents)
compareEvents(&events[0], &event, t)

pbEvents = []*proto.Event{
{
Host: pb.String("baz"),
TimeMicros: pb.Int64(100123456),
Ttl: pb.Float32(20),
Description: pb.String("desc"),
State: pb.String("critical"),
MetricSint64: pb.Int64(100),
Service: pb.String("foobar"),
Tags: []string{"hello"},
},
}
event = Event{
Host: "baz",
Time: time.Unix(100, 123456000),
Ttl: 20,
Description: "desc",
State: "critical",
Metric: int64(100),
Service: "foobar",
Tags: []string{"hello"},
}
events = ProtocolBuffersToEvents(pbEvents)
compareEvents(&events[0], &event, t)
}
Loading

0 comments on commit 121dccf

Please sign in to comment.