Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
use bytes.Buffer instead of string for path, refactor tests, make exa…
…mples more carbonapi-sh, add benchmarks
  • Loading branch information
kamaev committed Jul 11, 2021
commit 422223f56c3d223004ab5ef6239bf0d4ee3570e5
37 changes: 27 additions & 10 deletions carbon/datapoint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package carbon

import "strconv"
import (
"bytes"
"strconv"
)

const (
nextPart = " "
Expand All @@ -12,29 +15,35 @@ const (

// 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).
// the value will be None (null)
// Doc: https://graphite.readthedocs.io/en/latest/terminology.html
type Datapoint struct {
path string
path *bytes.Buffer
value float64
timestamp int64
}

// NewDatapoint is a datapoint building function
func NewDatapoint(name string, value float64, timestamp int64) *Datapoint {
return &Datapoint{
path: name,
datapoint := &Datapoint{
path: &bytes.Buffer{},
value: value,
timestamp: timestamp,
}
datapoint.path.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 {
d.path = prefix + nextNode + d.path
path := d.path.String()
d.path.Reset()
d.path.WriteString(prefix)
d.path.WriteString(nextNode)
d.path.WriteString(path)
return d
}

Expand All @@ -43,15 +52,23 @@ func (d *Datapoint) WithPrefix(prefix string) *Datapoint {
// 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.path += nextTag + name + nextTagValue + value
d.path.WriteString(nextTag)
d.path.WriteString(name)
d.path.WriteString(nextTagValue)
d.path.WriteString(value)
return d
}

// Plaintext returns Graphite Plaintext record form of datapoint
func (d *Datapoint) Plaintext() string {
// 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)
)
return d.path + nextPart + value + nextPart + timestamp + nextDatapoint
d.path.WriteString(nextPart)
d.path.WriteString(value)
d.path.WriteString(nextPart)
d.path.WriteString(timestamp)
d.path.WriteString(nextDatapoint)
return d.path.String()
}
173 changes: 83 additions & 90 deletions carbon/datapoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,98 +3,91 @@ package carbon
import "testing"

func Test_NewDatapoint(t *testing.T) {
const (
testName = "name"
testPrefixParentNode = "parent"
testPrefixChildNode = "child"
testFirstTagName = "firstTag"
testFirstTagValue = "firstTagValue"
testSecondTagName = "secondTag"
testSecondTagValue = "secondTagValue"
testValue = 3.14159265359
testTimestamp = 1552503600
)
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",
},
}

t.Run("simple", func(t *testing.T) {
var (
actual = NewDatapoint(testName, testValue, testTimestamp).Plaintext()
expected = "name 3.14159265359 1552503600\n"
)
if actual != expected {
t.Fatalf("expected: %s\nactual: %s", expected, actual)
for _, testCase := range testCases {
if testCase.actual != testCase.expected {
t.Fatalf("expected: %s\nactual: %s", testCase.expected, testCase.actual)
}
})

t.Run("with prefix", func(t *testing.T) {
var (
actual = NewDatapoint(testName, testValue, testTimestamp).
WithPrefix(testPrefixParentNode).
Plaintext()
expected = "parent.name 3.14159265359 1552503600\n"
)
if actual != expected {
t.Fatalf("expected: %s\nactual: %s", expected, actual)
}
})

t.Run("with nested prefix", func(t *testing.T) {
var (
actual = NewDatapoint(testName, testValue, testTimestamp).
WithPrefix(testPrefixChildNode).
WithPrefix(testPrefixParentNode).
Plaintext()
expected = "parent.child.name 3.14159265359 1552503600\n"
)
if actual != expected {
t.Fatalf("expected: %s\nactual: %s", expected, actual)
}
})
}
}

t.Run("with nested prefix and tag", func(t *testing.T) {
var (
recordForms = []string{
NewDatapoint(testName, testValue, testTimestamp).
WithPrefix(testPrefixChildNode).
WithPrefix(testPrefixParentNode).
WithTag(testFirstTagName, testFirstTagValue).
Plaintext(),
NewDatapoint(testName, testValue, testTimestamp).
WithTag(testFirstTagName, testFirstTagValue).
WithPrefix(testPrefixChildNode).
WithPrefix(testPrefixParentNode).
Plaintext(),
}
expected = "parent.child.name;firstTag=firstTagValue 3.14159265359 1552503600\n"
)
for _, actual := range recordForms {
if actual != expected {
t.Fatalf("expected: %s\nactual: %s", expected, 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()
}
}

t.Run("with nested prefix and tags", func(t *testing.T) {
var (
recordForms = []string{
NewDatapoint(testName, testValue, testTimestamp).
WithPrefix(testPrefixChildNode).
WithPrefix(testPrefixParentNode).
WithTag(testFirstTagName, testFirstTagValue).
WithTag(testSecondTagName, testSecondTagValue).
Plaintext(),
NewDatapoint(testName, testValue, testTimestamp).
WithTag(testFirstTagName, testFirstTagValue).
WithTag(testSecondTagName, testSecondTagValue).
WithPrefix(testPrefixChildNode).
WithPrefix(testPrefixParentNode).
Plaintext(),
}
expected = "parent.child.name;firstTag=firstTagValue;secondTag=secondTagValue 3.14159265359 1552503600\n"
)
for _, actual := range recordForms {
if actual != expected {
t.Fatalf("expected: %s\nactual: %s", expected, actual)
}
}
})
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()
}
}