func (c timingsCollector) Collect(ch chan<- prometheus.Metric) { for k, h := range c.t.Histograms() { if c.nLabels > 1 { labels := split(k) if len(labels) != c.nLabels { err := fmt.Errorf("wrong number of labels in MultiTimings key: %d != %d (key=%q)", len(labels), c.nLabels, k) ch <- prometheus.NewInvalidMetric(c.desc, err) continue } ch <- histogramMetric(c.desc, h, 1/float64(time.Second), labels...) continue } ch <- histogramMetric(c.desc, h, 1/float64(time.Second), k) } }
func (c countersCollector) Collect(ch chan<- prometheus.Metric) { for k, n := range c.c.Counts() { if c.nLabels > 1 { labels := split(k) if len(labels) != c.nLabels { err := fmt.Errorf("wrong number of labels in MultiCounters key: %d != %d (key=%q)", len(labels), c.nLabels, k) ch <- prometheus.NewInvalidMetric(c.desc, err) continue } ch <- prometheus.MustNewConstMetric(c.desc, prometheus.GaugeValue, float64(n), labels...) continue } ch <- prometheus.MustNewConstMetric(c.desc, prometheus.GaugeValue, float64(n), k) } }
// Collect implements Prometheus.Collector. func (c collector) Collect(ch chan<- prometheus.Metric) { start := time.Now() pdus, err := ScrapeTarget(c.target, c.module) if err != nil { log.Errorf("Error scraping target %s: %s", c.target, err) ch <- prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "Error scraping target", nil, nil), err) return } ch <- prometheus.MustNewConstMetric( prometheus.NewDesc("snmp_scrape_walk_duration_seconds", "Time SNMP walk/bulkwalk took.", nil, nil), prometheus.GaugeValue, float64(time.Since(start).Seconds())) ch <- prometheus.MustNewConstMetric( prometheus.NewDesc("snmp_scrape_pdus_returned", "PDUs returned from walk.", nil, nil), prometheus.GaugeValue, float64(len(pdus))) oidToPdu := make(map[string]gosnmp.SnmpPDU, len(pdus)) for _, pdu := range pdus { oidToPdu[pdu.Name[1:]] = pdu } metricTree := buildMetricTree(c.module.Metrics) // Look for metrics that match each pdu. PduLoop: for oid, pdu := range oidToPdu { head := metricTree oidList := oidToList(oid) for i, o := range oidList { var ok bool head, ok = head.children[o] if !ok { continue PduLoop } if head.metric != nil { // Found a match. ch <- pduToSample(oidList[i+1:], &pdu, head.metric, oidToPdu) break } } } ch <- prometheus.MustNewConstMetric( prometheus.NewDesc("snmp_scrape_duration_seconds", "Total SNMP time scrape took (walk and processing).", nil, nil), prometheus.GaugeValue, float64(time.Since(start).Seconds())) }
func (e errorCollector) Collect(ch chan<- prometheus.Metric) { ch <- prometheus.NewInvalidMetric( prometheus.NewDesc("invalid_metric", "not helpful", nil, nil), errors.New("collect error"), ) }