-
Notifications
You must be signed in to change notification settings - Fork 8.2k
add prometheus library #72804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
carlescufi
merged 3 commits into
zephyrproject-rtos:main
from
mustafaabdullahk:mustafaabdullahk/prometheus-library
Oct 18, 2024
Merged
add prometheus library #72804
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef ZEPHYR_INCLUDE_PROMETHEUS_COLLECTOR_H_ | ||
| #define ZEPHYR_INCLUDE_PROMETHEUS_COLLECTOR_H_ | ||
|
|
||
| /** | ||
| * @file | ||
| * | ||
| * @brief Prometheus collector APIs. | ||
| * | ||
| * @defgroup prometheus Prometheus API | ||
| * @since 4.0 | ||
| * @version 0.1.0 | ||
| * @ingroup networking | ||
| * @{ | ||
| */ | ||
|
|
||
| #include <zephyr/sys/iterable_sections.h> | ||
| #include <zephyr/net/prometheus/metric.h> | ||
|
|
||
| #include <stddef.h> | ||
|
|
||
| /** | ||
| * @brief Prometheus collector definition | ||
| * | ||
| * This structure defines a Prometheus collector. | ||
| */ | ||
| struct prometheus_collector { | ||
| /** Name of the collector */ | ||
| const char *name; | ||
| /** Array of metrics associated with the collector */ | ||
| struct prometheus_metric *metric[CONFIG_PROMETHEUS_MAX_METRICS]; | ||
| /** Number of metrics associated with the collector */ | ||
| size_t size; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Prometheus Collector definition. | ||
| * | ||
| * This macro defines a Collector. | ||
| * | ||
| * @param _name The collector's name. | ||
| */ | ||
| #define PROMETHEUS_COLLECTOR_DEFINE(_name) \ | ||
| static STRUCT_SECTION_ITERABLE(prometheus_collector, _name) = { \ | ||
| .name = STRINGIFY(_name), .size = 0, .metric = {0}} | ||
|
|
||
| /** | ||
| * @brief Register a metric with a Prometheus collector | ||
| * | ||
| * Registers the specified metric with the given collector. | ||
| * | ||
| * @param collector Pointer to the collector to register the metric with. | ||
| * @param metric Pointer to the metric to register. | ||
| * | ||
| * @return 0 if successful, otherwise a negative error code. | ||
| * @retval -EINVAL Invalid arguments. | ||
| * @retval -ENOMEM Not enough memory to register the metric. | ||
| */ | ||
| int prometheus_collector_register_metric(struct prometheus_collector *collector, | ||
| struct prometheus_metric *metric); | ||
|
|
||
| /** | ||
| * @brief Get a metric from a Prometheus collector | ||
| * | ||
| * Retrieves the metric with the specified name from the given collector. | ||
| * | ||
| * @param collector Pointer to the collector to retrieve the metric from. | ||
| * @param name Name of the metric to retrieve. | ||
| * @return Pointer to the retrieved metric, or NULL if not found. | ||
| */ | ||
| const void *prometheus_collector_get_metric(const struct prometheus_collector *collector, | ||
| const char *name); | ||
|
|
||
| /** | ||
| * @} | ||
| */ | ||
|
|
||
| #endif /* ZEPHYR_INCLUDE_PROMETHEUS_COLLECTOR_H_ */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef ZEPHYR_INCLUDE_PROMETHEUS_COUNTER_H_ | ||
| #define ZEPHYR_INCLUDE_PROMETHEUS_COUNTER_H_ | ||
|
|
||
| /** | ||
| * @file | ||
| * | ||
| * @brief Prometheus counter APIs. | ||
| * | ||
| * @addtogroup prometheus | ||
| * @{ | ||
| */ | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| #include <zephyr/sys/iterable_sections.h> | ||
| #include <zephyr/net/prometheus/metric.h> | ||
|
|
||
| /** | ||
| * @brief Type used to represent a Prometheus counter metric. | ||
| * | ||
| * * References | ||
| * * See https://prometheus.io/docs/concepts/metric_types/#counter | ||
| */ | ||
| struct prometheus_counter { | ||
| /** Base of the Prometheus counter metric */ | ||
| struct prometheus_metric *base; | ||
| /** Value of the Prometheus counter metric */ | ||
| uint64_t value; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Prometheus Counter definition. | ||
| * | ||
| * This macro defines a Counter metric. | ||
| * | ||
| * @param _name The channel's name. | ||
| * @param _detail The metric base. | ||
| * | ||
| * Example usage: | ||
| * @code{.c} | ||
| * | ||
| * struct prometheus_metric http_request_counter = { | ||
| * .type = PROMETHEUS_COUNTER, | ||
| * .name = "http_request_counter", | ||
| * .description = "HTTP request counter", | ||
| * .num_labels = 1, | ||
| * .labels = { | ||
| * { .key = "http_request", .value = "request_count",} | ||
| * }, | ||
| *}; | ||
| * | ||
| * PROMETHEUS_COUNTER_DEFINE(test_counter, &test_counter_metric); | ||
| * @endcode | ||
| */ | ||
| #define PROMETHEUS_COUNTER_DEFINE(_name, _detail) \ | ||
| static STRUCT_SECTION_ITERABLE(prometheus_counter, _name) = {.base = (void *)(_detail), \ | ||
| .value = 0} | ||
|
|
||
| /** | ||
| * @brief Increment the value of a Prometheus counter metric | ||
| * Increments the value of the specified counter metric by one. | ||
| * @param counter Pointer to the counter metric to increment. | ||
| * @return 0 on success, negative errno on error. | ||
| */ | ||
| int prometheus_counter_inc(struct prometheus_counter *counter); | ||
|
|
||
| /** | ||
| * @} | ||
| */ | ||
|
|
||
| #endif /* ZEPHYR_INCLUDE_PROMETHEUS_COUNTER_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef ZEPHYR_INCLUDE_PROMETHEUS_FORMATTER_H_ | ||
| #define ZEPHYR_INCLUDE_PROMETHEUS_FORMATTER_H_ | ||
|
|
||
| /** | ||
| * @file | ||
| * | ||
| * @brief Prometheus formatter APIs. | ||
| * | ||
| * @addtogroup prometheus | ||
| * @{ | ||
| */ | ||
|
|
||
| #include <zephyr/net/prometheus/collector.h> | ||
|
|
||
| /** | ||
| * @brief Format exposition data for Prometheus | ||
| * | ||
| * Formats the exposition data collected by the specified collector into the provided buffer. | ||
| * Function to format metric data according to Prometheus text-based format | ||
| * | ||
| * @param collector Pointer to the collector containing the data to format. | ||
| * @param buffer Pointer to the buffer where the formatted exposition data will be stored. | ||
| * @param buffer_size Size of the buffer. | ||
| * | ||
| * @return 0 on success, negative errno on error. | ||
| */ | ||
| int prometheus_format_exposition(const struct prometheus_collector *collector, char *buffer, | ||
| size_t buffer_size); | ||
|
|
||
| /** | ||
| * @} | ||
| */ | ||
|
|
||
| #endif /* ZEPHYR_INCLUDE_PROMETHEUS_FORMATTER_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef ZEPHYR_INCLUDE_PROMETHEUS_GAUGE_H_ | ||
| #define ZEPHYR_INCLUDE_PROMETHEUS_GAUGE_H_ | ||
|
|
||
| /** | ||
| * @file | ||
| * | ||
| * @brief Prometheus gauge APIs. | ||
| * | ||
| * @addtogroup prometheus | ||
| * @{ | ||
| */ | ||
|
|
||
| #include <zephyr/sys/iterable_sections.h> | ||
| #include <zephyr/net/prometheus/metric.h> | ||
|
|
||
| /** | ||
| * @brief Type used to represent a Prometheus gauge metric. | ||
| * | ||
| * * References | ||
| * * See https://prometheus.io/docs/concepts/metric_types/#gauge | ||
| */ | ||
| struct prometheus_gauge { | ||
| /** Base of the Prometheus gauge metric */ | ||
| struct prometheus_metric *base; | ||
| /** Value of the Prometheus gauge metric */ | ||
| double value; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Prometheus Gauge definition. | ||
| * | ||
| * This macro defines a Gauge metric. | ||
| * | ||
| * @param _name The channel's name. | ||
| * @param _detail The metric base. | ||
| * | ||
| * Example usage: | ||
| * @code{.c} | ||
| * | ||
| * struct prometheus_metric http_request_gauge = { | ||
| * .type = PROMETHEUS_GAUGE, | ||
| * .name = "http_request_gauge", | ||
| * .description = "HTTP request gauge", | ||
| * .num_labels = 1, | ||
| * .labels = { | ||
| * { .key = "http_request", .value = "request_count",} | ||
| * }, | ||
| * }; | ||
| * | ||
| * PROMETHEUS_GAUGE_DEFINE(test_gauge, &test_gauge_metric); | ||
| * | ||
| * @endcode | ||
| */ | ||
| #define PROMETHEUS_GAUGE_DEFINE(_name, _detail) \ | ||
| static STRUCT_SECTION_ITERABLE(prometheus_gauge, _name) = {.base = (void *)(_detail), \ | ||
| .value = 0} | ||
|
|
||
| /** | ||
| * @brief Set the value of a Prometheus gauge metric | ||
| * | ||
| * Sets the value of the specified gauge metric to the given value. | ||
| * | ||
| * @param gauge Pointer to the gauge metric to set. | ||
| * @param value Value to set the gauge metric to. | ||
| * | ||
| * @return 0 on success, -EINVAL if the value is negative. | ||
| */ | ||
| int prometheus_gauge_set(struct prometheus_gauge *gauge, double value); | ||
|
|
||
| /** | ||
| * @} | ||
| */ | ||
|
|
||
| #endif /* ZEPHYR_INCLUDE_PROMETHEUS_GAUGE_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef ZEPHYR_INCLUDE_PROMETHEUS_HISTOGRAM_H_ | ||
| #define ZEPHYR_INCLUDE_PROMETHEUS_HISTOGRAM_H_ | ||
|
|
||
| /** | ||
| * @file | ||
| * | ||
| * @brief Prometheus histogram APIs. | ||
| * | ||
| * @addtogroup prometheus | ||
| * @{ | ||
| */ | ||
|
|
||
| #include <zephyr/sys/iterable_sections.h> | ||
| #include <zephyr/net/prometheus/metric.h> | ||
|
|
||
| #include <stddef.h> | ||
|
|
||
| /** | ||
| * @brief Prometheus histogram bucket definition. | ||
| * | ||
| * This structure defines a Prometheus histogram bucket. | ||
| */ | ||
| struct prometheus_histogram_bucket { | ||
| /** Upper bound value of bucket */ | ||
| double upper_bound; | ||
| /** Cumulative count of observations in the bucket */ | ||
| unsigned long count; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Type used to represent a Prometheus histogram metric. | ||
| * | ||
| * * References | ||
| * * See https://prometheus.io/docs/concepts/metric_types/#histogram | ||
| */ | ||
| struct prometheus_histogram { | ||
| /** Base of the Prometheus histogram metric */ | ||
| struct prometheus_metric *base; | ||
| /** Array of buckets in the histogram */ | ||
| struct prometheus_histogram_bucket *buckets; | ||
| /** Number of buckets in the histogram */ | ||
| size_t num_buckets; | ||
| /** Sum of all observed values in the histogram */ | ||
| double sum; | ||
| /** Total count of observations in the histogram */ | ||
| unsigned long count; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Prometheus Histogram definition. | ||
| * | ||
| * This macro defines a Histogram metric. | ||
| * | ||
| * @param _name The channel's name. | ||
| * @param _detail The metric base. | ||
| * | ||
| * Example usage: | ||
| * @code{.c} | ||
| * | ||
| * struct prometheus_metric http_request_gauge = { | ||
| * .type = PROMETHEUS_HISTOGRAM, | ||
| * .name = "http_request_histograms", | ||
| * .description = "HTTP request histogram", | ||
| * .num_labels = 1, | ||
| * .labels = { | ||
| * { .key = "request_latency", .value = "request_latency_seconds",} | ||
| * }, | ||
| * }; | ||
| * | ||
| * PROMETHEUS_HISTOGRAM_DEFINE(test_histogram, &test_histogram_metric); | ||
| * | ||
| * @endcode | ||
| */ | ||
| #define PROMETHEUS_HISTOGRAM_DEFINE(_name, _detail) \ | ||
| static STRUCT_SECTION_ITERABLE(prometheus_histogram, _name) = {.base = (void *)(_detail), \ | ||
| .buckets = NULL, \ | ||
| .num_buckets = 0, \ | ||
| .sum = 0, \ | ||
| .count = 0} | ||
|
|
||
| /** | ||
| * @brief Observe a value in a Prometheus histogram metric | ||
| * | ||
| * Observes the specified value in the given histogram metric. | ||
| * | ||
| * @param histogram Pointer to the histogram metric to observe. | ||
| * @param value Value to observe in the histogram metric. | ||
| * @return 0 on success, -EINVAL if the value is negative. | ||
| */ | ||
| int prometheus_histogram_observe(struct prometheus_histogram *histogram, double value); | ||
|
|
||
| /** | ||
| * @} | ||
| */ | ||
|
|
||
| #endif /* ZEPHYR_INCLUDE_PROMETHEUS_HISTOGRAM_H_ */ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.