Prometheus Pushgateway: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(11 intermediate revisions by the same user not shown)
Line 8: Line 8:
=Overview=
=Overview=
Push gateways are used in case of applications or short-lived jobs that do not export metrics directly.
Push gateways are used in case of applications or short-lived jobs that do not export metrics directly.
=Installation=
{{Internal|Prometheus Pushgateway Installation|Installation}}
=Sending Metrics=
=Sending Metrics=
{{External|https://github.com/prometheus/pushgateway#command-line}}
Metrics are pushed into the gateway by sending a POST request to:
Metrics are pushed into the gateway by sending a POST request to:
<syntaxhighlight lang='text'>
<syntaxhighlight lang='text'>
http://<gateway-host>:<gateway-port>/metrics/job/<job-name>/instance/<instance-name>
http://<gateway-host>:<gateway-port>/metrics/job/<job-name>/instance/<instance-name>
</syntaxhighlight>
</syntaxhighlight>
The job name and instance name are attached as metadata to the sample, as values for the labels "job" and "instance". At least the job name must be specified. If the instance name is not specified, it will default to the empty string  
The job name and instance name are attached as metadata to the sample, as values for the labels "[[Prometheus_Concepts#Job|job]]" and "[[Prometheus_Concepts#Instance|instance]]". At least the job name must be specified. If the instance name is not specified, it will default to the empty string  
The simplest possible body of the POST request could be:
The simplest possible body of the POST request could be:
<syntaxhighlight lang='text'>
<syntaxhighlight lang='text'>
Line 22: Line 27:
some_metric 12.5
some_metric 12.5
</syntaxhighlight>
</syntaxhighlight>
As such,
<syntaxhighlight lang='text'>
<syntaxhighlight lang='text'>
echo "some_metric 1.2" | curl --data-binary @- http://localhost:9091/metrics/job/test-job/instance/test-instance
echo "some_metric 1.2" | curl --data-binary @- http://localhost:9091/metrics/job/test-job/instance/test-instance
Line 29: Line 35:
some_metric{instance="test-instance",job="test-job"} 1.2
some_metric{instance="test-instance",job="test-job"} 1.2
</syntaxhighlight>
</syntaxhighlight>
 
and
=Installation=
<syntaxhighlight lang='text'>
==Kubernetes==
echo "some_other_metric 1.3" | curl --data-binary @- http://localhost:9091/metrics/job/other-test-job
Deploy the pod and associated services:
</syntaxhighlight>
<syntaxhighlight lang='yaml'>
will produce:
apiVersion: v1
<syntaxhighlight lang='text'>
kind: Pod
some_other_metric{instance="",job="other-test-job"} 1.3
metadata:
</syntaxhighlight>
  name: 'pushgateway'
Multiple metrics with additional labels can be sent as such:
  labels:
<syntaxhighlight lang='bash'>
    function: 'pushgateway'
cat <<EOF | curl --data-binary @- http://localhost:9091/metrics/job/test-job/instance/test-instance
spec:
some_metric{color="blue",size="large"} 12.1
  containers:
some_other_metric{color="green"} 12.2
  - name: 'pushgateway'
EOF
    image: prom/pushgateway
</syntaxhighlight>
---
Alternatively, additional label name and values can be specified in the URL:
apiVersion: v1
<syntaxhighlight lang='bash'>
kind: Service
echo "some_metric 12.3" | curl --data-binary @- http://localhost:9091/metrics/job/test-job/instance/test-instance/color/blue/size/large
metadata:
  name: 'pushgateway'
  labels:
    function: 'pushgateway-target'
spec:
  type: 'ClusterIP'
  selector:
    function: 'pushgateway'
  ports:
    # 'name' is important, it will be referred by ServiceMonitor configuration
    - name: 'pushgateway'
      protocol: 'TCP'
      port: 9091
      targetPort: 9091
---
apiVersion: v1
kind: Service
metadata:
  name: 'pushgateway-lb'
spec:
  type: 'LoadBalancer'
  selector:
    function: 'pushgateway'
  ports:
    - name: 'pushgateway'
      protocol: 'TCP'
      port: 9091
      targetPort: 9091
</syntaxhighlight>
</syntaxhighlight>
Expose it then as a Prometheus scraping [[Prometheus_Concepts#Target|target]] with a [[Prometheus_Operator_Concepts#ServiceMonitor_Resource|ServiceMonitor]]:
=Storing Metrics=
<syntaxhighlight lang='yaml'>
Upon receipt, Prometheus add extra labels, converting the job and instance in "exported_job" and "exported_instance":
apiVersion: monitoring.coreos.com/v1
<syntaxhighlight lang='bash'>
kind: ServiceMonitor
action_queue_depth{exported_job="test-job",exported_instance="test-instance",job="pushgateway",instance="10.1.0.33:9091",endpoint="pushgateway",namespace="prometheus",pod="pushgateway",service="pushgateway"}
metadata:
  name: pushgateway
  namespace: prometheus
  labels:
    release: prometheus
spec:
  endpoints:
  - port: 'pushgateway' # must match the label from Service declaration
    path: /metrics
    interval: 15s
  namespaceSelector:
    matchNames:
    - prometheus
  selector:
    matchLabels:
      function: 'pushgateway-target'
</syntaxhighlight>
</syntaxhighlight>
Also see: {{Internal|Configuring_Prometheus_with_Prometheus_Operator#Add_a_Monitoring_Target|Configuring Prometheus with Prometheus Operator &#124; Add a Monitoring Target}}


=Playground=
=Playground=

Latest revision as of 03:23, 16 October 2020

External

Internal

Overview

Push gateways are used in case of applications or short-lived jobs that do not export metrics directly.

Installation

Installation

Sending Metrics

https://github.com/prometheus/pushgateway#command-line

Metrics are pushed into the gateway by sending a POST request to:

http://<gateway-host>:<gateway-port>/metrics/job/<job-name>/instance/<instance-name>

The job name and instance name are attached as metadata to the sample, as values for the labels "job" and "instance". At least the job name must be specified. If the instance name is not specified, it will default to the empty string The simplest possible body of the POST request could be:

<metric_name> <metric_value>

Example:

some_metric 12.5

As such,

echo "some_metric 1.2" | curl --data-binary @- http://localhost:9091/metrics/job/test-job/instance/test-instance

will produce:

some_metric{instance="test-instance",job="test-job"} 1.2

and

echo "some_other_metric 1.3" | curl --data-binary @- http://localhost:9091/metrics/job/other-test-job

will produce:

some_other_metric{instance="",job="other-test-job"} 1.3

Multiple metrics with additional labels can be sent as such:

cat <<EOF | curl --data-binary @- http://localhost:9091/metrics/job/test-job/instance/test-instance
some_metric{color="blue",size="large"} 12.1
some_other_metric{color="green"} 12.2
EOF

Alternatively, additional label name and values can be specified in the URL:

echo "some_metric 12.3" | curl --data-binary @- http://localhost:9091/metrics/job/test-job/instance/test-instance/color/blue/size/large

Storing Metrics

Upon receipt, Prometheus add extra labels, converting the job and instance in "exported_job" and "exported_instance":

action_queue_depth{exported_job="test-job",exported_instance="test-instance",job="pushgateway",instance="10.1.0.33:9091",endpoint="pushgateway",namespace="prometheus",pod="pushgateway",service="pushgateway"}

Playground

https://github.com/ovidiuf/playground/tree/master/prometheus/pushgateway

Operations

Push a Metric Sample

echo "some_metric 3.14" | curl --data-binary @- http://localhost:9091/metrics/job/some_job

Console

http://localhost:9091/

Query Metrics

Do not use trailing slash otherwise you'll get "Moved Permanently":

curl http://localhost:9091/metrics

The result is the last sample for each timeseries.

Query Health

curl http://localhost:9091/-/healthy

Query Readiness

curl http://localhost:9091/-/ready