-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Promtail: adding pipeline stage for dropping labels #2571
Merged
cyriltovena
merged 4 commits into
grafana:master
from
rsteneteg:rsteneteg/labeldrop-stage
Sep 10, 2020
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
088e2af
adding pipeline stage for dropping labels, our use case for this is t…
rsteneteg fb89455
Update docs/sources/clients/promtail/stages/labeldrop.md
rsteneteg 190ed05
Update docs/sources/clients/promtail/stages/_index.md
rsteneteg 2492377
fixed inconsistent naming, removed unused logger in the labeldrop stage
rsteneteg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
title: labeldrop | ||
--- | ||
# `labeldrop` stage | ||
|
||
The labeldrop stage is an action stage that takes drops labels from | ||
the label set that is sent to Loki with the log entry. | ||
|
||
## Schema | ||
|
||
```yaml | ||
labeldrop: | ||
- [<string>] | ||
... | ||
``` | ||
|
||
### Examples | ||
|
||
For the given pipeline: | ||
|
||
```yaml | ||
- replace: | ||
expression: "(.*)" | ||
replace: "pod_name:{{ .kubernetes_pod_name }} {{ .Value }}" | ||
- labeldrop: | ||
- kubernetes_pod_name | ||
``` | ||
|
||
Given the following log line: | ||
|
||
``` | ||
log message\n | ||
``` | ||
|
||
The first stage would append the value of the`kubernetes_pod_name` label into the beginning of the log line. | ||
The labeldrop stage would drop the label from being sent to Loki, and it would now be part of the log line instead. |
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,61 @@ | ||
package stages | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/mitchellh/mapstructure" | ||
"github.com/pkg/errors" | ||
"github.com/prometheus/common/model" | ||
) | ||
|
||
const ( | ||
// ErrEmptyLabelDropStageConfig error returned if config is empty | ||
ErrEmptyLabelDropStageConfig = "drop_label stage config cannot be empty" | ||
) | ||
|
||
// LabelDropConfig is a slice of labels to be dropped | ||
type LabelDropConfig []string | ||
|
||
func validateLabelDropConfig(c LabelDropConfig) error { | ||
if c == nil || len(c) < 1 { | ||
return errors.New(ErrEmptyLabelDropStageConfig) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func newLabelDropStage(logger log.Logger, configs interface{}) (*labelDropStage, error) { | ||
cfgs := &LabelDropConfig{} | ||
err := mapstructure.Decode(configs, cfgs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = validateLabelDropConfig(*cfgs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &labelDropStage{ | ||
cfgs: *cfgs, | ||
logger: logger, | ||
}, nil | ||
} | ||
|
||
type labelDropStage struct { | ||
cfgs LabelDropConfig | ||
logger log.Logger | ||
cyriltovena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Process implements Stage | ||
func (l *labelDropStage) Process(labels model.LabelSet, extracted map[string]interface{}, t *time.Time, entry *string) { | ||
for _, label := range l.cfgs { | ||
delete(labels, model.LabelName(label)) | ||
} | ||
} | ||
|
||
// Name implements Stage | ||
func (l *labelDropStage) Name() string { | ||
return StageTypeLabelDrop | ||
} |
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,69 @@ | ||
package stages | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cortexproject/cortex/pkg/util" | ||
"github.com/prometheus/common/model" | ||
"github.com/stretchr/testify/assert" | ||
ww "github.com/weaveworks/common/server" | ||
) | ||
|
||
func Test_dropLabelStage_Process(t *testing.T) { | ||
// Enable debug logging | ||
cfg := &ww.Config{} | ||
cfg.LogLevel.Set("debug") | ||
util.InitLogger(cfg) | ||
Debug = true | ||
|
||
tests := []struct { | ||
name string | ||
config *LabelDropConfig | ||
inputLabels model.LabelSet | ||
expectedLabels model.LabelSet | ||
}{ | ||
{ | ||
name: "drop one label", | ||
config: &LabelDropConfig{"testLabel1"}, | ||
inputLabels: model.LabelSet{ | ||
"testLabel1": "testValue", | ||
"testLabel2": "testValue", | ||
}, | ||
expectedLabels: model.LabelSet{ | ||
"testLabel2": "testValue", | ||
}, | ||
}, | ||
{ | ||
name: "drop two labels", | ||
config: &LabelDropConfig{"testLabel1", "testLabel2"}, | ||
inputLabels: model.LabelSet{ | ||
"testLabel1": "testValue", | ||
"testLabel2": "testValue", | ||
}, | ||
expectedLabels: model.LabelSet{}, | ||
}, | ||
{ | ||
name: "drop non-existing label", | ||
config: &LabelDropConfig{"foobar"}, | ||
inputLabels: model.LabelSet{ | ||
"testLabel1": "testValue", | ||
"testLabel2": "testValue", | ||
}, | ||
expectedLabels: model.LabelSet{ | ||
"testLabel1": "testValue", | ||
"testLabel2": "testValue", | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
st, err := newLabelDropStage(util.Logger, test.config) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
st.Process(test.inputLabels, map[string]interface{}{}, nil, nil) | ||
assert.Equal(t, test.expectedLabels, test.inputLabels) | ||
}) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drop_label and labeldrop ? There is some inconsistency here. Should we rename this to
drop_labels
everywhere ? WDYT ? naming is hard but also important.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for finding that, I switched that out to labeldrop, I picked "labeldrop" over "drop_label" because the relabel action to drop labels is called labeldrop so thought it would be more consistent.