Skip to content

Commit fe93e0d

Browse files
author
Azfaar Qureshi
committed
adding README
adding sample app adding examples readme fixing lint errors linting examples updating readme tls_config example excluding examples adding examples to exclude in all linters adding isort.cfg skip changing isort to path ignoring yml only adding it to excluded directories in pylintrc only adding exclude to directory removing readme.rst and adding explicit file names to ignore adding the rest of the files adding readme.rst back adding to ignore glob instead reverting back to ignore list converting README.md to README.rst
1 parent 5c646b8 commit fe93e0d

File tree

10 files changed

+571
-19
lines changed

10 files changed

+571
-19
lines changed

.flake8

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ignore =
44
F401 # unused import, defer to pylint
55
W503 # allow line breaks before binary ops
66
W504 # allow line breaks after binary ops
7-
E203 # allow whitespace before ':' (https://github.com/psf/black#slices)
7+
E203 # allow whitespace before ':' (https://github.com/psf/black#slices)
88
exclude =
99
.bzr
1010
.git

.pylintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extension-pkg-whitelist=
77

88
# Add list of files or directories to be excluded. They should be base names, not
99
# paths.
10-
ignore=CVS,gen
10+
ignore=CVS,gen,Dockerfile,docker-compose.yml,README.md,requirements.txt,cortex-config.yml
1111

1212
# Add files or directories matching the regex patterns to be excluded. The
1313
# regex matches against base names, not paths.
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,236 @@
1-
OpenTelemetry Prometheus Remote Write Exporter
2-
==============================================
1+
OpenTelemetry Python SDK Prometheus Remote Write Exporter
2+
=========================================================
33

4-
This library allows exporting metric data to `Prometheus Remote Write Integrated Backends
5-
<https://prometheus.io/docs/operating/integrations/>`_. Latest `types.proto
6-
<https://github.com/prometheus/prometheus/blob/master/prompb/types.proto>` and `remote.proto
7-
<https://github.com/prometheus/prometheus/blob/master/prompb/remote.proto>` Protocol Buffers
8-
used to create WriteRequest objects were taken from Prometheus repository. Development is
9-
currently in progress.
4+
This package contains an exporter to send `OTLP`_ metrics from the
5+
Python SDK directly to a Prometheus Remote Write integrated backend
6+
(such as Cortex or Thanos) without having to run an instance of the
7+
Prometheus server. The image below shows the two Prometheus exporters in
8+
the OpenTelemetry Python SDK.
9+
10+
Pipeline 1 illustrates the setup required for a Prometheus "pull"
11+
exporter.
12+
13+
Pipeline 2 illustrates the setup required for the Prometheus Remote
14+
Write exporter.
15+
16+
|Prometheus SDK pipelines|
17+
18+
The Prometheus Remote Write Exporter is a "push" based exporter and only
19+
works with the OpenTelemetry `push controller`_. The controller
20+
periodically collects data and passes it to the exporter. This exporter
21+
then converts the data into `timeseries`_ and sends it to the Remote
22+
Write integrated backend through HTTP POST requests. The metrics
23+
collection datapath is shown below:
24+
25+
|controller_datapath_final|
26+
27+
See the ``example`` folder for a demo usage of this exporter
28+
29+
Table of Contents
30+
=================
31+
32+
- `Summary`_
33+
- `Table of Contents`_
34+
35+
- `Installation`_
36+
- `Quickstart`_
37+
- `Configuring the Exporter`_
38+
- `Securing the Exporter`_
39+
40+
- `Authentication`_
41+
- `TLS`_
42+
43+
- `Supported Aggregators`_
44+
- `Error Handling`_
45+
- `Retry Logic`_
46+
- `Contributing`_
47+
48+
- `Design Doc`_
1049

