Exemplo n.º 1
0
func poll(client librato.Client) {
	resp, err := http.Get(os.Getenv("HAPROXY_URL"))

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	defer resp.Body.Close()

	reader := csv.NewReader(resp.Body)

	reader.Comment = '#'
	reader.FieldsPerRecord = -1

	data, err := reader.ReadAll()

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	gauges := make([]interface{}, 0)

	for _, row := range data {
		source := os.Getenv("LIBRATO_SOURCE")
		prefix := strings.Join([]string{"haproxy", row[0]}, ".")

		switch row[1] {
		case "BACKEND":
			prefix = strings.Join([]string{prefix, "backend"}, ".")
		case "FRONTEND":
			prefix = strings.Join([]string{prefix, "frontend"}, ".")
		default:
			source = strings.Join([]string{source, row[1]}, ".")
			prefix = strings.Join([]string{prefix, "upstream"}, ".")
		}

		gauges = addGauge(gauges, source, prefix, "qcur", row[2])
		gauges = addGauge(gauges, source, prefix, "qmax", row[3])
		gauges = addGauge(gauges, source, prefix, "scur", row[4])
		gauges = addGauge(gauges, source, prefix, "smax", row[5])
		gauges = addGauge(gauges, source, prefix, "downtime", row[24])

		gauges = addGauge(gauges, source, prefix, "hrsp_1xx", row[39])
		gauges = addGauge(gauges, source, prefix, "hrsp_2xx", row[40])
		gauges = addGauge(gauges, source, prefix, "hrsp_3xx", row[41])
		gauges = addGauge(gauges, source, prefix, "hrsp_4xx", row[42])
		gauges = addGauge(gauges, source, prefix, "hrsp_5xx", row[43])
	}

	metrics := &librato.Metrics{Gauges: gauges}

	err = client.PostMetrics(metrics)
	if err != nil {
		fmt.Println(err)
	}
}
Exemplo n.º 2
0
func sendMetricsLibrato(met *librato.Client, ts time.Time, counters map[string]int64, histograms map[string]metrics.Histogram) error {
	var metrics librato.Metrics
	for name, value := range counters {
		metrics.Counters = append(metrics.Counters, librato.Metric{Name: name, Value: float64(value)})
	}
	for name, hist := range histograms {
		metrics.Gauges = append(metrics.Gauges, librato.Metric{Name: name, Value: hist.Distribution().Mean()})
		for i, p := range hist.Percentiles(percentiles) {
			metrics.Gauges = append(metrics.Gauges,
				librato.Metric{Name: fmt.Sprintf("%s.%s", name, percentileNames[i]), Value: float64(p)})
		}
	}

	return met.PostMetrics(&metrics)
}