Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ReservedLabelTenantID out from a dedicated file #1239

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/logentry/stages/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/grafana/loki/pkg/promtail/constants"
"github.com/grafana/loki/pkg/promtail/client"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
Expand Down Expand Up @@ -75,7 +75,7 @@ func (s *tenantStage) Process(labels model.LabelSet, extracted map[string]interf
return
}

labels[constants.ReservedLabelTenantID] = model.LabelValue(tenantID)
labels[client.ReservedLabelTenantID] = model.LabelValue(tenantID)
}

// Name implements Stage
Expand Down
10 changes: 5 additions & 5 deletions pkg/logentry/stages/tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/cortexproject/cortex/pkg/util"
"github.com/grafana/loki/pkg/promtail/constants"
"github.com/grafana/loki/pkg/promtail/client"
lokiutil "github.com/grafana/loki/pkg/util"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestTenantStage_Process(t *testing.T) {
},
"should not override the tenant if the source field is not defined in the extracted map": {
config: &TenantConfig{Source: "tenant_id"},
inputLabels: model.LabelSet{constants.ReservedLabelTenantID: "foo"},
inputLabels: model.LabelSet{client.ReservedLabelTenantID: "foo"},
inputExtracted: map[string]interface{}{},
expectedTenant: lokiutil.StringRef("foo"),
},
Expand All @@ -102,7 +102,7 @@ func TestTenantStage_Process(t *testing.T) {
},
"should override the tenant if the source field is defined in the extracted map": {
config: &TenantConfig{Source: "tenant_id"},
inputLabels: model.LabelSet{constants.ReservedLabelTenantID: "foo"},
inputLabels: model.LabelSet{client.ReservedLabelTenantID: "foo"},
inputExtracted: map[string]interface{}{"tenant_id": "bar"},
expectedTenant: lokiutil.StringRef("bar"),
},
Expand All @@ -120,7 +120,7 @@ func TestTenantStage_Process(t *testing.T) {
},
"should override the tenant with the configured static value": {
config: &TenantConfig{Value: "bar"},
inputLabels: model.LabelSet{constants.ReservedLabelTenantID: "foo"},
inputLabels: model.LabelSet{client.ReservedLabelTenantID: "foo"},
inputExtracted: map[string]interface{}{},
expectedTenant: lokiutil.StringRef("bar"),
},
Expand All @@ -145,7 +145,7 @@ func TestTenantStage_Process(t *testing.T) {
assert.Equal(t, time.Unix(1, 1), timestamp)
assert.Equal(t, "hello world", entry)

actualTenant, ok := labels[constants.ReservedLabelTenantID]
actualTenant, ok := labels[client.ReservedLabelTenantID]
if testData.expectedTenant == nil {
assert.False(t, ok)
} else {
Expand Down
17 changes: 11 additions & 6 deletions pkg/promtail/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/grafana/loki/pkg/promtail/api"
"github.com/grafana/loki/pkg/promtail/constants"

"github.com/cortexproject/cortex/pkg/util"
"github.com/go-kit/kit/log"
Expand All @@ -25,8 +24,14 @@ import (
"github.com/prometheus/common/model"
)

const contentType = "application/x-protobuf"
const maxErrMsgLen = 1024
const (
contentType = "application/x-protobuf"
maxErrMsgLen = 1024

// Label reserved to override the tenant ID while processing
// pipeline stages
ReservedLabelTenantID = "__tenant_id__"
)

var (
encodedBytes = prometheus.NewCounterVec(prometheus.CounterOpts{
Expand Down Expand Up @@ -264,7 +269,7 @@ func (c *client) send(ctx context.Context, tenantID string, buf []byte) (int, er

func (c *client) getTenantID(labels model.LabelSet) string {
// Check if it has been overridden while processing the pipeline stages
if value, ok := labels[constants.ReservedLabelTenantID]; ok {
if value, ok := labels[ReservedLabelTenantID]; ok {
return string(value)
}

Expand Down Expand Up @@ -293,10 +298,10 @@ func (c *client) Handle(ls model.LabelSet, t time.Time, s string) error {
// Get the tenant ID in case it has been overridden while processing
// the pipeline stages, then remove the special label
tenantID := c.getTenantID(ls)
if _, ok := ls[constants.ReservedLabelTenantID]; ok {
if _, ok := ls[ReservedLabelTenantID]; ok {
// Clone the label set to not manipulate the input one
ls = ls.Clone()
delete(ls, constants.ReservedLabelTenantID)
delete(ls, ReservedLabelTenantID)
}

c.entries <- entry{tenantID, ls, logproto.Entry{
Expand Down
7 changes: 0 additions & 7 deletions pkg/promtail/constants/labels.go

This file was deleted.