Skip to content

Commit 71cc9e4

Browse files
mustafaabdullahkkrish2718
authored andcommitted
[nrf fromtree] samples: net: add prometheus library sample application
The sample uses the Zephyr HTTP server library and demonstrates the Prometheus metric server node. Prometheus client library runs as a pull method. The sample contains the HTTP request counter and increases when refresh path of '/metrics' from the browser. Signed-off-by: Mustafa Abdullah Kus <[email protected]> (cherry picked from commit 316e823)
1 parent fbffaf7 commit 71cc9e4

File tree

14 files changed

+535
-0
lines changed

14 files changed

+535
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
find_package(Python REQUIRED COMPONENTS Interpreter)
7+
8+
project(prometheus_sample)
9+
10+
if(CONFIG_NET_SOCKETS_SOCKOPT_TLS AND
11+
CONFIG_MBEDTLS_KEY_EXCHANGE_PSK_ENABLED AND
12+
(CONFIG_NET_SAMPLE_PSK_HEADER_FILE STREQUAL "dummy_psk.h"))
13+
add_custom_target(development_psk
14+
COMMAND ${CMAKE_COMMAND} -E echo "----------------------------------------------------------"
15+
COMMAND ${CMAKE_COMMAND} -E echo "--- WARNING: Using dummy PSK! Only suitable for ---"
16+
COMMAND ${CMAKE_COMMAND} -E echo "--- development. Set NET_SAMPLE_PSK_HEADER_FILE to use ---"
17+
COMMAND ${CMAKE_COMMAND} -E echo "--- own pre-shared key. ---"
18+
COMMAND ${CMAKE_COMMAND} -E echo "----------------------------------------------------------"
19+
)
20+
add_dependencies(app development_psk)
21+
endif()
22+
23+
24+
target_sources(app PRIVATE src/main.c)
25+
26+
set(gen_dir ${ZEPHYR_BINARY_DIR}/include/generated/)
27+
28+
target_link_libraries(app PRIVATE zephyr_interface zephyr)
29+
30+
zephyr_linker_sources(SECTIONS sections-rom.ld)
31+
zephyr_linker_section_ifdef(CONFIG_NET_SAMPLE_HTTPS_SERVICE NAME
32+
http_resource_desc_test_https_service
33+
KVMA RAM_REGION GROUP RODATA_REGION
34+
SUBALIGN Z_LINK_ITERABLE_SUBALIGN)
35+
zephyr_linker_section_ifdef(CONFIG_NET_SAMPLE_HTTP_SERVICE NAME
36+
http_resource_desc_test_http_service
37+
KVMA RAM_REGION GROUP RODATA_REGION
38+
SUBALIGN Z_LINK_ITERABLE_SUBALIGN)
39+
40+
foreach(inc_file
41+
ca.der
42+
server.der
43+
server_privkey.der
44+
https-server-cert.der
45+
https-server-key.der
46+
)
47+
generate_inc_file_for_target(
48+
app
49+
src/${inc_file}
50+
${gen_dir}/${inc_file}.inc
51+
)
52+
endforeach()

samples/net/prometheus/Kconfig

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Config options for prometheus sample application
2+
3+
# Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
mainmenu "Prometheus sample application"
7+
8+
config NET_SAMPLE_HTTP_SERVICE
9+
bool "Enable HTTP service"
10+
default y
11+
12+
config NET_SAMPLE_HTTP_SERVER_SERVICE_PORT
13+
int "Port number for HTTP service"
14+
default 80
15+
depends on NET_SAMPLE_HTTP_SERVICE
16+
17+
config NET_SAMPLE_HTTPS_SERVICE
18+
bool "Enable HTTPS service"
19+
depends on NET_SOCKETS_SOCKOPT_TLS || TLS_CREDENTIALS
20+
21+
config NET_SAMPLE_HTTPS_SERVER_SERVICE_PORT
22+
int "Port number for HTTPS service"
23+
default 443
24+
depends on NET_SAMPLE_HTTPS_SERVICE
25+
26+
config NET_SAMPLE_PSK_HEADER_FILE
27+
string "Header file containing PSK"
28+
default "dummy_psk.h"
29+
depends on MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
30+
help
31+
Name of a header file containing a
32+
pre-shared key.
33+
34+
config NET_SAMPLE_CERTS_WITH_SC
35+
bool "Signed certificates"
36+
depends on NET_SOCKETS_SOCKOPT_TLS
37+
help
38+
Enable this flag, if you are interested to run this
39+
application with signed certificates and keys.
40+
41+
source "Kconfig.zephyr"

