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

Fix multitenant client bug #113

Merged
merged 1 commit into from
Oct 6, 2021
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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ It also adds additional configurations that aim to improve plugin's performance
| DropLogEntryWithoutK8sMetadata | When metadata is missing for the log entry, it will be dropped | `false`
| ControllerSyncTimeout | Time to wait for cluster object synchronization | 60 seconds
| NumberOfBatchIDs | The number of id per batch. This increase the number of loki label streams | 10
| SendDeletedClustersLogsToDefaultClient | When cluster is marked for deletion the logs will be send to the default url `URL` | `false`
| DeletedClientTimeExpiration | The time duration after a client for deleted cluster will be considered for expired | 1 hour
| CleanExpiredClientsPeriod | Clean the expired clients every `CleanExpiredClientsPeriod` | 24 hours
| DynamicTenant | When set the value is split on space delimiter to 3 tokens. The first token is the tenant to use, the second one is the field to search for matching. The third is the regex to match token 2. | none
| RemoveTenantIdWhenSendingToDefaultURL | When `DynamicTenant` is set this flag decide whether to remove the record with dynamic tenant or not when sending them to the default `URL` | true
| Pprof | Activating the pprof packeg for debugging purpose | false
Expand Down
2 changes: 0 additions & 2 deletions cmd/fluent-bit-loki-plugin/out_loki.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ func FLBPluginInit(ctx unsafe.Pointer) int {
level.Info(paramLogger).Log("TagExpression", fmt.Sprintf("%+v", conf.PluginConfig.KubernetesMetadata.TagExpression))
level.Info(paramLogger).Log("DropLogEntryWithoutK8sMetadata", fmt.Sprintf("%+v", conf.PluginConfig.KubernetesMetadata.DropLogEntryWithoutK8sMetadata))
level.Info(paramLogger).Log("NumberOfBatchIDs", fmt.Sprintf("%+v", conf.ClientConfig.NumberOfBatchIDs))
level.Info(paramLogger).Log("SendDeletedClustersLogsToDefaultClient", fmt.Sprintf("%+v", conf.ControllerConfig.SendDeletedClustersLogsToDefaultClient))
level.Info(paramLogger).Log("DeletedClientTimeExpiration", fmt.Sprintf("%+v", conf.ControllerConfig.DeletedClientTimeExpiration))
level.Info(paramLogger).Log("CleanExpiredClientsPeriod", fmt.Sprintf("%+v", conf.ControllerConfig.CleanExpiredClientsPeriod))
level.Info(paramLogger).Log("DynamicTenant", fmt.Sprintf("%+v", conf.PluginConfig.DynamicTenant.Tenant))
level.Info(paramLogger).Log("DynamicField", fmt.Sprintf("%+v", conf.PluginConfig.DynamicTenant.Field))
level.Info(paramLogger).Log("DynamicRegex", fmt.Sprintf("%+v", conf.PluginConfig.DynamicTenant.Regex))
Expand Down
2 changes: 1 addition & 1 deletion pkg/buffer/dque.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (c *dqueClient) StopWait() {
if err := c.closeQue(true); err != nil {
level.Error(c.logger).Log("msg", "error closing buffered client", "queue", c.queue.Name, "err", err.Error())
}
c.loki.Stop()
c.loki.StopWait()
})
}

Expand Down
42 changes: 42 additions & 0 deletions pkg/client/multi_tenant_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

giterrors "github.com/pkg/errors"

"github.com/gardener/logging/pkg/batch"
"github.com/gardener/logging/pkg/types"

"github.com/grafana/loki/pkg/promtail/client"
Expand Down Expand Up @@ -63,7 +64,9 @@ func (c *multiTenantClient) Handle(ls model.LabelSet, t time.Time, s string) err
if c.copyLabelSet {
tmpLs = ls.Clone()
}

tmpLs[client.ReservedLabelTenantID] = model.LabelValue(tenant)

