-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support active count_method in label names cardinality (#7085)
* Support active count_method in label names cardinality In #5136, we added the `count_method` param to the label values cardinality API. This PR does the same for the label names cardinality API. * Fix failing test * Fix more failing tests * Fix more failing tests * Fix the last failing test * Add test for new functionality * Add test for filter and make it actually work * Update docs * Update changelog * Add license header * Apply PR feedback
- Loading branch information
1 parent
18c9df3
commit 7ce863f
Showing
19 changed files
with
393 additions
and
159 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package activeseries | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/prometheus/prometheus/tsdb/index" | ||
) | ||
|
||
type PostingsReader interface { | ||
Postings(ctx context.Context, name string, values ...string) (index.Postings, error) | ||
} | ||
|
||
func IsLabelValueActive(ctx context.Context, reader PostingsReader, activeSeries *ActiveSeries, name, value string) (bool, error) { | ||
valuePostings, err := reader.Postings(ctx, name, value) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
activePostings := NewPostings(activeSeries, valuePostings) | ||
return activePostings.Next(), nil | ||
} |
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,77 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package activeseries | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/prometheus/prometheus/storage" | ||
"github.com/prometheus/prometheus/tsdb/index" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type mockPostingsReader struct { | ||
postings *index.MemPostings | ||
} | ||
|
||
func (m *mockPostingsReader) Postings(ctx context.Context, name string, values ...string) (index.Postings, error) { | ||
valuePostings := make([]index.Postings, 0, len(values)) | ||
|
||
for _, value := range values { | ||
valuePostings = append(valuePostings, m.postings.Get(name, value)) | ||
} | ||
|
||
return index.Merge(ctx, valuePostings...), nil | ||
} | ||
|
||
func TestIsLabelValueActive(t *testing.T) { | ||
ctx := context.Background() | ||
ttl := 3 | ||
mockedTime := time.Unix(int64(ttl), 0) | ||
series := []labels.Labels{ | ||
labels.FromStrings("a", "1"), | ||
labels.FromStrings("a", "2"), | ||
labels.FromStrings("a", "3"), | ||
labels.FromStrings("a", "4"), | ||
labels.FromStrings("a", "5"), | ||
} | ||
allStorageRefs := []storage.SeriesRef{1, 2, 3, 4, 5} | ||
activeSeries := NewActiveSeries(&Matchers{}, time.Duration(ttl)) | ||
|
||
memPostings := index.NewMemPostings() | ||
for i, l := range series { | ||
memPostings.Add(allStorageRefs[i], l) | ||
} | ||
reader := &mockPostingsReader{postings: memPostings} | ||
|
||
// Update each series at a different time according to its index. | ||
for i := range allStorageRefs { | ||
activeSeries.UpdateSeries(series[i], allStorageRefs[i], time.Unix(int64(i), 0), -1) | ||
} | ||
|
||
valid := activeSeries.Purge(mockedTime) | ||
require.True(t, valid) | ||
|
||
result, err := IsLabelValueActive(ctx, reader, activeSeries, "a", "1") | ||
require.NoError(t, err) | ||
require.False(t, result) | ||
|
||
result, err = IsLabelValueActive(ctx, reader, activeSeries, "a", "2") | ||
require.NoError(t, err) | ||
require.False(t, result) | ||
|
||
result, err = IsLabelValueActive(ctx, reader, activeSeries, "a", "3") | ||
require.NoError(t, err) | ||
require.False(t, result) | ||
|
||
result, err = IsLabelValueActive(ctx, reader, activeSeries, "a", "4") | ||
require.NoError(t, err) | ||
require.True(t, result) | ||
|
||
result, err = IsLabelValueActive(ctx, reader, activeSeries, "a", "5") | ||
require.NoError(t, err) | ||
require.True(t, result) | ||
} |
Oops, something went wrong.