Skip to content

Commit f74e0e6

Browse files
authored
Merge pull request #1114 from circleci/dm/timestamp-httpmetrics
Add timestamp to httpmetric
2 parents 6cbe68c + ad19cfc commit f74e0e6

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

o11y/httpmetrics/provider.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ type Provider struct {
3939
}
4040

4141
type metricData struct {
42-
Type string `json:"type"`
43-
Name string `json:"name"`
44-
Value float64 `json:"value"`
45-
Tags []string `json:"tags"`
42+
Type string `json:"type"`
43+
Name string `json:"name"`
44+
Timestamp int64 `json:"timestamp"`
45+
Value float64 `json:"value"`
46+
Tags []string `json:"tags"`
4647
}
4748

4849
type Tags map[string]string
@@ -124,10 +125,11 @@ func (m *Provider) record(metricType, metricName string, metricValue float64, me
124125
name = fmt.Sprintf("%s.%s", m.namespace, name)
125126
}
126127
m.data = append(m.data, metricData{
127-
Type: metricType,
128-
Name: name,
129-
Value: metricValue,
130-
Tags: metricTags,
128+
Type: metricType,
129+
Name: name,
130+
Timestamp: time.Now().Unix(),
131+
Value: metricValue,
132+
Tags: metricTags,
131133
})
132134
}
133135

o11y/httpmetrics/provider_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55
"time"
66

7+
gcmp "github.com/google/go-cmp/cmp"
78
"golang.org/x/sync/errgroup"
89
"gotest.tools/v3/assert"
910
"gotest.tools/v3/assert/cmp"
@@ -132,7 +133,11 @@ func TestProvider_Record(t *testing.T) {
132133
m.record(aMet.Type, aMet.Name, aMet.Value, aMet.Tags)
133134
}
134135

135-
assert.Check(t, cmp.DeepEqual(m.data, tt.expectedMetricsData))
136+
for i := range tt.expectedMetricsData {
137+
tt.expectedMetricsData[i].Timestamp = time.Now().Unix()
138+
}
139+
140+
assert.Check(t, cmp.DeepEqual(m.data, tt.expectedMetricsData, equateApproxUnixTime(1)))
136141
})
137142
}
138143
}
@@ -255,7 +260,11 @@ func TestProvider_Publish(t *testing.T) {
255260
err := eg.Wait()
256261
assert.NilError(t, err)
257262

258-
assert.Check(t, cmp.DeepEqual(m.data, tt.expectedMetricsData))
263+
for i := range tt.expectedMetricsData {
264+
tt.expectedMetricsData[i].Timestamp = time.Now().Unix()
265+
}
266+
267+
assert.Check(t, cmp.DeepEqual(m.data, tt.expectedMetricsData, equateApproxUnixTime(1)))
259268
})
260269
}
261270
}
@@ -353,3 +362,28 @@ func TestProvider_New(t *testing.T) {
353362
})
354363
}
355364
}
365+
366+
func equateApproxUnixTime(marginSec int64) gcmp.Option {
367+
if marginSec < 0 {
368+
panic("margin must be a non-negative number")
369+
}
370+
a := timeApproximator{marginSec}
371+
return gcmp.FilterValues(areNonZeroTimes, gcmp.Comparer(a.compare))
372+
}
373+
374+
func areNonZeroTimes(x, y int64) bool {
375+
return x > 0 && y > 0
376+
}
377+
378+
type timeApproximator struct {
379+
marginSec int64
380+
}
381+
382+
func (a timeApproximator) compare(x, y int64) bool {
383+
if x > y {
384+
// Ensure x is always before y
385+
x, y = y, x
386+
}
387+
// We're within the margin if x+margin >= y.
388+
return x+a.marginSec >= y
389+
}

0 commit comments

Comments
 (0)