6
6
"compress/gzip"
7
7
"context"
8
8
"fmt"
9
+ "io"
9
10
"log"
11
+ "net/http"
10
12
"net/http/httptest"
11
13
"strings"
12
14
"testing"
@@ -16,6 +18,10 @@ import (
16
18
"github.com/prometheus/prometheus/model/labels"
17
19
"github.com/stretchr/testify/assert"
18
20
"github.com/stretchr/testify/require"
21
+ "go.opentelemetry.io/collector/pdata/pcommon"
22
+ "go.opentelemetry.io/collector/pdata/plog"
23
+
24
+ "github.com/grafana/dskit/flagext"
19
25
20
26
util_log "github.com/grafana/loki/v3/pkg/util/log"
21
27
)
@@ -256,7 +262,7 @@ func TestParseRequest(t *testing.T) {
256
262
}
257
263
258
264
tracker := NewMockTracker ()
259
- data , err := ParseRequest (util_log .Logger , "fake" , request , nil , & fakeLimits {test .enableServiceDiscovery }, ParseLokiRequest , tracker )
265
+ data , err := ParseRequest (util_log .Logger , "fake" , request , nil , & fakeLimits {enabled : test .enableServiceDiscovery }, ParseLokiRequest , tracker )
260
266
261
267
structuredMetadataBytesReceived := int (structuredMetadataBytesReceivedStats .Value ()["total" ].(int64 )) - previousStructuredMetadataBytesReceived
262
268
previousStructuredMetadataBytesReceived += structuredMetadataBytesReceived
@@ -314,19 +320,124 @@ func TestParseRequest(t *testing.T) {
314
320
}
315
321
}
316
322
323
+ func Test_ServiceDetection (t * testing.T ) {
324
+ tracker := NewMockTracker ()
325
+
326
+ createOtlpLogs := func (labels ... string ) []byte {
327
+ now := time .Unix (0 , time .Now ().UnixNano ())
328
+ ld := plog .NewLogs ()
329
+ for i := 0 ; i < len (labels ); i += 2 {
330
+ ld .ResourceLogs ().AppendEmpty ().Resource ().Attributes ().PutStr (labels [i ], labels [i + 1 ])
331
+ }
332
+ ld .ResourceLogs ().At (0 ).ScopeLogs ().AppendEmpty ().LogRecords ().AppendEmpty ().Body ().SetStr ("test body" )
333
+ ld .ResourceLogs ().At (0 ).ScopeLogs ().At (0 ).LogRecords ().At (0 ).SetTimestamp (pcommon .Timestamp (now .UnixNano ()))
334
+
335
+ jsonMarshaller := plog.JSONMarshaler {}
336
+ body , err := jsonMarshaller .MarshalLogs (ld )
337
+
338
+ require .NoError (t , err )
339
+ return body
340
+ }
341
+
342
+ createRequest := func (path string , body io.Reader ) * http.Request {
343
+ request := httptest .NewRequest (
344
+ "POST" ,
345
+ path ,
346
+ body ,
347
+ )
348
+ request .Header .Add ("Content-Type" , "application/json" )
349
+
350
+ return request
351
+ }
352
+
353
+ t .Run ("detects servce from loki push requests" , func (t * testing.T ) {
354
+ body := `{"streams": [{ "stream": { "foo": "bar" }, "values": [ [ "1570818238000000000", "fizzbuzz" ] ] }]}`
355
+ request := createRequest ("/loki/api/v1/push" , strings .NewReader (body ))
356
+
357
+ limits := & fakeLimits {enabled : true , labels : []string {"foo" }}
358
+ data , err := ParseRequest (util_log .Logger , "fake" , request , nil , limits , ParseLokiRequest , tracker )
359
+
360
+ require .NoError (t , err )
361
+ require .Equal (t , labels .FromStrings ("foo" , "bar" , LabelServiceName , "bar" ).String (), data .Streams [0 ].Labels )
362
+ })
363
+
364
+ t .Run ("detects servce from OTLP push requests using default indexing" , func (t * testing.T ) {
365
+ body := createOtlpLogs ("k8s.job.name" , "bar" )
366
+ request := createRequest ("/otlp/v1/push" , bytes .NewReader (body ))
367
+
368
+ limits := & fakeLimits {enabled : true }
369
+ data , err := ParseRequest (util_log .Logger , "fake" , request , limits , limits , ParseOTLPRequest , tracker )
370
+ require .NoError (t , err )
371
+ require .Equal (t , labels .FromStrings ("k8s_job_name" , "bar" , LabelServiceName , "bar" ).String (), data .Streams [0 ].Labels )
372
+ })
373
+
374
+ t .Run ("detects servce from OTLP push requests using custom indexing" , func (t * testing.T ) {
375
+ body := createOtlpLogs ("special" , "sauce" )
376
+ request := createRequest ("/otlp/v1/push" , bytes .NewReader (body ))
377
+
378
+ limits := & fakeLimits {
379
+ enabled : true ,
380
+ labels : []string {"special" },
381
+ indexAttributes : []string {"special" },
382
+ }
383
+ data , err := ParseRequest (util_log .Logger , "fake" , request , limits , limits , ParseOTLPRequest , tracker )
384
+ require .NoError (t , err )
385
+ require .Equal (t , labels .FromStrings ("special" , "sauce" , LabelServiceName , "sauce" ).String (), data .Streams [0 ].Labels )
386
+ })
387
+
388
+ t .Run ("only detects custom service label from indexed labels" , func (t * testing.T ) {
389
+ body := createOtlpLogs ("special" , "sauce" )
390
+ request := createRequest ("/otlp/v1/push" , bytes .NewReader (body ))
391
+
392
+ limits := & fakeLimits {
393
+ enabled : true ,
394
+ labels : []string {"special" },
395
+ indexAttributes : []string {},
396
+ }
397
+ data , err := ParseRequest (util_log .Logger , "fake" , request , limits , limits , ParseOTLPRequest , tracker )
398
+ require .NoError (t , err )
399
+ require .Equal (t , labels .FromStrings (LabelServiceName , ServiceUnknown ).String (), data .Streams [0 ].Labels )
400
+ })
401
+ }
402
+
317
403
type fakeLimits struct {
318
- enabled bool
404
+ enabled bool
405
+ labels []string
406
+ indexAttributes []string
319
407
}
320
408
321
- func (l * fakeLimits ) OTLPConfig (_ string ) OTLPConfig {
322
- return OTLPConfig {}
409
+ func (f * fakeLimits ) RetentionPeriodFor (_ string , _ labels. Labels ) time. Duration {
410
+ return time . Hour
323
411
}
324
412
325
- func (l * fakeLimits ) DiscoverServiceName (_ string ) []string {
326
- if ! l .enabled {
413
+ func (f * fakeLimits ) OTLPConfig (_ string ) OTLPConfig {
414
+ if len (f .indexAttributes ) > 0 {
415
+ return OTLPConfig {
416
+ ResourceAttributes : ResourceAttributesConfig {
417
+ AttributesConfig : []AttributesConfig {
418
+ {
419
+ Action : IndexLabel ,
420
+ Attributes : f .indexAttributes ,
421
+ },
422
+ },
423
+ },
424
+ }
425
+ }
426
+
427
+ defaultGlobalOTLPConfig := GlobalOTLPConfig {}
428
+ flagext .DefaultValues (& defaultGlobalOTLPConfig )
429
+ return DefaultOTLPConfig (defaultGlobalOTLPConfig )
430
+ }
431
+
432
+ func (f * fakeLimits ) DiscoverServiceName (_ string ) []string {
433
+ if ! f .enabled {
327
434
return nil
328
435
}
329
436
437
+ if len (f .labels ) > 0 {
438
+ return f .labels
439
+ }
440
+
330
441
return []string {
331
442
"service" ,
332
443
"app" ,
@@ -335,9 +446,11 @@ func (l *fakeLimits) DiscoverServiceName(_ string) []string {
335
446
"app_kubernetes_io_name" ,
336
447
"container" ,
337
448
"container_name" ,
449
+ "k8s_container_name" ,
338
450
"component" ,
339
451
"workload" ,
340
452
"job" ,
453
+ "k8s_job_name" ,
341
454
}
342
455
}
343
456
0 commit comments