Skip to content

lua: timestamp in millis#10329

Closed
mindyor wants to merge 9 commits intoenvoyproxy:masterfrom
mindyor:lua-timestamp
Closed

lua: timestamp in millis#10329
mindyor wants to merge 9 commits intoenvoyproxy:masterfrom
mindyor:lua-timestamp

Conversation

@mindyor
Copy link
Copy Markdown
Contributor

@mindyor mindyor commented Mar 11, 2020

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.rst
Release Notes: added release note in version_history.rst

Fixes #10282

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>
mindyor added 2 commits March 11, 2020 15:02
Signed-off-by: mindyor <or.mindy@gmail.com>
Signed-off-by: mindyor <or.mindy@gmail.com>
Signed-off-by: mindyor <or.mindy@gmail.com>
@mindyor mindyor changed the title [WIP] lua: timestamp in millis lua: timestamp in millis Mar 12, 2020
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) {
Copy link
Copy Markdown
Member

@dio dio Mar 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Copy link
Copy Markdown
Member

@dio dio Mar 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
                end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");
          },

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I think I see what you're saying, let me give it a shot.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dio
Copy link
Copy Markdown
Member

dio commented Mar 12, 2020

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

Signed-off-by: mindyor <or.mindy@gmail.com>
int StreamHandleWrapper::luaTimestamp(lua_State* state) {
auto now = time_source_.systemTime().time_since_epoch();

auto time_unit = luaL_optstring(state, 2, "");
Copy link
Copy Markdown
Contributor

@snowp snowp Mar 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't quite figured out the enum thing, pushed absl::string_view unit_paramter... change in the meantime.

mindyor added 4 commits March 13, 2020 20:53
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) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, should we do enumToInt() here?

if (resolution_as_int_from_state == enumToInt(Timestamp::Resolution::Millisecond)) {
  ...
}

@stale
Copy link
Copy Markdown

stale bot commented Mar 23, 2020

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!

@stale stale bot added the stale stalebot believes this issue/PR has not been touched recently label Mar 23, 2020
@stale
Copy link
Copy Markdown

stale bot commented Mar 30, 2020

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!

@stale stale bot closed this Mar 30, 2020
@dio
Copy link
Copy Markdown
Member

dio commented Mar 30, 2020

Hum, I can take over this if it is needed @mindyor.

@mindyor
Copy link
Copy Markdown
Contributor Author

mindyor commented Dec 11, 2020

Gah, sorry for ghosting. Any thoughts if I pick this back up again?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale stalebot believes this issue/PR has not been touched recently

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lua: timestamp in millis/ns

4 participants