Software /
code /
prosody
Annotate
plugins/mod_http_errors.lua @ 11523:5f15ab7c6ae5
Statistics: Rewrite statistics backends to use OpenMetrics
The metric subsystem of Prosody has had some shortcomings from
the perspective of the current state-of-the-art in metric
observability.
The OpenMetrics standard [0] is a formalization of the data
model (and serialization format) of the well-known and
widely-used Prometheus [1] software stack.
The previous stats subsystem of Prosody did not map well to that
format (see e.g. [2] and [3]); the key reason is that it was
trying to do too much math on its own ([2]) while lacking
first-class support for "families" of metrics ([3]) and
structured metric metadata (despite the `extra` argument to
metrics, there was no standard way of representing common things
like "tags" or "labels").
Even though OpenMetrics has grown from the Prometheus world of
monitoring, it maps well to other popular monitoring stacks
such as:
- InfluxDB (labels can be mapped to tags and fields as necessary)
- Carbon/Graphite (labels can be attached to the metric name with
dot-separation)
- StatsD (see graphite when assuming that graphite is used as
backend, which is the default)
The util.statsd module has been ported to use the OpenMetrics
model as a proof of concept. An implementation which exposes
the util.statistics backend data as Prometheus metrics is
ready for publishing in prosody-modules (most likely as
mod_openmetrics_prometheus to avoid breaking existing 0.11
deployments).
At the same time, the previous measure()-based API had one major
advantage: It is really simple and easy to use without requiring
lots of knowledge about OpenMetrics or similar concepts. For that
reason as well as compatibility with existing code, it is preserved
and may even be extended in the future.
However, code relying on the `stats-updated` event as well as
`get_stats` from `statsmanager` will break because the data
model has changed completely; in case of `stats-updated`, the
code will simply not run (as the event was renamed in order
to avoid conflicts); the `get_stats` function has been removed
completely (so it will cause a traceback when it is attempted
to be used).
Note that the measure_*_event methods have been removed from
the module API. I was unable to find any uses or documentation
and thus deemed they should not be ported. Re-implementation is
possible when necessary.
[0]: https://openmetrics.io/
[1]: https://prometheus.io/
[2]: #959
[3]: #960
author | Jonas Schäfer <jonas@wielicki.name> |
---|---|
date | Sun, 18 Apr 2021 11:47:41 +0200 |
parent | 11404:f7704f987439 |
child | 11662:a8798e04b5c8 |
rev | line source |
---|---|
4711
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 module:set_global(); |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local server = require "net.http.server"; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local codes = require "net.http.codes"; |
8364
f91ab40a3105
mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents:
8363
diff
changeset
|
5 local xml_escape = require "util.stanza".xml_escape; |
f91ab40a3105
mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents:
8363
diff
changeset
|
6 local render = require "util.interpolation".new("%b{}", xml_escape); |
4711
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local show_private = module:get_option_boolean("http_errors_detailed", false); |
4737
7b9e2a8c4710
mod_http_errors: Add two new config options, http_errors_always_show (show even for unknown errors) and http_errors_default_message (message for unknown errors)
Matthew Wild <mwild1@gmail.com>
parents:
4711
diff
changeset
|
9 local always_serve = module:get_option_boolean("http_errors_always_show", true); |
7b9e2a8c4710
mod_http_errors: Add two new config options, http_errors_always_show (show even for unknown errors) and http_errors_default_message (message for unknown errors)
Matthew Wild <mwild1@gmail.com>
parents:
4711
diff
changeset
|
10 local default_message = { module:get_option_string("http_errors_default_message", "That's all I know.") }; |
4711
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local default_messages = { |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 [400] = { "What kind of request do you call that??" }; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 [403] = { "You're not allowed to do that." }; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 [404] = { "Whatever you were looking for is not here. %"; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 "Where did you put it?", "It's behind you.", "Keep looking." }; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 [500] = { "% Check your error log for more info."; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 "Gremlins.", "It broke.", "Don't look at me." }; |
11403
747e8245f180
mod_http_errors: Add some silly variations for the '/' page
Kim Alvefur <zash@zash.se>
parents:
11395
diff
changeset
|
18 ["/"] = { |
747e8245f180
mod_http_errors: Add some silly variations for the '/' page
Kim Alvefur <zash@zash.se>
parents:
11395
diff
changeset
|
19 "A study in simplicity."; |
747e8245f180
mod_http_errors: Add some silly variations for the '/' page
Kim Alvefur <zash@zash.se>
parents:
11395
diff
changeset
|
20 "Better catch it!"; |
747e8245f180
mod_http_errors: Add some silly variations for the '/' page
Kim Alvefur <zash@zash.se>
parents:
11395
diff
changeset
|
21 "Don't just stand there, go after it!"; |
747e8245f180
mod_http_errors: Add some silly variations for the '/' page
Kim Alvefur <zash@zash.se>
parents:
11395
diff
changeset
|
22 "Well, say something, before it runs too far!"; |
747e8245f180
mod_http_errors: Add some silly variations for the '/' page
Kim Alvefur <zash@zash.se>
parents:
11395
diff
changeset
|
23 "Welcome to the world of XMPP!"; |
747e8245f180
mod_http_errors: Add some silly variations for the '/' page
Kim Alvefur <zash@zash.se>
parents:
11395
diff
changeset
|
24 "You can do anything in XMPP!"; -- "The only limit is XML."; |
747e8245f180
mod_http_errors: Add some silly variations for the '/' page
Kim Alvefur <zash@zash.se>
parents:
11395
diff
changeset
|
25 "You can do anything with Prosody!"; -- the only limit is memory? |
747e8245f180
mod_http_errors: Add some silly variations for the '/' page
Kim Alvefur <zash@zash.se>
parents:
11395
diff
changeset
|
26 }; |
4711
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 }; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 local messages = setmetatable(module:get_option("http_errors_messages", {}), { __index = default_messages }); |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 local html = [[ |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 <!DOCTYPE html> |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 <html> |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 <head> |
8364
f91ab40a3105
mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents:
8363
diff
changeset
|
35 <meta charset="utf-8"> |
f91ab40a3105
mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents:
8363
diff
changeset
|
36 <title>{title}</title> |
f91ab40a3105
mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents:
8363
diff
changeset
|
37 <style> |
11388 | 38 body{margin-top:14%;text-align:center;background-color:#f8f8f8;font-family:sans-serif} |
39 h1{font-size:xx-large} | |
40 p{font-size:x-large} | |
11395 | 41 p.warning>span{font-size:large;background-color:yellow} |
11388 | 42 p.extra{font-size:large;font-family:courier} |
43 @media(prefers-color-scheme:dark){ | |
44 body{background-color:#161616;color:#eee} | |
11395 | 45 p.warning>span{background-color:inherit;color:yellow} |
11154 | 46 } |
8364
f91ab40a3105
mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents:
8363
diff
changeset
|
47 </style> |
4711
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 </head> |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 <body> |
8364
f91ab40a3105
mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents:
8363
diff
changeset
|
50 <h1>{title}</h1> |
f91ab40a3105
mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents:
8363
diff
changeset
|
51 <p>{message}</p> |
11395 | 52 {warning&<p class="warning"><span>⚠ {warning?} ⚠</span></p>} |
11155
8d692a8a8f48
mod_http_errors: Remove 'extra' element when empty
Kim Alvefur <zash@zash.se>
parents:
11154
diff
changeset
|
53 {extra&<p class="extra">{extra?}</p>} |
4711
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 </body> |
7492
9a749cf8c1ba
mod_http_errors: Add a newline after end of HTML
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
55 </html> |
9a749cf8c1ba
mod_http_errors: Add a newline after end of HTML
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
56 ]]; |
4711
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 local function get_page(code, extra) |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 local message = messages[code]; |
4737
7b9e2a8c4710
mod_http_errors: Add two new config options, http_errors_always_show (show even for unknown errors) and http_errors_default_message (message for unknown errors)
Matthew Wild <mwild1@gmail.com>
parents:
4711
diff
changeset
|
60 if always_serve or message then |
7b9e2a8c4710
mod_http_errors: Add two new config options, http_errors_always_show (show even for unknown errors) and http_errors_default_message (message for unknown errors)
Matthew Wild <mwild1@gmail.com>
parents:
4711
diff
changeset
|
61 message = message or default_message; |
8364
f91ab40a3105
mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents:
8363
diff
changeset
|
62 return render(html, { |
4711
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 title = rawget(codes, code) or ("Code "..tostring(code)); |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 message = message[1]:gsub("%%", function () |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 return message[math.random(2, math.max(#message,2))]; |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 end); |
8364
f91ab40a3105
mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents:
8363
diff
changeset
|
67 extra = extra; |
f91ab40a3105
mod_http_errors: Use util.interpolation to render HTML template
Kim Alvefur <zash@zash.se>
parents:
8363
diff
changeset
|
68 }); |
4711
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 end |
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 |
11404
f7704f987439
mod_http_errors: Add some comments
Kim Alvefur <zash@zash.se>
parents:
11403
diff
changeset
|
72 -- Main error page handler |
4711
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 module:hook_object_event(server, "http-error", function (event) |
8363
e2460edc2a2f
mod_http_errors: Set Content-Type header to HTML (fixes #1030)
Kim Alvefur <zash@zash.se>
parents:
7492
diff
changeset
|
74 if event.response then |
e2460edc2a2f
mod_http_errors: Set Content-Type header to HTML (fixes #1030)
Kim Alvefur <zash@zash.se>
parents:
7492
diff
changeset
|
75 event.response.headers.content_type = "text/html; charset=utf-8"; |
e2460edc2a2f
mod_http_errors: Set Content-Type header to HTML (fixes #1030)
Kim Alvefur <zash@zash.se>
parents:
7492
diff
changeset
|
76 end |
10574
f70c874b7936
mod_http_errors: Use text from util.errror object if included
Kim Alvefur <zash@zash.se>
parents:
10430
diff
changeset
|
77 return get_page(event.code, (show_private and event.private_message) or event.message or (event.error and event.error.text)); |
4711
4ddf3ba0c749
mod_http_errors: Module to handle HTTP errors with a HTML page
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 end); |
10430
46dd9df2db0c
mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents:
9760
diff
changeset
|
79 |
11404
f7704f987439
mod_http_errors: Add some comments
Kim Alvefur <zash@zash.se>
parents:
11403
diff
changeset
|
80 -- Way to use the template for other things so to give a consistent appearance |
11389
29e7ed75ed3f
mod_http_errors: Add way to reuse the error page template
Kim Alvefur <zash@zash.se>
parents:
11388
diff
changeset
|
81 module:hook("http-message", function (event) |
29e7ed75ed3f
mod_http_errors: Add way to reuse the error page template
Kim Alvefur <zash@zash.se>
parents:
11388
diff
changeset
|
82 if event.response then |
29e7ed75ed3f
mod_http_errors: Add way to reuse the error page template
Kim Alvefur <zash@zash.se>
parents:
11388
diff
changeset
|
83 event.response.headers.content_type = "text/html; charset=utf-8"; |
29e7ed75ed3f
mod_http_errors: Add way to reuse the error page template
Kim Alvefur <zash@zash.se>
parents:
11388
diff
changeset
|
84 end |
29e7ed75ed3f
mod_http_errors: Add way to reuse the error page template
Kim Alvefur <zash@zash.se>
parents:
11388
diff
changeset
|
85 return render(html, event); |
29e7ed75ed3f
mod_http_errors: Add way to reuse the error page template
Kim Alvefur <zash@zash.se>
parents:
11388
diff
changeset
|
86 end); |
29e7ed75ed3f
mod_http_errors: Add way to reuse the error page template
Kim Alvefur <zash@zash.se>
parents:
11388
diff
changeset
|
87 |
11404
f7704f987439
mod_http_errors: Add some comments
Kim Alvefur <zash@zash.se>
parents:
11403
diff
changeset
|
88 -- Something nicer shown instead of 404 at the root path, if nothing else handles this path |
10430
46dd9df2db0c
mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents:
9760
diff
changeset
|
89 module:hook_object_event(server, "http-error", function (event) |
46dd9df2db0c
mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents:
9760
diff
changeset
|
90 local request, response = event.request, event.response; |
46dd9df2db0c
mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents:
9760
diff
changeset
|
91 if request and response and request.path == "/" and response.status_code == 404 then |
46dd9df2db0c
mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents:
9760
diff
changeset
|
92 response.headers.content_type = "text/html; charset=utf-8"; |
11403
747e8245f180
mod_http_errors: Add some silly variations for the '/' page
Kim Alvefur <zash@zash.se>
parents:
11395
diff
changeset
|
93 local message = messages["/"]; |
10430
46dd9df2db0c
mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents:
9760
diff
changeset
|
94 return render(html, { |
46dd9df2db0c
mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents:
9760
diff
changeset
|
95 title = "Prosody is running!"; |
11403
747e8245f180
mod_http_errors: Add some silly variations for the '/' page
Kim Alvefur <zash@zash.se>
parents:
11395
diff
changeset
|
96 message = message[math.random(#message)]; |
10430
46dd9df2db0c
mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents:
9760
diff
changeset
|
97 }); |
46dd9df2db0c
mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents:
9760
diff
changeset
|
98 end |
46dd9df2db0c
mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents:
9760
diff
changeset
|
99 end, 1); |
46dd9df2db0c
mod_http_errors: Show a friendly page instead of 404 on top level
Kim Alvefur <zash@zash.se>
parents:
9760
diff
changeset
|
100 |