-
Notifications
You must be signed in to change notification settings - Fork 4
/
record.go
93 lines (81 loc) · 1.78 KB
/
record.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package promproxy
import (
"encoding/json"
"io"
"strconv"
"time"
"github.com/go-faster/errors"
"github.com/go-faster/oteldb/internal/promapi"
)
// Recorder of prometheus queries.
type Recorder struct {
encoder *json.Encoder
}
// NewRecorder returns new Recorder.
func NewRecorder(writer io.Writer) *Recorder {
e := json.NewEncoder(writer)
return &Recorder{encoder: e}
}
func (r *Recorder) encode(v any) error {
if r == nil {
return nil
}
var q Query
switch t := v.(type) {
case RangeQuery:
q.SetRangeQuery(t)
case InstantQuery:
q.SetInstantQuery(t)
case SeriesQuery:
q.SetSeriesQuery(t)
default:
return errors.Errorf("unknown type %T", t)
}
if err := r.encoder.Encode(q); err != nil {
return errors.Wrap(err, "encode")
}
return nil
}
func ts(v promapi.PrometheusTimestamp) time.Time {
seconds, err := strconv.ParseInt(string(v), 10, 64)
if err != nil {
return time.Time{}
}
return time.Unix(seconds, 0)
}
func step(v string) OptInt {
seconds, err := strconv.Atoi(v)
if err != nil {
return OptInt{}
}
return NewOptInt(seconds)
}
func optTS(v promapi.OptPrometheusTimestamp) OptDateTime {
var dt OptDateTime
if t, ok := v.Get(); ok {
dt.SetTo(ts(t))
}
return dt
}
func (r *Recorder) RecordSeriesQuery(v promapi.GetSeriesParams) error {
return r.encode(SeriesQuery{
Start: optTS(v.Start),
End: optTS(v.End),
Matchers: v.Match,
})
}
func (r *Recorder) RecordGetQuery(v promapi.GetQueryParams) error {
return r.encode(InstantQuery{
Query: v.Query,
Time: optTS(v.Time),
})
}
// RecordGetQueryRange records range query.
func (r *Recorder) RecordGetQueryRange(v promapi.GetQueryRangeParams) error {
return r.encode(RangeQuery{
Query: v.Query,
Start: NewOptDateTime(ts(v.Start)),
End: NewOptDateTime(ts(v.End)),
Step: step(v.Step),
})
}