-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New loki.process stage structured_metadata_regex, to move all labels …
…matching a regex to structured metadata
- Loading branch information
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
internal/component/loki/process/stages/structured_metadata_regex.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package stages | ||
|
||
import ( | ||
"github.com/go-kit/log" | ||
"regexp" | ||
|
||
"github.com/grafana/loki/v3/pkg/logproto" | ||
) | ||
|
||
type StructuredMetadataRegexConfig struct { | ||
Regex string `alloy:"regex,attr"` | ||
} | ||
|
||
func newStructuredMetadataRegexStage(logger log.Logger, configs StructuredMetadataRegexConfig) (Stage, error) { | ||
|
||
re, error := regexp.Compile(configs.Regex) | ||
|
||
if error != nil { | ||
return &structuredMetadataRegexStage{}, error | ||
} | ||
|
||
return &structuredMetadataRegexStage{ | ||
logger: logger, | ||
regex: *re, | ||
}, nil | ||
} | ||
|
||
type structuredMetadataRegexStage struct { | ||
logger log.Logger | ||
regex regexp.Regexp | ||
} | ||
|
||
func (s *structuredMetadataRegexStage) Name() string { | ||
return StageTypeStructuredMetadataRegex | ||
} | ||
|
||
// Cleanup implements Stage. | ||
func (*structuredMetadataRegexStage) Cleanup() { | ||
// no-op | ||
} | ||
|
||
func (s *structuredMetadataRegexStage) Run(in chan Entry) chan Entry { | ||
return RunWith(in, func(e Entry) Entry { | ||
for labelName, labelValue := range e.Labels { | ||
if s.regex.MatchString(string(labelName)) { | ||
e.StructuredMetadata = append(e.StructuredMetadata, logproto.LabelAdapter{Name: string(labelName), Value: string(labelValue)}) | ||
delete(e.Labels, labelName) | ||
} | ||
} | ||
return e | ||
}) | ||
} |
52 changes: 52 additions & 0 deletions
52
internal/component/loki/process/stages/structured_metadata_regex_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package stages | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/common/model" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/grafana/loki/pkg/push" | ||
util_log "github.com/grafana/loki/v3/pkg/util/log" | ||
) | ||
|
||
var pipelineStagesStructuredMetadataRegexFromStaticLabels = ` | ||
stage.static_labels { | ||
values = {"component" = "querier", "pod" = "loki-querier-664f97db8d-qhnwg"} | ||
} | ||
stage.structured_metadata_regex { | ||
regex = "comp.*" | ||
} | ||
` | ||
|
||
func Test_structuredMetadataRegexStage(t *testing.T) { | ||
tests := map[string]struct { | ||
pipelineStagesYaml string | ||
logLine string | ||
expectedStructuredMetadata push.LabelsAdapter | ||
expectedLabels model.LabelSet | ||
}{ | ||
"expected ": { | ||
pipelineStagesYaml: pipelineStagesStructuredMetadataRegexFromStaticLabels, | ||
logLine: "", | ||
expectedStructuredMetadata: push.LabelsAdapter{push.LabelAdapter{Name: "component", Value: "querier"}}, | ||
expectedLabels: model.LabelSet{model.LabelName("pod"): model.LabelValue("loki-querier-664f97db8d-qhnwg")}, | ||
}, | ||
} | ||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
pl, err := NewPipeline(util_log.Logger, loadConfig(test.pipelineStagesYaml), nil, prometheus.DefaultRegisterer) | ||
require.NoError(t, err) | ||
|
||
result := processEntries(pl, newEntry(nil, nil, test.logLine, time.Now()))[0] | ||
require.Equal(t, test.expectedStructuredMetadata, result.StructuredMetadata) | ||
if test.expectedLabels != nil { | ||
require.Equal(t, test.expectedLabels, result.Labels) | ||
} else { | ||
require.Empty(t, result.Labels) | ||
} | ||
}) | ||
} | ||
} |