Annotate

util-src/strbitop.c @ 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 11175:235537247aa3
child 12469:2b3adaa6d38e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 /*
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 * This project is MIT licensed. Please see the
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 * COPYING file in the source package for more information.
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 *
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
5 * Copyright (C) 2016 Kim Alvefur
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 */
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 #include <lua.h>
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 #include <lauxlib.h>
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 #if (LUA_VERSION_NUM == 501)
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 #endif
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 /* TODO Deduplicate code somehow */
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
17 int strop_and(lua_State *L) {
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 luaL_Buffer buf;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 size_t a, b, i;
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
20 const char *str_a = luaL_checklstring(L, 1, &a);
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
21 const char *str_b = luaL_checklstring(L, 2, &b);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
11175
235537247aa3 Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents: 11172
diff changeset
23 luaL_buffinit(L, &buf);
235537247aa3 Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents: 11172
diff changeset
24
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 for(i = 0; i < a; i++) {
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
31 luaL_addchar(&buf, str_a[i] & str_b[i % b]);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
38 int strop_or(lua_State *L) {
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 luaL_Buffer buf;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 size_t a, b, i;
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
41 const char *str_a = luaL_checklstring(L, 1, &a);
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
42 const char *str_b = luaL_checklstring(L, 2, &b);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43
11175
235537247aa3 Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents: 11172
diff changeset
44 luaL_buffinit(L, &buf);
235537247aa3 Back out changeset 2c1583bb0e0f
Kim Alvefur <zash@zash.se>
parents: 11172
diff changeset
45
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 for(i = 0; i < a; i++) {
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
52 luaL_addchar(&buf, str_a[i] | str_b[i % b]);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
59 int strop_xor(lua_State *L) {
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 luaL_Buffer buf;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 size_t a, b, i;
11167
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
62 const char *str_a = luaL_checklstring(L, 1, &a);
ba32b9a6d75b util.strbitop: Reformat code
Kim Alvefur <zash@zash.se>
parents: 11163
diff changeset
63 const char *str_b = luaL_checklstring(L, 2, &b);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
65 luaL_buffinit(L, &buf);
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
66
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 if(a == 0 || b == 0) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 lua_settop(L, 1);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 for(i = 0; i < a; i++) {
11172
712b2e6a09d9 Back out 6dde2c9fa272: Doesn't work on Lua 5.1
Kim Alvefur <zash@zash.se>
parents: 11171
diff changeset
73 luaL_addchar(&buf, str_a[i] ^ str_b[i % b]);
11163
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 luaL_pushresult(&buf);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 LUA_API int luaopen_util_strbitop(lua_State *L) {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 luaL_Reg exports[] = {
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 { "sand", strop_and },
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 { "sor", strop_or },
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 { "sxor", strop_xor },
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 { NULL, NULL }
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 };
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 lua_newtable(L);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 luaL_setfuncs(L, exports, 0);
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 return 1;
37a6a535343e util.strbitop: Library for bitwise operations on strings
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 }