1150
Installation
1251
------------
52+
- To install from the latest PyPi release, run
53+
``pip install opentelemetry-exporter-prometheus-remote-write``
54+
- To install from the local repository, run
55+
``pip install -e exporter/opentelemetry-exporter-prometheus-remote-write/``
56+
in the project root
57+
58+
Quickstart
59+
----------
60+
61+
.. code:: python
62+
63+
from opentelemetry import metrics
64+
from opentelemetry.sdk.metrics import MeterProvider
65+
from opentelemetry.exporter.prometheus_remote_write import (
66+
PrometheusRemoteWriteMetricsExporter
67+
)
68+
69+
# Sets the global MeterProvider instance
70+
metrics.set_meter_provider(MeterProvider())
71+
72+
# The Meter is responsible for creating and recording metrics. Each meter has a unique name, which we set as the module's name here.
73+
meter = metrics.get_meter(__name__)
74+
75+
exporter = PrometheusRemoteWriteMetricsExporter(endpoint="endpoint_here") # add other params as needed
76+
77+
metrics.get_meter_provider().start_pipeline(meter, exporter, 5)
78+
79+
Configuring the Exporter
80+
------------------------
81+
82+
The exporter can be configured through parameters passed to the
83+
constructor. Here are all the options:
84+
85+
- ``endpoint``: url where data will be sent **(Required)**
86+
- ``basic_auth``: username and password for authentication
87+
**(Optional)**
88+
- ``headers``: additional headers for remote write request as
89+
determined by the remote write backend's API **(Optional)**
90+
- ``timeout``: timeout for requests to the remote write endpoint in
91+
seconds **(Optional)**
92+
- ``proxies``: dict mapping request proxy protocols to proxy urls
93+
**(Optional)**
94+
- ``tls_config``: configuration for remote write TLS settings
95+
**(Optional)**
96+
97+
Example with all the configuration options:
98+
99+
.. code:: python
100+
101+
exporter = PrometheusRemoteWriteMetricsExporter(
102+
endpoint="http://localhost:9009/api/prom/push",
103+
timeout=30,
104+
basic_auth={
105+
"username": "user",
106+
"password": "pass123",
107+
},
108+
headers={
109+
"X-Scope-Org-ID": "5",
110+
"Authorization": "Bearer mytoken123",
111+
},
112+
proxies={
113+
"http": "http://10.10.1.10:3000",
114+
"https": "http://10.10.1.10:1080",
115+
},
116+
tls_config={
117+
"cert_file": "path/to/file",
118+
"key_file": "path/to/file",
119+
"ca_file": "path_to_file",
120+
"insecure_skip_verify": true, # for developing purposes
121+
}
122+
)
123+
124+
Securing the Exporter
125+
---------------------
126+
127+
Authentication
128+
~~~~~~~~~~~~~~
129+
130+
The exporter provides two forms of authentication which are shown below.
131+
Users can add their own custom authentication by setting the appropriate
132+
values in the ``headers`` dictionary
133+
134+
1. Basic Authentication Basic authentication sets a HTTP Authorization
135+
header containing a base64 encoded username/password pair. See `RFC
136+
7617`_ for more information. This
137+
138+
.. code:: python
139+
140+
exporter = PrometheusRemoteWriteMetricsExporter(
141+
basic_auth={"username": "base64user", "password": "base64pass"}
142+
)
143+
144+
2. Bearer Token Authentication This custom configuration can be achieved
145+
by passing in a custom ``header`` to the constructor. See `RFC 6750`_
146+
for more information.
147+
148+
.. code:: python
149+
150+
header = {
151+
"Authorization": "Bearer mytoken123"
152+
}
153+
154+
TLS
155+
~~~
156+
157+
Users can add TLS to the exporter's HTTP Client by providing certificate
158+
and key files in the ``tls_config`` parameter.
159+
160+
Supported Aggregators
161+
---------------------
162+
163+
- Sum
164+
- MinMaxSumCount
165+
- Histogram
166+
- LastValue
167+
- ValueObserver
168+
169+
Error Handling
170+
--------------
171+
172+
In general, errors are raised by the calling function. The exception is
173+
for failed requests where any error status code is logged as a warning
174+
instead.
175+
176+
This is because the exporter does not implement any retry logic as it
177+
sends cumulative metrics data. This means that data will be preserved
178+
even if some exports fail.
179+
180+
For example, consider a situation where a user increments a Counter
181+
instrument 5 times and an export happens between each increment. If the
182+
exports happen like so:
13183

14184
::
15185

16-
pip install opentelemetry-exporter-prometheus-remote-write
186+
SUCCESS FAIL FAIL SUCCESS SUCCESS
187+
1 2 3 4 5
17188

189+
Then the recieved data will be:
18190

19-
.. _Prometheus: https://prometheus.io/
20-
.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/
191+
::
21192

193+
1 4 5
22194

23-
References
24-
----------
195+
The end result is the same since the aggregations are cumulative
196+
Contributing
197+
------------
198+
199+
This exporter's datapath is as follows:
200+
201+
|Exporter datapath| *Entites with ``*`` after their name are not actual
202+
classes but rather logical groupings of functions within the exporter.*
203+
204+
If you would like to learn more about the exporter's structure and
205+
design decisions please view the design document below
206+
207+
Design Doc
208+
~~~~~~~~~~
209+
210+
`Design Document`_
211+
212+
This document is stored elsewhere as it contains large images which will
213+
significantly increase the size of this repo.
25214

