func (g *Prometheus) gatherURL(url string, acc inputs.Accumulator) error { resp, err := http.Get(url) if err != nil { return fmt.Errorf("error making HTTP request to %s: %s", url, err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return fmt.Errorf("%s returned HTTP status %s", url, resp.Status) } format := expfmt.ResponseFormat(resp.Header) decoder := expfmt.NewDecoder(resp.Body, format) options := &expfmt.DecodeOptions{ Timestamp: model.Now(), } sampleDecoder := &expfmt.SampleDecoder{ Dec: decoder, Opts: options, } for { var samples model.Vector err := sampleDecoder.Decode(&samples) if err == io.EOF { break } else if err != nil { return fmt.Errorf("error getting processing samples for %s: %s", url, err) } for _, sample := range samples { tags := make(map[string]string) for key, value := range sample.Metric { if key == model.MetricNameLabel { continue } tags[string(key)] = string(value) } acc.Add("prometheus_"+string(sample.Metric[model.MetricNameLabel]), float64(sample.Value), tags) } } return nil }
func (s *Statsd) Gather(acc inputs.Accumulator) error { s.Lock() defer s.Unlock() for _, metric := range s.timings { acc.Add(metric.name+"_mean", metric.stats.Mean(), metric.tags) acc.Add(metric.name+"_stddev", metric.stats.Stddev(), metric.tags) acc.Add(metric.name+"_upper", metric.stats.Upper(), metric.tags) acc.Add(metric.name+"_lower", metric.stats.Lower(), metric.tags) acc.Add(metric.name+"_count", metric.stats.Count(), metric.tags) for _, percentile := range s.Percentiles { name := fmt.Sprintf("%s_percentile_%v", metric.name, percentile) acc.Add(name, metric.stats.Percentile(percentile), metric.tags) } } if s.DeleteTimings { s.timings = make(map[string]cachedtimings) } for _, metric := range s.gauges { acc.Add(metric.name, metric.value, metric.tags) } if s.DeleteGauges { s.gauges = make(map[string]cachedgauge) } for _, metric := range s.counters { acc.Add(metric.name, metric.value, metric.tags) } if s.DeleteCounters { s.counters = make(map[string]cachedcounter) } for _, metric := range s.sets { acc.Add(metric.name, int64(len(metric.set)), metric.tags) } if s.DeleteSets { s.sets = make(map[string]cachedset) } return nil }
func add(acc inputs.Accumulator, name string, val float64, tags map[string]string) { if val >= 0 { acc.Add(name, val, tags) } }