Skip to content

Commit

Permalink
feature(1026): add sessioncount to prometheus metrics (coturn#1075)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Kramer <[email protected]>
  • Loading branch information
paulkram and Paul Kramer authored Nov 6, 2022
1 parent 730996b commit 5a28394
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/apps/relay/ns_ioalib_engine_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3707,15 +3707,15 @@ void turn_report_allocation_set(void *a, turn_time_t lifetime, int refresh)
#if !defined(TURN_NO_PROMETHEUS)
{
if (!refresh)
prom_inc_allocation();
prom_inc_allocation(get_ioa_socket_type(ss->client_socket));
}
#endif
}
}
}
}

void turn_report_allocation_delete(void *a)
void turn_report_allocation_delete(void *a, SOCKET_TYPE socket_type)
{
if(a) {
ts_ur_super_session *ss = (ts_ur_super_session*)(((allocation*)a)->owner);
Expand Down Expand Up @@ -3765,7 +3765,7 @@ void turn_report_allocation_delete(void *a)
prom_set_finished_traffic(NULL, (const char*)ss->username, (unsigned long)(ss->t_received_packets), (unsigned long)(ss->t_received_bytes), (unsigned long)(ss->t_sent_packets), (unsigned long)(ss->t_sent_bytes), false);
prom_set_finished_traffic(NULL, (const char*)ss->username, (unsigned long)(ss->t_peer_received_packets), (unsigned long)(ss->t_peer_received_bytes), (unsigned long)(ss->t_peer_sent_packets), (unsigned long)(ss->t_peer_sent_bytes), true);
}
prom_dec_allocation();
prom_dec_allocation(socket_type);
}
#endif
}
Expand Down
12 changes: 7 additions & 5 deletions src/apps/relay/prom_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void start_prometheus_server(void){
turn_total_traffic_peer_sentb = prom_collector_registry_must_register_metric(prom_counter_new("turn_total_traffic_peer_sentb", "Represents total finished sessions peer sent bytes", 0, NULL));

// Create total allocations number gauge metric
turn_total_allocations = prom_collector_registry_must_register_metric(prom_gauge_new("turn_total_allocations", "Represents current allocations number", 0, NULL));
turn_total_allocations = prom_collector_registry_must_register_metric(prom_gauge_new("turn_total_allocations", "Represents current allocations number", 1, (const char*[]) {"type"}));

promhttp_set_active_collector_registry(NULL);

Expand Down Expand Up @@ -129,15 +129,17 @@ void prom_set_finished_traffic(const char* realm, const char* user, unsigned lon
}
}

