This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from justinfoote/add_events
Add custom Events; refactor Span Events and custom Events to use common struct
- Loading branch information
Showing
13 changed files
with
515 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
module github.com/newrelic/newrelic-telemetry-sdk-go | ||
|
||
go 1.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package telemetry | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/newrelic/newrelic-telemetry-sdk-go/internal" | ||
) | ||
|
||
// Event is a unique set of data that happened at a specific point in time | ||
type Event struct { | ||
// Required Fields: | ||
// | ||
// EventType is the name of the event | ||
EventType string | ||
// Timestamp is when this event happened. If Timestamp is not set, it | ||
// will be assigned to time.Now() in Harvester.RecordEvent. | ||
Timestamp time.Time | ||
|
||
// Recommended Fields: | ||
// | ||
// Attributes is a map of user specified data on this event. The map | ||
// values can be any of bool, number, or string. | ||
Attributes map[string]interface{} | ||
// AttributesJSON is a json.RawMessage of attributes for this metric. It | ||
// will only be sent if Attributes is nil. | ||
AttributesJSON json.RawMessage | ||
} | ||
|
||
func (e *Event) writeJSON(buf *bytes.Buffer) { | ||
w := internal.JSONFieldsWriter{Buf: buf} | ||
buf.WriteByte('{') | ||
|
||
w.StringField("eventType", e.EventType) | ||
w.IntField("timestamp", e.Timestamp.UnixNano()/(1000*1000)) | ||
|
||
internal.AddAttributes(&w, e.Attributes) | ||
|
||
buf.WriteByte('}') | ||
} | ||
|
||
// eventBatch represents a single batch of events to report to New Relic. | ||
type eventBatch struct { | ||
Events []Event | ||
} | ||
|
||
// split will split the eventBatch into 2 equally sized batches. | ||
// If the number of events in the original is 0 or 1 then nil is returned. | ||
func (batch *eventBatch) split() []requestsBuilder { | ||
if len(batch.Events) < 2 { | ||
return nil | ||
} | ||
|
||
half := len(batch.Events) / 2 | ||
b1 := *batch | ||
b1.Events = batch.Events[:half] | ||
b2 := *batch | ||
b2.Events = batch.Events[half:] | ||
|
||
return []requestsBuilder{ | ||
requestsBuilder(&b1), | ||
requestsBuilder(&b2), | ||
} | ||
} | ||
|
||
func (batch *eventBatch) writeJSON(buf *bytes.Buffer) { | ||
buf.WriteByte('[') | ||
for idx, s := range batch.Events { | ||
if idx > 0 { | ||
buf.WriteByte(',') | ||
} | ||
s.writeJSON(buf) | ||
} | ||
buf.WriteByte(']') | ||
} | ||
|
||
func (batch *eventBatch) makeBody() json.RawMessage { | ||
buf := &bytes.Buffer{} | ||
batch.writeJSON(buf) | ||
return buf.Bytes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2019 New Relic Corporation. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package telemetry | ||
|
||
import ( | ||
"io/ioutil" | ||
"testing" | ||
"time" | ||
|
||
"github.com/newrelic/newrelic-telemetry-sdk-go/internal" | ||
) | ||
|
||
func testEventBatchJSON(t testing.TB, batch *eventBatch, expect string) { | ||
if th, ok := t.(interface{ Helper() }); ok { | ||
th.Helper() | ||
} | ||
reqs, err := newRequests(batch, "apiKey", defaultSpanURL, "userAgent") | ||
if nil != err { | ||
t.Fatal(err) | ||
} | ||
if len(reqs) != 1 { | ||
t.Fatal(reqs) | ||
} | ||
req := reqs[0] | ||
actual := string(req.UncompressedBody) | ||
compact := compactJSONString(expect) | ||
if actual != compact { | ||
t.Errorf("\nexpect=%s\nactual=%s\n", compact, actual) | ||
} | ||
|
||
body, err := ioutil.ReadAll(req.Request.Body) | ||
req.Request.Body.Close() | ||
if err != nil { | ||
t.Fatal("unable to read body", err) | ||
} | ||
if len(body) != req.compressedBodyLength { | ||
t.Error("compressed body length mismatch", | ||
len(body), req.compressedBodyLength) | ||
} | ||
uncompressed, err := internal.Uncompress(body) | ||
if err != nil { | ||
t.Fatal("unable to uncompress body", err) | ||
} | ||
if string(uncompressed) != string(req.UncompressedBody) { | ||
t.Error("request JSON mismatch", string(uncompressed), string(req.UncompressedBody)) | ||
} | ||
} | ||
|
||
func TestEventsPayloadSplit(t *testing.T) { | ||
t.Parallel() | ||
|
||
// test len 0 | ||
ev := &eventBatch{} | ||
split := ev.split() | ||
if split != nil { | ||
t.Error(split) | ||
} | ||
|
||
// test len 1 | ||
ev = &eventBatch{Events: []Event{{EventType: "a"}}} | ||
split = ev.split() | ||
if split != nil { | ||
t.Error(split) | ||
} | ||
|
||
// test len 2 | ||
ev = &eventBatch{Events: []Event{{EventType: "a"}, {EventType: "b"}}} | ||
split = ev.split() | ||
if len(split) != 2 { | ||
t.Error("split into incorrect number of slices", len(split)) | ||
} | ||
|
||
testEventBatchJSON(t, split[0].(*eventBatch), `[{"eventType":"a","timestamp":-6795364578871}]`) | ||
testEventBatchJSON(t, split[1].(*eventBatch), `[{"eventType":"b","timestamp":-6795364578871}]`) | ||
|
||
// test len 3 | ||
ev = &eventBatch{Events: []Event{{EventType: "a"}, {EventType: "b"}, {EventType: "c"}}} | ||
split = ev.split() | ||
if len(split) != 2 { | ||
t.Error("split into incorrect number of slices", len(split)) | ||
} | ||
testEventBatchJSON(t, split[0].(*eventBatch), `[{"eventType":"a","timestamp":-6795364578871}]`) | ||
testEventBatchJSON(t, split[1].(*eventBatch), `[{"eventType":"b","timestamp":-6795364578871},{"eventType":"c","timestamp":-6795364578871}]`) | ||
} | ||
|
||
func TestEventsJSON(t *testing.T) { | ||
t.Parallel() | ||
|
||
batch := &eventBatch{Events: []Event{ | ||
{}, // Empty | ||
{ // with everything | ||
EventType: "testEvent", | ||
Timestamp: time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC), | ||
Attributes: map[string]interface{}{"zip": "zap"}, | ||
}, | ||
}} | ||
|
||
testEventBatchJSON(t, batch, `[ | ||
{ | ||
"eventType":"", | ||
"timestamp":-6795364578871 | ||
}, | ||
{ | ||
"eventType":"testEvent", | ||
"timestamp":1417136460000, | ||
"zip":"zap" | ||
} | ||
]`) | ||
} |
Oops, something went wrong.