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

Commit

Permalink
prometheus: Add UpdateFromSet to MetricSet
Browse files Browse the repository at this point in the history
Allows a set to updated by another.
  • Loading branch information
mjs committed Mar 27, 2018
1 parent 021fe84 commit 5cf0926
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions prometheus/metricset.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func (set *MetricSet) Update(m *Metric) {
set.metrics[metricKey(m)] = m
}

// UpdateFromSet updates the values in the set from another MetricSet.
func (set *MetricSet) UpdateFromSet(other *MetricSet) {
for _, m := range other.All() {
set.Update(m)
}
}

// ToBytes serialise the metrics contained in the MetricSet to the
// Prometheus exposition format.
func (set *MetricSet) ToBytes() []byte {
Expand Down
49 changes: 49 additions & 0 deletions prometheus/metricset_small_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,55 @@ func TestUpdateLabelOrdering(t *testing.T) {
assert.Equal(t, set.All(), []*prometheus.Metric{m1})
}

func TestUpdateFromSet(t *testing.T) {
// set0 has 2 metrics
set0 := prometheus.NewMetricSet()
m0 := &prometheus.Metric{
Name: []byte("uptime"),
Labels: prometheus.LabelPairs{
{Name: []byte("host"), Value: []byte("nyc01")},
},
Value: 222,
Milliseconds: 99,
}
set0.Update(m0)
set0.Update(&prometheus.Metric{
Name: []byte("temp"),
Labels: prometheus.LabelPairs{
{Name: []byte("host"), Value: []byte("nyc02")},
{Name: []byte("core"), Value: []byte("0")},
},
Value: 55,
Milliseconds: 100,
})

// set1 overwrites one item in set0 and introduces a new one.
set1 := prometheus.NewMetricSet()
m1 := &prometheus.Metric{
Name: []byte("temp"),
Labels: prometheus.LabelPairs{
{Name: []byte("host"), Value: []byte("nyc02")},
{Name: []byte("core"), Value: []byte("0")},
},
Value: 66,
Milliseconds: 111,
}
set1.Update(m1)
m2 := &prometheus.Metric{
Name: []byte("uptime"),
Labels: prometheus.LabelPairs{
{Name: []byte("host"), Value: []byte("nyc02")},
},
Value: 1234,
Milliseconds: 222,
}
set1.Update(m2)

set0.UpdateFromSet(set1)

assert.ElementsMatch(t, set0.All(), []*prometheus.Metric{m0, m1, m2})
}

func TestToBytes(t *testing.T) {
set := prometheus.NewMetricSet()

Expand Down

0 comments on commit 5cf0926

Please sign in to comment.