annotate mod_http_stats_stream/mod_http_stats_stream.lua @ 3462:d73ed7975d82

mod_storage_xmlarchive: Add some debug logging
author Kim Alvefur <zash@zash.se>
date Thu, 07 Feb 2019 15:26:03 +0100
parents 47a6f01231b2
children fd054689a64c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2432
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local statsman = require "core.statsmanager";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local json = require "util.json";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local sessions = {};
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local function updates_client_closed(response)
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 module:log("debug", "Streamstats client closed");
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 sessions[response] = nil;
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 end
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local function get_updates(event)
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local request, response = event.request, event.response;
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 response.on_destroy = updates_client_closed;
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 response.conn:write(table.concat({
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 "HTTP/1.1 200 OK";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 "Content-Type: text/event-stream";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 "X-Accel-Buffering: no"; -- For nginx maybe?
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 "";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 "event: stats-full";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 "data: "..json.encode(statsman.get_stats());
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 "";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 "";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 }, "\r\n"));
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 sessions[response] = request;
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 return true;
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 end
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 module:hook_global("stats-updated", function (event)
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 local data = table.concat({
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 "event: stats-updated";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 "data: "..json.encode(event.changed_stats);
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 "";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 "";
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 }, "\r\n")
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 for response in pairs(sessions) do
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 response.conn:write(data);
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 end
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 end);
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 module:depends("http");
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 module:provides("http", {
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 route = {
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 GET = get_updates;
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 }
47a6f01231b2 mod_http_stats_stream: Sends statistics from statsmanager over an HTTP event stream
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 });