-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
push.go
309 lines (258 loc) · 10.1 KB
/
push.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
297
298
299
300
301
302
303
304
305
306
307
308
309
package push
import (
"compress/flate"
"compress/gzip"
"fmt"
"io"
"math"
"mime"
"net/http"
"time"
"github.com/go-kit/log/level"
"github.com/grafana/loki/pkg/push"
"github.com/dustin/go-humanize"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/prometheus/model/labels"
loki_util "github.com/grafana/loki/v3/pkg/util"
"github.com/grafana/loki/v3/pkg/analytics"
"github.com/grafana/loki/v3/pkg/loghttp"
"github.com/grafana/loki/v3/pkg/logproto"
"github.com/grafana/loki/v3/pkg/logql/syntax"
"github.com/grafana/loki/v3/pkg/util"
"github.com/grafana/loki/v3/pkg/util/constants"
"github.com/grafana/loki/v3/pkg/util/unmarshal"
unmarshal2 "github.com/grafana/loki/v3/pkg/util/unmarshal/legacy"
)
var (
contentType = http.CanonicalHeaderKey("Content-Type")
contentEnc = http.CanonicalHeaderKey("Content-Encoding")
bytesIngested = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: constants.Loki,
Name: "distributor_bytes_received_total",
Help: "The total number of uncompressed bytes received per tenant. Includes structured metadata bytes.",
}, []string{"tenant", "retention_hours", "aggregated_metric"})
structuredMetadataBytesIngested = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: constants.Loki,
Name: "distributor_structured_metadata_bytes_received_total",
Help: "The total number of uncompressed bytes received per tenant for entries' structured metadata",
}, []string{"tenant", "retention_hours", "aggregated_metric"})
linesIngested = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: constants.Loki,
Name: "distributor_lines_received_total",
Help: "The total number of lines received per tenant",
}, []string{"tenant", "aggregated_metric"})
bytesReceivedStats = analytics.NewCounter("distributor_bytes_received")
structuredMetadataBytesReceivedStats = analytics.NewCounter("distributor_structured_metadata_bytes_received")
linesReceivedStats = analytics.NewCounter("distributor_lines_received")
)
const (
applicationJSON = "application/json"
LabelServiceName = "service_name"
ServiceUnknown = "unknown_service"
AggregatedMetricLabel = "__aggregated_metric__"
)
type TenantsRetention interface {
RetentionPeriodFor(userID string, lbs labels.Labels) time.Duration
}
type Limits interface {
OTLPConfig(userID string) OTLPConfig
DiscoverServiceName(userID string) []string
}
type EmptyLimits struct{}
func (EmptyLimits) OTLPConfig(string) OTLPConfig {
return DefaultOTLPConfig(GlobalOTLPConfig{})
}
func (EmptyLimits) DiscoverServiceName(string) []string {
return nil
}
type (
RequestParser func(userID string, r *http.Request, tenantsRetention TenantsRetention, limits Limits, tracker UsageTracker, logPushRequestStreams bool, logger log.Logger) (*logproto.PushRequest, *Stats, error)
RequestParserWrapper func(inner RequestParser) RequestParser
)
type Stats struct {
Errs []error
NumLines int64
LogLinesBytes map[time.Duration]int64
StructuredMetadataBytes map[time.Duration]int64
ResourceAndSourceMetadataLabels map[time.Duration]push.LabelsAdapter
StreamLabelsSize int64
MostRecentEntryTimestamp time.Time
ContentType string
ContentEncoding string
BodySize int64
// Extra is a place for a wrapped perser to record any interesting stats as key-value pairs to be logged
Extra []any
IsAggregatedMetric bool
}
func ParseRequest(logger log.Logger, userID string, r *http.Request, tenantsRetention TenantsRetention, limits Limits, pushRequestParser RequestParser, tracker UsageTracker, logPushRequestStreams bool) (*logproto.PushRequest, error) {
req, pushStats, err := pushRequestParser(userID, r, tenantsRetention, limits, tracker, logPushRequestStreams, logger)
if err != nil {
return nil, err
}
var (
entriesSize int64
structuredMetadataSize int64
)
isAggregatedMetric := fmt.Sprintf("%t", pushStats.IsAggregatedMetric)
for retentionPeriod, size := range pushStats.LogLinesBytes {
retentionHours := RetentionPeriodToString(retentionPeriod)
bytesIngested.WithLabelValues(userID, retentionHours, isAggregatedMetric).Add(float64(size))
bytesReceivedStats.Inc(size)
entriesSize += size
}
for retentionPeriod, size := range pushStats.StructuredMetadataBytes {
retentionHours := RetentionPeriodToString(retentionPeriod)
structuredMetadataBytesIngested.WithLabelValues(userID, retentionHours, isAggregatedMetric).Add(float64(size))
bytesIngested.WithLabelValues(userID, retentionHours, isAggregatedMetric).Add(float64(size))
bytesReceivedStats.Inc(size)
structuredMetadataBytesReceivedStats.Inc(size)
entriesSize += size
structuredMetadataSize += size
}
// incrementing tenant metrics if we have a tenant.
if pushStats.NumLines != 0 && userID != "" {
linesIngested.WithLabelValues(userID, isAggregatedMetric).Add(float64(pushStats.NumLines))
}
linesReceivedStats.Inc(pushStats.NumLines)
logValues := []interface{}{
"msg", "push request parsed",
"path", r.URL.Path,
"contentType", pushStats.ContentType,
"contentEncoding", pushStats.ContentEncoding,
"bodySize", humanize.Bytes(uint64(pushStats.BodySize)),
"streams", len(req.Streams),
"entries", pushStats.NumLines,
"streamLabelsSize", humanize.Bytes(uint64(pushStats.StreamLabelsSize)),
"entriesSize", humanize.Bytes(uint64(entriesSize)),
"structuredMetadataSize", humanize.Bytes(uint64(structuredMetadataSize)),
"totalSize", humanize.Bytes(uint64(entriesSize + pushStats.StreamLabelsSize)),
"mostRecentLagMs", time.Since(pushStats.MostRecentEntryTimestamp).Milliseconds(),
}
logValues = append(logValues, pushStats.Extra...)
level.Debug(logger).Log(logValues...)
return req, nil
}
func ParseLokiRequest(userID string, r *http.Request, tenantsRetention TenantsRetention, limits Limits, tracker UsageTracker, logPushRequestStreams bool, logger log.Logger) (*logproto.PushRequest, *Stats, error) {
// Body
var body io.Reader
// bodySize should always reflect the compressed size of the request body
bodySize := loki_util.NewSizeReader(r.Body)
contentEncoding := r.Header.Get(contentEnc)
switch contentEncoding {
case "":
body = bodySize
case "snappy":
// Snappy-decoding is done by `util.ParseProtoReader(..., util.RawSnappy)` below.
// Pass on body bytes. Note: HTTP clients do not need to set this header,
// but they sometimes do. See #3407.
body = bodySize
case "gzip":
gzipReader, err := gzip.NewReader(bodySize)
if err != nil {
return nil, nil, err
}
defer gzipReader.Close()
body = gzipReader
case "deflate":
flateReader := flate.NewReader(bodySize)
defer flateReader.Close()
body = flateReader
default:
return nil, nil, fmt.Errorf("Content-Encoding %q not supported", contentEncoding)
}
contentType := r.Header.Get(contentType)
var (
req logproto.PushRequest
pushStats = newPushStats()
)
contentType, _ /* params */, err := mime.ParseMediaType(contentType)
if err != nil {
return nil, nil, err
}
switch contentType {
case applicationJSON:
var err error
// todo once https://github.com/weaveworks/common/commit/73225442af7da93ec8f6a6e2f7c8aafaee3f8840 is in Loki.
// We can try to pass the body as bytes.buffer instead to avoid reading into another buffer.
if loghttp.GetVersion(r.RequestURI) == loghttp.VersionV1 {
err = unmarshal.DecodePushRequest(body, &req)
} else {
err = unmarshal2.DecodePushRequest(body, &req)
}
if err != nil {
return nil, nil, err
}
default:
// When no content-type header is set or when it is set to
// `application/x-protobuf`: expect snappy compression.
if err := util.ParseProtoReader(r.Context(), body, int(r.ContentLength), math.MaxInt32, &req, util.RawSnappy); err != nil {
return nil, nil, err
}
}
pushStats.BodySize = bodySize.Size()
pushStats.ContentType = contentType
pushStats.ContentEncoding = contentEncoding
discoverServiceName := limits.DiscoverServiceName(userID)
for i := range req.Streams {
s := req.Streams[i]
pushStats.StreamLabelsSize += int64(len(s.Labels))
lbs, err := syntax.ParseLabels(s.Labels)
if err != nil {
return nil, nil, fmt.Errorf("couldn't parse labels: %w", err)
}
if lbs.Has(AggregatedMetricLabel) {
pushStats.IsAggregatedMetric = true
}
var beforeServiceName string
if logPushRequestStreams {
beforeServiceName = lbs.String()
}
serviceName := ServiceUnknown
if !lbs.Has(LabelServiceName) && len(discoverServiceName) > 0 && !pushStats.IsAggregatedMetric {
for _, labelName := range discoverServiceName {
if labelVal := lbs.Get(labelName); labelVal != "" {
serviceName = labelVal
break
}
}
lb := labels.NewBuilder(lbs)
lbs = lb.Set(LabelServiceName, serviceName).Labels()
s.Labels = lbs.String()
}
if logPushRequestStreams {
level.Debug(logger).Log(
"msg", "push request stream before service name discovery",
"labels", beforeServiceName,
"service_name", serviceName,
)
}
var retentionPeriod time.Duration
if tenantsRetention != nil {
retentionPeriod = tenantsRetention.RetentionPeriodFor(userID, lbs)
}
for _, e := range s.Entries {
pushStats.NumLines++
entryLabelsSize := int64(util.StructuredMetadataSize(e.StructuredMetadata))
pushStats.LogLinesBytes[retentionPeriod] += int64(len(e.Line))
pushStats.StructuredMetadataBytes[retentionPeriod] += entryLabelsSize
if tracker != nil {
tracker.ReceivedBytesAdd(r.Context(), userID, retentionPeriod, lbs, float64(len(e.Line)))
tracker.ReceivedBytesAdd(r.Context(), userID, retentionPeriod, lbs, float64(entryLabelsSize))
}
if e.Timestamp.After(pushStats.MostRecentEntryTimestamp) {
pushStats.MostRecentEntryTimestamp = e.Timestamp
}
}
req.Streams[i] = s
}
return &req, pushStats, nil
}
func RetentionPeriodToString(retentionPeriod time.Duration) string {
var retentionHours string
if retentionPeriod > 0 {
retentionHours = fmt.Sprintf("%d", int64(math.Floor(retentionPeriod.Hours())))
}
return retentionHours
}