-
Notifications
You must be signed in to change notification settings - Fork 2
/
labels.go
67 lines (56 loc) · 1.37 KB
/
labels.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
package loghttp
import (
"bytes"
"net/http"
"sort"
"strconv"
"github.com/gorilla/mux"
"github.com/credativ/vali/pkg/logproto"
)
// LabelResponse represents the http json response to a label query
type LabelResponse struct {
Status string `json:"status"`
Data []string `json:"data,omitempty"`
}
// LabelSet is a key/value pair mapping of labels
type LabelSet map[string]string
// Map coerces LabelSet into a map[string]string. This is useful for working with adapter types.
func (l LabelSet) Map() map[string]string {
return l
}
// String implements the Stringer interface. It returns a formatted/sorted set of label key/value pairs.
func (l LabelSet) String() string {
var b bytes.Buffer
keys := make([]string, 0, len(l))
for k := range l {
keys = append(keys, k)
}
sort.Strings(keys)
b.WriteByte('{')
for i, k := range keys {
if i > 0 {
b.WriteByte(',')
b.WriteByte(' ')
}
b.WriteString(k)
b.WriteByte('=')
b.WriteString(strconv.Quote(l[k]))
}
b.WriteByte('}')
return b.String()
}
// ParseLabelQuery parses a LabelRequest request from an http request.
func ParseLabelQuery(r *http.Request) (*logproto.LabelRequest, error) {
name, ok := mux.Vars(r)["name"]
req := &logproto.LabelRequest{
Values: ok,
Name: name,
}
start, end, err := bounds(r)
if err != nil {
return nil, err
}
req.Start = &start
req.End = &end
return req, nil
}