@@ -8,12 +8,14 @@ import (
8
8
"io"
9
9
"net/http"
10
10
"net/url"
11
+ "strings"
11
12
"sync"
12
13
"time"
13
14
14
15
"github.com/go-kit/log"
15
16
"github.com/go-kit/log/level"
16
17
"github.com/golang/snappy"
18
+ "github.com/opentracing/opentracing-go"
17
19
"github.com/prometheus/common/config"
18
20
"github.com/prometheus/common/model"
19
21
"github.com/prometheus/prometheus/model/labels"
@@ -160,7 +162,13 @@ func (p *Push) Stop() {
160
162
}
161
163
162
164
// buildPayload creates the snappy compressed protobuf to send to Loki
163
- func (p * Push ) buildPayload () ([]byte , error ) {
165
+ func (p * Push ) buildPayload (ctx context.Context ) ([]byte , error ) {
166
+ sp , _ := opentracing .StartSpanFromContext (
167
+ ctx ,
168
+ "patternIngester.aggregation.Push.buildPayload" ,
169
+ )
170
+ defer sp .Finish ()
171
+
164
172
entries := p .entries .reset ()
165
173
166
174
entriesByStream := make (map [string ][]logproto.Entry )
@@ -179,6 +187,14 @@ func (p *Push) buildPayload() ([]byte, error) {
179
187
}
180
188
181
189
streams := make ([]logproto.Stream , 0 , len (entriesByStream ))
190
+
191
+ // limit the number of services to log to 1000
192
+ serviceLimit := len (entriesByStream )
193
+ if serviceLimit > 1000 {
194
+ serviceLimit = 1000
195
+ }
196
+
197
+ services := make ([]string , 0 , serviceLimit )
182
198
for s , entries := range entriesByStream {
183
199
lbls , err := syntax .ParseLabels (s )
184
200
if err != nil {
@@ -190,6 +206,10 @@ func (p *Push) buildPayload() ([]byte, error) {
190
206
Entries : entries ,
191
207
Hash : lbls .Hash (),
192
208
})
209
+
210
+ if len (services ) < serviceLimit {
211
+ services = append (services , lbls .Get (push .AggregatedMetricLabel ))
212
+ }
193
213
}
194
214
195
215
req := & logproto.PushRequest {
@@ -202,6 +222,14 @@ func (p *Push) buildPayload() ([]byte, error) {
202
222
203
223
payload = snappy .Encode (nil , payload )
204
224
225
+ sp .LogKV (
226
+ "event" , "build aggregated metrics payload" ,
227
+ "num_service" , len (entriesByStream ),
228
+ "first_1k_services" , strings .Join (services , "," ),
229
+ "num_streams" , len (streams ),
230
+ "num_entries" , len (entries ),
231
+ )
232
+
205
233
return payload , nil
206
234
}
207
235
@@ -221,7 +249,7 @@ func (p *Push) run(pushPeriod time.Duration) {
221
249
cancel ()
222
250
return
223
251
case <- pushTicker .C :
224
- payload , err := p .buildPayload ()
252
+ payload , err := p .buildPayload (ctx )
225
253
if err != nil {
226
254
level .Error (p .logger ).Log ("msg" , "failed to build payload" , "err" , err )
227
255
continue
@@ -265,9 +293,14 @@ func (p *Push) send(ctx context.Context, payload []byte) (int, error) {
265
293
err error
266
294
resp * http.Response
267
295
)
296
+
268
297
// Set a timeout for the request
269
298
ctx , cancel := context .WithTimeout (ctx , p .httpClient .Timeout )
270
299
defer cancel ()
300
+
301
+ sp , ctx := opentracing .StartSpanFromContext (ctx , "patternIngester.aggregation.Push.send" )
302
+ defer sp .Finish ()
303
+
271
304
req , err := http .NewRequestWithContext (ctx , "POST" , p .lokiURL , bytes .NewReader (payload ))
272
305
if err != nil {
273
306
return - 1 , fmt .Errorf ("failed to create push request: %w" , err )
0 commit comments