void prom_inc_allocation(void) {
void prom_inc_allocation(SOCKET_TYPE type) {
if (turn_params.prometheus == 1){
prom_gauge_inc(turn_total_allocations, NULL);
prom_gauge_inc(turn_total_allocations, (const char*[]) {"all"});
prom_gauge_inc(turn_total_allocations, (const char*[]) {socket_type_name(type)});
}
}

void prom_dec_allocation(void) {
void prom_dec_allocation(SOCKET_TYPE type) {
if (turn_params.prometheus == 1){
prom_gauge_dec(turn_total_allocations, NULL);
prom_gauge_dec(turn_total_allocations, (const char*[]) {"all"});
prom_gauge_dec(turn_total_allocations, (const char*[]) {socket_type_name(type)});
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/apps/relay/prom_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ void start_prometheus_server(void);

void prom_set_finished_traffic(const char* realm, const char* user, unsigned long rsvp, unsigned long rsvb, unsigned long sentp, unsigned long sentb, bool peer);

void prom_inc_allocation(void);
void prom_dec_allocation(void);
void prom_inc_allocation(SOCKET_TYPE type);
void prom_dec_allocation(SOCKET_TYPE type);
#else

void start_prometheus_server(void);
Expand Down
5 changes: 3 additions & 2 deletions src/server/ns_turn_allocation.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/

#include "ns_turn_allocation.h"
#include "ns_turn_ioalib.h"

/////////////// Permission forward declarations /////////////////

Expand All @@ -47,12 +48,12 @@ void init_allocation(void *owner, allocation* a, ur_map *tcp_connections) {
}
}

void clear_allocation(allocation *a)
void clear_allocation(allocation *a, SOCKET_TYPE socket_type)
{
if (a) {

if(a->is_valid)
turn_report_allocation_delete(a);
turn_report_allocation_delete(a, socket_type);

if(a->tcs.elems) {
size_t i;
Expand Down
2 changes: 1 addition & 1 deletion src/server/ns_turn_allocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void turn_channel_delete(ch_info* chn);
/////////// ALLOCATION ////////////

void init_allocation(void *owner, allocation* a, ur_map *tcp_connections);
void clear_allocation(allocation *a);
void clear_allocation(allocation *a, SOCKET_TYPE socket_type);

void turn_permission_clean(turn_permission_info* tinfo);

Expand Down
2 changes: 1 addition & 1 deletion src/server/ns_turn_ioalib.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void ioa_network_buffer_delete(ioa_engine_handle e, ioa_network_buffer_handle nb
* Status reporting functions
*/
void turn_report_allocation_set(void *a, turn_time_t lifetime, int refresh);
void turn_report_allocation_delete(void *a);
void turn_report_allocation_delete(void *a, SOCKET_TYPE socket_type);
void turn_report_session_usage(void *session, int force_invalid);

/*
Expand Down
2 changes: 1 addition & 1 deletion src/server/ns_turn_maps.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ int ur_map_foreach(ur_map* map, foreachcb_type func) {
return 0;
}

int ur_map_foreach_arg(ur_map* map, foreachcb_arg_type func, void* arg) {
int ur_map_foreach_arg(const ur_map* map, foreachcb_arg_type func, void* arg) {
if(map && func && ur_map_valid(map)) {
khiter_t k;
for (k = kh_begin((*map)->h); k != kh_end(map->h); ++k) {
Expand Down
2 changes: 1 addition & 1 deletion src/server/ns_turn_maps.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ size_t ur_map_size(const ur_map* map);

int ur_map_foreach(ur_map* map, foreachcb_type func);

int ur_map_foreach_arg(ur_map* map, foreachcb_arg_type func, void* arg);
int ur_map_foreach_arg(const ur_map* map, foreachcb_arg_type func, void* arg);

int ur_map_lock(const ur_map* map);
int ur_map_unlock(const ur_map* map);
Expand Down
14 changes: 8 additions & 6 deletions src/server/ns_turn_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,20 +802,20 @@ static ts_ur_super_session* create_new_ss(turn_turnserver* server) {
return ss;
}

static void delete_ur_map_ss(void *p) {
static void delete_ur_map_ss(void *p, SOCKET_TYPE socket_type) {
if (p) {
ts_ur_super_session* ss = (ts_ur_super_session*) p;
delete_session_from_map(ss);
IOA_CLOSE_SOCKET(ss->client_socket);
clear_allocation(get_allocation_ss(ss));
clear_allocation(get_allocation_ss(ss), socket_type);
IOA_EVENT_DEL(ss->to_be_allocated_timeout_ev);
free(p);
}
}

/////////// clean all /////////////////////

static int turn_server_remove_all_from_ur_map_ss(ts_ur_super_session* ss) {
static int turn_server_remove_all_from_ur_map_ss(ts_ur_super_session* ss, SOCKET_TYPE socket_type) {
if (!ss)
return 0;
else {
Expand All @@ -829,7 +829,7 @@ static int turn_server_remove_all_from_ur_map_ss(ts_ur_super_session* ss) {
if (get_relay_socket_ss(ss,AF_INET6)) {
clear_ioa_socket_session_if(get_relay_socket_ss(ss,AF_INET6), ss);
}
delete_ur_map_ss(ss);
delete_ur_map_ss(ss, socket_type);
return ret;
}
}
Expand Down Expand Up @@ -4169,6 +4169,8 @@ int shutdown_client_connection(turn_turnserver *server, ts_ur_super_session *ss,
if (!ss)
return -1;

SOCKET_TYPE socket_type = get_ioa_socket_type(ss->client_socket);

turn_report_session_usage(ss, 1);
dec_quota(ss);
dec_bps(ss);
Expand Down Expand Up @@ -4227,7 +4229,7 @@ int shutdown_client_connection(turn_turnserver *server, ts_ur_super_session *ss,
}
}

turn_server_remove_all_from_ur_map_ss(ss);
turn_server_remove_all_from_ur_map_ss(ss, socket_type);

FUNCEND;

Expand Down Expand Up @@ -4334,7 +4336,7 @@ static void client_ss_allocation_timeout_handler(ioa_engine_handle e, void *arg)
turn_turnserver* server = (turn_turnserver*) (ss->server);

if (!server) {
clear_allocation(a);
clear_allocation(a, get_ioa_socket_type(ss->client_socket));
return;
}

Expand Down

0 comments on commit 5a28394

Please sign in to comment.