26-
* `Prometheus <https://prometheus.io/>`_
27-
* `OpenTelemetry Project <https://opentelemetry.io/>`_
215+
.. _Design Document: https://github.com/open-o11y/docs/tree/master/python-prometheus-remote-write
216+
.. |Exporter datapath| image:: https://user-images.githubusercontent.com/20804975/100285717-604c7280-2f3f-11eb-9b73-bdf70afce9dd.png
217+
.. _OTLP: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/protocol/otlp.md
218+
.. _push controller: https://github.com/open-telemetry/opentelemetry-python/blob/master/opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/controller.py
219+
.. _`timeseries`: https://prometheus.io/docs/concepts/data_model/
220+
.. _Summary: #opentelemetry-python-sdk-prometheus-remote-write-exporter
221+
.. _Table of Contents: #table-of-contents
222+
.. _Installation: #installation
223+
.. _Quickstart: #quickstart
224+
.. _Configuring the Exporter: #configuring-the-exporter
225+
.. _Securing the Exporter: #securing-the-exporter
226+
.. _Authentication: #authentication
227+
.. _TLS: #tls
228+
.. _Supported Aggregators: #supported-aggregators
229+
.. _Error Handling: #error-handling
230+
.. _Retry Logic: #retry-logic
231+
.. _Contributing: #contributing
232+
.. _Design Doc: #design-doc
233+
.. |Prometheus SDK pipelines| image:: https://user-images.githubusercontent.com/20804975/100285430-e320fd80-2f3e-11eb-8217-a562c559153c.png
234+
.. |controller_datapath_final| image:: https://user-images.githubusercontent.com/20804975/100486582-79d1f380-30d2-11eb-8d17-d3e58e5c34e9.png
235+
.. _RFC 7617: https://tools.ietf.org/html/rfc7617
236+
.. _RFC 6750: https://tools.ietf.org/html/rfc6750
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM python:3.7
2+
WORKDIR /code
3+
4+
COPY . .
5+
RUN apt-get update -y && apt-get install libsnappy-dev -y
6+
RUN pip install -e .
7+
RUN pip install -r ./examples/requirements.txt
8+
CMD ["python", "./examples/sampleapp.py"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Prometheus Remote Write Exporter Example
2+
This example uses [Docker Compose](https://docs.docker.com/compose/) to set up:
3+
4+
1. A Python program that creates 5 instruments with 5 unique
5+
aggregators and a randomized load generator
6+
2. An instance of [Cortex](https://cortexmetrics.io/) to recieve the metrics
7+
data
8+
3. An instance of [Grafana](https://grafana.com/) to visualizse the exported
9+
data
10+
11+
## Requirements
12+
* Have Docker Compose [installed](https://docs.docker.com/compose/install/)
13+
14+
*Users do not need to install Python as the app will be run in the Docker Container*
15+
16+
## Instructions
17+
1. Run `docker-compose up -d` in the the `examples/` directory
18+
19+
The `-d` flag causes all services to run in detached mode and frees up your
20+
terminal session. This also causes no logs to show up. Users can attach themselves to the service's logs manually using `docker logs ${CONTAINER_ID} --follow`
21+
22+
2. Log into the Grafana instance at [http://localhost:3000](http://localhost:3000)
23+
* login credentials are `username: admin` and `password: admin`
24+
* There may be an additional screen on setting a new password. This can be skipped and is optional
25+
26+
3. Navigate to the `Data Sources` page
27+
* Look for a gear icon on the left sidebar and select `Data Sources`
28+
29+
4. Add a new Prometheus Data Source
30+
* Use `http://cortex:9009/api/prom` as the URL
31+
* (OPTIONAl) set the scrape interval to `2s` to make updates appear quickly
32+
* click `Save & Test`
33+
34+
5. Go to `Metrics Explore` to query metrics
35+
* Look for a compass icon on the left sidebar
36+
* click `Metrics` for a dropdown list of all the available metrics
37+
* (OPTIONAL) Adjust time range by clicking the `Last 6 hours` button on the upper right side of the graph
38+
* (OPTIONAL) Set up auto-refresh by selecting an option under the dropdown next to the refresh button on the upper right side of the graph
39+
* Click the refresh button and data should show up on hte graph
40+
41+
6. Shutdown the services when finished
42+
* Run `docker-compose down` in the examples directory

0 commit comments

Comments
 (0)