func (m Metrics) Validate(c *Config) error {
	usedNames := make(map[string]bool)
	for _, def := range m {
		if err := def.Validate(c); err != nil {
			return err
		}
		if usedNames[def.Name] {
			return errors.New(fmt.Sprintf("metric %v: duplicate name: %v", def.Name, def.Name))
		}
		usedNames[def.Name] = true
	}
	return nil
}
Validate checks validity of metric configuration. Specifically, it must not contain duplicate
metric definitions, and metric definitions must specify valid type names.
package config
import (
	"errors"
	"fmt"
	"reflect"
	"github.com/GoogleCloudPlatform/ubbagent/metrics"
)
type metricValidator interface {
	Validate(m *Metric, c *Config) error
}
type Metric struct {
	metrics.Definition `json:",inline"`
	Endpoints          []MetricEndpoint `json:"endpoints"`
	Aggregation *Aggregation `json:"aggregation"`
	Passthrough *Passthrough `json:"passthrough"`
}
func (m *Metric) Validate(c *Config) error {
	if err := m.Definition.Validate(); err != nil {
		return err
	}
	types := 0
	for _, v := range []metricValidator{m.Aggregation, m.Passthrough} {
		if reflect.ValueOf(v).IsNil() {
			continue
		}
		if err := v.Validate(m, c); err != nil {
			return fmt.Errorf("metric %v: %v", m.Name, err)
		}
		types++
	}
	if types == 0 {
		return fmt.Errorf("metric %v: missing buffering configuration", m.Name)
	}
	if types > 1 {
		return fmt.Errorf("metric %v: multiple buffering configurations", m.Name)
	}
	if len(m.Endpoints) == 0 {
		return fmt.Errorf("metric %v: no endpoints defined", m.Name)
	}
	usedEndpoints := make(map[string]bool)
	for _, e := range m.Endpoints {
		if e.Name == "" {
			return fmt.Errorf("metric %v: endpoint missing name", m.Name)
		}
		if !c.Endpoints.exists(e.Name) {
			return fmt.Errorf("metric %v: endpoint does not exist: %v", m.Name, e.Name)
		}
		if usedEndpoints[e.Name] {
			return fmt.Errorf("metric %v: endpoint listed twice: %v", m.Name, e.Name)
		}
		usedEndpoints[e.Name] = true
	}
	return nil
}
type Metrics []Metric
func (m Metrics) GetMetricDefinition(name string) *metrics.Definition {
	for i := range m {
		if m[i].Name == name {
			return &m[i].Definition
		}
	}
	return nil
}