func queryNamespaceMappings(ch chan<- prometheus.Metric, server *Server) map[string]error {
	// Return a map of namespace -> errors
	namespaceErrors := make(map[string]error)

	scrapeStart := time.Now()

	for namespace, mapping := range server.metricMap {
		log.Debugln("Querying namespace: ", namespace)

		if mapping.master && !server.master {
			log.Debugln("Query skipped...")
			continue
		}

		scrapeMetric := false
		// Check if the metric is cached
		server.cacheMtx.Lock()
		cachedMetric, found := server.metricCache[namespace]
		server.cacheMtx.Unlock()
		// If found, check if needs refresh from cache
		if found {
			if scrapeStart.Sub(cachedMetric.lastScrape).Seconds() > float64(mapping.cacheSeconds) {
				scrapeMetric = true
			}
		} else {
			scrapeMetric = true
		}

		var metrics []prometheus.Metric
		var nonFatalErrors []error
		var err error
		if scrapeMetric {
			metrics, nonFatalErrors, err = queryNamespaceMapping(server, namespace, mapping)
		} else {
			metrics = cachedMetric.metrics
		}

		// Serious error - a namespace disappeared
		if err != nil {
			namespaceErrors[namespace] = err
			log.Infoln(err)
		}
		// Non-serious errors - likely version or parsing problems.
		if len(nonFatalErrors) > 0 {
			for _, err := range nonFatalErrors {
				log.Infoln(err.Error())
			}
		}

		// Emit the metrics into the channel
		for _, metric := range metrics {
			ch <- metric
		}

		if scrapeMetric {
			// Only cache if metric is meaningfully cacheable
			if mapping.cacheSeconds > 0 {
				server.cacheMtx.Lock()
				server.metricCache[namespace] = cachedMetrics{
					metrics:    metrics,
					lastScrape: scrapeStart,
				}
				server.cacheMtx.Unlock()
			}
		}
	}

	return namespaceErrors
}
// Iterate through all the namespace mappings in the exporter and run their
// queries.
Iterate through all the namespace mappings in the exporter and run their
queries.
// Iterate through all the namespace mappings in the exporter and run their // queries .
func queryNamespaceMappings(ch chan<- prometheus.Metric, server *Server) map[string]error {
	namespaceErrors := make(map[string]error)
	scrapeStart := time.Now()
	for namespace, mapping := range server.metricMap {
		log.Debugln("Querying namespace: ", namespace)
		if mapping.master && !server.master {
			log.Debugln("Query skipped...")
			continue
		}
		scrapeMetric := false
		server.cacheMtx.Lock()
		cachedMetric, found := server.metricCache[namespace]
		server.cacheMtx.Unlock()
		if found {
			if scrapeStart.Sub(cachedMetric.lastScrape).Seconds() > float64(mapping.cacheSeconds) {
				scrapeMetric = true
			}
		} else {
			scrapeMetric = true
		}
		var metrics []prometheus.Metric
		var nonFatalErrors []error
		var err error
		if scrapeMetric {
			metrics, nonFatalErrors, err = queryNamespaceMapping(server, namespace, mapping)
		} else {
			metrics = cachedMetric.metrics
		}
		if err != nil {
			namespaceErrors[namespace] = err
			log.Infoln(err)
		}
		if len(nonFatalErrors) > 0 {
			for _, err := range nonFatalErrors {
				log.Infoln(err.Error())
			}
		}
		for _, metric := range metrics {
			ch <- metric
		}
		if scrapeMetric {
			if mapping.cacheSeconds > 0 {
				server.cacheMtx.Lock()
				server.metricCache[namespace] = cachedMetrics{
					metrics:    metrics,
					lastScrape: scrapeStart,
				}
				server.cacheMtx.Unlock()
			}
		}
	}
	return namespaceErrors
}
func queryNamespaceMappings ( ch chan <- prometheus . Metric , server * Server ) map [ string ] error { namespaceErrors := make ( map [ string ] error ) scrapeStart := time . Now ( ) for namespace , mapping := range server . metricMap { log . Debugln ( " " , namespace ) if mapping . master && ! server . master { log . Debugln ( " " ) continue } scrapeMetric := false server . cacheMtx . Lock ( ) cachedMetric , found := server . metricCache [ namespace ] server . cacheMtx . Unlock ( ) if found { if scrapeStart . Sub ( cachedMetric . lastScrape ) . Seconds ( ) > float64 ( mapping . cacheSeconds ) { scrapeMetric = true } } else { scrapeMetric = true } var metrics [ ] prometheus . Metric var nonFatalErrors [ ] error var err error if scrapeMetric { metrics , nonFatalErrors , err = queryNamespaceMapping ( server , namespace , mapping ) } else { metrics = cachedMetric . metrics } if err != nil { namespaceErrors [ namespace ] = err log . Infoln ( err ) } if len ( nonFatalErrors ) > 0 { for _ , err := range nonFatalErrors { log . Infoln ( err . Error ( ) ) } } for _ , metric := range metrics { ch <- metric } if scrapeMetric { if mapping . cacheSeconds > 0 { server . cacheMtx . Lock ( ) server . metricCache [ namespace ] = cachedMetrics { metrics : metrics , lastScrape : scrapeStart , } server . cacheMtx . Unlock ( ) } } } return namespaceErrors }
Iterate through all the namespace mappings in the exporter and run their
queries.
Iterate through all the namespace mappings in the exporter and run their queries .
// Return a map of namespace -> errors // Check if the metric is cached // If found, check if needs refresh from cache // Serious error - a namespace disappeared // Non-serious errors - likely version or parsing problems. // Emit the metrics into the channel // Only cache if metric is meaningfully cacheable