lua: add stats API for creating counters, gauges, and histograms #44334
Envoy/Prechecks (failure)
Check has finished
Details
Check run finished (failure ❌)
The check run can be viewed here:
Envoy/Prechecks (pr/44334/main@8dd7403)
Check started by
Request (pr/44334/main@8dd7403)
@derekargueta
8dd7403 #44334
merge main@7367e6c
lua: add stats API for creating counters, gauges, and histograms
Description
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 withinc(),add(),value()methodsgauge(name)- create/get a gauge withinc(),dec(),add(),sub(),set(),value()methodshistogram(name, unit)- create/get a histogram withrecordValue()methodExample 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) endStats are prefixed with
lua.plus the optionalstat_prefixfrom the filter config. Gauges useNeverImportmode since they track local process state and shouldn't be accumulated across hot restarts.Risk Level: Low
This is an additive API that doesn't change existing behavior.
Testing
- Unit tests for all wrapper operations and error cases
- Integration test verifying end-to-end behavior including cross-phase stat accumulation
Test Plan
- Unit tests pass (
//test/extensions/filters/http/lua:wrappers_test)- Filter tests pass (
//test/extensions/filters/http/lua:lua_filter_test)- Integration test passes (
//test/extensions/filters/http/lua:lua_integration_test --test_filter="*StatsApi*")Documentation
Added docs for the new
stats()method and all stat object APIs tolua_filter.rst.
Signed-off-by: Derek Argueta derek.argueta@airbnb.com
Environment
Request variables
| Key | Value |
|---|---|
| ref | 708390b |
| sha | 8dd7403 |
| pr | 44334 |
| base-sha | 7367e6c |
| actor | |
| message | lua: add stats API for creating counters, gauges, and histograms... |
| started | 1775675613.950105 |
| target-branch | main |
| trusted | false |
Build image
Container image/s (as used in this CI run)
| Key | Value |
|---|---|
| default | docker.io/envoyproxy/envoy-build:86873047235e9b8232df989a5999b9bebf9db69c |
| mobile | docker.io/envoyproxy/envoy-build:mobile-86873047235e9b8232df989a5999b9bebf9db69c |
Version
Envoy version (as used in this CI run)
| Key | Value |
|---|---|
| major | 1 |
| minor | 38 |
| patch | 0 |
| dev | true |