Skip to content

LAL: add def local variable support with toJson/toJsonArray and type cast#13737

Open
wu-sheng wants to merge 5 commits intomasterfrom
feature/lal-def-variables
Open

LAL: add def local variable support with toJson/toJsonArray and type cast#13737
wu-sheng wants to merge 5 commits intomasterfrom
feature/lal-def-variables

Conversation

@wu-sheng
Copy link
Member

Add def local variable support in LAL DSL

  • Update the documentation to include this new feature.
  • Tests(including UT, IT, E2E) are added to verify the new feature.

Grammar: def varName = expression with optional as TypeName cast (built-in types or FQCN).

Built-in functions: toJson() converts String/Map/protobuf Struct to Gson JsonObject; toJsonArray() converts to JsonArray.

Type inference: Variable type inferred at compile time from initializer expression. Supports null-safe navigation (?.) and method chaining on the inferred type. Optional as cast narrows type when the inferred type is too general (e.g., Object from 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 LALClassGeneratorTestBase with .class file output to target/lal-generated-classes/ and {TestClass}_{testMethod} naming.

ALS-specific tests: EnvoyAlsLalTest in envoy receiver module — 3 tests for JWT extraction from proto filter_metadata via chained def variables.

…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>
@wu-sheng wu-sheng added feature New feature backend OAP backend related. logging Log collecting and analysis labels Mar 10, 2026
@wu-sheng wu-sheng added this to the 10.4.0 milestone Mar 10, 2026
@wu-sheng wu-sheng requested review from Copilot and wankai123 March 10, 2026 14:06
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 = expression with optional as casts (built-ins + qualified names).
  • Introduce runtime JSON conversion helpers in LalRuntimeHelper and 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.

wu-sheng and others added 2 commits March 10, 2026 22:37
…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>
…tion arity

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

…il on unknown method arg identifiers

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend OAP backend related. feature New feature logging Log collecting and analysis

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants