processMetric is an internal helper method only used by the Gather method.
(
metric Metric,
metricFamiliesByName map[string]*dto.MetricFamily,
metricHashes map[uint64]struct{},
registeredDescIDs map[uint64]struct{},
)
| 618 | |
| 619 | // processMetric is an internal helper method only used by the Gather method. |
| 620 | func processMetric( |
| 621 | metric Metric, |
| 622 | metricFamiliesByName map[string]*dto.MetricFamily, |
| 623 | metricHashes map[uint64]struct{}, |
| 624 | registeredDescIDs map[uint64]struct{}, |
| 625 | ) error { |
| 626 | desc := metric.Desc() |
| 627 | // Wrapped metrics collected by an unchecked Collector can have an |
| 628 | // invalid Desc. |
| 629 | if desc.err != nil { |
| 630 | return desc.err |
| 631 | } |
| 632 | dtoMetric := &dto.Metric{} |
| 633 | if err := metric.Write(dtoMetric); err != nil { |
| 634 | return fmt.Errorf("error collecting metric %v: %w", desc, err) |
| 635 | } |
| 636 | metricFamily, ok := metricFamiliesByName[desc.fqName] |
| 637 | if ok { // Existing name. |
| 638 | if metricFamily.GetHelp() != desc.help { |
| 639 | return fmt.Errorf( |
| 640 | "collected metric %s %s has help %q but should have %q", |
| 641 | desc.fqName, dtoMetric, desc.help, metricFamily.GetHelp(), |
| 642 | ) |
| 643 | } |
| 644 | // TODO(beorn7): Simplify switch once Desc has type. |
| 645 | switch metricFamily.GetType() { |
| 646 | case dto.MetricType_COUNTER: |
| 647 | if dtoMetric.Counter == nil { |
| 648 | return fmt.Errorf( |
| 649 | "collected metric %s %s should be a Counter", |
| 650 | desc.fqName, dtoMetric, |
| 651 | ) |
| 652 | } |
| 653 | case dto.MetricType_GAUGE: |
| 654 | if dtoMetric.Gauge == nil { |
| 655 | return fmt.Errorf( |
| 656 | "collected metric %s %s should be a Gauge", |
| 657 | desc.fqName, dtoMetric, |
| 658 | ) |
| 659 | } |
| 660 | case dto.MetricType_SUMMARY: |
| 661 | if dtoMetric.Summary == nil { |
| 662 | return fmt.Errorf( |
| 663 | "collected metric %s %s should be a Summary", |
| 664 | desc.fqName, dtoMetric, |
| 665 | ) |
| 666 | } |
| 667 | case dto.MetricType_UNTYPED: |
| 668 | if dtoMetric.Untyped == nil { |
| 669 | return fmt.Errorf( |
| 670 | "collected metric %s %s should be Untyped", |
| 671 | desc.fqName, dtoMetric, |
| 672 | ) |
| 673 | } |
| 674 | case dto.MetricType_HISTOGRAM: |
| 675 | if dtoMetric.Histogram == nil { |
| 676 | return fmt.Errorf( |
| 677 | "collected metric %s %s should be a Histogram", |
no test coverage detected