Skip to content

Commit

Permalink
input: output: test: Invoke/Use plugin.Logger
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 committed Jul 5, 2022
1 parent d6c6972 commit c01ccbf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
9 changes: 4 additions & 5 deletions examples/in_gdummy/in_gdummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ import (
"github.com/calyptia/plugin/metric"
)

var logger plugin.Logger

func init() {
plugin.RegisterInput("gdummy", "dummy GO!", &gdummyPlugin{})
}

type gdummyPlugin struct {
counterSuccess metric.Counter
counterFailure metric.Counter
log plugin.Logger
}

func (plug *gdummyPlugin) Init(ctx context.Context, fbit *plugin.Fluentbit) error {
plug.counterSuccess = fbit.Metrics.NewCounter("operation_succeeded_total", "Total number of succeeded operations", "gdummy")
plug.counterFailure = fbit.Metrics.NewCounter("operation_failed_total", "Total number of failed operations", "gdummy")
logger = fbit.Logger
plug.log = fbit.Logger

return nil
}
Expand All @@ -37,15 +36,15 @@ func (plug gdummyPlugin) Collect(ctx context.Context, ch chan<- plugin.Message)
err := ctx.Err()
if err != nil && !errors.Is(err, context.Canceled) {
plug.counterFailure.Add(1)
logger.Error("[gdummy] operation failed")
plug.log.Error("[gdummy] operation failed")

return err
}

return nil
case <-tick.C:
plug.counterSuccess.Add(1)
logger.Debug("[gdummy] operation succeeded")
plug.log.Debug("[gdummy] operation succeeded")

ch <- plugin.Message{
Time: time.Now(),
Expand Down
4 changes: 4 additions & 0 deletions testdata/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ func init() {
type inputPlugin struct {
foo string
collectCounter metric.Counter
log plugin.Logger
}

func (plug *inputPlugin) Init(ctx context.Context, fbit *plugin.Fluentbit) error {
plug.foo = fbit.Conf.String("foo")
plug.collectCounter = fbit.Metrics.NewCounter("collect_total", "Total number of collects", "go-test-input-plugin")
plug.log = fbit.Logger
return nil
}

Expand All @@ -32,12 +34,14 @@ func (plug inputPlugin) Collect(ctx context.Context, ch chan<- plugin.Message) e
case <-ctx.Done():
err := ctx.Err()
if err != nil && !errors.Is(err, context.Canceled) {
plug.log.Error("[go-test-input-plugin] operation failed")
return err
}

return nil
case <-tick.C:
plug.collectCounter.Add(1)
plug.log.Info("[go-test-input-plugin] operation succeeded")

ch <- plugin.Message{
Time: time.Now(),
Expand Down
3 changes: 3 additions & 0 deletions testdata/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ func init() {

type outputPlugin struct {
flushCounter metric.Counter
log plugin.Logger
}

func (plug *outputPlugin) Init(ctx context.Context, fbit *plugin.Fluentbit) error {
plug.flushCounter = fbit.Metrics.NewCounter("flush_total", "Total number of flushes", "go-test-output-plugin")
plug.log = fbit.Logger
return nil
}

Expand All @@ -33,6 +35,7 @@ func (plug outputPlugin) Flush(ctx context.Context, ch <-chan plugin.Message) er

for msg := range ch {
plug.flushCounter.Add(1)
plug.log.Info("[go-test-output-plugin] operation proceeded")

_, err := fmt.Fprintf(f, "message=\"got record\" tag=%s time=%s record=%+v\n", msg.Tag(), msg.Time.Format(time.RFC3339), msg.Record)
if err != nil {
Expand Down

0 comments on commit c01ccbf

Please sign in to comment.