func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, server *Server) error {
	log.Debugf("Querying Postgres Version on %q", server)
	versionRow := server.db.QueryRow("SELECT version();")
	var versionString string
	err := versionRow.Scan(&versionString)
	if err != nil {
		return fmt.Errorf("Error scanning version string on %q: %v", server, err)
	}
	semanticVersion, err := parseVersion(versionString)
	if err != nil {
		return fmt.Errorf("Error parsing version string on %q: %v", server, err)
	}
	if !e.disableDefaultMetrics && semanticVersion.LT(lowestSupportedVersion) {
		log.Warnf("PostgreSQL version is lower on %q then our lowest supported version! Got %s minimum supported is %s.", server, semanticVersion, lowestSupportedVersion)
	}

	// Check if semantic version changed and recalculate maps if needed.
	if semanticVersion.NE(server.lastMapVersion) || server.metricMap == nil {
		log.Infof("Semantic Version Changed on %q: %s -> %s", server, server.lastMapVersion, semanticVersion)
		server.mappingMtx.Lock()

		// Get Default Metrics only for master database
		if !e.disableDefaultMetrics && server.master {
			server.metricMap = makeDescMap(semanticVersion, server.labels, e.builtinMetricMaps)
			server.queryOverrides = makeQueryOverrideMap(semanticVersion, queryOverrides)
		} else {
			server.metricMap = make(map[string]MetricMapNamespace)
			server.queryOverrides = make(map[string]string)
		}

		server.lastMapVersion = semanticVersion

		if e.userQueriesPath != "" {
			// Clear the metric while a reload is happening
			e.userQueriesError.Reset()

			// Calculate the hashsum of the useQueries
			userQueriesData, err := ioutil.ReadFile(e.userQueriesPath)
			if err != nil {
				log.Errorln("Failed to reload user queries:", e.userQueriesPath, err)
				e.userQueriesError.WithLabelValues(e.userQueriesPath, "").Set(1)
			} else {
				hashsumStr := fmt.Sprintf("%x", sha256.Sum256(userQueriesData))

				if err := addQueries(userQueriesData, semanticVersion, server); err != nil {
					log.Errorln("Failed to reload user queries:", e.userQueriesPath, err)
					e.userQueriesError.WithLabelValues(e.userQueriesPath, hashsumStr).Set(1)
				} else {
					// Mark user queries as successfully loaded
					e.userQueriesError.WithLabelValues(e.userQueriesPath, hashsumStr).Set(0)
				}
			}
		}

		server.mappingMtx.Unlock()
	}

	// Output the version as a special metric only for master database
	versionDesc := prometheus.NewDesc(fmt.Sprintf("%s_%s", namespace, staticLabelName),
		"Version string as reported by postgres", []string{"version", "short_version"}, server.labels)

	if !e.disableDefaultMetrics && server.master {
		ch <- prometheus.MustNewConstMetric(versionDesc,
			prometheus.UntypedValue, 1, versionString, semanticVersion.String())
	}
	return nil
}
// Check and update the exporters query maps if the version has changed.
Check and update the exporters query maps if the version has changed.
// Check and update the exporters query maps if the version has changed .
func (e *Exporter) checkMapVersions(ch chan<- prometheus.Metric, server *Server) error {
	log.Debugf("Querying Postgres Version on %q", server)
	versionRow := server.db.QueryRow("SELECT version();")
	var versionString string
	err := versionRow.Scan(&versionString)
	if err != nil {
		return fmt.Errorf("Error scanning version string on %q: %v", server, err)
	}
	semanticVersion, err := parseVersion(versionString)
	if err != nil {
		return fmt.Errorf("Error parsing version string on %q: %v", server, err)
	}
	if !e.disableDefaultMetrics && semanticVersion.LT(lowestSupportedVersion) {
		log.Warnf("PostgreSQL version is lower on %q then our lowest supported version! Got %s minimum supported is %s.", server, semanticVersion, lowestSupportedVersion)
	}
	if semanticVersion.NE(server.lastMapVersion) || server.metricMap == nil {
		log.Infof("Semantic Version Changed on %q: %s -> %s", server, server.lastMapVersion, semanticVersion)
		server.mappingMtx.Lock()
		if !e.disableDefaultMetrics && server.master {
			server.metricMap = makeDescMap(semanticVersion, server.labels, e.builtinMetricMaps)
			server.queryOverrides = makeQueryOverrideMap(semanticVersion, queryOverrides)
		} else {
			server.metricMap = make(map[string]MetricMapNamespace)
			server.queryOverrides = make(map[string]string)
		}
		server.lastMapVersion = semanticVersion
		if e.userQueriesPath != "" {
			e.userQueriesError.Reset()
			userQueriesData, err := ioutil.ReadFile(e.userQueriesPath)
			if err != nil {
				log.Errorln("Failed to reload user queries:", e.userQueriesPath, err)
				e.userQueriesError.WithLabelValues(e.userQueriesPath, "").Set(1)
			} else {
				hashsumStr := fmt.Sprintf("%x", sha256.Sum256(userQueriesData))
				if err := addQueries(userQueriesData, semanticVersion, server); err != nil {
					log.Errorln("Failed to reload user queries:", e.userQueriesPath, err)
					e.userQueriesError.WithLabelValues(e.userQueriesPath, hashsumStr).Set(1)
				} else {
					e.userQueriesError.WithLabelValues(e.userQueriesPath, hashsumStr).Set(0)
				}
			}
		}
		server.mappingMtx.Unlock()
	}
	versionDesc := prometheus.NewDesc(fmt.Sprintf("%s_%s", namespace, staticLabelName),
		"Version string as reported by postgres", []string{"version", "short_version"}, server.labels)
	if !e.disableDefaultMetrics && server.master {
		ch <- prometheus.MustNewConstMetric(versionDesc,
			prometheus.UntypedValue, 1, versionString, semanticVersion.String())
	}
	return nil
}
func ( e * Exporter ) checkMapVersions ( ch chan <- prometheus . Metric , server * Server ) error { log . Debugf ( " " , server ) versionRow := server . db . QueryRow ( " " ) var versionString string err := versionRow . Scan ( & versionString ) if err != nil { return fmt . Errorf ( " " , server , err ) } semanticVersion , err := parseVersion ( versionString ) if err != nil { return fmt . Errorf ( " " , server , err ) } if ! e . disableDefaultMetrics && semanticVersion . LT ( lowestSupportedVersion ) { log . Warnf ( " " , server , semanticVersion , lowestSupportedVersion ) } if semanticVersion . NE ( server . lastMapVersion ) || server . metricMap == nil { log . Infof ( " " , server , server . lastMapVersion , semanticVersion ) server . mappingMtx . Lock ( ) if ! e . disableDefaultMetrics && server . master { server . metricMap = makeDescMap ( semanticVersion , server . labels , e . builtinMetricMaps ) server . queryOverrides = makeQueryOverrideMap ( semanticVersion , queryOverrides ) } else { server . metricMap = make ( map [ string ] MetricMapNamespace ) server . queryOverrides = make ( map [ string ] string ) } server . lastMapVersion = semanticVersion if e . userQueriesPath != " " { e . userQueriesError . Reset ( ) userQueriesData , err := ioutil . ReadFile ( e . userQueriesPath ) if err != nil { log . Errorln ( " " , e . userQueriesPath , err ) e . userQueriesError . WithLabelValues ( e . userQueriesPath , " " ) . Set ( 1 ) } else { hashsumStr := fmt . Sprintf ( " " , sha256 . Sum256 ( userQueriesData ) ) if err := addQueries ( userQueriesData , semanticVersion , server ) ; err != nil { log . Errorln ( " " , e . userQueriesPath , err ) e . userQueriesError . WithLabelValues ( e . userQueriesPath , hashsumStr ) . Set ( 1 ) } else { e . userQueriesError . WithLabelValues ( e . userQueriesPath , hashsumStr ) . Set ( 0 ) } } } server . mappingMtx . Unlock ( ) } versionDesc := prometheus . NewDesc ( fmt . Sprintf ( " " , namespace , staticLabelName ) , " " , [ ] string { " " , " " } , server . labels ) if ! e . disableDefaultMetrics && server . master { ch <- prometheus . MustNewConstMetric ( versionDesc , prometheus . UntypedValue , 1 , versionString , semanticVersion . String ( ) ) } return nil }
Check and update the exporters query maps if the version has changed.
Check and update the exporters query maps if the version has changed .
// Check if semantic version changed and recalculate maps if needed. // Get Default Metrics only for master database // Clear the metric while a reload is happening // Calculate the hashsum of the useQueries // Mark user queries as successfully loaded // Output the version as a special metric only for master database