forked from grafana/k6
/
wrapper.go
48 lines (42 loc) · 1.21 KB
/
wrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package json
import (
"time"
"go.k6.io/k6/metrics"
)
//go:generate easyjson -pkg -no_std_marshalers -gen_build_flags -mod=mod .
//easyjson:json
type sampleEnvelope struct {
Metric string `json:"metric"`
Type string `json:"type"`
Data struct {
Time time.Time `json:"time"`
Value float64 `json:"value"`
Tags *metrics.TagSet `json:"tags"`
Metadata map[string]string `json:"metadata,omitempty"`
} `json:"data"`
}
// wrapSample is used to package a metric sample in a way that's nice to export
// to JSON and backwards-compatible.
func wrapSample(sample metrics.Sample) sampleEnvelope {
s := sampleEnvelope{
Type: "Point",
Metric: sample.Metric.Name,
}
s.Data.Time = sample.Time
s.Data.Value = sample.Value
s.Data.Tags = sample.Tags
s.Data.Metadata = sample.Metadata
return s
}
//easyjson:json
type metricEnvelope struct {
Type string `json:"type"`
Data struct {
Name string `json:"name"`
Type metrics.MetricType `json:"type"`
Contains metrics.ValueType `json:"contains"`
Thresholds metrics.Thresholds `json:"thresholds"`
Submetrics []*metrics.Submetric `json:"submetrics"`
} `json:"data"`
Metric string `json:"metric"`
}