zephyr/samples/net/prometheus
Robert Lubos a61287e876 net: lib: tls_credentials: Rename TLS_CREDENTIAL_SERVER_CERTIFICATE
TLS_CREDENTIAL_SERVER_CERTIFICATE credential type is misleading, as in
fact it just represents a public certificate, it does not matter if the
certificate belongs to a server or a client. And actually, it was
already used in-tree for clients as well, for example in LwM2M.

Therefore rename the credential type to a more generic
TLS_CREDENTIAL_PUBLIC_CERTIFICATE and deprecate the old one.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2025-03-21 14:18:52 +01:00
..
src net: lib: tls_credentials: Rename TLS_CREDENTIAL_SERVER_CERTIFICATE 2025-03-21 14:18:52 +01:00
CMakeLists.txt samples: net: prometheus: Add support for network statistics 2024-11-26 15:42:40 -05:00
Kconfig
prj.conf samples: net: prometheus: do not use deprecated posix kconfig 2025-03-12 19:03:52 +01:00
README.rst
sample.yaml tests/samples: use integration platforms where possible 2025-03-11 11:35:15 +01:00
sections-rom.ld

.. zephyr:code-sample:: prometheus
   :name: Prometheus Sample
   :relevant-api: http_service http_server tls_credentials prometheus

   Implement a Prometheus Metric Server demonstrating various metric types.

Overview
--------

This sample application demonstrates the use of the ``prometheus`` library.
This library provides prometheus client library(pull method) implementation.
By integrating this library into your code, you can expose internal metrics
via an HTTP endpoint on your application's instance, enabling Prometheus to
scrape and collect the metrics.

Requirement
-----------

`QEMU Networking <https://docs.zephyrproject.org/latest/connectivity/networking/qemu_setup.html#networking-with-qemu>`_

Building and running the server
-------------------------------

To build and run the application:

.. zephyr-app-commands::
   :zephyr-app: samples/net/prometheus
   :board: <board to use>
   :conf: <config file to use>
   :goals: build
   :compact:

When the server is up, we can make requests to the server using HTTP/1.1.

**With HTTP/1.1:**

- Using a browser: ``http://192.0.2.1/metrics``

See `Prometheus client library documentation
<https://prometheus.io/docs/instrumenting/clientlibs/>`_.

Metric Server Customization
---------------------------

The server sample contains several parameters that can be customized based on
the requirements. These are the configurable parameters:

- ``CONFIG_NET_SAMPLE_HTTP_SERVER_SERVICE_PORT``: Configures the service port.

- ``CONFIG_HTTP_SERVER_MAX_CLIENTS``: Defines the maximum number of HTTP/2
  clients that the server can handle simultaneously.

- ``CONFIG_HTTP_SERVER_MAX_STREAMS``: Specifies the maximum number of HTTP/2
  streams that can be established per client.

- ``CONFIG_HTTP_SERVER_CLIENT_BUFFER_SIZE``: Defines the buffer size allocated
  for each client. This limits the maximum length of an individual HTTP header
  supported.

- ``CONFIG_HTTP_SERVER_MAX_URL_LENGTH``: Specifies the maximum length of an HTTP
  URL that the server can process.

To customize these options, we can run ``west build -t menuconfig``, which provides
us with an interactive configuration interface. Then we could navigate from the top-level
menu to: ``-> Subsystems and OS Services -> Networking -> Network Protocols``.


Prometheus Configuration
------------------------

.. code-block:: yaml

    scrape_configs:
      - job_name: 'your_server_metrics'
        static_configs:
          - targets: ['your_server_ip:your_server_port']
        # Optional: Configure scrape interval
        # scrape_interval: 15s

Replace ``'your_server_metrics'`` with a descriptive name for your job,
``'your_server_ip'`` with the IP address or hostname of your server, and
``'your_server_port'`` with the port number where your server exposes Prometheus metrics.

Make sure to adjust the configuration according to your server's setup and requirements.

After updating the configuration, save the file and restart the Prometheus server.
Once restarted, Prometheus will start scraping metrics from your server according
to the defined scrape configuration. You can verify that your server's metrics are
being scraped by checking the Prometheus targets page or querying Prometheus for
metrics from your server.

See `Prometheus configuration docs
<https://prometheus.io/docs/prometheus/latest/configuration/configuration>`_.