-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**What this PR does / why we need it**: Introduces `distinct` to LogQL. Usage: `{job="varlogs"} | distinct filename` It is similar to `distinct` in SQL and especially useful for sampling. Similar syntax exists in other log query languages. **Example** For the following log lines: ```json {"event": "access", "id": "1", "time": "2023-02-28 15:12:11"} {"event": "access", "id": "1", "time": "2023-02-28 15:13:11"} {"event": "access", "id": "2", "time": "2023-02-28 15:14:11"} {"event": "access", "id": "2", "time": "2023-02-28 15:15:11"} ``` The query below: ```logql {app="order"} | json | distinct id ``` Will return: ```json {"event": "access", `"id": "1",` "time": "2023-02-28 15:13:11"} {"event": "access", `"id": "2", `"time": "2023-02-28 15:15:11"} ``` **Example with multiple labels** ```logql {app="order"} | json | distinct id,time ``` **Which issue(s) this PR fixes**: Fixes #8649 --------- Co-authored-by: J Stickler <[email protected]> Co-authored-by: Dylan Guedes <[email protected]> Co-authored-by: Karsten Jeschkies <[email protected]>
- Loading branch information
1 parent
411f384
commit 38b298c
Showing
10 changed files
with
832 additions
and
413 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
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,37 @@ | ||
package log | ||
|
||
type distinctFilter struct { | ||
datas map[string]map[string]int | ||
labels []string | ||
} | ||
|
||
func NewDistinctFilter(labels []string) (Stage, error) { | ||
datas := make(map[string]map[string]int, 0) | ||
for _, label := range labels { | ||
datas[label] = make(map[string]int, 0) | ||
} | ||
return &distinctFilter{ | ||
labels: labels, | ||
datas: datas, | ||
}, nil | ||
} | ||
func (r *distinctFilter) Process(_ int64, line []byte, lbs *LabelsBuilder) ([]byte, bool) { | ||
keep := false | ||
for _, label := range r.labels { | ||
val, ok := lbs.Get(label) | ||
if !ok { | ||
return line, true | ||
} | ||
_, ok = r.datas[label][val] | ||
if ok { | ||
return line, false | ||
} | ||
r.datas[label][val] = 1 | ||
keep = true | ||
} | ||
return line, keep | ||
} | ||
|
||
func (r *distinctFilter) RequiredLabelNames() []string { | ||
return []string{} | ||
} |
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,62 @@ | ||
package log | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/grafana/loki/pkg/logqlmodel" | ||
) | ||
|
||
func Test_DistinctFilter(t *testing.T) { | ||
|
||
c := struct { | ||
name string | ||
label []string | ||
lbs labels.Labels | ||
input []string | ||
expectedCount int | ||
expectedLines []string | ||
}{ | ||
name: "distinct test", | ||
label: []string{"id", "time", "none"}, | ||
lbs: labels.Labels{ | ||
{Name: logqlmodel.ErrorLabel, Value: errJSON}, | ||
{Name: "status", Value: "200"}, | ||
{Name: "method", Value: "POST"}, | ||
}, | ||
input: []string{ | ||
`{"event": "access", "id": "1", "time": "1"}`, | ||
`{"event": "access", "id": "1", "time": "2"}`, | ||
`{"event": "access", "id": "2", "time": "3"}`, | ||
`{"event": "access", "id": "2", "time": "4"}`, | ||
`{"event": "access", "id": "1", "time": "5"}`, | ||
`{"event": "delete", "id": "1", "time": "1"}`, | ||
}, | ||
expectedCount: 2, | ||
expectedLines: []string{ | ||
`{"event": "access", "id": "1", "time": "1"}`, | ||
`{"event": "access", "id": "2", "time": "3"}`, | ||
}, | ||
} | ||
|
||
distinctFilter, err := NewDistinctFilter(c.label) | ||
require.NoError(t, err) | ||
|
||
total := 0 | ||
lines := make([]string, 0) | ||
b := NewBaseLabelsBuilder().ForLabels(c.lbs, c.lbs.Hash()) | ||
for _, line := range c.input { | ||
NewJSONParser().Process(1, []byte(line), b) | ||
_, ok := distinctFilter.Process(1, []byte(line), b) | ||
if ok { | ||
total++ | ||
lines = append(lines, line) | ||
} | ||
} | ||
|
||
require.Equal(t, c.expectedCount, total) | ||
require.Equal(t, c.expectedLines, lines) | ||
|
||
} |
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
Oops, something went wrong.