func ExampleAddFromGatherer() {
	registry := prometheus.NewRegistry()
	registry.MustRegister(completionTime, duration, records)
	// Note that successTime is not registered at this time.

	start := time.Now()
	n, err := performBackup()
	records.Set(float64(n))
	duration.Set(time.Since(start).Seconds())
	completionTime.SetToCurrentTime()
	if err != nil {
		fmt.Println("DB backup failed:", err)
	} else {
		// Only now register successTime.
		registry.MustRegister(successTime)
		successTime.SetToCurrentTime()
	}
	// AddFromGatherer is used here rather than FromGatherer to not delete a
	// previously pushed success timestamp in case of a failure of this
	// backup.
	if err := push.AddFromGatherer(
		"db_backup", nil,
		"http://pushgateway:9091",
		registry,
	); err != nil {
		fmt.Println("Could not push to Pushgateway:", err)
	}
}
Example #2
0
File: main.go Project: pingcap/tidb
// PrometheusPushClient pushs metrics to Prometheus Pushgateway.
func prometheusPushClient(addr string, interval time.Duration) {
	// TODO: TiDB do not have uniq name, so we use host+port to compose a name.
	job := "tidb"
	for {
		err := push.AddFromGatherer(
			job, push.HostnameGroupingKey(),
			addr,
			prometheus.DefaultGatherer,
		)
		if err != nil {
			log.Errorf("could not push metrics to Prometheus Pushgateway: %v", err)
		}
		time.Sleep(interval)
	}
}
Example #3
0
// pushMetrics attempts to send some new metrics to the server. It returns the new number of errors.
func (m *metrics) pushMetrics() int {
	if !m.newMetrics {
		return m.errors
	}
	start := time.Now()
	m.newMetrics = false
	if err := deadline(func() error {
		return push.AddFromGatherer("please", push.HostnameGroupingKey(), m.url, prometheus.DefaultGatherer)
	}, 500*time.Millisecond); err != nil {
		log.Warning("Could not push metrics to the repository: %s", err)
		m.newMetrics = true
		return m.errors + 1
	}
	m.pushes += 1
	log.Debug("Push #%d of metrics in %0.3fs", m.pushes, time.Since(start).Seconds())
	return 0
}