Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Carbon Plaintext #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions carbon/datapoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package carbon

import (
"bytes"
"strconv"
)

const (
nextPart = " "
nextNode = "."
nextTag = ";"
nextTagValue = "="
nextDatapoint = "\n"
)

// Datapoint is a value stored at a timestamp bucket.
// If no value is recorded at a particular timestamp bucket in a series,
// the value will be None (null)
// Doc: https://graphite.readthedocs.io/en/latest/terminology.html
type Datapoint struct {
buffer *bytes.Buffer
value float64
timestamp int64
}

// NewDatapoint is a datapoint building function
func NewDatapoint(name string, value float64, timestamp int64) *Datapoint {
datapoint := &Datapoint{
buffer: &bytes.Buffer{},
value: value,
timestamp: timestamp,
}
datapoint.buffer.WriteString(name)
return datapoint
}

// WithPrefix adds provided prefix to datapoint
// Every function call extends datapoint path from start
// For example WithPrefix("second").WithPrefix("first") is
// equal to WithPrefix("first.second")
func (d *Datapoint) WithPrefix(prefix string) *Datapoint {
path := d.buffer.String()
d.buffer.Reset()
d.buffer.WriteString(prefix)
d.buffer.WriteString(nextNode)
d.buffer.WriteString(path)
return d
}

// WithTag adds provided tag=value pair to datapoint
// Every function call extends datapoint path
// For example WithTag("tag1","tag1Value").WithTag("tag2","tag2Value") adds ";tag1=tag1Value;tag2=tag2Value"
// While WithTag("tag2","tag2Value").WithTag("tag1","tag1Value") adds ";tag2=tag2Value;tag1=tag1Value"
func (d *Datapoint) WithTag(name, value string) *Datapoint {
d.buffer.WriteString(nextTag)
d.buffer.WriteString(name)
d.buffer.WriteString(nextTagValue)
d.buffer.WriteString(value)
return d
}

// String returns Graphite Plaintext record form of datapoint
func (d *Datapoint) String() string {
var (
value = strconv.FormatFloat(d.value, 'f', -1, 64)
timestamp = strconv.FormatInt(d.timestamp, 10)
)
d.buffer.WriteString(nextPart)
d.buffer.WriteString(value)
d.buffer.WriteString(nextPart)
d.buffer.WriteString(timestamp)
d.buffer.WriteString(nextDatapoint)
return d.buffer.String()
}
93 changes: 93 additions & 0 deletions carbon/datapoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package carbon

import "testing"

func Test_NewDatapoint(t *testing.T) {
testCases := []struct {
actual string
expected string
}{
{
actual: NewDatapoint("metric1", 3.14159265359, 1552503600).String(),
expected: "metric1 3.14159265359 1552503600\n",
},
{
actual: NewDatapoint("metric1", 3.14159265359, 1552503600).
WithPrefix("foo").
String(),
expected: "foo.metric1 3.14159265359 1552503600\n",
},
{
actual: NewDatapoint("metric1", 3.14159265359, 1552503600).
WithPrefix("foo").
WithPrefix("bar").
String(),
expected: "bar.foo.metric1 3.14159265359 1552503600\n",
},
{
actual: NewDatapoint("metric1", 3.14159265359, 1552503600).
WithPrefix("foo").
WithPrefix("bar").
WithTag("cpu", "cpu1").
String(),
expected: "bar.foo.metric1;cpu=cpu1 3.14159265359 1552503600\n",
},
{
actual: NewDatapoint("metric1", 3.14159265359, 1552503600).
WithTag("cpu", "cpu1").
WithPrefix("foo").
WithPrefix("bar").
String(),
expected: "bar.foo.metric1;cpu=cpu1 3.14159265359 1552503600\n",
},
{
actual: NewDatapoint("metric1", 3.14159265359, 1552503600).
WithPrefix("foo").
WithPrefix("bar").
WithTag("cpu", "cpu1").
WithTag("dc", "dc1").
String(),
expected: "bar.foo.metric1;cpu=cpu1;dc=dc1 3.14159265359 1552503600\n",
},
{
actual: NewDatapoint("metric1", 3.14159265359, 1552503600).
WithTag("cpu", "cpu1").
WithTag("dc", "dc1").
WithPrefix("foo").
WithPrefix("bar").
String(),
expected: "bar.foo.metric1;cpu=cpu1;dc=dc1 3.14159265359 1552503600\n",
},
}

for _, testCase := range testCases {
if testCase.actual != testCase.expected {
t.Fatalf("expected: %s\nactual: %s", testCase.expected, testCase.actual)
}
}
}

func BenchmarkDatapoint_String(b *testing.B) {
for i := 0; i < b.N; i++ {
NewDatapoint("metric1", 3.14159265359, 1552503600).
WithPrefix("foo").
WithPrefix("bar").
WithTag("cpu", "cpu1").
WithTag("dc", "dc1").
WithTag("env", "stage").
WithTag("service", "exporter").
WithTag("version", "v0.0.1").
WithTag("git-commit", "0b1a09c").
String()
}
}

func BenchmarkDatapoint_StringByName(b *testing.B) {
for i := 0; i < b.N; i++ {
NewDatapoint(
"bar.foo.metric1;cpu=cpu1;dc=dc1;env=stage;service=exporter;version=v0.0.1;git-commit=0b1a09c",
3.14159265359,
1552503600).
String()
}
}