Envoy Proxy Configuration

The following Lua Request and Response filters can be used to meter arbitrary API traffic passing through an Envoy Proxy.

request.lua

function envoy_on_request(request_handle)
    local api_client = request_handle:headers():get("x-revenium-product-key")
    if (api_client ~= nil) then
        if request_handle:connection():ssl() == nil then
            request_handle:streamInfo():dynamicMetadata():set("revenium", "scheme", "http")
        else
            request_handle:streamInfo():dynamicMetadata():set("revenium", "scheme", "https")
        end
        local method = request_handle:headers():get(":method")
        local path = request_handle:headers():get(":path")
        request_handle:streamInfo():dynamicMetadata():set("revenium", "address", address)
        request_handle:streamInfo():dynamicMetadata():set("revenium", "method", method)
        request_handle:streamInfo():dynamicMetadata():set("revenium", "path", path)
        request_handle:streamInfo():dynamicMetadata():set("revenium", "api_client", api_client)
    end
end

response.lua

function envoy_on_response(response_handle)
    local api_client = response_handle:streamInfo():dynamicMetadata():get("profitstream")["api_client"]
    local status = tonumber(response_handle:headers():get(":status"))
    if (api_client ~= nil and (status >= 200 and status <= 299)) then
        local method = response_handle:streamInfo():dynamicMetadata():get("profitstream")["method"]
        local path = response_handle:streamInfo():dynamicMetadata():get("profitstream")["path"]
        local scheme = response_handle:streamInfo():dynamicMetadata():get("profitstream")["scheme"]
        local api = response_handle:metadata():get("profitstream_api_id")
        local body = "api=" .. api .. "&apiClient=" .. api_client .. "&method=" .. method .. "&url=" .. path
        local headers, response_body = response_handle:httpCall("profitstream",
            {
                [":method"] = "POST",
                [":path"] = "/",
                [":authority"] = "profitstream",
            },
            body, 5000)
        local ps_status = tonumber(headers[":status"])
        if (ps_status > 200 or ps_status > 299) then
            response_handle:logError("Could not post body to metering beacon: " .. body)
        else
            response_handle:logDebug("Successfully posted body to metering beacon: " .. body)
        end
    end
end

Last updated

© Revenium - www.revenium.io