github.com/Financial-Times/publish-availability-monitor@v1.12.0/metrics/aggregator.go (about)

     1  package metrics
     2  
     3  import (
     4  	"github.com/Financial-Times/go-logger/v2"
     5  )
     6  
     7  // Destination is the interface which defines a method to send
     8  // PublishMetrics to a certain destination.
     9  type Destination interface {
    10  	Send(pm PublishMetric)
    11  }
    12  
    13  // Aggregator reads PublishMetrics from a channel and distributes them to
    14  // the configured Destinations.
    15  type Aggregator struct {
    16  	publishMetricSource          chan PublishMetric
    17  	publishMetricDestinations    []Destination
    18  	capabilityMetricDestinations []Destination
    19  	log                          *logger.UPPLogger
    20  }
    21  
    22  // NewAggregator returns an Aggregator which reads metrics from inputChannel and
    23  // distributes them to destinations.
    24  func NewAggregator(inputChannel chan PublishMetric, publishMetricDestinations, capabilityMetricDestinations []Destination, log *logger.UPPLogger) *Aggregator {
    25  	return &Aggregator{
    26  		publishMetricSource:          inputChannel,
    27  		publishMetricDestinations:    publishMetricDestinations,
    28  		capabilityMetricDestinations: capabilityMetricDestinations,
    29  		log:                          log,
    30  	}
    31  }
    32  
    33  // Run reads PublishMetrics from a channel and distributes them to a list of
    34  // Destinations.
    35  // Stops reading when the channel is closed.
    36  func (a *Aggregator) Run() {
    37  	for publishMetric := range a.publishMetricSource {
    38  		if publishMetric.Capability != nil {
    39  			a.log.Infof("Got a E2E metric [%s] in aggregator", publishMetric.String())
    40  			for _, sender := range a.capabilityMetricDestinations {
    41  				go sender.Send(publishMetric)
    42  			}
    43  
    44  			continue
    45  		}
    46  
    47  		for _, sender := range a.publishMetricDestinations {
    48  			go sender.Send(publishMetric)
    49  		}
    50  	}
    51  }