$ show apache/skywalking-nginx-lua
This repository contains the Apache SkyWalking Nginx Agent, a Lua-based library that provides distributed tracing capabilities for Nginx and OpenResty deployments. The agent integrates with Nginx's Lua API to automatically trace incoming and outgoing HTTP requests, propagate trace context between services using custom headers (sw8 and sw8-correlation), and report trace data to a SkyWalking OAP server. The architecture uses Nginx's phased execution model (init_worker, rewrite, body_filter, log) to manage the tracing lifecycle, with a shared dictionary for buffering trace segments and a timer for periodic reporting and heartbeats. The codebase demonstrates strong engineering practices with clear separation of concerns, comprehensive error handling, and performance optimizations like table pooling to reduce garbage collection pressure in LuaJIT. Several components show particular craftsmanship and reusability: the utility module provides high-performance table pooling and ID generation; the correlation context module implements lightweight context propagation via base64-encoded headers; and the segment reference module handles trace context serialization for cross-process tracing.
The LLM identified 3 files worth highlighting for reuse:
Provides utility functions including timestamp/ID generation, string splitting, and a sophisticated table pooling mechanism to reduce GC pressure in LuaJIT. The tablepool implementation shows advanced LuaJIT performance optimization knowledge by reusing temporary tables across requests. Conditionals handle both OpenResty and standard Lua environments with appropriate fallbacks. This component is highly reusable in any Lua/JIT project needing efficient temporary object management.
Implements lightweight correlation context propagation using base64-encoded key-value pairs in HTTP headers. Enforces size limits (max 3 elements, 128 chars/value) for header safety. Provides clean API for putting/getting correlation data with automatic serialization/deserialization. This self-contained solution for propagating lightweight context between services could be extracted and used in any distributed tracing or microservices project with minimal modification.
Handles serialization and deserialization of segment references for cross-process trace context propagation using the SW8 header format. Encodes trace IDs, segment IDs, span IDs, and parent references into a compact base64 string. Clean separation of concerns focused solely on reference data format. This component is essential for distributed tracing and could be reused in any tracing system needing to propagate context between services with minimal adaptation.