Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Implement turning metrics into Promethesus protocol lines
Browse files Browse the repository at this point in the history
  • Loading branch information
mjs committed Mar 20, 2018
1 parent 4cded0c commit 3f384e7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 18 deletions.
64 changes: 46 additions & 18 deletions monitor/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,11 @@ package monitor
import (
"bytes"
"errors"
"fmt"

"github.com/jumptrading/influx-spout/convert"
)

// Metric represents a single Prometheus metric line, including its
// labels and timestamp.
type Metric struct {
Name []byte
Labels LabelPairs
Value int64
Milliseconds int64
}

// LabelPairs contains the set of labels for a metric.
type LabelPairs []LabelPair

// LabelPair contains a label name and value.
type LabelPair struct {
Name []byte
Value []byte
}

// ParseMetric parses a single Promethesus metric line.
//
// Note: The implementation currently only supports integer values and
Expand Down Expand Up @@ -127,3 +110,48 @@ func parseLabels(s []byte) (LabelPairs, int, error) {
}
}
}

// Metric represents a single Prometheus metric line, including its
// labels and timestamp.
type Metric struct {
Name []byte
Labels LabelPairs
Value int64
Milliseconds int64
}

// ToBytes renders the metric to wire format.
func (m *Metric) ToBytes() []byte {
out := bytes.NewBuffer(m.Name)
if len(m.Labels) > 0 {
out.Write(m.Labels.ToBytes())
}
fmt.Fprintf(out, " %d", m.Value)
if m.Milliseconds > 0 {
fmt.Fprintf(out, " %d", m.Milliseconds)
}
return out.Bytes()
}

// LabelPairs contains the set of labels for a metric.
type LabelPairs []LabelPair

// ToBytes renders the label name and value to wire format.
func (p LabelPairs) ToBytes() []byte {
out := new(bytes.Buffer)
out.WriteByte('{')
for i, label := range p {
fmt.Fprintf(out, `%s="%s"`, label.Name, label.Value)
if i < len(p)-1 {
out.WriteByte(',')
}
}
out.WriteByte('}')
return out.Bytes()
}

// LabelPair contains a label name and value.
type LabelPair struct {
Name []byte
Value []byte
}
36 changes: 36 additions & 0 deletions monitor/prometheus_small_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,39 @@ func TestLabelsAndTimestamp(t *testing.T) {
Milliseconds: 123456789,
}, m)
}

func TestMetricBytesMinimal(t *testing.T) {
m := &monitor.Metric{
Name: []byte("foo"),
Value: 42,
}
assert.Equal(t, []byte("foo 42"), m.ToBytes())
}

func TestMetricBytesTimestamp(t *testing.T) {
m := &monitor.Metric{
Name: []byte("foo"),
Value: 42,
Milliseconds: 123456,
}
assert.Equal(t, []byte("foo 42 123456"), m.ToBytes())
}

func TestMetricBytesLabels(t *testing.T) {
m := &monitor.Metric{
Name: []byte("foo"),
Labels: monitor.LabelPairs{
{
Name: []byte("host"),
Value: []byte("nyc01"),
},
{
Name: []byte("thing"),
Value: []byte("forgot"),
},
},
Value: 42,
Milliseconds: 123456,
}
assert.Equal(t, []byte(`foo{host="nyc01",thing="forgot"} 42 123456`), m.ToBytes())
}

0 comments on commit 3f384e7

Please sign in to comment.