Skip to content

Add prom_counter_set() #81

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions autolib/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ autolib_build() {
pushd ${lib}/build > /dev/null || return $?
autolib_output_banner "${lib}: CMake Build Stage"
# YOU MUST set TEST to 1 in order to build the tests
TEST=$build_test cmake -v .. || {
TEST=$build_test cmake --log-level=VERBOSE .. || {
autolib_output_error "${lib}: CMake Failure"
return 1
}
Expand All @@ -29,4 +29,4 @@ autolib_build() {
return 1
}
popd > /dev/null || return $?
}
}
6 changes: 5 additions & 1 deletion prom/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ target_sources(
PRIVATE ${private_files}
)

target_link_libraries(prom PUBLIC Threads::Threads)
if(UNIX AND NOT APPLE)
target_link_libraries(prom PUBLIC Threads::Threads -latomic)
else()
target_link_libraries(prom PUBLIC Threads::Threads )
endif()

if ($ENV{TEST})
include(test/CMakeLists.txt)
Expand Down
19 changes: 19 additions & 0 deletions prom/include/prom_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,23 @@ int prom_counter_inc(prom_counter_t *self, const char **label_values);
*/
int prom_counter_add(prom_counter_t *self, double r_value, const char **label_values);

/**
* @brief Set the value to the prom_counter_t*. A non-zero integer value will be returned on failure.
* @param self The target prom_counter_t*
* @param r_value The double to set to the prom_counter_t passed as self. The value MUST be greater than or equal to 0.
* @param label_values The label values associated with the metric sample being updated. The number of labels must
* match the value passed to label_key_count in the counter's constructor. If no label values are
* necessary, pass NULL. Otherwise, It may be convenient to pass this value as a literal.
* @return A non-zero integer value upon failure.
*
* *Example*
*
* // An example with labels
* prom_counter_set(foo_counter, 22, (const char**) { "bar", "bang" });
*
* // An example without labels
* prom_counter_set(foo_counter, 22, NULL);
*/
int prom_counter_set(prom_counter_t *self, double r_value, const char **label_values);

#endif // PROM_COUNTER_H
12 changes: 12 additions & 0 deletions prom/src/prom_counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,15 @@ int prom_counter_add(prom_counter_t *self, double r_value, const char **label_va
if (sample == NULL) return 1;
return prom_metric_sample_add(sample, r_value);
}

int prom_counter_set(prom_counter_t *self, double r_value, const char **label_values) {
PROM_ASSERT(self != NULL);
if (self == NULL) return 1;
if (self->type != PROM_COUNTER) {
PROM_LOG(PROM_METRIC_INCORRECT_TYPE);
return 1;
}
prom_metric_sample_t *sample = prom_metric_sample_from_labels(self, label_values);
if (sample == NULL) return 1;
return prom_metric_sample_set(sample, r_value);
}
7 changes: 4 additions & 3 deletions prom/src/prom_metric_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int prom_metric_sample_add(prom_metric_sample_t *self, double r_value) {
if (r_value < 0) {
return 1;
}
_Atomic double old = atomic_load(&self->r_value);
double old = atomic_load(&self->r_value);
for (;;) {
_Atomic double new = ATOMIC_VAR_INIT(old + r_value);
if (atomic_compare_exchange_weak(&self->r_value, &old, new)) {
Expand All @@ -78,7 +78,7 @@ int prom_metric_sample_sub(prom_metric_sample_t *self, double r_value) {
PROM_LOG(PROM_METRIC_INCORRECT_TYPE);
return 1;
}
_Atomic double old = atomic_load(&self->r_value);
double old = atomic_load(&self->r_value);
for (;;) {
_Atomic double new = ATOMIC_VAR_INIT(old - r_value);
if (atomic_compare_exchange_weak(&self->r_value, &old, new)) {
Expand All @@ -88,10 +88,11 @@ int prom_metric_sample_sub(prom_metric_sample_t *self, double r_value) {
}

int prom_metric_sample_set(prom_metric_sample_t *self, double r_value) {
if (self->type != PROM_GAUGE) {
if ((self->type == PROM_COUNTER) && (r_value < self->r_value)) {
PROM_LOG(PROM_METRIC_INCORRECT_TYPE);
return 1;
}

atomic_store(&self->r_value, r_value);
return 0;
}
10 changes: 10 additions & 0 deletions promhttp/include/promhttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
*/
void promhttp_set_active_collector_registry(prom_collector_registry_t *active_registry);

#if MHD_VERSION >= 0x00097002
#define PROM_MHD_RESULT enum MHD_Result
#else
#define PROM_MHD_RESULT int
#endif

PROM_MHD_RESULT promhttp_handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method,
const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls);


/**
* @brief Starts a daemon in the background and returns a pointer to an HMD_Daemon.
*
Expand Down
3 changes: 2 additions & 1 deletion promhttp/src/promhttp.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "microhttpd.h"
#include "prom.h"
#include "promhttp.h"

prom_collector_registry_t *PROM_ACTIVE_REGISTRY;

Expand All @@ -29,7 +30,7 @@ void promhttp_set_active_collector_registry(prom_collector_registry_t *active_re
}
}

int promhttp_handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method,
PROM_MHD_RESULT promhttp_handler(void *cls, struct MHD_Connection *connection, const char *url, const char *method,
const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls) {
if (strcmp(method, "GET") != 0) {
char *buf = "Invalid HTTP Method\n";
Expand Down