func addMetric()

in internal/telemetry/metric_exporter.go [147:212]


func addMetric(sm metricdata.Metrics, ms metricsets) error {
	switch m := sm.Data.(type) {
	case metricdata.Histogram[int64]:
		for _, dp := range m.DataPoints {
			if hist := buildHistogram(dp); hist != nil {
				sample := modelpb.MetricsetSample{
					Name:      sm.Name,
					Type:      modelpb.MetricType_METRIC_TYPE_HISTOGRAM,
					Histogram: hist,
				}
				ms.upsert(dp.Time, dp.Attributes, &sample)
			}
		}
	case metricdata.Histogram[float64]:
		for _, dp := range m.DataPoints {
			if hist := buildHistogram(dp); hist != nil {
				sample := modelpb.MetricsetSample{
					Name:      sm.Name,
					Type:      modelpb.MetricType_METRIC_TYPE_HISTOGRAM,
					Histogram: hist,
				}
				ms.upsert(dp.Time, dp.Attributes, &sample)
			}
		}
	case metricdata.Sum[int64]:
		for _, dp := range m.DataPoints {
			sample := modelpb.MetricsetSample{
				Name:  sm.Name,
				Type:  modelpb.MetricType_METRIC_TYPE_COUNTER,
				Value: float64(dp.Value),
			}
			ms.upsert(dp.Time, dp.Attributes, &sample)
		}
	case metricdata.Sum[float64]:
		for _, dp := range m.DataPoints {
			sample := modelpb.MetricsetSample{
				Name:  sm.Name,
				Type:  modelpb.MetricType_METRIC_TYPE_COUNTER,
				Value: dp.Value,
			}
			ms.upsert(dp.Time, dp.Attributes, &sample)
		}
	case metricdata.Gauge[int64]:
		for _, dp := range m.DataPoints {
			sample := modelpb.MetricsetSample{
				Name:  sm.Name,
				Type:  modelpb.MetricType_METRIC_TYPE_GAUGE,
				Value: float64(dp.Value),
			}
			ms.upsert(dp.Time, dp.Attributes, &sample)
		}
	case metricdata.Gauge[float64]:
		for _, dp := range m.DataPoints {
			sample := modelpb.MetricsetSample{
				Name:  sm.Name,
				Type:  modelpb.MetricType_METRIC_TYPE_GAUGE,
				Value: dp.Value,
			}
			ms.upsert(dp.Time, dp.Attributes, &sample)
		}
	default:
		return fmt.Errorf("unknown metric type %q", m)
	}

	return nil
}