Skip to content

Commit 4cad743

Browse files
committed
add variable $ws_total_payload_size to record the total size on one websocket connection
1 parent 22d57d3 commit 4cad743

File tree

2 files changed

+54
-21
lines changed

2 files changed

+54
-21
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Here is a list of variables you can use in log format string:
3535

3636
* $ws_opcode - websocket packet opcode. Look into https://tools.ietf.org/html/rfc6455 Section 5.2, Base Framing Protocol.
3737
* $ws_payload_size - Websocket packet size without protocol specific data. Only data that been sent or received by the client
38+
* $ws_total_payload_size - total packet size on a Websocket connection, without protocol specific data. Only data that been sent or received by the client
3839
* $ws_packet_source - Could be "client" if packet has been sent by the user or "upstream" if it has been received from the server
3940
* $ws_conn_age - Number of seconds connection is alive
4041
* $time_local - Nginx local time, date and timezone

ngx_http_websocket_stat_module.c

+53-21
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ websocket_log(char *str)
100100
void
101101
ws_do_log(compiled_template *template, ngx_http_request_t *r, void *ctx)
102102
{
103-
if (ws_log) {
103+
if (ws_log && template) {
104104
char *log_line = apply_template(template, r, ctx);
105105
websocket_log(log_line);
106106
free(log_line);
@@ -330,6 +330,7 @@ my_send(ngx_connection_t *c, u_char *buf, size_t size)
330330
ngx_atomic_fetch_add(frame_counter->frames, 1);
331331
ngx_atomic_fetch_add(frame_counter->total_payload_size,
332332
ctx->frame_counter.current_payload_size);
333+
ctx->frame_counter.total_payload_size += ctx->frame_counter.current_payload_size;
333334
ws_do_log(log_template, r, &template_ctx);
334335
}
335336
}
@@ -371,6 +372,7 @@ my_recv(ngx_connection_t *c, u_char *buf, size_t size)
371372
ngx_atomic_fetch_add(frame_counter->frames, 1);
372373
ngx_atomic_fetch_add(frame_counter->total_payload_size,
373374
ctx->frame_counter.current_payload_size);
375+
ctx->frame_counter.total_payload_size += ctx->frame_counter.current_payload_size;
374376
ws_do_log(log_template, r, &template_ctx);
375377
}
376378
}
@@ -408,6 +410,7 @@ ngx_http_websocket_stat_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
408410
ctx->connection_id.data = ngx_pcalloc(r->pool, UID_LENGTH + 1);
409411
ctx->connection_id.len = UID_LENGTH;
410412
memcpy(ctx->connection_id.data, request_id_str, UID_LENGTH + 1);
413+
ctx->frame_counter.total_payload_size = 0;
411414

412415
ws_do_log(log_open_template, r, &template_ctx);
413416
ngx_http_set_ctx(r, ctx, ngx_http_websocket_stat_module);
@@ -428,16 +431,16 @@ ngx_http_websocket_stat_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
428431
return ngx_http_next_body_filter(r, in);
429432
}
430433

431-
char buff[100];
432-
433434
const char *
434435
ws_packet_type(ngx_http_request_t *r, void *data)
435436
{
436437
template_ctx_s *ctx = data;
437438
ngx_frame_counter_t *frame_cntr = &(ctx->ws_ctx->frame_counter);
438439
if (!ctx || !frame_cntr)
439440
return UNKNOWN_VAR;
440-
sprintf(buff, "%d", frame_cntr->current_frame_type);
441+
442+
char* buff = ngx_pcalloc(r->pool, NGX_ATOMIC_T_LEN);
443+
snprintf(buff, NGX_ATOMIC_T_LEN, "%d", frame_cntr->current_frame_type);
441444
return buff;
442445
}
443446

@@ -448,7 +451,22 @@ ws_packet_size(ngx_http_request_t *r, void *data)
448451
ngx_frame_counter_t *frame_cntr = &ctx->ws_ctx->frame_counter;
449452
if (!ctx || !frame_cntr)
450453
return UNKNOWN_VAR;
451-
sprintf(buff, "%lu", frame_cntr->current_payload_size);
454+
455+
char* buff = ngx_pcalloc(r->pool, NGX_ATOMIC_T_LEN);
456+
snprintf(buff, NGX_ATOMIC_T_LEN, "%lu", frame_cntr->current_payload_size);
457+
return (char *)buff;
458+
}
459+
460+
const char *
461+
ws_total_payload_size(ngx_http_request_t *r, void *data)
462+
{
463+
template_ctx_s *ctx = data;
464+
ngx_frame_counter_t *frame_cntr = &ctx->ws_ctx->frame_counter;
465+
if (!ctx || !frame_cntr)
466+
return UNKNOWN_VAR;
467+
468+
char* buff = ngx_pcalloc(r->pool, NGX_ATOMIC_T_LEN);
469+
snprintf(buff, NGX_ATOMIC_T_LEN, "%lu", frame_cntr->total_payload_size);
452470
return (char *)buff;
453471
}
454472

@@ -475,6 +493,7 @@ get_core_var(ngx_http_request_t *r, const char *variable)
475493
key = ngx_hash(key, *(variable++));
476494

