LAL: add def local variable support with toJson/toJsonArray and type cast#13737
Open
LAL: add def local variable support with toJson/toJsonArray and type cast#13737
Conversation
…cast Add `def` keyword to LAL grammar for declaring local variables in extractor and filter blocks. Type is inferred from the initializer expression at compile time, with optional `as` cast for type narrowing (supports built-in types and fully qualified class names). Split monolithic LALClassGeneratorTest into 5 scoped test classes plus a shared base. Add EnvoyAlsLalTest in envoy receiver module for ALS-specific proto chain tests with generated .class output. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds def local variables to the LAL DSL, including compile-time type inference and optional as TypeName casts, plus JSON helper functions (toJson/toJsonArray) and reorganized test coverage to validate parsing, codegen, and execution.
Changes:
- Extend LAL grammar/model/parser/codegen to support
def var = expressionwith optionalascasts (built-ins + qualified names). - Introduce runtime JSON conversion helpers in
LalRuntimeHelperand add Gson dependency. - Add/reshape tests and script-based execution cases (including Envoy ALS-specific coverage) and update docs/changes log.
Reviewed changes
Copilot reviewed 23 out of 23 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| test/script-cases/scripts/lal/test-lal/feature-cases/execution-def.yaml | New script-case rules exercising def + toJson chains and safe navigation. |
| test/script-cases/scripts/lal/test-lal/feature-cases/execution-def.input.data | New script-case inputs/expected outputs for execution-def.yaml. |
| oap-server/server-receiver-plugin/envoy-metrics-receiver-plugin/src/test/java/org/apache/skywalking/oap/server/receiver/envoy/persistence/EnvoyAlsLalTest.java | New ALS-focused integration-style tests for chained def + toJson on proto Struct. |
| oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALScriptParserTest.java | Adds parser tests for def statements, toJson/toJsonArray, and qualified-name casts. |
| oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALExpressionExecutionTest.java | Updates assertions to consider output-builder stored standard fields. |
| oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALClassGeneratorTestBase.java | New shared base for generator tests, with .class dump support and naming. |
| oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALClassGeneratorTest.java | Removes the monolithic generator test class (split into scoped tests). |
| oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALClassGeneratorSinkTest.java | New scoped tests for sink/sampler/rateLimit/interpolation. |
| oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALClassGeneratorExtractorTest.java | New scoped tests for extractor behaviors (ProcessRegistry, metrics, input/output types). |
| oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALClassGeneratorDefTest.java | New scoped tests for def type inference + toJson/toJsonArray codegen. |
| oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALClassGeneratorConditionTest.java | New scoped tests for conditions/safe-nav/tag()/else-if generation. |
| oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALClassGeneratorBasicTest.java | New scoped tests for basic compilation and error handling. |
| oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/rt/LalRuntimeHelper.java | Adds Gson-based JSON conversion helpers (toJsonObject/toJsonArray) + Struct conversion. |
| oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALScriptParser.java | Adds parsing for def and qualified-name casts. |
| oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALScriptModel.java | Introduces DefStatement in the AST model. |
| oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALClassGenerator.java | Tracks def locals in codegen context and includes them in LVT + execute method generation. |
| oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALBlockCodegen.java | Implements def declaration/assignment codegen, typed chain resolution, and safe-nav updates. |
| oap-server/analyzer/log-analyzer/src/main/antlr4/org/apache/skywalking/lal/rt/grammar/LALParser.g4 | Extends grammar with defStatement and qualified-name casts. |
| oap-server/analyzer/log-analyzer/src/main/antlr4/org/apache/skywalking/lal/rt/grammar/LALLexer.g4 | Adds DEF keyword token. |
| oap-server/analyzer/log-analyzer/pom.xml | Adds Gson dependency for runtime JSON conversions. |
| oap-server/analyzer/log-analyzer/CLAUDE.md | Updates contributor guide with new test layout and def design notes. |
| docs/en/concepts-and-designs/lal.md | Documents def, type inference/casts, and toJson/toJsonArray functions; updates builder init signature docs. |
| docs/en/changes/changes.md | Records new def feature and LALOutputBuilder.init() signature breaking change. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
...alyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALBlockCodegen.java
Show resolved
Hide resolved
...er/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/rt/LalRuntimeHelper.java
Show resolved
Hide resolved
...er/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/rt/LalRuntimeHelper.java
Show resolved
Hide resolved
...test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALExpressionExecutionTest.java
Show resolved
Hide resolved
...alyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALBlockCodegen.java
Show resolved
Hide resolved
...alyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALBlockCodegen.java
Show resolved
Hide resolved
…d null literals Previously, method arguments in def variable chains only handled string and number literals — everything else silently became null. Now resolves local def variable names, boolean literals (true/false), and null. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…based _d0/_d1 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
wu-sheng
commented
Mar 10, 2026
.../test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALClassGeneratorTestBase.java
Show resolved
Hide resolved
…tion arity Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 23 out of 23 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
...alyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALBlockCodegen.java
Outdated
Show resolved
Hide resolved
...alyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALBlockCodegen.java
Outdated
Show resolved
Hide resolved
...alyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALBlockCodegen.java
Show resolved
Hide resolved
…il on unknown method arg identifiers Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
deflocal variable support in LAL DSLGrammar:
def varName = expressionwith optionalas TypeNamecast (built-in types or FQCN).Built-in functions:
toJson()converts String/Map/protobufStructto GsonJsonObject;toJsonArray()converts toJsonArray.Type inference: Variable type inferred at compile time from initializer expression. Supports null-safe navigation (
?.) and method chaining on the inferred type. Optionalascast narrows type when the inferred type is too general (e.g.,Objectfrom generic API).Runtime helpers:
LalRuntimeHelper.toJsonObject()overloads for String, Map, and protobuf Struct (recursive field conversion).Test reorganization: Split monolithic
LALClassGeneratorTest(641 lines) into 5 scoped test classes:LALClassGeneratorBasicTest(10 tests)LALClassGeneratorConditionTest(10 tests)LALClassGeneratorExtractorTest(10 tests)LALClassGeneratorDefTest(10 tests)LALClassGeneratorSinkTest(5 tests)Plus shared
LALClassGeneratorTestBasewith.classfile output totarget/lal-generated-classes/and{TestClass}_{testMethod}naming.ALS-specific tests:
EnvoyAlsLalTestin envoy receiver module — 3 tests for JWT extraction from protofilter_metadatavia chaineddefvariables.CHANGESlog.