github.com/criteo/command-launcher@v0.0.0-20230407142452-fb616f546e98/cmd/metrics/composite.go (about)

     1  package metrics
     2  
     3  import "fmt"
     4  
     5  type compositeMetrics struct {
     6  	metricsList []Metrics
     7  }
     8  
     9  func NewCompositeMetricsCollector(list ...Metrics) Metrics {
    10  	return &compositeMetrics{
    11  		metricsList: list,
    12  	}
    13  }
    14  
    15  func (metrics *compositeMetrics) Collect(uid uint8, repo string, pkg string, group string, name string) error {
    16  	errPool := []error{}
    17  	for _, m := range metrics.metricsList {
    18  		if err := m.Collect(uid, repo, pkg, group, name); err != nil {
    19  			errPool = append(errPool, err)
    20  		}
    21  	}
    22  	if len(errPool) > 0 {
    23  		return fmt.Errorf("multiple errors (%d), displaying first: %v", len(errPool), errPool[0])
    24  	}
    25  	return nil
    26  }
    27  
    28  func (metrics *compositeMetrics) Send(cmdExitCode int, cmdError error) error {
    29  	errPool := []error{}
    30  	for _, m := range metrics.metricsList {
    31  		if err := m.Send(cmdExitCode, cmdError); err != nil {
    32  			errPool = append(errPool, err)
    33  		}
    34  	}
    35  	if len(errPool) > 0 {
    36  		return fmt.Errorf("multiple errors (%d), displaying first: %v", len(errPool), errPool[0])
    37  	}
    38  	return nil
    39  
    40  }