-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FIFO queue persistent buffering for fluent bit output plugin (#2142)
* Add FIFO queue persistent buffering for fluent bit output plugin * Fix configuration names in logging * Fix typos & suggested improvments
- Loading branch information
Showing
26 changed files
with
2,134 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/grafana/loki/pkg/promtail/client" | ||
) | ||
|
||
type bufferConfig struct { | ||
buffer bool | ||
bufferType string | ||
dqueConfig dqueConfig | ||
} | ||
|
||
var defaultBufferConfig = bufferConfig{ | ||
buffer: false, | ||
bufferType: "dque", | ||
dqueConfig: defaultDqueConfig, | ||
} | ||
|
||
// NewBuffer makes a new buffered Client. | ||
func NewBuffer(cfg *config, logger log.Logger) (client.Client, error) { | ||
switch cfg.bufferConfig.bufferType { | ||
case "dque": | ||
return newDque(cfg, logger) | ||
default: | ||
return nil, fmt.Errorf("failed to parse bufferType: %s", cfg.bufferConfig.bufferType) | ||
} | ||
} |
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,14 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/go-kit/kit/log" | ||
"github.com/grafana/loki/pkg/promtail/client" | ||
) | ||
|
||
// NewClient creates a new client based on the fluentbit configuration. | ||
func NewClient(cfg *config, logger log.Logger) (client.Client, error) { | ||
if cfg.bufferConfig.buffer { | ||
return NewBuffer(cfg, logger) | ||
} | ||
return client.New(cfg.clientConfig, logger) | ||
} |
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,132 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/go-kit/kit/log/level" | ||
|
||
"github.com/grafana/loki/pkg/promtail/client" | ||
"github.com/joncrlsn/dque" | ||
"github.com/prometheus/common/model" | ||
) | ||
|
||
type dqueConfig struct { | ||
queueDir string | ||
queueSegmentSize int | ||
queueSync bool | ||
queueName string | ||
} | ||
|
||
var defaultDqueConfig = dqueConfig{ | ||
queueDir: "/tmp/flb-storage/loki", | ||
queueSegmentSize: 500, | ||
queueSync: false, | ||
queueName: "dque", | ||
} | ||
|
||
type dqueEntry struct { | ||
Lbs model.LabelSet | ||
Ts time.Time | ||
Line string | ||
} | ||
|
||
func dqueEntryBuilder() interface{} { | ||
return &dqueEntry{} | ||
} | ||
|
||
type dqueClient struct { | ||
logger log.Logger | ||
queue *dque.DQue | ||
loki client.Client | ||
quit chan struct{} | ||
once sync.Once | ||
wg sync.WaitGroup | ||
} | ||
|
||
// New makes a new dque loki client | ||
func newDque(cfg *config, logger log.Logger) (client.Client, error) { | ||
var err error | ||
|
||
q := &dqueClient{ | ||
logger: log.With(logger, "component", "queue", "name", cfg.bufferConfig.dqueConfig.queueName), | ||
quit: make(chan struct{}), | ||
} | ||
|
||
err = os.MkdirAll(cfg.bufferConfig.dqueConfig.queueDir, 0644) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot create queue directory: %s", err) | ||
} | ||
|
||
q.queue, err = dque.NewOrOpen(cfg.bufferConfig.dqueConfig.queueName, cfg.bufferConfig.dqueConfig.queueDir, cfg.bufferConfig.dqueConfig.queueSegmentSize, dqueEntryBuilder) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !cfg.bufferConfig.dqueConfig.queueSync { | ||
q.queue.TurboOn() | ||
} | ||
|
||
q.loki, err = client.New(cfg.clientConfig, logger) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
q.wg.Add(1) | ||
go q.dequeuer() | ||
return q, nil | ||
} | ||
|
||
func (c *dqueClient) dequeuer() { | ||
defer func() { | ||
if err := c.queue.Close(); err != nil { | ||
level.Error(c.logger).Log("msg", "error closing queue", "err", err) | ||
} | ||
c.wg.Done() | ||
}() | ||
|
||
for { | ||
select { | ||
case <-c.quit: | ||
return | ||
default: | ||
} | ||
|
||
// Dequeue the next item in the queue | ||
entry, err := c.queue.DequeueBlock() | ||
if err != nil { | ||
level.Error(c.logger).Log("msg", "error dequeuing record", "error", err) | ||
continue | ||
} | ||
|
||
// Assert type of the response to an Item pointer so we can work with it | ||
record, ok := entry.(*dqueEntry) | ||
if !ok { | ||
level.Error(c.logger).Log("msg", "error dequeued record is not an valid type", "error") | ||
continue | ||
} | ||
|
||
if err := c.loki.Handle(record.Lbs, record.Ts, record.Line); err != nil { | ||
level.Error(c.logger).Log("msg", "error sending record to Loki", "error", err) | ||
} | ||
} | ||
} | ||
|
||
// Stop the client | ||
func (c *dqueClient) Stop() { | ||
c.once.Do(func() { close(c.quit) }) | ||
c.loki.Stop() | ||
c.wg.Wait() | ||
} | ||
|
||
// Handle implement EntryHandler; adds a new line to the next batch; send is async. | ||
func (c *dqueClient) Handle(ls model.LabelSet, t time.Time, s string) error { | ||
if err := c.queue.Enqueue(&dqueEntry{ls, t, s}); err != nil { | ||
return fmt.Errorf("cannot enqueue record %s: %s", s, err) | ||
} | ||
|
||
return 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.