mirror of https://github.com/grafana/loki
Loki: implement decolorize filter (#7602)
Implement `decolorize` filter that get rids of ANSI color codes in the log message. **Which issue(s) this PR fixes**: Fixes #7601pull/6360/head
parent
3bcc7ccd5a
commit
c5911d5342
@ -0,0 +1,35 @@ |
||||
package stages |
||||
|
||||
import ( |
||||
"github.com/grafana/loki/pkg/logql/log" |
||||
) |
||||
|
||||
type decolorizeStage struct{} |
||||
|
||||
func newDecolorizeStage(_ interface{}) (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 |
||||
} |
@ -0,0 +1,52 @@ |
||||
package stages |
||||
|
||||
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 = ` |
||||
pipeline_stages: |
||||
- 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) |
||||
}) |
||||
} |
||||
} |
@ -0,0 +1,39 @@ |
||||
--- |
||||
title: decolorize |
||||
--- |
||||
# `decolorize` stage |
||||
|
||||
The `decolorize` stage is a transform stage that lets you strip |
||||
ANSI color codes from the log line, thus making it easier to |
||||
parse logs further. |
||||
|
||||
There are examples below to help explain. |
||||
|
||||
## Decolorize stage schema |
||||
|
||||
```yaml |
||||
decolorize: |
||||
# Currently this stage has no configurable options |
||||
``` |
||||
|
||||
## Examples |
||||
|
||||
The following is an example showing the use of the `decolorize` stage. |
||||
|
||||
Given the pipeline: |
||||
|
||||
```yaml |
||||
- decolorize: |
||||
``` |
||||
|
||||
Would turn each line having a color code into a non-colored one, e.g. |
||||
|
||||
``` |
||||
[2022-11-04 22:17:57.811] \033[0;32http\033[0m: GET /_health (0 ms) 204 |
||||
``` |
||||
|
||||
is turned into |
||||
|
||||
``` |
||||
[2022-11-04 22:17:57.811] http: GET /_health (0 ms) 204 |
||||
``` |
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue