/
receiver.go
296 lines (273 loc) · 8.49 KB
/
receiver.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package loki
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/openshift/cluster-logging-operator/internal/utils"
"github.com/openshift/cluster-logging-operator/internal/runtime"
log "github.com/ViaQ/logerr/v2/log/static"
openshiftv1 "github.com/openshift/api/route/v1"
"github.com/openshift/cluster-logging-operator/test"
"github.com/openshift/cluster-logging-operator/test/client"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
Image = "grafana/loki:2.8.4"
Port = int32(3100)
lokiReceiver = "loki-receiver"
)
// Receiver is a service running loki in single-process mode.
type Receiver struct {
Name string
Pod *corev1.Pod
service *corev1.Service
route *openshiftv1.Route // TODO use k8s Ingress instead?
ready chan struct{}
timeout time.Duration
}
// NewReceiver creates a Receiver to run Loki in single-process mode.
func NewReceiver(ns, name string) *Receiver {
r := &Receiver{
Name: name,
Pod: runtime.NewPod(ns, name),
service: runtime.NewService(ns, name),
route: runtime.NewRoute(ns, name, name, "loki"),
ready: make(chan struct{}),
}
runtime.Labels(r.Pod)[lokiReceiver] = name
r.Pod.Spec.Containers = []corev1.Container{{
Name: name,
Image: Image,
Ports: []corev1.ContainerPort{{Name: name, ContainerPort: Port}},
Args: []string{
"-config.file=/etc/loki/local-config.yaml",
"-print-config-stderr=true",
"-server.grpc-max-recv-msg-size-bytes", "20971520",
"-distributor.ingestion-rate-limit-mb", "200",
"-distributor.ingestion-burst-size-mb", "200",
},
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: utils.GetPtr(false),
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "data",
MountPath: "/loki",
},
},
}}
r.Pod.Spec.SecurityContext = &corev1.PodSecurityContext{
RunAsNonRoot: utils.GetPtr(true),
RunAsUser: utils.GetPtr[int64](10001), // uid of user loki in the loki image
SeccompProfile: &corev1.SeccompProfile{
Type: corev1.SeccompProfileTypeRuntimeDefault,
},
}
r.Pod.Spec.Volumes = append(r.Pod.Spec.Volumes, corev1.Volume{
Name: "data",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
})
r.service.Spec = corev1.ServiceSpec{
Selector: map[string]string{lokiReceiver: name},
Ports: []corev1.ServicePort{{Name: "loki", Port: Port}},
}
return r
}
// Create the receiver's resources. Blocks till created.
func (r *Receiver) Create(c *client.Client) error {
r.timeout = c.Timeout()
g := errgroup.Group{}
for _, o := range []crclient.Object{r.Pod, r.service, r.route} {
if err := c.Create(o); err != nil {
return err
}
}
g.Go(func() error { return c.WaitFor(r.Pod, client.PodRunning) })
g.Go(func() error { return c.WaitFor(r.route, client.RouteReady) })
if err := g.Wait(); err != nil {
return err
}
// Wait till we can get the metrics page, means server is up.
return wait.PollUntilContextTimeout(context.TODO(), time.Second, r.timeout, true, func(ctx context.Context) (done bool, err error) {
req, _ := http.NewRequestWithContext(ctx, "GET", r.ExternalURL("/metrics").String(), nil)
client := &http.Client{}
resp, err := client.Do(req)
if err == nil {
defer resp.Body.Close()
err = test.HTTPError(resp)
}
return err == nil, nil
})
}
// Delete the receiver's resources. Blocks till created.
func (r *Receiver) Delete(c *client.Client) error {
r.timeout = c.Timeout()
for _, o := range []crclient.Object{r.Pod, r.service, r.route} {
if err := c.Delete(o); err != nil {
return err
}
}
return nil
}
// ExternalURL returns the URL of the external route. Only valid after Create()
func (r *Receiver) ExternalURL(path string) *url.URL {
return &url.URL{Scheme: "http", Host: r.route.Spec.Host, Path: path}
}
// InternalURL returns the internal svc.cluster.local URL
func (r *Receiver) InternalURL(path string) *url.URL {
host := runtime.SvcClusterLocal(r.service.Namespace, r.service.Name)
return &url.URL{Scheme: "http", Host: fmt.Sprintf("%v:%v", host, Port), Path: path}
}
// Query from outside cluster for logs matching logQL query expression.
// Returns up to limit values.
func (r *Receiver) Query(logQL string, orgID string, limit int) ([]StreamValues, error) {
u := r.ExternalURL("/loki/api/v1/query_range")
q := url.Values{}
q.Add("query", logQL)
q.Add("limit", strconv.Itoa(limit))
q.Add("direction", "FORWARD")
u.RawQuery = q.Encode()
log.V(3).Info("Loki Query", "url", u.String(), "org-id", orgID)
header := http.Header{}
if orgID != "" {
header.Add("X-Scope-OrgID", orgID)
}
req := &http.Request{
Method: "GET",
URL: u,
Header: header,
}
resp, err := http.DefaultClient.Do(req)
if err == nil {
err = test.HTTPError(resp)
}
if err != nil {
log.V(3).Error(err, "Loki Query", "url", u.String())
return nil, fmt.Errorf("%w\nURL: %v", err, u)
}
defer resp.Body.Close()
qr := QueryResponse{}
if err = json.NewDecoder(resp.Body).Decode(&qr); err != nil {
return nil, err
}
if qr.Status != "success" {
return nil, fmt.Errorf("expected 'status: success' in %v", qr)
}
if qr.Data.ResultType != "streams" {
return nil, fmt.Errorf("expected 'resultType: streams' in %v", qr)
}
log.V(3).Info("Loki Query done", "result", test.JSONString(qr.Data.Result))
return qr.Data.Result, nil
}
// QueryUntil repeats the query until at least n lines are received.
func (r *Receiver) QueryUntil(logQL string, orgID string, n int) (values []StreamValues, err error) {
log.V(2).Info("Loki QueryUntil", "query", logQL, "n", n)
err = wait.PollUntilContextTimeout(context.TODO(), time.Second, r.timeout, true, func(cxt context.Context) (done bool, err error) {
values, err = r.Query(logQL, orgID, n)
if err != nil {
return false, err
}
got := 0
for _, v := range values {
got += len(v.Values)
}
return got >= n, nil
})
return values, errors.Wrap(err, fmt.Sprintf("waiting for loki query %q, orgID %q", logQL, orgID))
}
// QueryResponse is the response to a loki query.
type QueryResponse struct {
Status string `json:"status"`
Data QueryData `json:"data"`
}
// QueryData holds the data for a query
type QueryData struct {
ResultType string `json:"resultType"`
Result []StreamValues `json:"result"`
}
// StreamValues is a set of log values ["time", "line"] for a log stream.
type StreamValues struct {
Stream map[string]string `json:"stream"`
Values [][]string `json:"values"`
}
// Lines extracts all the log lines from a QueryResult
func (sv StreamValues) Lines() (lines []string) {
for _, l := range sv.Values { // Values are ["time", "line"]
lines = append(lines, l[1])
}
return lines
}
// Records extracts log lines and parses as JSON maps.
// Lines that are not valid JSON are are returned as: {"INVALID <error-message>": "original line"}
func (sv StreamValues) Records() (records []map[string]interface{}) {
for _, l := range sv.Lines() {
m := map[string]interface{}{}
if err := json.Unmarshal([]byte(l), &m); err != nil {
m["INVALID "+err.Error()] = l
}
records = append(records, m)
}
return records
}
// MakeValue returns a [timestamp, line] pair.
func MakeValue(t time.Time, line string) []string {
return []string{fmt.Sprintf("%v", t.UnixNano()), line}
}
// MakeValues takes a slice of entries and returns a slice of [timestamp,line] values.
func MakeValues(lines []string) (values [][]string) {
t := time.Now()
for _, e := range lines {
values = append(values, MakeValue(t, e))
}
return values
}
type labelResponse struct {
Status string `json:"status"`
Data []string `json:"data"`
}
func (r *Receiver) Labels() ([]string, error) {
u := r.ExternalURL("/loki/api/v1/labels")
resp, err := http.Get(u.String())
if err == nil {
err = test.HTTPError(resp)
}
if err != nil {
return nil, fmt.Errorf("get %q: %w", u, err)
}
defer resp.Body.Close()
lr := labelResponse{}
if err := json.NewDecoder(resp.Body).Decode(&lr); err != nil {
return nil, err
}
return lr.Data, nil
}
func (r *Receiver) Push(sv ...StreamValues) error {
u := r.ExternalURL("/loki/api/v1/push")
b, err := json.Marshal(map[string][]StreamValues{"streams": sv})
if err != nil {
return err
}
resp, err := http.Post(u.String(), "application/json", bytes.NewReader(b))
if err == nil {
err = test.HTTPError(resp)
}
if err != nil {
return fmt.Errorf("post %q: %w", u, err)
}
resp.Body.Close()
return nil
}