Skip to content

Commit

Permalink
Fix global tenant id usage for label limits query
Browse files Browse the repository at this point in the history
Modifies the label limits query methods to only accept global IDs, and
updates the respective calls from scraper.
  • Loading branch information
ka3de authored and mem committed Feb 12, 2024
1 parent 1a3b765 commit e950699
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
4 changes: 2 additions & 2 deletions internal/checks/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ type testLabelsLimiter struct {
logLabelsLimit int
}

func (l testLabelsLimiter) MetricLabels(ctx context.Context, tenantID int64) (int, error) {
func (l testLabelsLimiter) MetricLabels(ctx context.Context, tenantID model.GlobalID) (int, error) {
return l.metricLabelsLimit, nil
}

func (l testLabelsLimiter) LogLabels(ctx context.Context, tenantID int64) (int, error) {
func (l testLabelsLimiter) LogLabels(ctx context.Context, tenantID model.GlobalID) (int, error) {
return l.logLabelsLimit, nil
}

Expand Down
13 changes: 7 additions & 6 deletions internal/limits/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"

"github.com/grafana/synthetic-monitoring-agent/internal/model"
sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
)

Expand Down Expand Up @@ -36,9 +37,9 @@ func NewTenantLimits(tp TenantProvider) *TenantLimits {
}

// MetricLabels returns the metric labels limit for the specified tenant.
func (tl *TenantLimits) MetricLabels(ctx context.Context, tenantID int64) (int, error) {
func (tl *TenantLimits) MetricLabels(ctx context.Context, tenantID model.GlobalID) (int, error) {
tenant, err := tl.tp.GetTenant(ctx, &sm.TenantInfo{
Id: tenantID,
Id: int64(tenantID),
})
if err != nil {
return 0, err
Expand All @@ -53,9 +54,9 @@ func (tl *TenantLimits) MetricLabels(ctx context.Context, tenantID int64) (int,
}

// LogLabels returns the log labels limit for the specified tenant.
func (tl *TenantLimits) LogLabels(ctx context.Context, tenantID int64) (int, error) {
func (tl *TenantLimits) LogLabels(ctx context.Context, tenantID model.GlobalID) (int, error) {
tenant, err := tl.tp.GetTenant(ctx, &sm.TenantInfo{
Id: tenantID,
Id: int64(tenantID),
})
if err != nil {
return 0, err
Expand All @@ -70,7 +71,7 @@ func (tl *TenantLimits) LogLabels(ctx context.Context, tenantID int64) (int, err
}

// ValidateMetricLabels validates the given number of metric labels against the specific tenant limits.
func (tl *TenantLimits) ValidateMetricLabels(ctx context.Context, tenantID int64, n int) error {
func (tl *TenantLimits) ValidateMetricLabels(ctx context.Context, tenantID model.GlobalID, n int) error {
max, err := tl.MetricLabels(ctx, tenantID)
if err != nil {
return err
Expand All @@ -84,7 +85,7 @@ func (tl *TenantLimits) ValidateMetricLabels(ctx context.Context, tenantID int64
}

// ValidateLogLabels validates the given number of log labels against the specific tenant limits.
func (tl *TenantLimits) ValidateLogLabels(ctx context.Context, tenantID int64, n int) error {
func (tl *TenantLimits) ValidateLogLabels(ctx context.Context, tenantID model.GlobalID, n int) error {
max, err := tl.LogLabels(ctx, tenantID)
if err != nil {
return err
Expand Down
17 changes: 9 additions & 8 deletions internal/limits/tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"testing"

"github.com/grafana/synthetic-monitoring-agent/internal/model"
sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -45,7 +46,7 @@ func TestMetricLabels(t *testing.T) {

testCases := []struct {
name string
tenantID int
tenantID model.GlobalID
expLabels int
expErr error
}{
Expand Down Expand Up @@ -73,7 +74,7 @@ func TestMetricLabels(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ll, err := tl.MetricLabels(ctx, int64(tc.tenantID))
ll, err := tl.MetricLabels(ctx, tc.tenantID)
if err != nil {
require.Equal(t, tc.expErr, err)
}
Expand All @@ -87,7 +88,7 @@ func TestLogLabels(t *testing.T) {

testCases := []struct {
name string
tenantID int
tenantID model.GlobalID
expLabels int
expErr error
}{
Expand Down Expand Up @@ -115,7 +116,7 @@ func TestLogLabels(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ll, err := tl.LogLabels(ctx, int64(tc.tenantID))
ll, err := tl.LogLabels(ctx, tc.tenantID)
if err != nil {
require.Equal(t, tc.expErr, err)
}
Expand All @@ -129,7 +130,7 @@ func TestValidateMetricLabels(t *testing.T) {

testCases := []struct {
name string
tenantID int
tenantID model.GlobalID
nLabels int
expErr error
}{
Expand Down Expand Up @@ -169,7 +170,7 @@ func TestValidateMetricLabels(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tl.ValidateMetricLabels(ctx, int64(tc.tenantID), tc.nLabels)
err := tl.ValidateMetricLabels(ctx, tc.tenantID, tc.nLabels)
require.Equal(t, tc.expErr, err)
})
}
Expand All @@ -180,7 +181,7 @@ func TestValidateLogLabels(t *testing.T) {

testCases := []struct {
name string
tenantID int
tenantID model.GlobalID
nLabels int
expErr error
}{
Expand Down Expand Up @@ -220,7 +221,7 @@ func TestValidateLogLabels(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tl.ValidateLogLabels(ctx, int64(tc.tenantID), tc.nLabels)
err := tl.ValidateLogLabels(ctx, tc.tenantID, tc.nLabels)
require.Equal(t, tc.expErr, err)
})
}
Expand Down
8 changes: 4 additions & 4 deletions internal/scraper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func NewIncrementerFromCounterVec(c *prometheus.CounterVec) IncrementerVec {
}

type LabelsLimiter interface {
MetricLabels(ctx context.Context, tenantID int64) (int, error)
LogLabels(ctx context.Context, tenantID int64) (int, error)
MetricLabels(ctx context.Context, tenantID model.GlobalID) (int, error)
LogLabels(ctx context.Context, tenantID model.GlobalID) (int, error)
}

type Scraper struct {
Expand Down Expand Up @@ -365,11 +365,11 @@ func (s Scraper) collectData(ctx context.Context, t time.Time) (*probeData, erro
checkInfoLabels = s.buildCheckInfoLabels(userLabels)
)

maxMetricLabels, err := s.labelsLimiter.MetricLabels(ctx, s.check.TenantId)
maxMetricLabels, err := s.labelsLimiter.MetricLabels(ctx, s.check.GlobalTenantID())
if err != nil {
return nil, fmt.Errorf("retrieving tenant metric labels limit: %w", err)
}
maxLogLabels, err := s.labelsLimiter.LogLabels(ctx, s.check.TenantId)
maxLogLabels, err := s.labelsLimiter.LogLabels(ctx, s.check.GlobalTenantID())
if err != nil {
return nil, fmt.Errorf("retrieving tenant log labels limit: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/scraper/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1047,11 +1047,11 @@ type testLabelsLimiter struct {
maxLogLabels int
}

func (l testLabelsLimiter) MetricLabels(ctx context.Context, tenantID int64) (int, error) {
func (l testLabelsLimiter) MetricLabels(ctx context.Context, tenantID model.GlobalID) (int, error) {
return l.maxMetricLabels, nil
}

func (l testLabelsLimiter) LogLabels(ctx context.Context, tenantID int64) (int, error) {
func (l testLabelsLimiter) LogLabels(ctx context.Context, tenantID model.GlobalID) (int, error) {
return l.maxLogLabels, nil
}

Expand Down

0 comments on commit e950699

Please sign in to comment.