func makeDescMap(pgVersion semver.Version, serverLabels prometheus.Labels, metricMaps map[string]intermediateMetricMap) map[string]MetricMapNamespace {
	var metricMap = make(map[string]MetricMapNamespace)

	for namespace, intermediateMappings := range metricMaps {
		thisMap := make(map[string]MetricMap)

		// Get the constant labels
		var variableLabels []string
		for columnName, columnMapping := range intermediateMappings.columnMappings {
			if columnMapping.usage == LABEL {
				variableLabels = append(variableLabels, columnName)
			}
		}

		for columnName, columnMapping := range intermediateMappings.columnMappings {
			// Check column version compatibility for the current map
			// Force to discard if not compatible.
			if columnMapping.supportedVersions != nil {
				if !columnMapping.supportedVersions(pgVersion) {
					// It's very useful to be able to see what columns are being
					// rejected.
					log.Debugln(columnName, "is being forced to discard due to version incompatibility.")
					thisMap[columnName] = MetricMap{
						discard: true,
						conversion: func(_ interface{}) (float64, bool) {
							return math.NaN(), true
						},
					}
					continue
				}
			}

			// Determine how to convert the column based on its usage.
			// nolint: dupl
			switch columnMapping.usage {
			case DISCARD, LABEL:
				thisMap[columnName] = MetricMap{
					discard: true,
					conversion: func(_ interface{}) (float64, bool) {
						return math.NaN(), true
					},
				}
			case COUNTER:
				thisMap[columnName] = MetricMap{
					vtype: prometheus.CounterValue,
					desc:  prometheus.NewDesc(fmt.Sprintf("%s_%s", namespace, columnName), columnMapping.description, variableLabels, serverLabels),
					conversion: func(in interface{}) (float64, bool) {
						return dbToFloat64(in)
					},
				}
			case GAUGE:
				thisMap[columnName] = MetricMap{
					vtype: prometheus.GaugeValue,
					desc:  prometheus.NewDesc(fmt.Sprintf("%s_%s", namespace, columnName), columnMapping.description, variableLabels, serverLabels),
					conversion: func(in interface{}) (float64, bool) {
						return dbToFloat64(in)
					},
				}
			case MAPPEDMETRIC:
				thisMap[columnName] = MetricMap{
					vtype: prometheus.GaugeValue,
					desc:  prometheus.NewDesc(fmt.Sprintf("%s_%s", namespace, columnName), columnMapping.description, variableLabels, serverLabels),
					conversion: func(in interface{}) (float64, bool) {
						text, ok := in.(string)
						if !ok {
							return math.NaN(), false
						}

						val, ok := columnMapping.mapping[text]
						if !ok {
							return math.NaN(), false
						}
						return val, true
					},
				}
			case DURATION:
				thisMap[columnName] = MetricMap{
					vtype: prometheus.GaugeValue,
					desc:  prometheus.NewDesc(fmt.Sprintf("%s_%s_milliseconds", namespace, columnName), columnMapping.description, variableLabels, serverLabels),
					conversion: func(in interface{}) (float64, bool) {
						var durationString string
						switch t := in.(type) {
						case []byte:
							durationString = string(t)
						case string:
							durationString = t
						default:
							log.Errorln("DURATION conversion metric was not a string")
							return math.NaN(), false
						}

						if durationString == "-1" {
							return math.NaN(), false
						}

						d, err := time.ParseDuration(durationString)
						if err != nil {
							log.Errorln("Failed converting result to metric:", columnName, in, err)
							return math.NaN(), false
						}
						return float64(d / time.Millisecond), true
					},
				}
			}
		}

		metricMap[namespace] = MetricMapNamespace{variableLabels, thisMap, intermediateMappings.master, intermediateMappings.cacheSeconds}
	}

	return metricMap
}
// Turn the MetricMap column mapping into a prometheus descriptor mapping.
Turn the MetricMap column mapping into a prometheus descriptor mapping.
// Turn the MetricMap column mapping into a prometheus descriptor mapping .
func makeDescMap(pgVersion semver.Version, serverLabels prometheus.Labels, metricMaps map[string]intermediateMetricMap) map[string]MetricMapNamespace {
	var metricMap = make(map[string]MetricMapNamespace)
	for namespace, intermediateMappings := range metricMaps {
		thisMap := make(map[string]MetricMap)
		var variableLabels []string
		for columnName, columnMapping := range intermediateMappings.columnMappings {
			if columnMapping.usage == LABEL {
				variableLabels = append(variableLabels, columnName)
			}
		}
		for columnName, columnMapping := range intermediateMappings.columnMappings {
			if columnMapping.supportedVersions != nil {
				if !columnMapping.supportedVersions(pgVersion) {
					log.Debugln(columnName, "is being forced to discard due to version incompatibility.")
					thisMap[columnName] = MetricMap{
						discard: true,
						conversion: func(_ interface{}) (float64, bool) {
							return math.NaN(), true
						},
					}
					continue
				}
			}
			switch columnMapping.usage {
			case DISCARD, LABEL:
				thisMap[columnName] = MetricMap{
					discard: true,
					conversion: func(_ interface{}) (float64, bool) {
						return math.NaN(), true
					},
				}
			case COUNTER:
				thisMap[columnName] = MetricMap{
					vtype: prometheus.CounterValue,
					desc:  prometheus.NewDesc(fmt.Sprintf("%s_%s", namespace, columnName), columnMapping.description, variableLabels, serverLabels),
					conversion: func(in interface{}) (float64, bool) {
						return dbToFloat64(in)
					},
				}
			case GAUGE:
				thisMap[columnName] = MetricMap{
					vtype: prometheus.GaugeValue,
					desc:  prometheus.NewDesc(fmt.Sprintf("%s_%s", namespace, columnName), columnMapping.description, variableLabels, serverLabels),
					conversion: func(in interface{}) (float64, bool) {
						return dbToFloat64(in)
					},
				}
			case MAPPEDMETRIC:
				thisMap[columnName] = MetricMap{
					vtype: prometheus.GaugeValue,
					desc:  prometheus.NewDesc(fmt.Sprintf("%s_%s", namespace, columnName), columnMapping.description, variableLabels, serverLabels),
					conversion: func(in interface{}) (float64, bool) {
						text, ok := in.(string)
						if !ok {
							return math.NaN(), false
						}
						val, ok := columnMapping.mapping[text]
						if !ok {
							return math.NaN(), false
						}
						return val, true
					},
				}
			case DURATION:
				thisMap[columnName] = MetricMap{
					vtype: prometheus.GaugeValue,
					desc:  prometheus.NewDesc(fmt.Sprintf("%s_%s_milliseconds", namespace, columnName), columnMapping.description, variableLabels, serverLabels),
					conversion: func(in interface{}) (float64, bool) {
						var durationString string
						switch t := in.(type) {
						case []byte:
							durationString = string(t)
						case string:
							durationString = t
						default:
							log.Errorln("DURATION conversion metric was not a string")
							return math.NaN(), false
						}
						if durationString == "-1" {
							return math.NaN(), false
						}
						d, err := time.ParseDuration(durationString)
						if err != nil {
							log.Errorln("Failed converting result to metric:", columnName, in, err)
							return math.NaN(), false
						}
						return float64(d / time.Millisecond), true
					},
				}
			}
		}
		metricMap[namespace] = MetricMapNamespace{variableLabels, thisMap, intermediateMappings.master, intermediateMappings.cacheSeconds}
	}
	return metricMap
}
func makeDescMap ( pgVersion semver . Version , serverLabels prometheus . Labels , metricMaps map [ string ] intermediateMetricMap ) map [ string ] MetricMapNamespace { var metricMap = make ( map [ string ] MetricMapNamespace ) for namespace , intermediateMappings := range metricMaps { thisMap := make ( map [ string ] MetricMap ) var variableLabels [ ] string for columnName , columnMapping := range intermediateMappings . columnMappings { if columnMapping . usage == LABEL { variableLabels = append ( variableLabels , columnName ) } } for columnName , columnMapping := range intermediateMappings . columnMappings { if columnMapping . supportedVersions != nil { if ! columnMapping . supportedVersions ( pgVersion ) { log . Debugln ( columnName , " " ) thisMap [ columnName ] = MetricMap { discard : true , conversion : func ( _ interface { } ) ( float64 , bool ) { return math . NaN ( ) , true } , } continue } } switch columnMapping . usage { case DISCARD , LABEL : thisMap [ columnName ] = MetricMap { discard : true , conversion : func ( _ interface { } ) ( float64 , bool ) { return math . NaN ( ) , true } , } case COUNTER : thisMap [ columnName ] = MetricMap { vtype : prometheus . CounterValue , desc : prometheus . NewDesc ( fmt . Sprintf ( " " , namespace , columnName ) , columnMapping . description , variableLabels , serverLabels ) , conversion : func ( in interface { } ) ( float64 , bool ) { return dbToFloat64 ( in ) } , } case GAUGE : thisMap [ columnName ] = MetricMap { vtype : prometheus . GaugeValue , desc : prometheus . NewDesc ( fmt . Sprintf ( " " , namespace , columnName ) , columnMapping . description , variableLabels , serverLabels ) , conversion : func ( in interface { } ) ( float64 , bool ) { return dbToFloat64 ( in ) } , } case MAPPEDMETRIC : thisMap [ columnName ] = MetricMap { vtype : prometheus . GaugeValue , desc : prometheus . NewDesc ( fmt . Sprintf ( " " , namespace , columnName ) , columnMapping . description , variableLabels , serverLabels ) , conversion : func ( in interface { } ) ( float64 , bool ) { text , ok := in . ( string ) if ! ok { return math . NaN ( ) , false } val , ok := columnMapping . mapping [ text ] if ! ok { return math . NaN ( ) , false } return val , true } , } case DURATION : thisMap [ columnName ] = MetricMap { vtype : prometheus . GaugeValue , desc : prometheus . NewDesc ( fmt . Sprintf ( " " , namespace , columnName ) , columnMapping . description , variableLabels , serverLabels ) , conversion : func ( in interface { } ) ( float64 , bool ) { var durationString string switch t := in . ( type ) { case [ ] byte : durationString = string ( t ) case string : durationString = t default : log . Errorln ( " " ) return math . NaN ( ) , false } if durationString == " " { return math . NaN ( ) , false } d , err := time . ParseDuration ( durationString ) if err != nil { log . Errorln ( " " , columnName , in , err ) return math . NaN ( ) , false } return float64 ( d / time . Millisecond ) , true } , } } } metricMap [ namespace ] = MetricMapNamespace { variableLabels , thisMap , intermediateMappings . master , intermediateMappings . cacheSeconds } } return metricMap }
Turn the MetricMap column mapping into a prometheus descriptor mapping.
Turn the MetricMap column mapping into a prometheus descriptor mapping .
// Get the constant labels // Check column version compatibility for the current map // Force to discard if not compatible. // It's very useful to be able to see what columns are being // rejected. // Determine how to convert the column based on its usage. // nolint: dupl