-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
loki.process: add decolorise stage support (#5416)
- Loading branch information
Showing
12 changed files
with
182 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package stages | ||
|
||
// NOTE: This code is copied from Promtail (07cbef92268aecc0f20d1791a6df390c2df5c072) with changes kept to the minimum. | ||
|
||
import ( | ||
"github.com/grafana/loki/pkg/logql/log" | ||
) | ||
|
||
type DecolorizeConfig struct{} | ||
|
||
type decolorizeStage struct{} | ||
|
||
func newDecolorizeStage(_ DecolorizeConfig) (Stage, error) { | ||
return &decolorizeStage{}, nil | ||
} | ||
|
||
// Run implements Stage | ||
func (m *decolorizeStage) Run(in chan Entry) chan Entry { | ||
decolorizer, _ := log.NewDecolorizer() | ||
out := make(chan Entry) | ||
go func() { | ||
defer close(out) | ||
for e := range in { | ||
decolorizedLine, _ := decolorizer.Process( | ||
e.Timestamp.Unix(), | ||
[]byte(e.Entry.Line), | ||
nil, | ||
) | ||
e.Entry.Line = string(decolorizedLine) | ||
out <- e | ||
} | ||
}() | ||
return out | ||
} | ||
|
||
// Name implements Stage | ||
func (m *decolorizeStage) Name() string { | ||
return StageTypeDecolorize | ||
} |
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,53 @@ | ||
package stages | ||
|
||
// NOTE: This code is copied from Promtail (07cbef92268aecc0f20d1791a6df390c2df5c072) with changes kept to the minimum. | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/assert" | ||
|
||
util_log "github.com/grafana/loki/pkg/util/log" | ||
) | ||
|
||
var testDecolorizePipeline = ` | ||
stage.decolorize {} | ||
` | ||
|
||
func TestPipeline_Decolorize(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := map[string]struct { | ||
config string | ||
entry string | ||
expectedEntry string | ||
}{ | ||
"successfully run pipeline on non-colored text": { | ||
testDecolorizePipeline, | ||
"sample text", | ||
"sample text", | ||
}, | ||
"successfully run pipeline on colored text": { | ||
testDecolorizePipeline, | ||
"\033[0;32mgreen\033[0m \033[0;31mred\033[0m", | ||
"green red", | ||
}, | ||
} | ||
|
||
for testName, testData := range tests { | ||
testData := testData | ||
|
||
t.Run(testName, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
pl, err := NewPipeline(util_log.Logger, loadConfig(testData.config), nil, prometheus.DefaultRegisterer) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
out := processEntries(pl, newEntry(nil, nil, testData.entry, time.Now()))[0] | ||
assert.Equal(t, testData.expectedEntry, out.Line) | ||
}) | ||
} | ||
} |
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
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
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
1 change: 0 additions & 1 deletion
1
converter/internal/promtailconvert/testdata/pipeline_stages_unsupported.diags
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
(Error) pipeline_stages.sampling is currently not supported: map[rate:100] | ||
(Error) pipeline_stages.decolorize is not supported | ||
(Error) pipeline_stages.eventlogmessage is not supported |
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