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

feat(common.shim): Add batch to shim #16148

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions plugins/common/shim/batch_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package shim

import (
"github.com/influxdata/telegraf"
"sync"
)

type batchMetrics struct {
metrics []telegraf.Metric
wg *sync.WaitGroup
mu *sync.RWMutex
}

func (bm *batchMetrics) add(metric telegraf.Metric) {
bm.mu.Lock()
defer bm.mu.Unlock()

bm.metrics = append(bm.metrics, metric)
}

func (bm *batchMetrics) clear() {
bm.mu.Lock()
defer bm.mu.Unlock()

bm.wg.Add(-len(bm.metrics))
bm.metrics = bm.metrics[:0]
}

func (bm *batchMetrics) len() int {
bm.mu.RLock()
defer bm.mu.RUnlock()

return len(bm.metrics)
}
68 changes: 68 additions & 0 deletions plugins/common/shim/batch_metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package shim

import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
"sync"
"testing"
)

func TestBatchMetricsAdd(t *testing.T) {
var wg sync.WaitGroup
var mu sync.RWMutex

bm := &batchMetrics{
metrics: make([]telegraf.Metric, 0),
wg: &wg,
mu: &mu,
}

metric := testutil.TestMetric(101, "metric1")

bm.add(metric)

testutil.RequireMetricsEqual(t, []telegraf.Metric{metric}, bm.metrics)
}

func TestBatchMetricsClear(t *testing.T) {
var wg sync.WaitGroup
var mu sync.RWMutex

wg.Add(2)

bm := &batchMetrics{
metrics: make([]telegraf.Metric, 0),
wg: &wg,
mu: &mu,
}

metric1 := testutil.TestMetric(101, "metric1")
metric2 := testutil.TestMetric(102, "metric2")

bm.add(metric1)
bm.add(metric2)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
require.Len(t, bm.metrics, 2)

require.Len(t, bm.metrics, 2)
bm.clear()

require.Empty(t, bm.metrics)
}

func TestBatchMetricsLen(t *testing.T) {
var wg sync.WaitGroup
var mu sync.RWMutex

bm := &batchMetrics{
metrics: make([]telegraf.Metric, 0),
wg: &wg,
mu: &mu,
}

require.Empty(t, bm.metrics)

metric := testutil.TestMetric(101, "metric1")
bm.add(metric)

require.Equal(t, 1, bm.len())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this can be used instead?

Suggested change
require.Equal(t, 1, bm.len())
require.Len(t, bm, 1)

}
15 changes: 10 additions & 5 deletions plugins/common/shim/goshim.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type Shim struct {
Processor telegraf.StreamingProcessor
Output telegraf.Output

BatchSize int
BatchTimeout time.Duration

log telegraf.Logger

// streams
Expand All @@ -56,11 +59,13 @@ type Shim struct {
// New creates a new shim interface
func New() *Shim {
return &Shim{
metricCh: make(chan telegraf.Metric, 1),
stdin: os.Stdin,
stdout: os.Stdout,
stderr: os.Stderr,
log: logger.New("", "", ""),
BatchSize: 1,
BatchTimeout: 10 * time.Second,
metricCh: make(chan telegraf.Metric, 1),
stdin: os.Stdin,
stdout: os.Stdout,
stderr: os.Stderr,
log: logger.New("", "", ""),
}
}

Expand Down
53 changes: 48 additions & 5 deletions plugins/common/shim/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package shim
import (
"bufio"
"fmt"
"os"
"sync"
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/models"
Expand Down Expand Up @@ -36,19 +39,59 @@ func (s *Shim) RunOutput() error {
}
defer s.Output.Close()

var m telegraf.Metric
mCh := make(chan telegraf.Metric)
done := make(chan struct{})
batch := batchMetrics{wg: &sync.WaitGroup{}, mu: &sync.RWMutex{}}

go func() {
timer := time.NewTimer(s.BatchTimeout)
defer timer.Stop()

for {
select {
case m := <-mCh:
batch.add(m)
if batch.len() >= s.BatchSize {
if err = s.Output.Write(batch.metrics); err != nil {
fmt.Fprintf(os.Stderr, "Failed to write metrics: %s\n", err)
}
batch.clear()
timer.Reset(s.BatchTimeout)
}
case <-timer.C:
if batch.len() > 0 {
if err = s.Output.Write(batch.metrics); err != nil {
fmt.Fprintf(os.Stderr, "Failed to write metrics: %s\n", err)
}
batch.clear()
}
timer.Reset(s.BatchTimeout)
case <-done:
if batch.len() > 0 {
if err = s.Output.Write(batch.metrics); err != nil {
fmt.Fprintf(os.Stderr, "Failed to write remaining metrics: %s\n", err)
}
}
return
}
}
}()

scanner := bufio.NewScanner(s.stdin)
for scanner.Scan() {
m, err = parser.ParseLine(scanner.Text())
m, err := parser.ParseLine(scanner.Text())
if err != nil {
fmt.Fprintf(s.stderr, "Failed to parse metric: %s\n", err)
continue
}
if err = s.Output.Write([]telegraf.Metric{m}); err != nil {
fmt.Fprintf(s.stderr, "Failed to write metric: %s\n", err)
}

batch.wg.Add(1)
mCh <- m
}

batch.wg.Wait()

close(done)

return nil
}
Loading