MCPcopy
hub / github.com/prometheus/client_golang / checkSuffixCollisions

Function checkSuffixCollisions

prometheus/registry.go:808–863  ·  view source on GitHub ↗

checkSuffixCollisions checks for collisions with the “magic” suffixes the Prometheus text format and the internal metric representation of the Prometheus server add while flattening Summaries and Histograms.

(mf *dto.MetricFamily, mfs map[string]*dto.MetricFamily)

Source from the content-addressed store, hash-verified

806// Prometheus text format and the internal metric representation of the
807// Prometheus server add while flattening Summaries and Histograms.
808func checkSuffixCollisions(mf *dto.MetricFamily, mfs map[string]*dto.MetricFamily) error {
809 var (
810 newName = mf.GetName()
811 newType = mf.GetType()
812 newNameWithoutSuffix = ""
813 )
814 switch {
815 case strings.HasSuffix(newName, "_count"):
816 newNameWithoutSuffix = newName[:len(newName)-6]
817 case strings.HasSuffix(newName, "_sum"):
818 newNameWithoutSuffix = newName[:len(newName)-4]
819 case strings.HasSuffix(newName, "_bucket"):
820 newNameWithoutSuffix = newName[:len(newName)-7]
821 }
822 if newNameWithoutSuffix != "" {
823 if existingMF, ok := mfs[newNameWithoutSuffix]; ok {
824 switch existingMF.GetType() {
825 case dto.MetricType_SUMMARY:
826 if !strings.HasSuffix(newName, "_bucket") {
827 return fmt.Errorf(
828 "collected metric named %q collides with previously collected summary named %q",
829 newName, newNameWithoutSuffix,
830 )
831 }
832 case dto.MetricType_HISTOGRAM:
833 return fmt.Errorf(
834 "collected metric named %q collides with previously collected histogram named %q",
835 newName, newNameWithoutSuffix,
836 )
837 }
838 }
839 }
840 if newType == dto.MetricType_SUMMARY || newType == dto.MetricType_HISTOGRAM {
841 if _, ok := mfs[newName+"_count"]; ok {
842 return fmt.Errorf(
843 "collected histogram or summary named %q collides with previously collected metric named %q",
844 newName, newName+"_count",
845 )
846 }
847 if _, ok := mfs[newName+"_sum"]; ok {
848 return fmt.Errorf(
849 "collected histogram or summary named %q collides with previously collected metric named %q",
850 newName, newName+"_sum",
851 )
852 }
853 }
854 if newType == dto.MetricType_HISTOGRAM {
855 if _, ok := mfs[newName+"_bucket"]; ok {
856 return fmt.Errorf(
857 "collected histogram named %q collides with previously collected metric named %q",
858 newName, newName+"_bucket",
859 )
860 }
861 }
862 return nil
863}
864
865// checkMetricConsistency checks if the provided Metric is consistent with the

Callers 2

processMetricFunction · 0.85
GatherMethod · 0.85

Calls 1

GetTypeMethod · 0.80

Tested by

no test coverage detected