|
| 1 | +.. zephyr:code-sample:: prometheus |
| 2 | + :name: Prometheus Sample |
| 3 | + :relevant-api: http_service http_server tls_credentials prometheus |
| 4 | + |
| 5 | + Implement a Prometheus Metric Server demonstrating various metric types. |
| 6 | + |
| 7 | +Overview |
| 8 | +-------- |
| 9 | + |
| 10 | +This sample application demonstrates the use of the ``prometheus`` library. |
| 11 | +This library provides prometheus client library(pull method) implementation. |
| 12 | +By integrating this library into your code, you can expose internal metrics |
| 13 | +via an HTTP endpoint on your application's instance, enabling Prometheus to |
| 14 | +scrape and collect the metrics. |
| 15 | + |
| 16 | +Requirement |
| 17 | +----------- |
| 18 | + |
| 19 | +`QEMU Networking <https://docs.zephyrproject.org/latest/connectivity/networking/qemu_setup.html#networking-with-qemu>`_ |
| 20 | + |
| 21 | +Building and running the server |
| 22 | +------------------------------- |
| 23 | + |
| 24 | +To build and run the application: |
| 25 | + |
| 26 | +.. zephyr-app-commands:: |
| 27 | + :zephyr-app: samples/net/prometheus |
| 28 | + :board: <board to use> |
| 29 | + :conf: <config file to use> |
| 30 | + :goals: build |
| 31 | + :compact: |
| 32 | + |
| 33 | +When the server is up, we can make requests to the server using HTTP/1.1. |
| 34 | + |
| 35 | +**With HTTP/1.1:** |
| 36 | + |
| 37 | +- Using a browser: ``http://192.0.2.1/metrics`` |
| 38 | + |
| 39 | +See `Prometheus client library documentation |
| 40 | +<https://prometheus.io/docs/instrumenting/clientlibs/>`_. |
| 41 | + |
| 42 | +Metric Server Customization |
| 43 | +--------------------------- |
| 44 | + |
| 45 | +The server sample contains several parameters that can be customized based on |
| 46 | +the requirements. These are the configurable parameters: |
| 47 | + |
| 48 | +- ``CONFIG_NET_SAMPLE_HTTP_SERVER_SERVICE_PORT``: Configures the service port. |
| 49 | + |
| 50 | +- ``CONFIG_HTTP_SERVER_MAX_CLIENTS``: Defines the maximum number of HTTP/2 |
| 51 | + clients that the server can handle simultaneously. |
| 52 | + |
| 53 | +- ``CONFIG_HTTP_SERVER_MAX_STREAMS``: Specifies the maximum number of HTTP/2 |
| 54 | + streams that can be established per client. |
| 55 | + |
| 56 | +- ``CONFIG_HTTP_SERVER_CLIENT_BUFFER_SIZE``: Defines the buffer size allocated |
| 57 | + for each client. This limits the maximum length of an individual HTTP header |
| 58 | + supported. |
| 59 | + |
| 60 | +- ``CONFIG_HTTP_SERVER_MAX_URL_LENGTH``: Specifies the maximum length of an HTTP |
| 61 | + URL that the server can process. |
| 62 | + |
| 63 | +To customize these options, we can run ``west build -t menuconfig``, which provides |
| 64 | +us with an interactive configuration interface. Then we could navigate from the top-level |
| 65 | +menu to: ``-> Subsystems and OS Services -> Networking -> Network Protocols``. |
| 66 | + |
| 67 | + |
| 68 | +Prometheus Configuration |
| 69 | +------------------------ |
| 70 | + |
| 71 | +.. code-block:: yaml |
| 72 | +
|
| 73 | + scrape_configs: |
| 74 | + - job_name: 'your_server_metrics' |
| 75 | + static_configs: |
| 76 | + - targets: ['your_server_ip:your_server_port'] |
| 77 | + # Optional: Configure scrape interval |
| 78 | + # scrape_interval: 15s |
| 79 | +
|
| 80 | +Replace ``'your_server_metrics'`` with a descriptive name for your job, |
| 81 | +``'your_server_ip'`` with the IP address or hostname of your server, and |
| 82 | +``'your_server_port'`` with the port number where your server exposes Prometheus metrics. |
| 83 | + |
| 84 | +Make sure to adjust the configuration according to your server's setup and requirements. |
| 85 | + |
| 86 | +After updating the configuration, save the file and restart the Prometheus server. |
| 87 | +Once restarted, Prometheus will start scraping metrics from your server according |
| 88 | +to the defined scrape configuration. You can verify that your server's metrics are |
| 89 | +being scraped by checking the Prometheus targets page or querying Prometheus for |
| 90 | +metrics from your server. |
| 91 | + |
| 92 | +See `Prometheus configuration docs |
| 93 | +<https://prometheus.io/docs/prometheus/latest/configuration/configuration>`_. |
0 commit comments