Skip to content

lua: add stats API for creating counters, gauges, and histograms#44334

Merged
wbpcode merged 5 commits intoenvoyproxy:mainfrom
derekargueta:dargueta-lua-stats-api
Apr 16, 2026
Merged

lua: add stats API for creating counters, gauges, and histograms#44334
wbpcode merged 5 commits intoenvoyproxy:mainfrom
derekargueta:dargueta-lua-stats-api

Conversation

@derekargueta
Copy link
Copy Markdown
Member

@derekargueta derekargueta commented Apr 8, 2026

Adds a stats() method to the Lua stream handle that returns a stats scope object, allowing Lua scripts to create and manipulate Envoy stats directly. This has been a requested feature for a while - being able to emit custom metrics from Lua scripts without needing to write a native filter.

The API provides:

  • counter(name) - create/get a counter with inc(), add(), value() methods
  • gauge(name) - create/get a gauge with inc(), dec(), add(), sub(), set(), value() methods
  • histogram(name, unit) - create/get a histogram with recordValue() method

Example usage:

function envoy_on_request(request_handle)
  local stats = request_handle:stats()
  
  local counter = stats:counter("my_requests")
  counter:inc()
  
  local gauge = stats:gauge("active_connections")
  gauge:set(10)
  
  local histogram = stats:histogram("request_latency", "ms")
  histogram:recordValue(42)
end

Stats are prefixed with lua. plus the optional stat_prefix from the filter config. Gauges use NeverImport mode since they track local process state and shouldn't be accumulated across hot restarts.

Risk Level: low
Testing: unit tests & integration test
Docs Changes: done
Release Notes: done
Platform Specific Features: no

@repokitteh-read-only
Copy link
Copy Markdown

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #44334 was opened by derekargueta.

see: more, trace.

@derekargueta derekargueta force-pushed the dargueta-lua-stats-api branch 5 times, most recently from 18b7bed to 8dd7403 Compare April 8, 2026 19:13
@derekargueta derekargueta marked this pull request as ready for review April 10, 2026 21:56
@derekargueta
Copy link
Copy Markdown
Member Author

All the CI failures I'm seeing are pointing to the Rust SDK which should be unrelated to this PR

Notice: INFO: Streaming build results to: https://mordenite.cluster.engflow.com/invocation/76748376-5a51-4e2c-9a18-32793a97c024
Notice: INFO: Streaming build results to: https://mordenite.cluster.engflow.com/invocation/a8fb24ec-1273-4d79-8a7f-1874a23e774d
Notice: INFO: Streaming build results to: https://mordenite.cluster.engflow.com/invocation/b83dac8c-aa81-4dfb-aa23-185dc616a6b4
Notice: INFO: Streaming build results to: https://mordenite.cluster.engflow.com/invocation/e279ddee-7510-4292-9b54-6923a0084147
Error: error: aborting due to 2 previous errors
Error: error: unnecessary `unsafe` block
Error: ERROR: /source/test/extensions/dynamic_modules/BUILD:66:10: Compiling Rust bin rust_sdk_test (19 files) failed: (Exit 1): process_wrapper failed: error executing Rustc command (from target //test/extensions/dynamic_modules:rust_sdk_test)
Error: ERROR: Build did NOT complete successfully
Warning: WARNING: The background upload of the Build Event Protocol for the previous invocation failed to complete in 5.000 seconds. Cancelling and starting a new invocation...

so opening...

hello Envoy friends :) 👋

@derekargueta derekargueta force-pushed the dargueta-lua-stats-api branch from 8dd7403 to cef13e4 Compare April 10, 2026 23:17
Copy link
Copy Markdown
Member

@wbpcode wbpcode left a comment

Choose a reason for hiding this comment

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

Thanks for this great contribution. I guess this cannot catch up 1.38 but should be good to be part of 1.39.

Could you merge main to fix the CI also? Thanks.

* @return Stats::ScopeSharedPtr the stats scope for creating custom Lua stats. The scope
* is pre-configured with the appropriate lua stat prefix.
*/
virtual Stats::ScopeSharedPtr statsScope() PURE;
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.

Suggested change
virtual Stats::ScopeSharedPtr statsScope() PURE;
virtual Stats::Scope& statsScope() PURE;

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.

we needn't to extend the lifetime of scope so reference is preferred..

@wbpcode wbpcode self-assigned this Apr 11, 2026
@derekargueta derekargueta force-pushed the dargueta-lua-stats-api branch 2 times, most recently from 09aeec5 to 3cb64a6 Compare April 13, 2026 17:24
@derekargueta
Copy link
Copy Markdown
Member Author

@wbpcode updated - sorry about the force-pushes, had to fix my DCO workflow.

@derekargueta
Copy link
Copy Markdown
Member Author

derekargueta commented Apr 13, 2026

MacOS and do_ci.sh config Precheck builds hit a network flake with www.antlr.org

Error: ERROR: /private/var/tmp/_bazel_runner/24e7a101d26e6b05dd6bbfabf7fb2d6f/external/bazel_tools/tools/build_defs/repo/http.bzl:224:33: An error occurred during the fetch of repository 'antlr4_jar':
Error: ERROR: /private/var/tmp/_bazel_runner/24e7a101d26e6b05dd6bbfabf7fb2d6f/external/cel-cpp/bazel/BUILD:4:12: @@cel-cpp//bazel:antlr4_tool depends on @@antlr4_jar//jar:jar in repository @@antlr4_jar which failed to fetch. no such package '@@antlr4_jar//jar': java.io.IOException: Error downloading [https://www.antlr.org/download/antlr-4.13.1-complete.jar] to /private/var/tmp/_bazel_runner/24e7a101d26e6b05dd6bbfabf7fb2d6f/external/antlr4_jar/jar/downloaded.jar: GET returned 503 Service Unavailable
Error: ERROR: Analysis of target '//source/exe:envoy-static' failed; build aborted: Analysis failed
Error: ERROR: Build did NOT complete successfully
Error: ERROR: no such package '@@antlr4_jar//jar': java.io.IOException: Error downloading [https://www.antlr.org/download/antlr-4.13.1-complete.jar] to /private/var/tmp/_bazel_runner/24e7a101d26e6b05dd6bbfabf7fb2d6f/external/antlr4_jar/jar/downloaded.jar: GET returned 503 Service Unavailable

Signed-off-by: Derek Argueta <derek.argueta@airbnb.com>
Signed-off-by: Derek Argueta <derek.argueta@airbnb.com>
Signed-off-by: Derek Argueta <derek.argueta@airbnb.com>
Signed-off-by: Derek Argueta <derek.argueta@airbnb.com>
@derekargueta derekargueta force-pushed the dargueta-lua-stats-api branch from 9b3530c to 758ad2e Compare April 13, 2026 22:08
@derekargueta derekargueta requested a review from wbpcode April 14, 2026 03:58
Signed-off-by: Derek Argueta <derek.argueta@airbnb.com>
Copy link
Copy Markdown
Member

@wbpcode wbpcode left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks.

@wbpcode
Copy link
Copy Markdown
Member

wbpcode commented Apr 16, 2026

The release of 1.38 is delayed and this change is low risk change.

@wbpcode wbpcode merged commit 0614eca into envoyproxy:main Apr 16, 2026
29 checks passed
@derekargueta derekargueta deleted the dargueta-lua-stats-api branch April 16, 2026 17:12
@derekargueta
Copy link
Copy Markdown
Member Author

Thanks for squeezing this in @wbpcode!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants