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

Feature: Graceful shutdown #13

Merged
merged 1 commit into from
Feb 12, 2022
Merged
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
28 changes: 25 additions & 3 deletions store/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"io"
"sync"
"time"

"github.com/Azure/azure-kusto-go/kusto/ingest"
Expand All @@ -22,6 +23,8 @@ type kustoSpanWriter struct {
ingest kustoIngest
logger hclog.Logger
spanInput chan []string
shutdown chan bool
shutdownWg sync.WaitGroup
}

func newKustoSpanWriter(factory *kustoFactory, logger hclog.Logger) (*kustoSpanWriter, error) {
Expand All @@ -36,21 +39,35 @@ func newKustoSpanWriter(factory *kustoFactory, logger hclog.Logger) (*kustoSpanW
ingest: in,
logger: logger,
spanInput: make(chan []string, factory.PluginConfig.WriterSpanBufferSize),
shutdown: make(chan bool),
shutdownWg: sync.WaitGroup{},
}

go writer.ingestCSV()

return writer, nil
}

func (kw kustoSpanWriter) WriteSpan(_ context.Context, span *model.Span) error {
func (kw *kustoSpanWriter) WriteSpan(_ context.Context, span *model.Span) error {
spanStringArray, err := TransformSpanToStringArray(span)

kw.spanInput <- spanStringArray
return err
}

func (kw kustoSpanWriter) ingestCSV() {
func (kw *kustoSpanWriter) Close() error {
kw.logger.Debug("plugin shutdown started")

kw.shutdownWg.Add(1)
kw.shutdown <- true
kw.shutdownWg.Wait()
close(kw.spanInput)

kw.logger.Debug("plugin shutdown completed")
return nil
}

func (kw *kustoSpanWriter) ingestCSV() {
ticker := time.NewTicker(kw.batchTimeout)

b := &bytes.Buffer{}
Expand Down Expand Up @@ -78,11 +95,16 @@ func (kw kustoSpanWriter) ingestCSV() {
batchSize := b.Len()
kw.ingestBatch(b)
kw.logger.Debug("Ingested batch by time", "batchSize", batchSize)
case <-kw.shutdown:
batchSize := b.Len()
kw.ingestBatch(b)
kw.logger.Debug("Ingested batch by shutdown", "batchSize", batchSize)
kw.shutdownWg.Done()
}
}
}

func (kw kustoSpanWriter) ingestBatch(b *bytes.Buffer) {
func (kw *kustoSpanWriter) ingestBatch(b *bytes.Buffer) {
if b.Len() == 0 {
return
}
Expand Down