1// Copyright 2015 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14// Copyright (c) 2013, The Prometheus Authors
15// All rights reserved.
16//
17// Use of this source code is governed by a BSD-style license that can be found
18// in the LICENSE file.
19
20package prometheus
21
22// Push triggers a metric collection by the default registry and pushes all
23// collected metrics to the Pushgateway specified by url. See the Pushgateway
24// documentation for detailed implications of the job and instance
25// parameter. instance can be left empty. You can use just host:port or ip:port
26// as url, in which case 'http://' is added automatically. You can also include
27// the schema in the URL. However, do not include the '/metrics/jobs/...' part.
28//
29// Note that all previously pushed metrics with the same job and instance will
30// be replaced with the metrics pushed by this call. (It uses HTTP method 'PUT'
31// to push to the Pushgateway.)
32func Push(job, instance, url string) error {
33	return defRegistry.Push(job, instance, url, "PUT")
34}
35
36// PushAdd works like Push, but only previously pushed metrics with the same
37// name (and the same job and instance) will be replaced. (It uses HTTP method
38// 'POST' to push to the Pushgateway.)
39func PushAdd(job, instance, url string) error {
40	return defRegistry.Push(job, instance, url, "POST")
41}
42
43// PushCollectors works like Push, but it does not collect from the default
44// registry. Instead, it collects from the provided collectors. It is a
45// convenient way to push only a few metrics.
46func PushCollectors(job, instance, url string, collectors ...Collector) error {
47	return pushCollectors(job, instance, url, "PUT", collectors...)
48}
49
50// PushAddCollectors works like PushAdd, but it does not collect from the
51// default registry. Instead, it collects from the provided collectors. It is a
52// convenient way to push only a few metrics.
53func PushAddCollectors(job, instance, url string, collectors ...Collector) error {
54	return pushCollectors(job, instance, url, "POST", collectors...)
55}
56
57func pushCollectors(job, instance, url, method string, collectors ...Collector) error {
58	r := newRegistry()
59	for _, collector := range collectors {
60		if _, err := r.Register(collector); err != nil {
61			return err
62		}
63	}
64	return r.Push(job, instance, url, method)
65}
66