Skip to content

Commit

Permalink
Add tests for OTLP config conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
xperimental committed Oct 7, 2024
1 parent 9919c58 commit 8258bbd
Showing 1 changed file with 200 additions and 0 deletions.
200 changes: 200 additions & 0 deletions operator/internal/manifests/config_otlp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package manifests

import (

Check failure on line 3 in operator/internal/manifests/config_otlp_test.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/grafana/loki/operator) -s blank -s dot (gci)
lokiv1 "github.com/grafana/loki/operator/apis/loki/v1"
"github.com/grafana/loki/operator/internal/manifests/internal/config"
"github.com/stretchr/testify/assert"

Check failure on line 6 in operator/internal/manifests/config_otlp_test.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/grafana/loki/operator) -s blank -s dot (gci)
"testing"

Check failure on line 7 in operator/internal/manifests/config_otlp_test.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

File is not `gofumpt`-ed (gofumpt)
)

func TestOtlpAttributeConfig(t *testing.T) {
tt := []struct {
desc string
spec lokiv1.LokiStackSpec
wantConfig config.OTLPAttributeConfig
}{
{
desc: "empty",
},
{
desc: "global stream label",
spec: lokiv1.LokiStackSpec{
Limits: &lokiv1.LimitsSpec{
Global: &lokiv1.LimitsTemplateSpec{
OTLP: &lokiv1.OTLPSpec{
StreamLabels: &lokiv1.OTLPStreamLabelSpec{
ResourceAttributes: []lokiv1.OTLPAttributeReference{
{
Name: "stream.label",
},
},
},
},
},
},
},
wantConfig: config.OTLPAttributeConfig{
DefaultIndexLabels: []string{"stream.label"},
},
},
{
desc: "global stream label regex",
spec: lokiv1.LokiStackSpec{
Limits: &lokiv1.LimitsSpec{
Global: &lokiv1.LimitsTemplateSpec{
OTLP: &lokiv1.OTLPSpec{
StreamLabels: &lokiv1.OTLPStreamLabelSpec{
ResourceAttributes: []lokiv1.OTLPAttributeReference{
{
Name: "stream\\.label\\.regex\\..+",
Regex: true,
},
},
},
},
},
},
},
wantConfig: config.OTLPAttributeConfig{
Global: &config.OTLPTenantAttributeConfig{
ResourceAttributes: []config.OTLPAttribute{
{
Action: config.OTLPAttributeActionStreamLabel,
Regex: "stream\\.label\\.regex\\..+",
},
},
},
},
},
{
desc: "global metadata",
spec: lokiv1.LokiStackSpec{
Limits: &lokiv1.LimitsSpec{
Global: &lokiv1.LimitsTemplateSpec{
OTLP: &lokiv1.OTLPSpec{
StructuredMetadata: &lokiv1.OTLPMetadataSpec{
ResourceAttributes: []lokiv1.OTLPAttributeReference{
{
Name: "metadata",
},
},
},
},
},
},
},
wantConfig: config.OTLPAttributeConfig{
Global: &config.OTLPTenantAttributeConfig{
ResourceAttributes: []config.OTLPAttribute{
{
Action: config.OTLPAttributeActionMetadata,
Names: []string{"metadata"},
},
},
},
},
},
{
desc: "global combined",
spec: lokiv1.LokiStackSpec{
Limits: &lokiv1.LimitsSpec{
Global: &lokiv1.LimitsTemplateSpec{
OTLP: &lokiv1.OTLPSpec{
StreamLabels: &lokiv1.OTLPStreamLabelSpec{
ResourceAttributes: []lokiv1.OTLPAttributeReference{
{
Name: "stream.label",
},
{
Name: "stream\\.label\\.regex\\..+",
Regex: true,
},
},
},
StructuredMetadata: &lokiv1.OTLPMetadataSpec{
ResourceAttributes: []lokiv1.OTLPAttributeReference{
{
Name: "resource.metadata",
},
{
Name: "resource.metadata\\.other\\..+",
Regex: true,
},
},
ScopeAttributes: []lokiv1.OTLPAttributeReference{
{
Name: "scope.metadata",
},
{
Name: "scope.metadata\\.other\\..+",
Regex: true,
},
},
LogAttributes: []lokiv1.OTLPAttributeReference{
{
Name: "log.metadata",
},
{
Name: "log.metadata\\.other\\..+",
Regex: true,
},
},
},
},
},
},
},
wantConfig: config.OTLPAttributeConfig{
DefaultIndexLabels: []string{"stream.label"},
Global: &config.OTLPTenantAttributeConfig{
ResourceAttributes: []config.OTLPAttribute{
{
Action: config.OTLPAttributeActionStreamLabel,
Regex: "stream\\.label\\.regex\\..+",
},
{
Action: config.OTLPAttributeActionMetadata,
Names: []string{"resource.metadata"},
},
{
Action: config.OTLPAttributeActionMetadata,
Regex: "resource.metadata\\.other\\..+",
},
},
ScopeAttributes: []config.OTLPAttribute{
{
Action: config.OTLPAttributeActionMetadata,
Names: []string{"scope.metadata"},
},
{
Action: config.OTLPAttributeActionMetadata,
Regex: "scope.metadata\\.other\\..+",
},
},
LogAttributes: []config.OTLPAttribute{
{
Action: config.OTLPAttributeActionMetadata,
Names: []string{"log.metadata"},
},
{
Action: config.OTLPAttributeActionMetadata,
Regex: "log.metadata\\.other\\..+",
},
},
},
},
},
}

for _, tc := range tt {
tc := tc

t.Run(tc.desc, func(t *testing.T) {
t.Parallel()

cfg := otlpAttributeConfig(&tc.spec)

assert.Equal(t, tc.wantConfig, cfg)
})
}
}

0 comments on commit 8258bbd

Please sign in to comment.