-
Notifications
You must be signed in to change notification settings - Fork 808
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ruler: Add support for per-user external labels (#6340)
* Ruler: Add support for per-user external labels Signed-off-by: Xiaochao Dong (@damnever) <[email protected]> * Add more test cases Signed-off-by: Xiaochao Dong (@damnever) <[email protected]> * Update the cache before updating the rule manager Signed-off-by: Xiaochao Dong (@damnever) <[email protected]> --------- Signed-off-by: Xiaochao Dong (@damnever) <[email protected]>
- Loading branch information
Showing
16 changed files
with
380 additions
and
37 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
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,68 @@ | ||
package ruler | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/prometheus/prometheus/model/labels" | ||
) | ||
|
||
// userExternalLabels checks and merges per-user external labels with global external labels. | ||
type userExternalLabels struct { | ||
global labels.Labels | ||
limits RulesLimits | ||
builder *labels.Builder | ||
|
||
mtx sync.Mutex | ||
users map[string]labels.Labels | ||
} | ||
|
||
func newUserExternalLabels(global labels.Labels, limits RulesLimits) *userExternalLabels { | ||
return &userExternalLabels{ | ||
global: global, | ||
limits: limits, | ||
builder: labels.NewBuilder(nil), | ||
|
||
mtx: sync.Mutex{}, | ||
users: map[string]labels.Labels{}, | ||
} | ||
} | ||
|
||
func (e *userExternalLabels) get(userID string) (labels.Labels, bool) { | ||
e.mtx.Lock() | ||
defer e.mtx.Unlock() | ||
lset, ok := e.users[userID] | ||
return lset, ok | ||
} | ||
|
||
func (e *userExternalLabels) update(userID string) (labels.Labels, bool) { | ||
lset := e.limits.RulerExternalLabels(userID) | ||
|
||
e.mtx.Lock() | ||
defer e.mtx.Unlock() | ||
|
||
e.builder.Reset(e.global) | ||
for _, l := range lset { | ||
e.builder.Set(l.Name, l.Value) | ||
} | ||
lset = e.builder.Labels() | ||
|
||
if !labels.Equal(e.users[userID], lset) { | ||
e.users[userID] = lset | ||
return lset, true | ||
} | ||
return lset, false | ||
} | ||
|
||
func (e *userExternalLabels) remove(user string) { | ||
e.mtx.Lock() | ||
defer e.mtx.Unlock() | ||
delete(e.users, user) | ||
} | ||
|
||
func (e *userExternalLabels) cleanup() { | ||
e.mtx.Lock() | ||
defer e.mtx.Unlock() | ||
for user := range e.users { | ||
delete(e.users, user) | ||
} | ||
} |
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 ruler | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prometheus/prometheus/model/labels" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUserExternalLabels(t *testing.T) { | ||
limits := ruleLimits{} | ||
e := newUserExternalLabels(labels.FromStrings("from", "cortex"), &limits) | ||
|
||
tests := []struct { | ||
name string | ||
removeBeforeTest bool | ||
exists bool | ||
userExternalLabels labels.Labels | ||
expectedExternalLabels labels.Labels | ||
}{ | ||
{ | ||
name: "global labels only", | ||
removeBeforeTest: false, | ||
exists: false, | ||
userExternalLabels: nil, | ||
expectedExternalLabels: labels.FromStrings("from", "cortex"), | ||
}, | ||
{ | ||
name: "local labels without overriding", | ||
removeBeforeTest: true, | ||
exists: false, | ||
userExternalLabels: labels.FromStrings("tag", "local"), | ||
expectedExternalLabels: labels.FromStrings("from", "cortex", "tag", "local"), | ||
}, | ||
{ | ||
name: "local labels that override globals", | ||
removeBeforeTest: false, | ||
exists: true, | ||
userExternalLabels: labels.FromStrings("from", "cloud", "tag", "local"), | ||
expectedExternalLabels: labels.FromStrings("from", "cloud", "tag", "local"), | ||
}, | ||
} | ||
|
||
const userID = "test-user" | ||
for _, data := range tests { | ||
data := data | ||
t.Run(data.name, func(t *testing.T) { | ||
if data.removeBeforeTest { | ||
e.remove(userID) | ||
} | ||
_, exists := e.get(userID) | ||
require.Equal(t, data.exists, exists) | ||
|
||
limits.externalLabels = data.userExternalLabels | ||
lset, ok := e.update(userID) | ||
require.True(t, ok) | ||
require.Equal(t, data.expectedExternalLabels, lset) | ||
lset1, ok := e.update(userID) | ||
require.False(t, ok) // Not updated. | ||
require.Equal(t, data.expectedExternalLabels, lset1) | ||
}) | ||
} | ||
|
||
_, ok := e.get(userID) | ||
require.True(t, ok) | ||
e.cleanup() | ||
_, ok = e.get(userID) | ||
require.False(t, ok) | ||
} |
Oops, something went wrong.