github.com/yandex/pandora@v0.5.32/core/aggregator/log.go (about)

     1  package aggregator
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/yandex/pandora/core"
     7  	"go.uber.org/zap"
     8  )
     9  
    10  func NewLog() core.Aggregator {
    11  	return &logging{sink: make(chan core.Sample, 128)}
    12  }
    13  
    14  type logging struct {
    15  	sink chan core.Sample
    16  	log  *zap.SugaredLogger
    17  }
    18  
    19  func (l *logging) Report(sample core.Sample) {
    20  	l.sink <- sample
    21  }
    22  
    23  func (l *logging) Run(ctx context.Context, deps core.AggregatorDeps) error {
    24  	l.log = deps.Log.Sugar()
    25  loop:
    26  	for {
    27  		select {
    28  		case sample := <-l.sink:
    29  			l.handle(sample)
    30  		case <-ctx.Done():
    31  			break loop
    32  		}
    33  	}
    34  	for {
    35  		// Context is done, but we should read all data from sink.
    36  		select {
    37  		case r := <-l.sink:
    38  			l.handle(r)
    39  		default:
    40  			return nil
    41  		}
    42  	}
    43  }
    44  
    45  func (l *logging) handle(sample core.Sample) {
    46  	l.log.Info("Sample reported: ", sample)
    47  }