Skip to content

lua: add support for stats API

8dd7403
Select commit
Loading
Failed to load commit list.
Draft

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

lua: add support for stats API
8dd7403
Select commit
Loading
Failed to load commit list.
CI (Envoy) / Mobile/iOS tests skipped Apr 8, 2026 in 0s

Check was skipped

This check was not triggered in this CI run

Details

Request (pr/44334/main@8dd7403)

derekargueta @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 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

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 to lua_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 derekargueta @derekargueta
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