err := c.lokiclient.Handle(tmpLs, t, s)
if err != nil {
errs = append(errs, err)
Expand Down Expand Up @@ -106,3 +109,42 @@ func (c *multiTenantClient) Stop() {
func (c *multiTenantClient) StopWait() {
c.lokiclient.StopWait()
}

func (c *multiTenantClient) handleStream(stream batch.Stream) error {
tenantsIDs, ok := stream.Labels[MultiTenantClientLabel]
if !ok {
return c.handleEntries(stream.Labels, stream.Entries)
}

tenants := getTenants(string(tenantsIDs))
delete(stream.Labels, MultiTenantClientLabel)
if len(tenants) < 1 {
return c.handleEntries(stream.Labels, stream.Entries)
}

var combineErr error
for _, tenant := range tenants {
ls := stream.Labels
if c.copyLabelSet {
ls = stream.Labels.Clone()
}
ls[client.ReservedLabelTenantID] = model.LabelValue(tenant)
err := c.handleEntries(ls, stream.Entries)
if err != nil {
combineErr = giterrors.Wrap(combineErr, err.Error())
}

}
return nil
}

func (c *multiTenantClient) handleEntries(ls model.LabelSet, entries []batch.Entry) error {
var combineErr error
for _, entry := range entries {
err := c.lokiclient.Handle(ls, entry.Timestamp, entry.Line)
if err != nil {
combineErr = giterrors.Wrap(combineErr, err.Error())
}
}
return combineErr
}
11 changes: 6 additions & 5 deletions pkg/client/sorted_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import (
"github.com/gardener/logging/pkg/types"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/grafana/loki/pkg/logproto"
"github.com/grafana/loki/pkg/promtail/client"
"github.com/prometheus/common/model"
)

type sortedClient struct {
logger log.Logger
lokiclient types.LokiClient
lokiclient multiTenantClient
batch *batch.Batch
batchWait time.Duration
batchLock sync.Mutex
Expand All @@ -50,11 +51,10 @@ func newSortedClient(cfg client.Config, numberOfBatchIds uint64, logger log.Logg
if err != nil {
return nil, err
}
lokiclient = NewMultiTenantClientWrapper(lokiclient, false)

c := &sortedClient{
logger: log.With(logger, "component", "client", "host", cfg.URL.Host),
lokiclient: lokiclient,
lokiclient: multiTenantClient{lokiclient: lokiclient},
batchWait: batchWait,
batchSize: cfg.BatchSize,
batchID: 0,
Expand Down Expand Up @@ -136,9 +136,10 @@ func (c *sortedClient) sendBatch() {
}

c.batch.Sort()

for _, stream := range c.batch.Streams {
for _, entry := range stream.Entries {
_ = c.lokiclient.Handle(stream.Labels, entry.Timestamp, entry.Line)
if err := c.lokiclient.handleStream(*stream); err != nil {
level.Error(c.logger).Log("msg", "error sending stream", "stream", stream.Labels.String())
}
}
c.batch = nil
Expand Down
23 changes: 0 additions & 23 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,9 @@ type ControllerConfig struct {
DynamicHostPrefix string
// DynamicHostSuffix is the suffix of the dynamic host endpoint
DynamicHostSuffix string
// SendDeletedClustersLogsToDefaultClient indicates whether the logs from
// shoot in deleting state should be save in the default url or not
SendDeletedClustersLogsToDefaultClient bool
// DeletedClientTimeExpiration is the time after a client for
// deleted shoot should be cosidered for removal
DeletedClientTimeExpiration time.Duration
// CleanExpiredClientsPeriod is the period of deletion of expired clients
CleanExpiredClientsPeriod time.Duration
// MainControllerClientConfig configure to whether to send or not the log to the shoot
// Loki for a particular shoot state.
MainControllerClientConfig ControllerClientConfiguration
Expand Down Expand Up @@ -417,14 +412,6 @@ func initControllerConfig(cfg Getter, res *Config) error {
res.ControllerConfig.DynamicHostPrefix = cfg.Get("DynamicHostPrefix")
res.ControllerConfig.DynamicHostSuffix = cfg.Get("DynamicHostSuffix")

sendDeletedClustersLogsToDefaultClient := cfg.Get("SendDeletedClustersLogsToDefaultClient")
if sendDeletedClustersLogsToDefaultClient != "" {
res.ControllerConfig.SendDeletedClustersLogsToDefaultClient, err = strconv.ParseBool(sendDeletedClustersLogsToDefaultClient)
if err != nil {
return fmt.Errorf("invalid string SendDeletedClustersLogsToDefaultClient: %v", err)
}
}

deletedClientTimeExpiration := cfg.Get("DeletedClientTimeExpiration")
if deletedClientTimeExpiration != "" {
res.ControllerConfig.DeletedClientTimeExpiration, err = time.ParseDuration(deletedClientTimeExpiration)
Expand All @@ -435,16 +422,6 @@ func initControllerConfig(cfg Getter, res *Config) error {
res.ControllerConfig.DeletedClientTimeExpiration = time.Hour
}

cleanExpiredClientsPeriod := cfg.Get("CleanExpiredClientsPeriod")
if cleanExpiredClientsPeriod != "" {
res.ControllerConfig.CleanExpiredClientsPeriod, err = time.ParseDuration(cleanExpiredClientsPeriod)
if err != nil {
return fmt.Errorf("failed to parse CleanExpiredClientsPeriod: %s", cleanExpiredClientsPeriod)
}
} else {
res.ControllerConfig.CleanExpiredClientsPeriod = 24 * time.Hour
}

return initControllerClientConfig(cfg, res)
}

Expand Down
11 changes: 0 additions & 11 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ var _ = Describe("Config", func() {
ControllerConfig: config.ControllerConfig{
CtlSyncTimeout: 60000000000,
DeletedClientTimeExpiration: 3600000000000,
CleanExpiredClientsPeriod: 86400000000000,
MainControllerClientConfig: MainControllerClientConfig,
DefaultControllerClientConfig: DefaultControllerClientConfig,
},
Expand Down Expand Up @@ -176,7 +175,6 @@ var _ = Describe("Config", func() {
ControllerConfig: config.ControllerConfig{
CtlSyncTimeout: 60000000000,
DeletedClientTimeExpiration: 3600000000000,
CleanExpiredClientsPeriod: 86400000000000,
MainControllerClientConfig: MainControllerClientConfig,
DefaultControllerClientConfig: DefaultControllerClientConfig,
},
Expand Down Expand Up @@ -253,7 +251,6 @@ var _ = Describe("Config", func() {
ControllerConfig: config.ControllerConfig{
CtlSyncTimeout: 60000000000,
DeletedClientTimeExpiration: 3600000000000,
CleanExpiredClientsPeriod: 86400000000000,
MainControllerClientConfig: MainControllerClientConfig,
DefaultControllerClientConfig: DefaultControllerClientConfig,
},
Expand Down Expand Up @@ -327,7 +324,6 @@ var _ = Describe("Config", func() {
DynamicHostSuffix: ".svc:3100/loki/api/v1/push",
CtlSyncTimeout: 60000000000,
DeletedClientTimeExpiration: 3600000000000,
CleanExpiredClientsPeriod: 86400000000000,
MainControllerClientConfig: MainControllerClientConfig,
DefaultControllerClientConfig: DefaultControllerClientConfig,
},
Expand Down Expand Up @@ -396,7 +392,6 @@ var _ = Describe("Config", func() {
ControllerConfig: config.ControllerConfig{
CtlSyncTimeout: 60000000000,
DeletedClientTimeExpiration: 3600000000000,
CleanExpiredClientsPeriod: 86400000000000,
MainControllerClientConfig: MainControllerClientConfig,
DefaultControllerClientConfig: DefaultControllerClientConfig,
},
Expand Down Expand Up @@ -463,7 +458,6 @@ var _ = Describe("Config", func() {
ControllerConfig: config.ControllerConfig{
CtlSyncTimeout: 60000000000,
DeletedClientTimeExpiration: 3600000000000,
CleanExpiredClientsPeriod: 86400000000000,
MainControllerClientConfig: MainControllerClientConfig,
DefaultControllerClientConfig: DefaultControllerClientConfig,
},
Expand Down Expand Up @@ -533,7 +527,6 @@ var _ = Describe("Config", func() {
ControllerConfig: config.ControllerConfig{
CtlSyncTimeout: 60000000000,
DeletedClientTimeExpiration: 3600000000000,
CleanExpiredClientsPeriod: 86400000000000,
MainControllerClientConfig: MainControllerClientConfig,
DefaultControllerClientConfig: DefaultControllerClientConfig,
},
Expand Down Expand Up @@ -599,7 +592,6 @@ var _ = Describe("Config", func() {
ControllerConfig: config.ControllerConfig{
CtlSyncTimeout: 60000000000,
DeletedClientTimeExpiration: 3600000000000,
CleanExpiredClientsPeriod: 86400000000000,
MainControllerClientConfig: MainControllerClientConfig,
DefaultControllerClientConfig: DefaultControllerClientConfig,
},
Expand Down Expand Up @@ -657,7 +649,6 @@ var _ = Describe("Config", func() {
ControllerConfig: config.ControllerConfig{
CtlSyncTimeout: 60000000000,
DeletedClientTimeExpiration: 3600000000000,
CleanExpiredClientsPeriod: 86400000000000,
MainControllerClientConfig: MainControllerClientConfig,
DefaultControllerClientConfig: DefaultControllerClientConfig,
},
Expand Down Expand Up @@ -715,7 +706,6 @@ var _ = Describe("Config", func() {
ControllerConfig: config.ControllerConfig{
CtlSyncTimeout: 60000000000,
DeletedClientTimeExpiration: 3600000000000,
CleanExpiredClientsPeriod: 86400000000000,
MainControllerClientConfig: MainControllerClientConfig,
DefaultControllerClientConfig: DefaultControllerClientConfig,
},
Expand Down Expand Up @@ -773,7 +763,6 @@ var _ = Describe("Config", func() {
ControllerConfig: config.ControllerConfig{
CtlSyncTimeout: 60000000000,
DeletedClientTimeExpiration: 3600000000000,
CleanExpiredClientsPeriod: 86400000000000,
MainControllerClientConfig: MainControllerClientConfig,
DefaultControllerClientConfig: DefaultControllerClientConfig,
},
Expand Down