Software /
code /
prosody
Comparison
core/statsmanager.lua @ 11515:10d13e0554f9
core.statsmanager: Allow special "manual" value for statistics_interval
When set, no periodic statistics collection is done by
core.statsmanager, instead some module is expected to call collect()
when it suits. Obviously only one such module should be enabled.
Quoth jonas’
> correct way is to scrape the internal sources on each call to /metrics
> in the context of Prometheus
"manual" as opposed to "automatic", from the point of view of
statsmanager.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 06 Apr 2021 23:25:15 +0200 |
parent | 11506:9a3ebdd65f9c |
child | 11523:5f15ab7c6ae5 |
comparison
equal
deleted
inserted
replaced
11514:11186af62c87 | 11515:10d13e0554f9 |
---|---|
4 local timer = require "util.timer"; | 4 local timer = require "util.timer"; |
5 local fire_event = prosody.events.fire_event; | 5 local fire_event = prosody.events.fire_event; |
6 | 6 |
7 local stats_interval_config = config.get("*", "statistics_interval"); | 7 local stats_interval_config = config.get("*", "statistics_interval"); |
8 local stats_interval = tonumber(stats_interval_config); | 8 local stats_interval = tonumber(stats_interval_config); |
9 if stats_interval_config and not stats_interval then | 9 if stats_interval_config and not stats_interval and stats_interval_config ~= "manual" then |
10 log("error", "Invalid 'statistics_interval' setting, statistics will be disabled"); | 10 log("error", "Invalid 'statistics_interval' setting, statistics will be disabled"); |
11 end | 11 end |
12 | 12 |
13 local stats_provider_name; | 13 local stats_provider_name; |
14 local stats_provider_config = config.get("*", "statistics"); | 14 local stats_provider_config = config.get("*", "statistics"); |
16 | 16 |
17 if not stats_provider and stats_interval then | 17 if not stats_provider and stats_interval then |
18 stats_provider = "internal"; | 18 stats_provider = "internal"; |
19 elseif stats_provider and not stats_interval then | 19 elseif stats_provider and not stats_interval then |
20 stats_interval = 60; | 20 stats_interval = 60; |
21 end | |
22 if stats_interval_config == "manual" then | |
23 stats_interval = nil; | |
21 end | 24 end |
22 | 25 |
23 local builtin_providers = { | 26 local builtin_providers = { |
24 internal = "util.statistics"; | 27 internal = "util.statistics"; |
25 statsd = "util.statsd"; | 28 statsd = "util.statsd"; |
63 function measure(type, name, conf) | 66 function measure(type, name, conf) |
64 local f = assert(stats[type], "unknown stat type: "..type); | 67 local f = assert(stats[type], "unknown stat type: "..type); |
65 return f(name, conf); | 68 return f(name, conf); |
66 end | 69 end |
67 | 70 |
68 if stats_interval then | 71 if stats_interval or stats_interval_config == "manual" then |
69 log("debug", "Statistics enabled using %s provider, collecting every %d seconds", stats_provider_name, stats_interval); | |
70 | 72 |
71 local mark_collection_start = measure("times", "stats.collection"); | 73 local mark_collection_start = measure("times", "stats.collection"); |
72 local mark_processing_start = measure("times", "stats.processing"); | 74 local mark_processing_start = measure("times", "stats.processing"); |
73 | 75 |
74 function collect() | 76 function collect() |
94 fire_event("stats-updated", { stats = latest_stats, changed_stats = changed_stats, stats_extra = stats_extra }); | 96 fire_event("stats-updated", { stats = latest_stats, changed_stats = changed_stats, stats_extra = stats_extra }); |
95 mark_processing_done(); | 97 mark_processing_done(); |
96 end | 98 end |
97 return stats_interval; | 99 return stats_interval; |
98 end | 100 end |
99 timer.add_task(stats_interval, collect); | 101 if stats_interval then |
100 prosody.events.add_handler("server-started", function () collect() end, -1); | 102 log("debug", "Statistics enabled using %s provider, collecting every %d seconds", stats_provider_name, stats_interval); |
101 prosody.events.add_handler("server-stopped", function () collect() end, -1); | 103 timer.add_task(stats_interval, collect); |
104 prosody.events.add_handler("server-started", function () collect() end, -1); | |
105 prosody.events.add_handler("server-stopped", function () collect() end, -1); | |
106 else | |
107 log("debug", "Statistics enabled using %s provider, no scheduled collection", stats_provider_name); | |
108 end | |
102 else | 109 else |
103 log("debug", "Statistics enabled using %s provider, collection is disabled", stats_provider_name); | 110 log("debug", "Statistics enabled using %s provider, collection is disabled", stats_provider_name); |
104 end | 111 end |
105 else | 112 else |
106 log("debug", "Statistics disabled"); | 113 log("debug", "Statistics disabled"); |
107 function measure() return measure; end | 114 function measure() return measure; end |
108 end | 115 end |
109 | 116 |
117 local exported_collect = nil; | |
118 if stats_interval_config == "manual" then | |
119 exported_collect = collect; | |
120 end | |
110 | 121 |
111 return { | 122 return { |
123 collect = exported_collect; | |
112 measure = measure; | 124 measure = measure; |
113 get_stats = function () | 125 get_stats = function () |
114 return latest_stats, changed_stats, stats_extra; | 126 return latest_stats, changed_stats, stats_extra; |
115 end; | 127 end; |
116 get = function (name) | 128 get = function (name) |