Conversation
Signed-off-by: mindyor <or.mindy@gmail.com> update url for programming in lua pdf Signed-off-by: mindyor <or.mindy@gmail.com> draft documentation Signed-off-by: mindyor <or.mindy@gmail.com> timestamp yields millis since unix epoch Signed-off-by: mindyor <or.mindy@gmail.com> formatting nits Signed-off-by: mindyor <or.mindy@gmail.com>
Signed-off-by: mindyor <or.mindy@gmail.com>
Signed-off-by: mindyor <or.mindy@gmail.com>
Signed-off-by: mindyor <or.mindy@gmail.com>
| auto now = time_source_.systemTime().time_since_epoch(); | ||
|
|
||
| auto time_unit = luaL_optstring(state, 2, ""); | ||
| if (strcmp(time_unit, "") == 0 || strcmp(time_unit, "milliseconds_since_epoch") == 0) { |
There was a problem hiding this comment.
Can we do enum (enum class in C++, table in lua) here? To accomplish that, I can think probably we can add an additional initializer function when we initialize the lua_state_. This initializer basically populate global state with e.g. a global table.
There was a problem hiding this comment.
Hm, were you thinking of initializing it here? https://github.com/mindyor/envoy/blob/master/source/extensions/filters/http/lua/lua_filter.cc#L518
lua_newtable/lua_createtable want a lua_State, but I only see lua_state_ which is a ThreadLocalState. Is there a way to unwrap lua_State from here? I'm all ears!
There was a problem hiding this comment.
I put a quick hack: https://github.com/envoyproxy/envoy/compare/master...dio:lua-enum?expand=1. Sure you can hack it up to create a registerEnum() function, better macros etc too.
By having that,
I managed to log the enums, within each global "hook" function that we have:
- name: envoy.filters.http.lua
typed_config:
"@type": type.googleapis.com/envoy.config.filter.http.lua.v2.Lua
inline_code: |
function envoy_on_request(request_handle)
request_handle:logInfo(tostring(EnvoyTimestampResolution.MILLISECOND))
request_handle:logInfo(tostring(EnvoyTimestampResolution.NANOSECOND))
end
function envoy_on_response(response_handle)
endThere was a problem hiding this comment.
This is the way I register it. A bit ugly.
[](lua_State* state) {
lua_newtable(state);
{
LUA_ENUM(state, MILLISECOND, Timestamp::Resolution::Millisecond);
LUA_ENUM(state, NANOSECOND, Timestamp::Resolution::Nanosecond);
}
lua_setglobal(state, "EnvoyTimestampResolution");
},There was a problem hiding this comment.
Hm, I think I see what you're saying, let me give it a shot.
There was a problem hiding this comment.
Pushing an implementation per your sketch (thank you for all of the detail!). I will try to come back to this PR a bit later this week (getting pulled into other things) to refactor it.
|
Hum, sorry, it seems like merging "suggested" code change from github UI gives you DCO error :(. https://github.com/envoyproxy/envoy/blob/master/CONTRIBUTING.md#fixing-dco |
| int StreamHandleWrapper::luaTimestamp(lua_State* state) { | ||
| auto now = time_source_.systemTime().time_since_epoch(); | ||
|
|
||
| auto time_unit = luaL_optstring(state, 2, ""); |
There was a problem hiding this comment.
i think you can do absl::string_view unit_parameter = ... and not have to use the strcmp helpers, relying on empty() and operator== instead
that said id be curious to see if @dio's suggestion makes things cleaner, wdyt?
There was a problem hiding this comment.
Haven't quite figured out the enum thing, pushed absl::string_view unit_paramter... change in the meantime.
Signed-off-by: mindyor <or.mindy@gmail.com>
Signed-off-by: mindyor <or.mindy@gmail.com>
Signed-off-by: mindyor <or.mindy@gmail.com>
Signed-off-by: mindyor <or.mindy@gmail.com>
| auto millisecond = get_field(state, "EnvoyTimestampResolution", "MILLISECOND"); | ||
| auto nanosecond = get_field(state, "EnvoyTimestampResolution", "NANOSECOND"); | ||
|
|
||
| if (unit_parameter.empty() || unit_parameter.compare(std::to_string(millisecond)) == 0) { |
There was a problem hiding this comment.
Hum, should we do enumToInt() here?
if (resolution_as_int_from_state == enumToInt(Timestamp::Resolution::Millisecond)) {
...
}|
This pull request has been automatically marked as stale because it has not had activity in the last 7 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions! |
|
This pull request has been automatically closed because it has not had activity in the last 14 days. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions! |
|
Hum, I can take over this if it is needed @mindyor. |
|
Gah, sorry for ghosting. Any thoughts if I pick this back up again? |
Description: adds Lua function to get timestamp in higher resolution. Resolutions are: milliseconds since epoch and nanoseconds since epoch. Resolution defaults to milliseconds since epoch.
Risk Level: small-medium?
Testing: added unit test.
Docs Changes: added documentation for function in
lua_filter.rstRelease Notes: added release note in
version_history.rstFixes #10282