477495
vv = ngx_http_get_variable(r, &var, key);
496+
char* buff = ngx_pcalloc(r->pool, vv->len + 1);
478497
memcpy(buff, vv->data, vv->len);
479498
buff[vv->len] = '\0';
480499
return buff;
@@ -486,20 +505,26 @@ ws_connection_age(ngx_http_request_t *r, void *data)
486505
template_ctx_s *ctx = data;
487506
if (!ctx || !ctx->ws_ctx)
488507
return UNKNOWN_VAR;
489-
sprintf(buff, "%lu", ngx_time() - ctx->ws_ctx->ws_conn_start_time);
508+
509+
char* buff = ngx_pcalloc(r->pool, NGX_ATOMIC_T_LEN);
510+
snprintf(buff, NGX_ATOMIC_T_LEN, "%lu", ngx_time() - ctx->ws_ctx->ws_conn_start_time);
490511

491512
return (char *)buff;
492513
}
493514

494515
const char *
495516
local_time(ngx_http_request_t *r, void *data)
496517
{
497-
return memcpy(buff, ngx_cached_http_time.data, ngx_cached_http_time.len);
518+
char* buff = ngx_pcalloc(r->pool, ngx_cached_http_time.len + 1);
519+
memcpy(buff, ngx_cached_http_time.data, ngx_cached_http_time.len);
520+
buff[ngx_cached_http_time.len] = '\0';
521+
return buff;
498522
}
499523

500524
const char *
501525
remote_ip(ngx_http_request_t *r, void *data)
502526
{
527+
char* buff = ngx_pcalloc(r->pool, r->connection->addr_text.len + 1);
503528
memcpy(buff, r->connection->addr_text.data, r->connection->addr_text.len);
504529
buff[r->connection->addr_text.len] = '\0';
505530

@@ -545,6 +570,7 @@ GEN_CORE_GET_FUNC(server_port, "server_port")
545570
const template_variable variables[] = {
546571
{VAR_NAME("$ws_opcode"), sizeof("ping") - 1, ws_packet_type},
547572
{VAR_NAME("$ws_payload_size"), NGX_SIZE_T_LEN, ws_packet_size},
573+
{VAR_NAME("$ws_total_payload_size"), NGX_SIZE_T_LEN, ws_total_payload_size},
548574
{VAR_NAME("$ws_packet_source"), sizeof("upstream") - 1, ws_packet_source},
549575
{VAR_NAME("$ws_conn_age"), NGX_SIZE_T_LEN, ws_connection_age},
550576
{VAR_NAME("$time_local"), sizeof("Mon, 23 Oct 2017 11:27:42 GMT") - 1,
@@ -585,12 +611,18 @@ ngx_http_ws_log_format(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
585611
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "Wrong argument number");
586612
return NGX_CONF_ERROR;
587613
}
614+
588615
if (cf->args->nelts == 2) {
589616
log_template =
590617
compile_template((char *)args[1].data, variables, cf->pool);
591618
return NGX_CONF_OK;
619+
592620
}
593-
if (strcmp((char *)args[1].data, "close") == 0) {
621+
if (strcmp((char *)args[1].data, "packet") == 0) {
622+
log_template =
623+
compile_template((char *)args[2].data, variables, cf->pool);
624+
return NGX_CONF_OK;
625+
} else if (strcmp((char *)args[1].data, "close") == 0) {
594626
log_close_template =
595627
compile_template((char *)args[2].data, variables, cf->pool);
596628
return NGX_CONF_OK;
@@ -700,7 +732,7 @@ complete_ws_handshake(ngx_connection_t *connection, const char *ws_key)
700732
Base64Encode(hash, SHA_DIGEST_LENGTH, access_key, ACCEPT_SIZE);
701733
access_key[ACCEPT_SIZE] = '\0';
702734
char resp[256];
703-
sprintf(resp, resp_template, access_key);
735+
snprintf(resp, 256, resp_template, access_key);
704736
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
705737
"Websocket connection closed");
706738
connection->send(connection, (unsigned char *)resp, strlen(resp));
@@ -746,18 +778,18 @@ ngx_http_websocket_stat_init(ngx_conf_t *cf)
746778
ngx_http_next_body_filter = ngx_http_top_body_filter;
747779
ngx_http_top_body_filter = ngx_http_websocket_stat_body_filter;
748780

749-
if (!log_template) {
750-
log_template =
751-
compile_template(default_log_template_str, variables, cf->pool);
752-
}
753-
if (!log_open_template) {
754-
log_open_template = compile_template(default_open_log_template_str,
755-
variables, cf->pool);
756-
}
757-
if (!log_close_template) {
758-
log_close_template = compile_template(default_close_log_template_str,
759-
variables, cf->pool);
760-
}
781+
// if (!log_template) {
782+
// log_template =
783+
// compile_template(default_log_template_str, variables, cf->pool);
784+
// }
785+
// if (!log_open_template) {
786+
// log_open_template = compile_template(default_open_log_template_str,
787+
// variables, cf->pool);
788+
// }
789+
// if (!log_close_template) {
790+
// log_close_template = compile_template(default_close_log_template_str,
791+
// variables, cf->pool);
792+
// }
761793

762794
ngx_http_handler_pt *h;
763795
ngx_http_core_main_conf_t *cmcf;

0 commit comments

Comments
 (0)