From: Kentaro Hayashi Date: Sun, 20 Dec 2020 13:27:30 +0000 (+0900) Subject: lua: migrate interface to v6 plugin API X-Git-Tag: 6.0.0-rc0~14^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e116230499c698240b4ddf9554ab874cab6c876f;p=thirdparty%2Fcollectd.git lua: migrate interface to v6 plugin API Since v6, value_list_t is deprecated and metric_family_t is introduced. metric_family_t is mapped to the following Lua table: { name => ..., help => ..., unit => ..., type => ..., resource => { key1 => value1, ..., keyN => valueN } metric => { [1] => { label => { key1 => value1, ..., keyN => valueN } value => ..., time => ..., interval => ..., meta => { key1 => value1, ..., keyN => valueN } }, ... [N] => { ... } } Closes: #3654 Signed-off-by: Kentaro Hayashi --- diff --git a/src/lua.c b/src/lua.c index 3f48a55de..9434659c4 100644 --- a/src/lua.c +++ b/src/lua.c @@ -139,7 +139,7 @@ static int clua_read(user_data_t *ud) /* {{{ */ return status; } /* }}} int clua_read */ -static int clua_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */ +static int clua_write(metric_family_t const *mf, /* {{{ */ user_data_t *ud) { clua_callback_data_t *cb = ud->data; @@ -156,11 +156,11 @@ static int clua_write(const data_set_t *ds, const value_list_t *vl, /* {{{ */ } /* +1 = 1 */ - status = luaC_pushvaluelist(L, ds, vl); + status = luaC_pushmetricfamily(L, mf); if (status != 0) { lua_pop(L, 1); /* -1 = 0 */ pthread_mutex_unlock(&cb->lock); - ERROR("Lua plugin: luaC_pushvaluelist failed."); + ERROR("Lua plugin: luaC_pushmetricfamily failed."); return -1; } /* +1 = 2 */ diff --git a/src/utils_lua.c b/src/utils_lua.c index bbec52c69..de5220884 100644 --- a/src/utils_lua.c +++ b/src/utils_lua.c @@ -98,45 +98,6 @@ static int ltoc_table_values(lua_State *L, int idx, /* {{{ */ return status; } /* }}} int ltoc_table_values */ -static int luaC_pushvalues(lua_State *L, const data_set_t *ds, - const value_list_t *vl) /* {{{ */ -{ - assert(vl->values_len == ds->ds_num); - - lua_newtable(L); - for (size_t i = 0; i < vl->values_len; i++) { - lua_pushinteger(L, (lua_Integer)i + 1); - luaC_pushvalue(L, vl->values[i], ds->ds[i].type); - lua_settable(L, -3); - } - - return 0; -} /* }}} int luaC_pushvalues */ - -static int luaC_pushdstypes(lua_State *L, const data_set_t *ds) /* {{{ */ -{ - lua_newtable(L); - for (size_t i = 0; i < ds->ds_num; i++) { - lua_pushinteger(L, (lua_Integer)i); - lua_pushstring(L, DS_TYPE_TO_STRING(ds->ds[i].type)); - lua_settable(L, -3); - } - - return 0; -} /* }}} int luaC_pushdstypes */ - -static int luaC_pushdsnames(lua_State *L, const data_set_t *ds) /* {{{ */ -{ - lua_newtable(L); - for (size_t i = 0; i < ds->ds_num; i++) { - lua_pushinteger(L, (lua_Integer)i); - lua_pushstring(L, ds->ds[i].name); - lua_settable(L, -3); - } - - return 0; -} /* }}} int luaC_pushdsnames */ - /* * Public functions */ @@ -274,38 +235,161 @@ int luaC_pushvalue(lua_State *L, value_t v, int ds_type) /* {{{ */ return 0; } /* }}} int luaC_pushvalue */ -int luaC_pushvaluelist(lua_State *L, const data_set_t *ds, - const value_list_t *vl) /* {{{ */ +static int luaC_pushlabelset(lua_State *L, label_set_t label) /* {{{ */ { - lua_newtable(L); - - lua_pushstring(L, vl->host); - lua_setfield(L, -2, "host"); - - lua_pushstring(L, vl->plugin); - lua_setfield(L, -2, "plugin"); - lua_pushstring(L, vl->plugin_instance); - lua_setfield(L, -2, "plugin_instance"); - - lua_pushstring(L, vl->type); - lua_setfield(L, -2, "type"); - lua_pushstring(L, vl->type_instance); - lua_setfield(L, -2, "type_instance"); - - luaC_pushvalues(L, ds, vl); - lua_setfield(L, -2, "values"); + /* + label_set_t will be mapped to the following Lua table: - luaC_pushdstypes(L, ds); - lua_setfield(L, -2, "dstypes"); + label = { + name1 => value1, + ... + nameN => valueN + } + */ + lua_newtable(L); + label_pair_t *ptr = label.ptr; + for (int i = 0; i < label.num; i++) { + /* label set key must be unique */ + lua_pushstring(L, ptr->value); + lua_setfield(L, -2, ptr->name); + ptr++; + } + return 0; +} /* }}} int luaC_pushlabelset */ - luaC_pushdsnames(L, ds); - lua_setfield(L, -2, "dsnames"); +static int luaC_pushmetadata(lua_State *L, meta_data_t *meta) /* {{{ */ +{ + /* + meta_data_t will be mapped to the following Lua table: - luaC_pushcdtime(L, vl->time); - lua_setfield(L, -2, "time"); + meta => { + key1 => value1, + ..., + keyN => valueN + } + */ + char **toc = NULL; + lua_newtable(L); + if (!meta) { + /* meta = {} */ + return -1; + } + int meta_count = meta_data_toc(meta, &toc); + if (meta_count == 0 || toc == NULL) { + /* meta = {} */ + WARNING("lua: can't count meta_entry_t in meta_data_t: %d", meta_count); + return -1; + } + for (int i = 0; i < meta_count; i++) { + int meta_type = meta_data_type(meta, toc[i]); + int exist = meta_data_exists(meta, toc[i]); + if (!exist) { + WARNING("lua: can't get nth key (%s) of meta_data_t", toc[i]); + continue; + } + switch (meta_type) { + case MD_TYPE_STRING: { + char *string = NULL; + meta_data_get_string(meta, toc[i], &string); + lua_pushstring(L, string); + } break; + case MD_TYPE_SIGNED_INT: { + int64_t i64value = 0; + meta_data_get_signed_int(meta, toc[i], &i64value); + lua_pushnumber(L, i64value); + } break; + case MD_TYPE_UNSIGNED_INT: { + uint64_t u64value = 0; + meta_data_get_unsigned_int(meta, toc[i], &u64value); + lua_pushnumber(L, u64value); + } break; + case MD_TYPE_DOUBLE: { + double dvalue = 0.0; + meta_data_get_double(meta, toc[i], &dvalue); + lua_pushnumber(L, dvalue); + } break; + case MD_TYPE_BOOLEAN: { + bool bvalue = false; + meta_data_get_boolean(meta, toc[i], &bvalue); + lua_pushboolean(L, bvalue); + } break; + default: + /* should not happen */ + break; + } + if (MD_TYPE_STRING >= meta_type && meta_type <= MD_TYPE_BOOLEAN) { + /* metadata keys must be unique */ + lua_setfield(L, -2, toc[i]); + } + } + return 0; +} /* }}} int luaC_pushmetadata */ - luaC_pushcdtime(L, vl->interval); - lua_setfield(L, -2, "interval"); +static int luaC_pushmetriclist(lua_State *L, metric_list_t metric) /* {{{ */ +{ + /* + metric_list_t will be mapped to the following Lua table: + + metric = { + [1] => { + label => luaC_pushlabelset(), + value => luaC_pushvalue() + time => luaC_pushcdtime(), + interval => luaC_pushcdtime(), + meta => luaC_pushmetadata(), + }, + ... + [N] => { + ... + } + */ + lua_newtable(L); + metric_t *ptr = metric.ptr; + for (int i = 0; i < metric.num; i++) { + lua_newtable(L); + luaC_pushlabelset(L, ptr->label); + lua_setfield(L, -2, "label"); + luaC_pushvalue(L, ptr->value, ptr->family->type); + lua_setfield(L, -2, "value"); + luaC_pushcdtime(L, ptr->time); + lua_setfield(L, -2, "time"); + luaC_pushcdtime(L, ptr->interval); + lua_setfield(L, -2, "interval"); + /* metric[N] = {} */ + luaC_pushmetadata(L, ptr->meta); + lua_setfield(L, -2, "meta"); + lua_rawseti(L, -2, i + 1); + ptr++; + } + return 0; +} /* }}} int luaC_pushmetriclist */ +int luaC_pushmetricfamily(lua_State *L, metric_family_t const *mf) /* {{{ */ +{ + /* + metric_family_t is mapped to the following Lua table: + + { + name => ..., + help => ..., + unit => ..., + type => ..., + resource => luaC_pushlabelset(), + metric => luaC_pushmetriclist(), + } + */ + lua_newtable(L); + lua_pushstring(L, mf->name); + lua_setfield(L, -2, "name"); + lua_pushstring(L, mf->help); + lua_setfield(L, -2, "help"); + lua_pushstring(L, mf->unit); + lua_setfield(L, -2, "unit"); + lua_pushinteger(L, (lua_Integer)mf->type); + lua_setfield(L, -2, "type"); + luaC_pushlabelset(L, mf->resource); + lua_setfield(L, -2, "resource"); + luaC_pushmetriclist(L, mf->metric); + lua_setfield(L, -2, "metric"); return 0; -} /* }}} int luaC_pushvaluelist */ +} /* }}} int luaC_pushmetricfamily */ diff --git a/src/utils_lua.h b/src/utils_lua.h index e5a3d7467..c73775986 100644 --- a/src/utils_lua.h +++ b/src/utils_lua.h @@ -46,7 +46,6 @@ value_list_t *luaC_tovaluelist(lua_State *L, int idx); */ int luaC_pushcdtime(lua_State *L, cdtime_t t); int luaC_pushvalue(lua_State *L, value_t v, int ds_type); -int luaC_pushvaluelist(lua_State *L, const data_set_t *ds, - const value_list_t *vl); +int luaC_pushmetricfamily(lua_State *L, metric_family_t const *mf); #endif /* UTILS_LUA_H */