samples/net/prometheus/README.rst

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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>`_.

samples/net/prometheus/prj.conf

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# General config
2+
CONFIG_MAIN_STACK_SIZE=3072
3+
CONFIG_SHELL=y
4+
CONFIG_LOG=y
5+
CONFIG_ENTROPY_GENERATOR=y
6+
CONFIG_TEST_RANDOM_GENERATOR=y
7+
CONFIG_INIT_STACKS=y
8+
CONFIG_POSIX_MAX_FDS=32
9+
CONFIG_POSIX_API=y
10+
CONFIG_FDTABLE=y
11+
CONFIG_NET_SOCKETS_POLL_MAX=32
12+
CONFIG_REQUIRES_FULL_LIBC=y
13+
CONFIG_HEAP_MEM_POOL_SIZE=2048
14+
CONFIG_ZVFS_OPEN_MAX=32
15+
16+
# Prometheus
17+
CONFIG_PROMETHEUS=y
18+
19+
# Eventfd
20+
CONFIG_EVENTFD=y
21+
22+
# Networking config
23+
CONFIG_NETWORKING=y
24+
CONFIG_NET_IPV4=y
25+
CONFIG_NET_IPV6=y
26+
CONFIG_NET_TCP=y
27+
CONFIG_NET_SOCKETS=y
28+
CONFIG_NET_CONNECTION_MANAGER=y
29+
CONFIG_NET_SHELL=y
30+
CONFIG_NET_LOG=y
31+
CONFIG_NET_DHCPV4=y
32+
33+
# JSON
34+
CONFIG_JSON_LIBRARY=y
35+
36+
# HTTP parser
37+
CONFIG_HTTP_PARSER_URL=y
38+
CONFIG_HTTP_PARSER=y
39+
CONFIG_HTTP_SERVER=y
40+
CONFIG_HTTP_SERVER_WEBSOCKET=y
41+
CONFIG_HTTP_SERVER_MAX_CONTENT_TYPE_LENGTH=128
42+
43+
# Network buffers
44+
CONFIG_NET_PKT_RX_COUNT=16
45+
CONFIG_NET_PKT_TX_COUNT=16
46+
CONFIG_NET_BUF_RX_COUNT=128
47+
CONFIG_NET_BUF_TX_COUNT=128
48+
CONFIG_NET_CONTEXT_NET_PKT_POOL=y
49+
50+
# IP address options
51+
CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3
52+
CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=4
53+
CONFIG_NET_MAX_CONTEXTS=32
54+
CONFIG_NET_MAX_CONN=32
55+
56+
# Network address config
57+
CONFIG_NET_CONFIG_SETTINGS=y
58+
CONFIG_NET_CONFIG_NEED_IPV4=y
59+
CONFIG_NET_CONFIG_NEED_IPV6=y
60+
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
61+
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"
62+
CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2"
63+
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1"
64+
CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2"
65+
66+
# TLS configuration
67+
CONFIG_MBEDTLS=y
68+
CONFIG_MBEDTLS_BUILTIN=y
69+
CONFIG_MBEDTLS_ENABLE_HEAP=y
70+
CONFIG_MBEDTLS_HEAP_SIZE=60000
71+
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=2048
72+
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
73+
CONFIG_NET_SOCKETS_TLS_MAX_CONTEXTS=6
74+
CONFIG_TLS_CREDENTIALS=y
75+
CONFIG_TLS_MAX_CREDENTIALS_NUMBER=5
76+
77+
# Networking tweaks
78+
# Required to handle large number of consecutive connections,
79+
# e.g. when testing with ApacheBench.
80+
CONFIG_NET_TCP_TIME_WAIT_DELAY=0
81+
82+
# Network debug config
83+
CONFIG_NET_SOCKETS_LOG_LEVEL_DBG=n
84+
CONFIG_NET_HTTP_LOG_LEVEL_DBG=n
85+
CONFIG_NET_IPV6_LOG_LEVEL_DBG=n
86+
CONFIG_NET_IPV6_ND_LOG_LEVEL_DBG=n

samples/net/prometheus/sample.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
sample:
2+
description: Prometheus Client Sample
3+
name: prometheus_client_sample
4+
common:
5+
harness: net
6+
min_ram: 192
7+
tags:
8+
- http
9+
- net
10+
- server
11+
- socket
12+
- prometheus
13+
platform_exclude:
14+
- native_posix
15+
- native_posix/native/64
16+
tests:
17+
sample.net.prometheus: {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <zephyr/linker/iterable_sections.h>
2+
3+
ITERABLE_SECTION_ROM(http_resource_desc_test_http_service, Z_LINK_ITERABLE_SUBALIGN)
4+
ITERABLE_SECTION_ROM(http_resource_desc_test_https_service, Z_LINK_ITERABLE_SUBALIGN)

samples/net/prometheus/src/ca.der

783 Bytes
Binary file not shown.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2023 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef __CERTIFICATE_H__
8+
#define __CERTIFICATE_H__
9+
10+
enum tls_tag {
11+
/** The Certificate Authority public key */
12+
HTTP_SERVER_CA_CERTIFICATE_TAG,
13+
/** Used for both the public and private server keys */
14+
HTTP_SERVER_CERTIFICATE_TAG,
15+
/** Used for both the public and private client keys */
16+
HTTP_SERVER_CLIENT_CERTIFICATE_TAG,
17+
PSK_TAG,
18+
};
19+
20+
#if !defined(CONFIG_NET_SAMPLE_CERTS_WITH_SC)
21+
static const unsigned char server_certificate[] = {
22+
#include "https-server-cert.der.inc"
23+
};
24+
25+
/* This is the private key in pkcs#8 format. */
26+
static const unsigned char private_key[] = {
27+
#include "https-server-key.der.inc"
28+
};
29+
30+
#else
31+
32+
static const unsigned char ca_certificate[] = {
33+
#include "ca.der.inc"
34+
};
35+
36+
static const unsigned char server_certificate[] = {
37+
#include "server.der.inc"
38+
};
39+
40+
/* This is the private key in pkcs#8 format. */
41+
static const unsigned char private_key[] = {
42+
#include "server_privkey.der.inc"
43+
};
44+
#endif
45+
46+
#if defined(CONFIG_MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
47+
#include CONFIG_NET_SAMPLE_PSK_HEADER_FILE
48+
#endif
49+
50+
#endif /* __CERTIFICATE_H__ */
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) 2019 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef __DUMMY_PSK_H__
8+
#define __DUMMY_PSK_H__
9+
10+
static const unsigned char psk[] = {0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
11+
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
12+
static const char psk_id[] = "PSK_identity";
13+
14+
#endif /* __DUMMY_PSK_H__ */
767 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)