Skip to content

Add prometheus-client hooks #3

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 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion include/mgos_mqtt.h
Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@

#ifndef CS_FW_SRC_MGOS_MQTT_H_
#define CS_FW_SRC_MGOS_MQTT_H_

#include <stdbool.h>

#include "mgos_features.h"
38 changes: 38 additions & 0 deletions src/mgos_mqtt.c
Original file line number Diff line number Diff line change
@@ -19,6 +19,9 @@
#include "mgos_sys_config.h"
#include "mgos_timers.h"
#include "mgos_utils.h"
#if MGOS_HAVE_PROMETHEUS_METRICS
#include "mgos_prometheus_metrics.h"
#endif // MGOS_HAVE_PROMETHEUS_METRICS

#ifndef MGOS_MQTT_LOG_PUSHBACK_THRESHOLD
#define MGOS_MQTT_LOG_PUSHBACK_THRESHOLD 2048
@@ -28,6 +31,29 @@
#define MGOS_MQTT_SUBSCRIBE_QOS 1
#endif

#if MGOS_HAVE_PROMETHEUS_METRICS
static uint32_t metrics_mqtt_sent_topics_count = 0;
static uint32_t metrics_mqtt_sent_topics_bytes_total = 0;
static uint32_t metrics_mqtt_received_topics_count = 0;
static uint32_t metrics_mqtt_received_topics_bytes_total = 0;

static void metrics_mqtt(struct mg_connection *nc, void *user_data) {
mgos_prometheus_metrics_printf(nc, COUNTER, "mgos_mqtt_sent_topics_count", "MQTT topics sent",
"%u", metrics_mqtt_sent_topics_count);

mgos_prometheus_metrics_printf(nc, COUNTER, "mgos_mqtt_sent_topics_bytes_total", "Total bytes sent in MQTT topics",
"%u", metrics_mqtt_sent_topics_bytes_total);

mgos_prometheus_metrics_printf(nc, COUNTER, "mgos_mqtt_received_topics_count", "MQTT topics sent",
"%u", metrics_mqtt_received_topics_count);

mgos_prometheus_metrics_printf(nc, COUNTER, "mgos_mqtt_received_topics_bytes_total", "Total bytes received in MQTT topics",
"%u", metrics_mqtt_received_topics_bytes_total);

(void) user_data;
}
#endif // MGOS_HAVE_PROMETHEUS_METRICS

struct topic_handler {
struct mg_str topic;
mg_event_handler_t handler;
@@ -282,6 +308,10 @@ bool mgos_mqtt_init(void) {

mgos_hook_register(MGOS_HOOK_DEBUG_WRITE, s_debug_write_hook, NULL);

#if MGOS_HAVE_PROMETHEUS_METRICS
mgos_prometheus_metrics_add_handler(metrics_mqtt, NULL);
#endif

return true;
}

@@ -363,6 +393,10 @@ bool mgos_mqtt_pub(const char *topic, const void *message, size_t len, int qos,
(retain ? " (RETAIN)" : ""), (int) len, (int) len,
(const char *) message));
mg_mqtt_publish(c, topic, mgos_mqtt_get_packet_id(), flags, message, len);
#if MGOS_HAVE_PROMETHEUS_METRICS
metrics_mqtt_sent_topics_count++;
metrics_mqtt_sent_topics_bytes_total+=len;
#endif // MGOS_HAVE_PROMETHEUS_METRICS
return true;
}

@@ -378,6 +412,10 @@ static void mqttsubtrampoline(struct mg_connection *c, int ev, void *ev_data,
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
sd->handler(c, mm->topic.p, mm->topic.len, mm->payload.p, mm->payload.len,
sd->user_data);
#if MGOS_HAVE_PROMETHEUS_METRICS
metrics_mqtt_received_topics_count++;
metrics_mqtt_received_topics_bytes_total+=mm->topic.len;
#endif // MGOS_HAVE_PROMETHEUS_METRICS
}

void mgos_mqtt_sub(const char *topic, sub_handler_t handler, void *user_data) {