From 536b6e61817e1a2f0dfa4fa0c0d4bf2ee5dc7d81 Mon Sep 17 00:00:00 2001 From: wankai123 Date: Tue, 17 Nov 2026 10:02:12 +0800 Subject: [PATCH 1/2] Decide the layer of a log without one by LAL fallback rules, and save OTLP_GENERAL logs A log without a layer runs the `layer: auto` rules, then, when none claimed it, the new `fallback: true` auto rules, and is dropped when none claims it either. The routing to the GENERAL rules in LogAnalyzer and the GENERAL fallback in TrafficSinkListener are removed; the new `default-fallback` rule in lal/default.yaml files such a log under GENERAL, as before, and operators edit it to choose another layer. Fallback rules share the auto rules' name space and rule key, `fallback: true` is allowed only with `layer: auto`, and the fallback pass starts from the layer as received, since the rules share one LogMetadata. The new `otlp-general` rule saves the logs of the OpenTelemetry services that set `service.layer=OTLP_GENERAL`, which no rule matched, so they were dropped. A static LAL rule declaring an unregistered or missing layer is skipped with a warning at boot, and a runtime rule declaring one is rejected, instead of loading under UNDEFINED and never running. The mini-program error logs are handled by rules declared for WECHAT_MINI_PROGRAM and ALIPAY_MINI_PROGRAM, replacing the `layer: auto` rule; mini-program-monitor >= v0.5.0 sets service.layer (SkyAPM/mini-program-monitor#11). The e2e cases run its simulator images of that merge commit. LALExpressionExecutionTest looked for its cases in directories that no longer exist and ran none; it reads them from the module's test resources again, with its harness aligned to LALScriptExecutionTest. --- docs/en/changes/changes.md | 3 + docs/en/concepts-and-designs/lal.md | 52 ++++++- .../backend/admin-api/dsl-debugging-lal.md | 5 +- .../backend-alipay-mini-program-monitoring.md | 15 +- .../backend-wechat-mini-program-monitoring.md | 19 ++- oap-server/analyzer/log-analyzer/CLAUDE.md | 27 +++- .../log/analyzer/v2/provider/LALConfig.java | 6 + .../analyzer/v2/provider/log/LogAnalyzer.java | 66 +++++---- .../listener/LogAnalysisListenerFactory.java | 8 ++ .../log/listener/LogFilterListener.java | 112 +++++++++++---- .../log/listener/TrafficSinkListener.java | 9 +- .../compiler/LALExpressionExecutionTest.java | 26 +++- .../provider/log/LogAnalyzerRoutingTest.java | 122 +++++++++++++++++ .../listener/LogFilterListenerLayerTest.java | 129 ++++++++++++++++++ .../lal/test-lal/oap-cases/default.data.yaml | 17 +++ .../lal/test-lal/oap-cases/default.yaml | 21 +++ .../test-lal/oap-cases/miniprogram.data.yaml | 69 ++++++++++ .../lal/test-lal/oap-cases/miniprogram.yaml | 87 ++++++++++++ .../src/main/resources/lal/default.yaml | 21 +++ .../src/main/resources/lal/miniprogram.yaml | 57 +++++--- .../seed-rules/lal-multistatement.yaml | 4 +- test/e2e-v2/cases/log/lal.yaml | 4 +- .../miniprogram/alipay/docker-compose.yml | 2 +- test/e2e-v2/cases/miniprogram/alipay/e2e.yaml | 2 +- .../miniprogram/wechat/docker-compose.yml | 2 +- test/e2e-v2/cases/miniprogram/wechat/e2e.yaml | 2 +- .../native/expected/logs-otlp-general.yml | 31 +++++ .../expected/services-otlp-general-log.yml | 24 ++++ .../cases/otlp-traces/native/log-cases.yaml | 10 ++ .../runtime-rule/lal/seed-rules/lal-v1.yaml | 4 +- .../runtime-rule/lal/seed-rules/lal-v2.yaml | 4 +- .../cases/satellite/native-protocols/lal.yaml | 4 +- 32 files changed, 847 insertions(+), 117 deletions(-) create mode 100644 oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzerRoutingTest.java create mode 100644 oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListenerLayerTest.java create mode 100644 oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/miniprogram.data.yaml create mode 100644 oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/miniprogram.yaml create mode 100644 test/e2e-v2/cases/otlp-traces/native/expected/logs-otlp-general.yml create mode 100644 test/e2e-v2/cases/otlp-traces/native/expected/services-otlp-general-log.yml diff --git a/docs/en/changes/changes.md b/docs/en/changes/changes.md index 1fb8e2436b46..42807996760b 100644 --- a/docs/en/changes/changes.md +++ b/docs/en/changes/changes.md @@ -42,6 +42,9 @@ * Fix the trace and Zipkin queries putting an event of eBPF profiling on the wrong span, or losing it. A server-side event (a request read, or a response written, on the server, attached to the client's span through its trace context) belongs on the server's span, while the client-side events of that span stay on it; but the span an event went to was remembered by the span the event named alone, so every event of a span followed its first one. On a Zipkin trace, a span that took both its own events and the server-side events of its client span was also rebuilt twice, keeping only one of the two sets. Each event now goes to the span of its own side. * Fix span-attached events of eBPF profiling overwriting each other. An event's storage ID was its segment ID, start time and name. Rover sends a Zipkin event with an empty segment ID, so two Zipkin events with the same name and start time, in any two traces, had one ID, and so could two SkyWalking events on different spans of one segment. Elasticsearch kept only the one written last, and JDBC, whose row key is the same ID, cannot hold both. The ID now also carries the trace ID and span ID. * Add the `query-server` module, so the native query APIs can have their own port, apart from the ports agents report to. Set `SW_QUERY_SERVER_REST_PORT` to a port greater than 0, and GraphQL (`/graphql`), the AI agent conversation view and files (`/ai-agent/conversations/*`), and `/status/config/ttl` move from the core REST port (default `12800`) to it, with their own host, context path and TLS settings (`SW_QUERY_SERVER_REST_*`). Point the UI's `queryUrl`, `swctl --base-url` and other GraphQL clients at the new port. With the default `0`, nothing changes. Together with `receiver-sharing-server`, no agent or query traffic is left on the core ports. +* LAL: save the logs of the OpenTelemetry services that set `service.layer=OTLP_GENERAL` through the new `otlp-general` rule in `lal/default.yaml`; no rule matched that layer, so these logs were dropped. A LAL rule declaring an unregistered or missing layer (names are case-sensitive) is now skipped with a warning at boot and rejected when applied as a runtime rule, instead of loading silently under `UNDEFINED` and never running. +* LAL: a log without a layer that no `layer: auto` rule claims runs the new `fallback: true` auto rules, instead of the rules declared for `GENERAL`, and is dropped when none claims it either, so where such a log is filed is decided by a rule, not hard-coded. The new `default-fallback` rule in `lal/default.yaml` files it under `GENERAL`, as before; edit its extractor to file it under another layer. A fallback rule must set the layer, as any auto rule. A custom `layer: GENERAL` rule written for the logs without a layer must declare `layer: auto` and `fallback: true` and set `layer "GENERAL"` in its extractor; it then runs next to `default-fallback`, as it ran next to `default` before. +* LAL: handle the mini-program error logs with the `miniprogram-errors-wechat` and `miniprogram-errors-alipay` rules in `lal/miniprogram.yaml`, declared for the `WECHAT_MINI_PROGRAM` and `ALIPAY_MINI_PROGRAM` layers, replacing the `layer: auto` rule `miniprogram-errors`. The mini-program SDK v0.5.0 sets `service.layer`, which routes its error logs to these rules; it is now the minimum supported SDK version, since an older SDK's error logs carry no layer and are filed under `GENERAL` without the `error_count` metric. #### UI diff --git a/docs/en/concepts-and-designs/lal.md b/docs/en/concepts-and-designs/lal.md index 65e11e65fa79..736fd6129f42 100644 --- a/docs/en/concepts-and-designs/lal.md +++ b/docs/en/concepts-and-designs/lal.md @@ -29,6 +29,28 @@ Use `tag 'key': sourceAttribute("attr")` in the extractor to selectively persist ## Layer Layer should be declared in the LAL script to represent the analysis scope of the logs. LAL rules are routed by layer — only rules matching the incoming log's layer are evaluated. +A rule's `layer` must be a registered layer name (case-sensitive) or `auto`. A rule declaring an unregistered or +missing layer is skipped with a warning at boot, and rejected when applied as a runtime rule. + +A log is routed as follows: + +1. A log whose layer is set, by the agent's `LogData.layer` or the OTLP `service.layer` resource attribute, + runs the rules declared for that layer. A log whose layer is not registered is dropped. +2. A log without a layer runs the `layer: auto` rules, see [Auto layer](#auto-layer). +3. When no `layer: auto` rule claims it, it runs the `layer: auto` rules declared `fallback: true`. +4. When no fallback rule claims it either, it is dropped. + +The shipped `lal/default.yaml` declares: + +- `default` (`layer: GENERAL`), saving the logs that arrive with the `GENERAL` layer; +- `default-fallback` (`layer: auto`, `fallback: true`), saving the logs without a layer under `GENERAL`. Edit + its extractor to file them under another layer, for example by `sourceAttribute(...)` or `tag(...)`; +- `otlp-general` (`layer: OTLP_GENERAL`), saving the logs of the OpenTelemetry services that set + `service.layer=OTLP_GENERAL`, the layer their spans are analyzed under. + +A rule written for the logs without a layer declares `layer: auto`, with `fallback: true` when it is to take +only what the other auto rules leave, and sets the layer in its extractor, e.g. `extractor { layer "GENERAL" }`. +A `layer: GENERAL` rule sees only the logs that arrive with the `GENERAL` layer. ### Inline layer declarations (`layerDefinitions:`) @@ -75,6 +97,8 @@ the built-in static fields in `Layer.java` for distribution layers. See [`Layer.java`](../../../oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/Layer.java) javadoc for the full picture. +### Auto layer + When `layer: auto` is declared, the rule matches logs where `service.layer` is absent (common for OTLP sources that don't set this attribute). The script is expected to set the layer in the extractor: @@ -98,15 +122,37 @@ rules: In `layer: auto` mode, `abort` and `drop` have distinct meanings: -- **`abort {}`** — "not mine, let others try." If all auto rules abort, the log falls back to - `GENERAL` layer processing automatically. -- **`drop {}`** — "mine, but sampled out." The rule claimed the log (no GENERAL fallback) but +- **`abort {}`** — "not mine, let others try." If all auto rules abort, the log runs the fallback auto rules. +- **`drop {}`** — "mine, but sampled out." The rule claimed the log (the fallback auto rules do not run) but chose not to persist it (e.g., sampling). Use this for rate limiting or conditional dropping within a recognized log type. If an auto rule claims the log (doesn't abort) but doesn't set a layer, the log is warned and dropped at persistence time. +All the auto rules run on such a log, in no defined order, and each one that claims it saves a copy of +its own, so an auto rule should claim only the logs it recognizes and `abort {}` on the others. + +A rule declaring `fallback: true` (allowed only with `layer: auto`) runs after the other auto rules, only when +none of them claimed the log. It is the catch-all for the logs without a layer: `default-fallback` in +`lal/default.yaml` files them under `GENERAL`. A fallback rule follows the same rules as any auto rule: it +must set the layer, and `abort {}` leaves the log unclaimed, which drops it. Auto and fallback rule names +share one name space. + +```yaml +rules: + - name: default-fallback + layer: auto + fallback: true + dsl: | + filter { + extractor { + layer "GENERAL" + } + sink {} + } +``` + ## Filter A filter is a group of [parser](#parser), [extractor](#extractor) and [sink](#sink). Users can use one or more filters diff --git a/docs/en/setup/backend/admin-api/dsl-debugging-lal.md b/docs/en/setup/backend/admin-api/dsl-debugging-lal.md index 86534c9ec8b8..afccbeccf3d6 100644 --- a/docs/en/setup/backend/admin-api/dsl-debugging-lal.md +++ b/docs/en/setup/backend/admin-api/dsl-debugging-lal.md @@ -210,10 +210,13 @@ samples are the entry-point `input` and the terminal `output`. # /tmp/lal-multi.yaml rules: - name: app-extractor - layer: GENERAL + # Agent logs carry no layer, so a rule for them is a fallback auto rule that sets one. + layer: auto + fallback: true dsl: | filter { extractor { + layer "GENERAL" tag stage: 'extractor' tag emitter: 'demo' tag rule: 'multi-statement' diff --git a/docs/en/setup/backend/backend-alipay-mini-program-monitoring.md b/docs/en/setup/backend/backend-alipay-mini-program-monitoring.md index 47d947abd3b2..8bf27d454325 100644 --- a/docs/en/setup/backend/backend-alipay-mini-program-monitoring.md +++ b/docs/en/setup/backend/backend-alipay-mini-program-monitoring.md @@ -109,11 +109,12 @@ metrics on the dashboard come from the `miniprogram.request.duration` histogram' ## Error Logs -The LAL rule `lal/miniprogram.yaml` uses `layer: auto` mode and dispatches on the -`miniprogram.platform` resource attribute — one rule file produces both the -`WECHAT_MINI_PROGRAM` and `ALIPAY_MINI_PROGRAM` layers. For each error log, the extractor -emits a `miniprogram_error_count` counter sample, which the log-MAL rule in -`log-mal-rules/miniprogram.yaml` aggregates into the per-layer `error_count` metric. +The LAL rules in `lal/miniprogram.yaml` handle the error logs. The SDK sets the `service.layer` +resource attribute, so its logs reach the `miniprogram-errors-wechat` and `miniprogram-errors-alipay` +rules, declared for the `WECHAT_MINI_PROGRAM` and `ALIPAY_MINI_PROGRAM` layers. For each error log, +the extractor also emits a `miniprogram_error_count` counter sample, which the log-MAL rules in +`log-mal-rules/miniprogram-wechat.yaml` and `log-mal-rules/miniprogram-alipay.yaml` aggregate into the +per-layer `error_count` metric. Error categories (`exception.type` tag): @@ -147,6 +148,10 @@ value. then handle absence differently: native segments produce a literal `-` instance entity; OTLP logs and metrics create no instance entity at all. Per-instance dashboards are meaningful only when the operator sets `serviceInstance`. +- **SDK ≥ v0.5.0 is required.** It sets the `service.layer` resource attribute, which routes its + error logs to the LAL rules of the mini-program layers. An older SDK sets none, so its error logs + are filed under `GENERAL` and produce no `error_count` metric. Conversely, an OAP older than this + version has no LAL rule for these layers and drops the error logs of SDK ≥ v0.5.0. - The recommended pattern (SDK docs + e2e CI) is to set `serviceInstance` to a version-scoped value. diff --git a/docs/en/setup/backend/backend-wechat-mini-program-monitoring.md b/docs/en/setup/backend/backend-wechat-mini-program-monitoring.md index 4eaa4286d1f8..20cad70bbda0 100644 --- a/docs/en/setup/backend/backend-wechat-mini-program-monitoring.md +++ b/docs/en/setup/backend/backend-wechat-mini-program-monitoring.md @@ -109,11 +109,12 @@ metrics on the dashboard come from the `miniprogram.request.duration` histogram' ## Error Logs -The LAL rule `lal/miniprogram.yaml` uses `layer: auto` mode. It dispatches on the -`miniprogram.platform` resource attribute — one rule file produces both the `WECHAT_MINI_PROGRAM` -and `ALIPAY_MINI_PROGRAM` layers. For each error log, the extractor also emits a -`miniprogram_error_count` counter sample, which the log-MAL rule in `log-mal-rules/miniprogram.yaml` -aggregates into the per-layer `error_count` metric. +The LAL rules in `lal/miniprogram.yaml` handle the error logs. The SDK sets the `service.layer` +resource attribute, so its logs reach the `miniprogram-errors-wechat` and `miniprogram-errors-alipay` +rules, declared for the `WECHAT_MINI_PROGRAM` and `ALIPAY_MINI_PROGRAM` layers. For each error log, +the extractor also emits a `miniprogram_error_count` counter sample, which the log-MAL rules in +`log-mal-rules/miniprogram-wechat.yaml` and `log-mal-rules/miniprogram-alipay.yaml` aggregate into the +per-layer `error_count` metric. Error categories (`exception.type` tag): @@ -140,8 +141,8 @@ no longer auto-generates a device id; operators set `serviceInstance` to a versi - **SDK ≤ v0.2.x** emits `componentId = 10001` (ajax-inherited). Its segments resolve to `Layer.GENERAL` and do not benefit from this integration's layer / topology work. OTLP - metrics + logs still flow through MAL / LAL because they key on the `miniprogram.platform` - resource attribute, which v0.2 already emits. + metrics still flow through MAL because it keys on the `miniprogram.platform` resource + attribute, which v0.2 already emits. - **SDK ≤ v0.3.x** auto-generates `service.instance.id = mp-{random}` per session, creating one OAP instance entity per device — usually undesirable. Operators on v0.3.x can avoid this by passing `init({ serviceInstance: serviceVersion })` explicitly. @@ -149,6 +150,10 @@ no longer auto-generates a device id; operators set `serviceInstance` to a versi then handle absence differently: native segments produce a literal `-` instance entity; OTLP logs and metrics create no instance entity at all. Per-instance dashboards are meaningful only when the operator sets `serviceInstance`. +- **SDK ≥ v0.5.0 is required.** It sets the `service.layer` resource attribute, which routes its + error logs to the LAL rules of the mini-program layers. An older SDK sets none, so its error logs + are filed under `GENERAL` and produce no `error_count` metric. Conversely, an OAP older than this + version has no LAL rule for these layers and drops the error logs of SDK ≥ v0.5.0. - The recommended pattern (SDK docs + e2e CI) is to set `serviceInstance` to a version-scoped value (mirroring `service.version` or a release tag). Then all three signal pipelines aggregate under the same OAP instance entity. diff --git a/oap-server/analyzer/log-analyzer/CLAUDE.md b/oap-server/analyzer/log-analyzer/CLAUDE.md index 534fae6148fa..cac8e06e2d1a 100644 --- a/oap-server/analyzer/log-analyzer/CLAUDE.md +++ b/oap-server/analyzer/log-analyzer/CLAUDE.md @@ -192,12 +192,27 @@ The generator detects the parser type from the AST at compile time and generates ### `layer: auto` Mode -Rules with `layer: auto` match logs where `service.layer` is absent (empty). Stored in -`LogFilterListener.Factory.autoDsls` (separate from the layer-keyed `dsls` map). At runtime, -`LogAnalyzer.doAnalysis()` routes empty-layer logs to auto rules via `createAnalysisListeners(null)`. - -The `autoLayerMode` flag on `ExecutionContext` triggers a layer check in `FilterSpec.doSink()`: -if the extractor didn't set a layer, the log is warned and dropped. +Rules with `layer: auto` match logs where `service.layer` is absent (empty). `LogAnalyzer.doAnalysis()` +routes an empty-layer log in up to two passes, stopping at the first pass in which a rule claims it +(`LogAnalysisListener.claimed()`: did not abort): + +1. The regular auto rules — `LogFilterListener.Factory.autoDsls`, via `create(null)`. +2. The `fallback: true` auto rules — `Factory.fallbackDsls`, via `createFallback()`. Before this pass the + metadata layer is reset to the value received, since the rules share one `LogMetadata` and an aborted + auto rule may have set a layer on it. +3. Nothing claimed it: the log is dropped (debug log). There is no fallback to the `GENERAL` rules; + `lal/default.yaml` ships `default-fallback`, which sets `GENERAL`. + +Auto and fallback rules share one name space and rule key (`ruleKey(null, name)` → `|name`), so +`find` / `contains` / `remove` / suspend / runtime-rule keys treat them alike; `addOrReplace` moves a rule +between the two maps when its `fallback` flag changes. `fallback: true` on a non-auto rule fails compile. + +The `autoLayerMode` flag on `ExecutionContext` (set for both passes) triggers a layer check in +`FilterSpec.doSink()`: if the extractor didn't set a layer, the log is warned and dropped. Because of it, +`TrafficSinkListener` never sees an empty layer and has no `GENERAL` fallback. + +A static rule whose `layer` is neither `auto` nor a registered layer name (case-sensitive) is skipped with +a warning at boot (`Factory.loadStaticRules`); a runtime rule declaring one is rejected (`resolveLayer`). ### inputType and LALSourceTypeProvider SPI diff --git a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/LALConfig.java b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/LALConfig.java index b7d96dcf46cf..6d32c59cd76a 100644 --- a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/LALConfig.java +++ b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/LALConfig.java @@ -37,6 +37,12 @@ public class LALConfig { private String layer; + /** + * Only with {@code layer: auto}: the rule runs on a log without a layer only when no other auto rule + * claimed it. A log no fallback rule claims either is dropped. + */ + private boolean fallback; + /** * Fully qualified class name of the input type (the extra log proto/POJO) * for compile-time {@code parsed.*} getter resolution. diff --git a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzer.java b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzer.java index 51eb9a494f64..208143ab1dda 100644 --- a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzer.java +++ b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzer.java @@ -20,8 +20,10 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.function.Function; import lombok.extern.slf4j.Slf4j; import org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener.LogAnalysisListener; +import org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener.LogAnalysisListenerFactory; import org.apache.skywalking.oap.server.core.analysis.Layer; import org.apache.skywalking.oap.server.core.source.LogMetadata; import org.apache.skywalking.oap.server.library.util.StringUtil; @@ -32,9 +34,9 @@ *

Runtime execution ({@link #doAnalysis}): *

    *
  1. Validates the incoming log (service name must be non-empty, layer must be valid).
  2. - *
  3. Calls {@code createAnalysisListeners(layer)} — asks all registered - * {@link org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener.LogAnalysisListenerFactory} - * instances to create listeners for the log's layer. For LAL, this is + *
  4. Asks all registered {@link LogAnalysisListenerFactory} instances to create listeners for the log's + * layer; a log without a layer gets the auto rules', then, when none claimed it, the fallback auto + * rules', and is dropped when those don't claim it either. For LAL, the factory is * {@link org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener.LogFilterListener.Factory}, * which returns a listener wrapping all compiled {@link org.apache.skywalking.oap.log.analyzer.v2.dsl.DSL} * instances for that layer.
  5. @@ -61,39 +63,45 @@ public void doAnalysis(LogMetadata metadata, final Object input) { log.debug("The log is ignored because the Service name is empty"); return; } + if (metadata.getTimestamp() == 0) { + metadata.setTimestamp(System.currentTimeMillis()); + } if (metadata.getLayer() == null || metadata.getLayer().isEmpty()) { - // Empty layer: try auto-layer rules first. - createAnalysisListeners(null); - if (!listeners.isEmpty()) { - if (metadata.getTimestamp() == 0) { - metadata.setTimestamp(System.currentTimeMillis()); - } - notifyAnalysisListener(metadata, input); - notifyAnalysisListenerToBuild(); - - // If any auto rule claimed the log (didn't abort), we're done. - if (listeners.stream().anyMatch(LogAnalysisListener::claimed)) { - return; - } - // All auto rules aborted — fall back to GENERAL - listeners.clear(); + // A log without a layer: the auto rules, then the fallback auto rules, until one claims it. + final String arrivedLayer = metadata.getLayer(); + if (analyzeUntilClaimed(metadata, input, factory -> factory.create(null))) { + return; } - // No auto rules configured, or all aborted — fall back to GENERAL - createAnalysisListeners(Layer.GENERAL); - } else { - final Layer layer = Layer.nameOf(metadata.getLayer()); - if (layer == Layer.UNDEFINED) { - log.warn("The Layer {} is not recognized, abandon the log.", metadata.getLayer()); + // The rules share this metadata, and an auto rule may have set a layer before it aborted + // (LALBlockCodegen writes `layer` into it); the fallback rules start from the layer as received. + metadata.setLayer(arrivedLayer); + if (analyzeUntilClaimed(metadata, input, LogAnalysisListenerFactory::createFallback)) { return; } - createAnalysisListeners(layer); + log.debug("No LAL rule claimed the log without a layer of service {}, dropping it", metadata.getService()); + return; } - if (metadata.getTimestamp() == 0) { - metadata.setTimestamp(System.currentTimeMillis()); + final Layer layer = Layer.nameOf(metadata.getLayer()); + if (layer == Layer.UNDEFINED) { + log.warn("The Layer {} is not recognized, abandon the log.", metadata.getLayer()); + return; } + createAnalysisListeners(factory -> factory.create(layer)); + notifyAnalysisListener(metadata, input); + notifyAnalysisListenerToBuild(); + } + private boolean analyzeUntilClaimed(final LogMetadata metadata, + final Object input, + final Function create) { + listeners.clear(); + createAnalysisListeners(create); + if (listeners.isEmpty()) { + return false; + } notifyAnalysisListener(metadata, input); notifyAnalysisListenerToBuild(); + return listeners.stream().anyMatch(LogAnalysisListener::claimed); } private void notifyAnalysisListener(final LogMetadata metadata, final Object input) { @@ -104,10 +112,10 @@ private void notifyAnalysisListenerToBuild() { listeners.forEach(LogAnalysisListener::build); } - private void createAnalysisListeners(Layer layer) { + private void createAnalysisListeners(final Function create) { factoryManager.getLogAnalysisListenerFactories() .stream() - .map(factory -> factory.create(layer)) + .map(create) .filter(Objects::nonNull) .forEach(listeners::add); } diff --git a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogAnalysisListenerFactory.java b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogAnalysisListenerFactory.java index d2d9f03964a7..f32648789eac 100644 --- a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogAnalysisListenerFactory.java +++ b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogAnalysisListenerFactory.java @@ -26,4 +26,12 @@ public interface LogAnalysisListenerFactory { LogAnalysisListener create(Layer layer); + + /** + * @return the listener of the {@code fallback} auto rules, run on a log without a layer that no other + * auto rule claimed; null when there are none. + */ + default LogAnalysisListener createFallback() { + return null; + } } diff --git a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListener.java b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListener.java index 2e36b767716b..fdd1e783c219 100644 --- a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListener.java +++ b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListener.java @@ -48,6 +48,7 @@ import org.apache.skywalking.oap.server.library.module.ModuleManager; import org.apache.skywalking.oap.server.library.module.ModuleStartException; import org.apache.skywalking.oap.server.library.module.Service; +import org.apache.skywalking.oap.server.library.util.StringUtil; import javassist.ClassPool; /** @@ -173,6 +174,11 @@ public static class Factory implements LogAnalysisListenerFactory, Service { */ private volatile Map> dsls; private volatile Map autoDsls; + /** + * The {@code fallback: true} auto rules. They share the auto rules' name space and rule keys + * ({@link #ruleKey} with a null layer), so a name is in at most one of the two maps. + */ + private volatile Map fallbackDsls; /** * Suspended rule keys — encoded as {@code layer.name() + "|" + ruleName} for layer-keyed @@ -225,6 +231,7 @@ public Factory(final ModuleManager moduleManager, final LogAnalyzerModuleConfig // Empty registry — populated by {@link #loadStaticRules}. this.dsls = new HashMap<>(); this.autoDsls = new HashMap<>(); + this.fallbackDsls = new HashMap<>(); } /** @@ -241,17 +248,26 @@ public Factory(final ModuleManager moduleManager, final LogAnalyzerModuleConfig public void loadStaticRules() throws Exception { final Map> initDsls = new HashMap<>(); final Map initAutoDsls = new HashMap<>(); + final Map initFallbackDsls = new HashMap<>(); final List configList = LALConfigs.load(analyzerConfig.getLalPath(), analyzerConfig.lalFiles()) .stream() .flatMap(it -> it.getRules().stream()) .collect(Collectors.toList()); for (final LALConfig c : configList) { + if (!LALConfig.LAYER_AUTO.equalsIgnoreCase(c.getLayer()) && !isRegisteredLayer(c.getLayer())) { + // Such a rule never ran (no log is routed to UNDEFINED); skip it rather than fail the boot. + // A runtime rule declaring it is rejected instead, by compile's resolveLayer. + log.warn("LAL rule {} in {} declares an unregistered layer: {}; the rule is skipped", + c.getName(), c.getSourcePath(), c.getLayer()); + continue; + } final CompiledLAL compiled = compile(c); if (compiled.layer == null) { - if (initAutoDsls.put(c.getName(), compiled.dsl) != null) { + if (initAutoDsls.containsKey(c.getName()) || initFallbackDsls.containsKey(c.getName())) { throw new ModuleStartException( "Auto-layer rules have duplicate name: " + c.getName()); } + (compiled.fallback ? initFallbackDsls : initAutoDsls).put(c.getName(), compiled.dsl); } else { final Map layerDsls = initDsls.computeIfAbsent(compiled.layer, k -> new HashMap<>()); if (layerDsls.put(c.getName(), compiled.dsl) != null) { @@ -267,6 +283,7 @@ public void loadStaticRules() throws Exception { // Publish: readers from now on see the startup-complete registry. this.dsls = initDsls; this.autoDsls = initAutoDsls; + this.fallbackDsls = initFallbackDsls; } /** @@ -288,7 +305,12 @@ public CompiledLAL compile(final LALConfig c, final ClassPool pool, final ClassLoader targetClassLoader) throws ModuleStartException { final boolean isAuto = LALConfig.LAYER_AUTO.equalsIgnoreCase(c.getLayer()); - final Layer layer = isAuto ? null : Layer.nameOf(c.getLayer()); + final Layer layer = isAuto ? null : resolveLayer(c); + if (c.isFallback() && !isAuto) { + throw new ModuleStartException( + "LAL rule " + c.getName() + " in " + c.getSourcePath() + + " declares fallback: true, which only a layer: auto rule may"); + } final LALSourceTypeProvider spiProvider = isAuto ? null : spiProviders.get(layer); final Class resolvedInputType = resolveInputType(c, spiProvider); final Class resolvedOutputType = resolveOutputType(c, spiProvider); @@ -300,7 +322,24 @@ public CompiledLAL compile(final LALConfig c, // operator can open. sourceName is the debug registry's key and must not vary. DslSourceRef.ofRule(c.getSourcePath(), c.getLineNo()), pool, targetClassLoader); - return new CompiledLAL(layer, c.getName(), dsl); + return new CompiledLAL(layer, c.getName(), dsl, c.isFallback()); + } + + /** + * {@link Layer#nameOf} answers UNDEFINED for an unregistered name, which no log is routed to, so a + * misspelled or missing layer would leave its rule silently never running. + */ + static Layer resolveLayer(final LALConfig c) throws ModuleStartException { + if (!isRegisteredLayer(c.getLayer())) { + throw new ModuleStartException( + "LAL rule " + c.getName() + " in " + c.getSourcePath() + + " declares an unregistered layer: " + c.getLayer()); + } + return Layer.nameOf(c.getLayer()); + } + + static boolean isRegisteredLayer(final String name) { + return StringUtil.isNotEmpty(name) && Layer.nameOf(name) != Layer.UNDEFINED; } /** @@ -313,9 +352,14 @@ public CompiledLAL compile(final LALConfig c, public void addOrReplace(final CompiledLAL compiled) { synchronized (writeLock) { if (compiled.layer == null) { - final Map next = new HashMap<>(autoDsls); - next.put(compiled.ruleName, compiled.dsl); - autoDsls = next; + // A replace may move the rule between the auto and the fallback auto rules. + final Map nextAuto = new HashMap<>(autoDsls); + final Map nextFallback = new HashMap<>(fallbackDsls); + nextAuto.remove(compiled.ruleName); + nextFallback.remove(compiled.ruleName); + (compiled.fallback ? nextFallback : nextAuto).put(compiled.ruleName, compiled.dsl); + autoDsls = nextAuto; + fallbackDsls = nextFallback; } else { final Map> next = new HashMap<>(dsls); final Map layerMap = @@ -342,7 +386,8 @@ public DSL find(final Layer layer, final String ruleName) { return null; } if (layer == null) { - return autoDsls.get(ruleName); + final DSL auto = autoDsls.get(ruleName); + return auto != null ? auto : fallbackDsls.get(ruleName); } final Map layerMap = dsls.get(layer); return layerMap == null ? null : layerMap.get(ruleName); @@ -352,12 +397,15 @@ public DSL find(final Layer layer, final String ruleName) { public void remove(final Layer layer, final String ruleName) { synchronized (writeLock) { if (layer == null) { - if (!autoDsls.containsKey(ruleName)) { - return; + if (autoDsls.containsKey(ruleName)) { + final Map next = new HashMap<>(autoDsls); + next.remove(ruleName); + autoDsls = next; + } else if (fallbackDsls.containsKey(ruleName)) { + final Map next = new HashMap<>(fallbackDsls); + next.remove(ruleName); + fallbackDsls = next; } - final Map next = new HashMap<>(autoDsls); - next.remove(ruleName); - autoDsls = next; } else { final Map layerMap = dsls.get(layer); if (layerMap == null || !layerMap.containsKey(ruleName)) { @@ -380,7 +428,7 @@ public void remove(final Layer layer, final String ruleName) { * detect cross-file collisions before registering. */ public boolean contains(final Layer layer, final String ruleName) { if (layer == null) { - return autoDsls.containsKey(ruleName); + return autoDsls.containsKey(ruleName) || fallbackDsls.containsKey(ruleName); } final Map layerMap = dsls.get(layer); return layerMap != null && layerMap.containsKey(ruleName); @@ -440,11 +488,18 @@ public static final class CompiledLAL { public final Layer layer; public final String ruleName; public final DSL dsl; + /** A {@code fallback: true} auto rule; always false with a non-null layer. */ + public final boolean fallback; public CompiledLAL(final Layer layer, final String ruleName, final DSL dsl) { + this(layer, ruleName, dsl, false); + } + + public CompiledLAL(final Layer layer, final String ruleName, final DSL dsl, final boolean fallback) { this.layer = layer; this.ruleName = ruleName; this.dsl = dsl; + this.fallback = fallback; } } @@ -564,18 +619,7 @@ public LogAnalysisListener create(Layer layer) { // mid-iteration. The reference is volatile + copy-on-write so this is lock-free. final Set susp = suspendedKeys; if (layer == null) { - // null layer → route to auto-layer rules - if (autoDsls.isEmpty()) { - return null; - } - final Collection eligible = susp.isEmpty() - ? autoDsls.values() - : filterSuspended(autoDsls, susp, null); - if (eligible.isEmpty()) { - return null; - } - // auto-mode crosses layers — no single SPI provider applies - return new LogFilterListener(eligible, true, null); + return createAuto(autoDsls); } final Map dsl = dsls.get(layer); if (dsl == null) { @@ -590,6 +634,24 @@ public LogAnalysisListener create(Layer layer) { return new LogFilterListener(eligible, false, spiProviders.get(layer)); } + @Override + public LogAnalysisListener createFallback() { + return createAuto(fallbackDsls); + } + + private LogAnalysisListener createAuto(final Map rules) { + if (rules.isEmpty()) { + return null; + } + final Set susp = suspendedKeys; + final Collection eligible = susp.isEmpty() ? rules.values() : filterSuspended(rules, susp, null); + if (eligible.isEmpty()) { + return null; + } + // auto-mode crosses layers — no single SPI provider applies + return new LogFilterListener(eligible, true, null); + } + private static Collection filterSuspended(final Map source, final Set suspended, final Layer layer) { diff --git a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/TrafficSinkListener.java b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/TrafficSinkListener.java index 9679a3354fd5..2bb08473a0aa 100644 --- a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/TrafficSinkListener.java +++ b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/TrafficSinkListener.java @@ -65,12 +65,9 @@ public void build() { public LogSinkListener parse(final LogMetadata metadata, final Object input, final ExecutionContext ctx) { - Layer layer; - if (StringUtil.isNotEmpty(metadata.getLayer())) { - layer = Layer.valueOf(metadata.getLayer()); - } else { - layer = Layer.GENERAL; - } + // Never empty here: a log without a layer is saved only by an auto rule, which must set one + // (FilterSpec#doSink drops the log otherwise). + final Layer layer = Layer.valueOf(metadata.getLayer()); final long timeBucket = TimeBucket.getTimeBucket(System.currentTimeMillis(), DownSampling.Minute); // to service traffic String serviceName = namingControl.formatServiceName(metadata.getService()); diff --git a/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALExpressionExecutionTest.java b/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALExpressionExecutionTest.java index 10432b4bfffe..a554481894ec 100644 --- a/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALExpressionExecutionTest.java +++ b/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/compiler/LALExpressionExecutionTest.java @@ -117,9 +117,8 @@ Collection lalExecutionTests() throws Exception { final LALClassGenerator generator = new LALClassGenerator(); final Path testLalDir = DslRuleLoader.findScriptsDir( - "test/script-cases/scripts/lal/test-lal", - "../../../test/script-cases/scripts/lal/test-lal", - "../../scripts/lal/test-lal"); + "src/test/resources/scripts/lal/test-lal", + "oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal"); if (testLalDir == null) { return tests; } @@ -181,6 +180,18 @@ private void executeAndAssert( final LogMetadata metadata = sourceAttrs != null ? LogMetadataUtils.fromLogData(logData, sourceAttrs) : LogMetadataUtils.fromLogData(logData); + final Object otlpSpanId = input.get("otlp-span-id"); + if (otlpSpanId != null) { + // No native protocol carries an OTLP span id; the OTLP receiver puts it on the metadata. + final LogMetadata.TraceContext tc = metadata.getTraceContext(); + metadata.setTraceContext( + LogMetadata.TraceContext.builder() + .traceId(tc.getTraceId()) + .traceSegmentId(tc.getTraceSegmentId()) + .spanId(tc.getSpanId()) + .otlpSpanId(String.valueOf(otlpSpanId)) + .build()); + } final Message extraLog = LalLogDataBuilder.buildExtraLog(input); final Object lalInput = extraLog != null ? extraLog : logData; @@ -341,9 +352,12 @@ private void assertLalTag(final String ruleName, final Object output, f.setAccessible(true); @SuppressWarnings("unchecked") final List tags = (List) f.get(output); - assertTrue(tags.stream().anyMatch(t -> key.equals(t[0]) && expected.equals(t[1])), - ruleName + ": expected tag " + key + "=" + expected + ", got: " - + tags.stream().map(t -> t[0] + "=" + t[1]).collect(Collectors.joining(", "))); + // An empty tag value is not added, so an absent tag reads as "". + final String actual = tags.stream() + .filter(t -> key.equals(t[0])) + .map(t -> t[1]) + .findFirst().orElse(""); + assertEquals(expected, actual, ruleName + ": expect.tag." + key + " mismatch"); } private Map> outputBuilderNames; diff --git a/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzerRoutingTest.java b/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzerRoutingTest.java new file mode 100644 index 000000000000..421659dbcdb7 --- /dev/null +++ b/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzerRoutingTest.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.log.analyzer.v2.provider.log; + +import java.util.Collections; +import org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener.LogAnalysisListener; +import org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener.LogAnalysisListenerFactory; +import org.apache.skywalking.oap.server.core.analysis.Layer; +import org.apache.skywalking.oap.server.core.source.LogMetadata; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * A log without a layer runs the auto rules, then, when none claimed it, the fallback auto rules, and is + * dropped when those don't claim it either; nothing routes it to the GENERAL rules. + */ +class LogAnalyzerRoutingTest { + + private LogAnalysisListenerFactory factory; + private LogAnalysisListener auto; + private LogAnalysisListener fallback; + private LogAnalysisListener general; + private LogAnalyzer analyzer; + + @BeforeEach + void setUp() { + factory = mock(LogAnalysisListenerFactory.class); + auto = mock(LogAnalysisListener.class); + fallback = mock(LogAnalysisListener.class); + general = mock(LogAnalysisListener.class); + when(factory.create(null)).thenReturn(auto); + when(factory.createFallback()).thenReturn(fallback); + when(factory.create(Layer.GENERAL)).thenReturn(general); + final ILogAnalysisListenerManager manager = mock(ILogAnalysisListenerManager.class); + when(manager.getLogAnalysisListenerFactories()).thenReturn(Collections.singletonList(factory)); + analyzer = new LogAnalyzer(manager); + } + + private static LogMetadata log(final String layer) { + return LogMetadata.builder().service("svc").layer(layer).build(); + } + + @Test + void autoRuleClaimingTheLogSkipsTheFallbackRules() { + when(auto.claimed()).thenReturn(true); + + analyzer.doAnalysis(log(""), null); + + verify(auto).build(); + verify(factory, never()).createFallback(); + verify(general, never()).build(); + } + + @Test + void fallbackRulesRunWhenNoAutoRuleClaimedTheLog() { + when(fallback.claimed()).thenReturn(true); + + analyzer.doAnalysis(log(""), null); + + verify(auto).build(); + verify(fallback).build(); + verify(general, never()).build(); + } + + @Test + void fallbackRulesDoNotSeeALayerAnAbortedAutoRuleSet() { + // An auto rule sets a layer on the shared metadata, then aborts (claimed() stays false). + doAnswer(inv -> { + ((LogMetadata) inv.getArgument(0)).setLayer("IOS"); + return auto; + }).when(auto).parse(any(), any()); + + analyzer.doAnalysis(log(""), null); + + final ArgumentCaptor seen = ArgumentCaptor.forClass(LogMetadata.class); + verify(fallback).parse(seen.capture(), any()); + assertEquals("", seen.getValue().getLayer()); + } + + @Test + void logNoRuleClaimedIsNotRoutedToGeneral() { + analyzer.doAnalysis(log(""), null); + + verify(fallback).build(); + verify(factory, never()).create(Layer.GENERAL); + } + + @Test + void logWithLayerRunsOnlyThatLayersRules() { + analyzer.doAnalysis(log("GENERAL"), null); + + verify(general).parse(any(), any()); + verify(general).build(); + verify(factory, never()).create(null); + verify(factory, never()).createFallback(); + } +} diff --git a/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListenerLayerTest.java b/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListenerLayerTest.java new file mode 100644 index 000000000000..a4143ead4c1d --- /dev/null +++ b/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListenerLayerTest.java @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener; + +import org.apache.skywalking.oap.log.analyzer.v2.dsl.DSL; +import org.apache.skywalking.oap.log.analyzer.v2.provider.LALConfig; +import org.apache.skywalking.oap.log.analyzer.v2.provider.LogAnalyzerModuleConfig; +import org.apache.skywalking.oap.server.core.analysis.Layer; +import org.apache.skywalking.oap.server.core.source.LogMetadata; +import org.apache.skywalking.oap.server.library.module.ModuleManager; +import org.apache.skywalking.oap.server.library.module.ModuleStartException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +/** + * A rule's layer must be a registered one: an unregistered or missing layer would leave the rule + * silently never running, so a static rule declaring one is skipped and a runtime rule rejected. {@code fallback: true} is an auto rule's only, and puts it in a group of its own. + */ +class LogFilterListenerLayerTest { + + private static LALConfig rule(final String layer) { + final LALConfig c = new LALConfig(); + c.setName("r"); + c.setSourcePath("lal/test.yaml"); + c.setLayer(layer); + return c; + } + + @Test + void registeredLayerResolves() throws ModuleStartException { + assertEquals(Layer.GENERAL, LogFilterListener.Factory.resolveLayer(rule("GENERAL"))); + } + + @Test + void unregisteredLayerIsRejected() { + final ModuleStartException e = assertThrows( + ModuleStartException.class, () -> LogFilterListener.Factory.resolveLayer(rule("GENERL"))); + assertTrue(e.getMessage().contains("GENERL"), e.getMessage()); + } + + @Test + void undefinedLayerIsRejected() { + assertThrows(ModuleStartException.class, () -> LogFilterListener.Factory.resolveLayer(rule("UNDEFINED"))); + } + + @Test + void missingLayerIsRejected() { + assertThrows(ModuleStartException.class, () -> LogFilterListener.Factory.resolveLayer(rule(null))); + assertThrows(ModuleStartException.class, () -> LogFilterListener.Factory.resolveLayer(rule(""))); + } + + @Test + void registeredLayerNamesAreCaseSensitive() { + assertTrue(LogFilterListener.Factory.isRegisteredLayer("GENERAL")); + assertFalse(LogFilterListener.Factory.isRegisteredLayer("General")); + assertFalse(LogFilterListener.Factory.isRegisteredLayer("UNDEFINED")); + assertFalse(LogFilterListener.Factory.isRegisteredLayer(null)); + } + + @Test + void fallbackIsRejectedOnALayeredRule() throws Exception { + final LALConfig c = rule("GENERAL"); + c.setFallback(true); + final ModuleStartException e = assertThrows(ModuleStartException.class, () -> factory().compile(c)); + assertTrue(e.getMessage().contains("fallback"), e.getMessage()); + } + + @Test + void autoAndFallbackRulesAreSeparateGroupsOfOneNameSpace() throws Exception { + final LogFilterListener.Factory factory = factory(); + final DSL autoRule = mock(DSL.class); + final DSL fallbackRule = mock(DSL.class); + factory.addOrReplace(new LogFilterListener.Factory.CompiledLAL(null, "a", autoRule, false)); + factory.addOrReplace(new LogFilterListener.Factory.CompiledLAL(null, "f", fallbackRule, true)); + + evaluate(factory.create(null)); + verify(autoRule).evaluate(any()); + verify(fallbackRule, never()).evaluate(any()); + + evaluate(factory.createFallback()); + verify(fallbackRule).evaluate(any()); + + assertSame(fallbackRule, factory.find(null, "f")); + assertTrue(factory.contains(null, "f")); + + // A replace that turns "a" into a fallback rule moves it between the groups. + factory.addOrReplace(new LogFilterListener.Factory.CompiledLAL(null, "a", autoRule, true)); + assertNull(factory.create(null)); + + factory.remove(null, "a"); + factory.remove(null, "f"); + assertNull(factory.createFallback()); + } + + private static LogFilterListener.Factory factory() throws Exception { + return new LogFilterListener.Factory(mock(ModuleManager.class), mock(LogAnalyzerModuleConfig.class)); + } + + private static void evaluate(final LogAnalysisListener listener) { + listener.parse(LogMetadata.builder().service("svc").layer("").build(), null); + listener.build(); + } +} diff --git a/oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/default.data.yaml b/oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/default.data.yaml index 6823207fb044..96d3599a8d49 100644 --- a/oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/default.data.yaml +++ b/oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/default.data.yaml @@ -21,3 +21,20 @@ default: expect: save: true abort: false + +default-fallback: + service: test-svc + body-type: text + body: 'a log without a layer' + expect: + save: true + abort: false + layer: GENERAL + +otlp-general: + service: test-svc + body-type: text + body: 'a log of an OTLP_GENERAL service' + expect: + save: true + abort: false diff --git a/oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/default.yaml b/oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/default.yaml index 12317a95bf55..5f9e51823c77 100644 --- a/oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/default.yaml +++ b/oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/default.yaml @@ -22,3 +22,24 @@ rules: sink { } } + # The logs of a service whose OTLP resource sets `service.layer=OTLP_GENERAL`, as its spans do. + - name: otlp-general + layer: OTLP_GENERAL + dsl: | + filter { + sink { + } + } + # The logs that arrive without a layer and no other `layer: auto` rule claimed. Its extractor decides the + # layer the log and its service are filed under; a log no rule claims is dropped. + - name: default-fallback + layer: auto + fallback: true + dsl: | + filter { + extractor { + layer "GENERAL" + } + sink { + } + } diff --git a/oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/miniprogram.data.yaml b/oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/miniprogram.data.yaml new file mode 100644 index 000000000000..f1ba387ad27c --- /dev/null +++ b/oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/miniprogram.data.yaml @@ -0,0 +1,69 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Mock input data for miniprogram.yaml rules. + +miniprogram-errors-wechat: + # An error log, routed here by service.layer=WECHAT_MINI_PROGRAM. + - service: my-mini-program + body-type: text + body: 'request:fail timeout' + source-attributes: + miniprogram.platform: "wechat" + service.layer: "WECHAT_MINI_PROGRAM" + service.instance.id: "v1.2.0" + tags: + exception.type: "ajax" + miniprogram.page.path: "pages/cart/cart" + http.request.method: "GET" + http.response.status_code: "500" + server.address: "api.example.com" + expect: + save: true + abort: false + instance: "v1.2.0" + endpoint: "pages/cart/cart" + tag.platform: "wechat" + tag.exception.type: "ajax" + tag.http.method: "GET" + tag.http.status: "500" + tag.server.address: "api.example.com" + + # A log that is not an error is not saved. + - service: my-mini-program + body-type: text + body: 'page shown' + source-attributes: + miniprogram.platform: "wechat" + service.layer: "WECHAT_MINI_PROGRAM" + expect: + abort: true + +miniprogram-errors-alipay: + - service: my-mini-program + body-type: text + body: 'TypeError: x is undefined' + source-attributes: + miniprogram.platform: "alipay" + service.layer: "ALIPAY_MINI_PROGRAM" + tags: + exception.type: "js" + miniprogram.page.path: "pages/index/index" + expect: + save: true + abort: false + endpoint: "pages/index/index" + tag.platform: "alipay" + tag.exception.type: "js" diff --git a/oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/miniprogram.yaml b/oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/miniprogram.yaml new file mode 100644 index 000000000000..6a572c684fff --- /dev/null +++ b/oap-server/analyzer/log-analyzer/src/test/resources/scripts/lal/test-lal/oap-cases/miniprogram.yaml @@ -0,0 +1,87 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# WeChat / Alipay Mini Program error-log processing. +# The SDK (>= v0.5.0) sets the service.layer resource attribute, so its logs reach the rule of the +# WECHAT_MINI_PROGRAM or ALIPAY_MINI_PROGRAM layer. Each rule emits a miniprogram_error_count counter +# sample for every error log, picked up by log-mal-rules/miniprogram-{wechat,alipay}.yaml which +# aggregate into per-platform error_count metrics. + +rules: + - name: miniprogram-errors-wechat + layer: WECHAT_MINI_PROGRAM + dsl: | + filter { + // LAL tag() returns "" (not null) for a missing tag, so guard on + // both to short-circuit non-error logs cleanly. + if (tag("exception.type") == null || tag("exception.type") == "") { + abort {} + } + + extractor { + instance sourceAttribute("service.instance.id") + endpoint tag("miniprogram.page.path") + + tag 'platform': sourceAttribute("miniprogram.platform") + tag 'exception.type': tag("exception.type") + tag 'exception.stacktrace': tag("exception.stacktrace") + tag 'http.method': tag("http.request.method") + tag 'http.status': tag("http.response.status_code") + tag 'server.address': tag("server.address") + + metrics { + timestamp log.timestamp as Long + labels service_name: log.service, exception_type: tag("exception.type"), miniprogram_platform: sourceAttribute("miniprogram.platform") + name "miniprogram_error_count" + value 1 + } + } + + sink { + } + } + + - name: miniprogram-errors-alipay + layer: ALIPAY_MINI_PROGRAM + dsl: | + filter { + // LAL tag() returns "" (not null) for a missing tag, so guard on + // both to short-circuit non-error logs cleanly. + if (tag("exception.type") == null || tag("exception.type") == "") { + abort {} + } + + extractor { + instance sourceAttribute("service.instance.id") + endpoint tag("miniprogram.page.path") + + tag 'platform': sourceAttribute("miniprogram.platform") + tag 'exception.type': tag("exception.type") + tag 'exception.stacktrace': tag("exception.stacktrace") + tag 'http.method': tag("http.request.method") + tag 'http.status': tag("http.response.status_code") + tag 'server.address': tag("server.address") + + metrics { + timestamp log.timestamp as Long + labels service_name: log.service, exception_type: tag("exception.type"), miniprogram_platform: sourceAttribute("miniprogram.platform") + name "miniprogram_error_count" + value 1 + } + } + + sink { + } + } diff --git a/oap-server/server-starter/src/main/resources/lal/default.yaml b/oap-server/server-starter/src/main/resources/lal/default.yaml index 12317a95bf55..5f9e51823c77 100644 --- a/oap-server/server-starter/src/main/resources/lal/default.yaml +++ b/oap-server/server-starter/src/main/resources/lal/default.yaml @@ -22,3 +22,24 @@ rules: sink { } } + # The logs of a service whose OTLP resource sets `service.layer=OTLP_GENERAL`, as its spans do. + - name: otlp-general + layer: OTLP_GENERAL + dsl: | + filter { + sink { + } + } + # The logs that arrive without a layer and no other `layer: auto` rule claimed. Its extractor decides the + # layer the log and its service are filed under; a log no rule claims is dropped. + - name: default-fallback + layer: auto + fallback: true + dsl: | + filter { + extractor { + layer "GENERAL" + } + sink { + } + } diff --git a/oap-server/server-starter/src/main/resources/lal/miniprogram.yaml b/oap-server/server-starter/src/main/resources/lal/miniprogram.yaml index 7e17095d4189..6a572c684fff 100644 --- a/oap-server/server-starter/src/main/resources/lal/miniprogram.yaml +++ b/oap-server/server-starter/src/main/resources/lal/miniprogram.yaml @@ -14,21 +14,16 @@ # limitations under the License. # WeChat / Alipay Mini Program error-log processing. -# One rule covers both runtimes — layer is decided script-side from the -# miniprogram.platform resource attribute, using the SWIP-11 layer:auto -# mechanism. Also emits a miniprogram_error_count counter sample for every -# error log, picked up by log-mal-rules/miniprogram-{wechat,alipay}.yaml -# which aggregate into per-platform error_count metrics. +# The SDK (>= v0.5.0) sets the service.layer resource attribute, so its logs reach the rule of the +# WECHAT_MINI_PROGRAM or ALIPAY_MINI_PROGRAM layer. Each rule emits a miniprogram_error_count counter +# sample for every error log, picked up by log-mal-rules/miniprogram-{wechat,alipay}.yaml which +# aggregate into per-platform error_count metrics. rules: - - name: miniprogram-errors - layer: auto + - name: miniprogram-errors-wechat + layer: WECHAT_MINI_PROGRAM dsl: | filter { - if (sourceAttribute("miniprogram.platform") != "wechat" - && sourceAttribute("miniprogram.platform") != "alipay") { - abort {} - } // LAL tag() returns "" (not null) for a missing tag, so guard on // both to short-circuit non-error logs cleanly. if (tag("exception.type") == null || tag("exception.type") == "") { @@ -36,17 +31,39 @@ rules: } extractor { - if (sourceAttribute("miniprogram.platform") == "wechat") { - layer "WECHAT_MINI_PROGRAM" - } else if (sourceAttribute("miniprogram.platform") == "alipay") { - layer "ALIPAY_MINI_PROGRAM" + instance sourceAttribute("service.instance.id") + endpoint tag("miniprogram.page.path") + + tag 'platform': sourceAttribute("miniprogram.platform") + tag 'exception.type': tag("exception.type") + tag 'exception.stacktrace': tag("exception.stacktrace") + tag 'http.method': tag("http.request.method") + tag 'http.status': tag("http.response.status_code") + tag 'server.address': tag("server.address") + + metrics { + timestamp log.timestamp as Long + labels service_name: log.service, exception_type: tag("exception.type"), miniprogram_platform: sourceAttribute("miniprogram.platform") + name "miniprogram_error_count" + value 1 } + } + + sink { + } + } - // Instance source matches what OTLP metrics use, so logs aggregate under - // the same OAP instance entity when operators follow the recommended - // serviceInstance == serviceVersion pattern (SDK >= v0.4.0). If absent, - // sourceAttribute() returns null/empty -> TrafficSinkListener skips the - // instance traffic, matching MAL's empty-dim behavior. + - name: miniprogram-errors-alipay + layer: ALIPAY_MINI_PROGRAM + dsl: | + filter { + // LAL tag() returns "" (not null) for a missing tag, so guard on + // both to short-circuit non-error logs cleanly. + if (tag("exception.type") == null || tag("exception.type") == "") { + abort {} + } + + extractor { instance sourceAttribute("service.instance.id") endpoint tag("miniprogram.page.path") diff --git a/test/e2e-v2/cases/dsl-debugging/lal-statement/seed-rules/lal-multistatement.yaml b/test/e2e-v2/cases/dsl-debugging/lal-statement/seed-rules/lal-multistatement.yaml index 31f21d02e8de..f904b2858b58 100644 --- a/test/e2e-v2/cases/dsl-debugging/lal-statement/seed-rules/lal-multistatement.yaml +++ b/test/e2e-v2/cases/dsl-debugging/lal-statement/seed-rules/lal-multistatement.yaml @@ -19,10 +19,12 @@ # would only show one extractor entry, masking line-mode behaviour. rules: - name: e2e-dsldbg-statement - layer: GENERAL + layer: auto + fallback: true dsl: | filter { extractor { + layer "GENERAL" tag stage: 'extractor' tag emitter: 'e2e-dsldbg' tag rule: 'multi-statement' diff --git a/test/e2e-v2/cases/log/lal.yaml b/test/e2e-v2/cases/log/lal.yaml index 4d98c93167f3..47e9d603f5a5 100644 --- a/test/e2e-v2/cases/log/lal.yaml +++ b/test/e2e-v2/cases/log/lal.yaml @@ -15,7 +15,8 @@ rules: - name: example - layer: GENERAL + layer: auto + fallback: true dsl: | filter { text { @@ -23,6 +24,7 @@ rules: regexp $/(?s)(?\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}) \[TID:(?.+?)] \[(?.+?)] (?\w{4,}) (?.{1,36}) (?.+)/$ } extractor { + layer "GENERAL" metrics { timestamp log.timestamp as Long labels level: parsed.level, service: log.service, instance: log.serviceInstance diff --git a/test/e2e-v2/cases/miniprogram/alipay/docker-compose.yml b/test/e2e-v2/cases/miniprogram/alipay/docker-compose.yml index 8ebcc64165f9..2fdf6c153849 100644 --- a/test/e2e-v2/cases/miniprogram/alipay/docker-compose.yml +++ b/test/e2e-v2/cases/miniprogram/alipay/docker-compose.yml @@ -38,7 +38,7 @@ services: # Upstream SkyAPM mini-program-monitor simulator (shipped from # https://github.com/SkyAPM/mini-program-monitor). sim-alipay: - image: ghcr.io/skyapm/mini-program-monitor/sim-alipay:v0.4.1 + image: ghcr.io/skyapm/mini-program-monitor/sim-alipay:9f59eb574addad78ffd2b84b8fc1de99c3071224 depends_on: oap: condition: service_healthy diff --git a/test/e2e-v2/cases/miniprogram/alipay/e2e.yaml b/test/e2e-v2/cases/miniprogram/alipay/e2e.yaml index afc5ccac6ebc..13ae22ce30ef 100644 --- a/test/e2e-v2/cases/miniprogram/alipay/e2e.yaml +++ b/test/e2e-v2/cases/miniprogram/alipay/e2e.yaml @@ -14,7 +14,7 @@ # limitations under the License. # Alipay Mini Program e2e test — drives the published -# ghcr.io/skyapm/mini-program-monitor/sim-alipay:v0.4.1 image in MODE=timed +# ghcr.io/skyapm/mini-program-monitor/sim-alipay:9f59eb574addad78ffd2b84b8fc1de99c3071224 image in MODE=timed # against OAP. Alipay emits a strict subset of metrics relative to WeChat # (no route / script / package_load / first_paint) so this case verifies # only the metrics Alipay actually produces. diff --git a/test/e2e-v2/cases/miniprogram/wechat/docker-compose.yml b/test/e2e-v2/cases/miniprogram/wechat/docker-compose.yml index 98012d252607..25f42fff7a5a 100644 --- a/test/e2e-v2/cases/miniprogram/wechat/docker-compose.yml +++ b/test/e2e-v2/cases/miniprogram/wechat/docker-compose.yml @@ -39,7 +39,7 @@ services: # https://github.com/SkyAPM/mini-program-monitor). Drives realistic # OTLP logs + metrics + SW native trace segments against OAP. sim-wechat: - image: ghcr.io/skyapm/mini-program-monitor/sim-wechat:v0.4.1 + image: ghcr.io/skyapm/mini-program-monitor/sim-wechat:9f59eb574addad78ffd2b84b8fc1de99c3071224 depends_on: oap: condition: service_healthy diff --git a/test/e2e-v2/cases/miniprogram/wechat/e2e.yaml b/test/e2e-v2/cases/miniprogram/wechat/e2e.yaml index 559b0d4da685..8351d942c523 100644 --- a/test/e2e-v2/cases/miniprogram/wechat/e2e.yaml +++ b/test/e2e-v2/cases/miniprogram/wechat/e2e.yaml @@ -14,7 +14,7 @@ # limitations under the License. # WeChat Mini Program e2e test — drives the published -# ghcr.io/skyapm/mini-program-monitor/sim-wechat:v0.4.1 image in MODE=timed +# ghcr.io/skyapm/mini-program-monitor/sim-wechat:9f59eb574addad78ffd2b84b8fc1de99c3071224 image in MODE=timed # against OAP wired with the miniprogram MAL / LAL / log-MAL rules. The sim # emits realistic OTLP logs + metrics (perf gauges + request-duration # histogram + errors) plus SW native trace segments (/v3/segments) with diff --git a/test/e2e-v2/cases/otlp-traces/native/expected/logs-otlp-general.yml b/test/e2e-v2/cases/otlp-traces/native/expected/logs-otlp-general.yml new file mode 100644 index 000000000000..e6e7014af90a --- /dev/null +++ b/test/e2e-v2/cases/otlp-traces/native/expected/logs-otlp-general.yml @@ -0,0 +1,31 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +errorreason: null +logs: +{{- contains .logs }} +- servicename: e2e-otlp-general-log + serviceid: {{ b64enc "e2e-otlp-general-log" }}.1 + serviceinstancename: null + serviceinstanceid: null + endpointname: null + endpointid: null + traceid: null + timestamp: {{ gt .timestamp 0 }} + contenttype: TEXT + content: a log of an OTLP_GENERAL service + tags: [] +{{- end }} +debuggingtrace: null diff --git a/test/e2e-v2/cases/otlp-traces/native/expected/services-otlp-general-log.yml b/test/e2e-v2/cases/otlp-traces/native/expected/services-otlp-general-log.yml new file mode 100644 index 000000000000..e4593e30f44e --- /dev/null +++ b/test/e2e-v2/cases/otlp-traces/native/expected/services-otlp-general-log.yml @@ -0,0 +1,24 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +{{- contains . }} +- id: {{ b64enc "e2e-otlp-general-log" }}.1 + name: e2e-otlp-general-log + group: "" + shortname: e2e-otlp-general-log + layers: + - OTLP_GENERAL + normal: true +{{- end }} diff --git a/test/e2e-v2/cases/otlp-traces/native/log-cases.yaml b/test/e2e-v2/cases/otlp-traces/native/log-cases.yaml index 7b87dd62de9e..bb02a9f38fa8 100644 --- a/test/e2e-v2/cases/otlp-traces/native/log-cases.yaml +++ b/test/e2e-v2/cases/otlp-traces/native/log-cases.yaml @@ -33,3 +33,13 @@ cases: byOtherSpan: (($other.data.queryLogs.logs // []) | length), mixedRefused: ((($mixed.errors // []) | length) > 0 and ($mixed.data.queryLogs == null))}' | yq -P expected: expected/logs-by-otlp-span.yml + # An OTLP log whose resource sets service.layer=OTLP_GENERAL, as an application whose spans are analyzed sets it + # for all its signals, is saved by the otlp-general LAL rule and files its service under OTLP_GENERAL. + - query: | + curl -s -X POST http://${oap_host}:${oap_12800}/v1/logs -H 'Content-Type: application/json' \ + -d '{"resourceLogs":[{"resource":{"attributes":[{"key":"service.name","value":{"stringValue":"e2e-otlp-general-log"}},{"key":"service.layer","value":{"stringValue":"OTLP_GENERAL"}}]},"scopeLogs":[{"logRecords":[{"timeUnixNano":"'$(date +%s)'000000000","body":{"stringValue":"a log of an OTLP_GENERAL service"}}]}]}]}' > /dev/null + sleep 5 + swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql logs list --service-name=e2e-otlp-general-log + expected: expected/logs-otlp-general.yml + - query: swctl --display yaml --base-url=http://${oap_host}:${oap_12800}/graphql service ly OTLP_GENERAL + expected: expected/services-otlp-general-log.yml diff --git a/test/e2e-v2/cases/runtime-rule/lal/seed-rules/lal-v1.yaml b/test/e2e-v2/cases/runtime-rule/lal/seed-rules/lal-v1.yaml index 83cb4d37325d..48d8f0521dcb 100644 --- a/test/e2e-v2/cases/runtime-rule/lal/seed-rules/lal-v1.yaml +++ b/test/e2e-v2/cases/runtime-rule/lal/seed-rules/lal-v1.yaml @@ -21,13 +21,15 @@ # the running extraction — the new step value shows up in queries. rules: - name: e2e-rr-lal-live - layer: GENERAL + layer: auto + fallback: true dsl: | filter { text { regexp $/.*e2e_rr_lal_live.*/$ } extractor { + layer "GENERAL" metrics { timestamp log.timestamp as Long labels service: log.service, service_instance_id: log.serviceInstance, step: 'v1' diff --git a/test/e2e-v2/cases/runtime-rule/lal/seed-rules/lal-v2.yaml b/test/e2e-v2/cases/runtime-rule/lal/seed-rules/lal-v2.yaml index 854fdcd5ae43..8719b3d2d1d6 100644 --- a/test/e2e-v2/cases/runtime-rule/lal/seed-rules/lal-v2.yaml +++ b/test/e2e-v2/cases/runtime-rule/lal/seed-rules/lal-v2.yaml @@ -20,13 +20,15 @@ # extraction switched bodies (not just the persisted rule row). rules: - name: e2e-rr-lal-live - layer: GENERAL + layer: auto + fallback: true dsl: | filter { text { regexp $/.*e2e_rr_lal_live.*/$ } extractor { + layer "GENERAL" metrics { timestamp log.timestamp as Long labels service: log.service, service_instance_id: log.serviceInstance, step: 'v2' diff --git a/test/e2e-v2/cases/satellite/native-protocols/lal.yaml b/test/e2e-v2/cases/satellite/native-protocols/lal.yaml index e94fb2aceb2f..db7008b6e2db 100644 --- a/test/e2e-v2/cases/satellite/native-protocols/lal.yaml +++ b/test/e2e-v2/cases/satellite/native-protocols/lal.yaml @@ -15,7 +15,8 @@ rules: - name: example - layer: GENERAL + layer: auto + fallback: true dsl: | filter { text { @@ -23,6 +24,7 @@ rules: regexp $/(?s)(?\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}) \[TID:(?.+?)] \[(?.+?)] (?\w{4,}) (?.{1,36}) (?.+)/$ } extractor { + layer "GENERAL" } sink { } From 251b6d5b311c084638c8bdecc4609566005413fc Mon Sep 17 00:00:00 2001 From: wankai123 Date: Tue, 17 Nov 2026 10:35:11 +0800 Subject: [PATCH 2/2] Publish the auto and fallback LAL rules as one snapshot A runtime replace that flipped a rule's fallback flag wrote the auto and the fallback rule maps one after the other, so a concurrent log could find the rule in neither group and lose its extraction, metrics or the log itself. Both groups now live in one immutable snapshot, replaced whole, and LogAnalyzer takes both passes of a log from one snapshot through the new LogAnalysisListenerFactory.createAuto(). --- oap-server/analyzer/log-analyzer/CLAUDE.md | 13 ++-- .../analyzer/v2/provider/log/LogAnalyzer.java | 32 ++++---- .../provider/log/listener/AutoListeners.java | 35 +++++++++ .../listener/LogAnalysisListenerFactory.java | 12 ++- .../log/listener/LogFilterListener.java | 75 +++++++++++-------- .../provider/log/LogAnalyzerRoutingTest.java | 9 +-- .../listener/LogFilterListenerLayerTest.java | 53 ++++++++++++- 7 files changed, 166 insertions(+), 63 deletions(-) create mode 100644 oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/AutoListeners.java diff --git a/oap-server/analyzer/log-analyzer/CLAUDE.md b/oap-server/analyzer/log-analyzer/CLAUDE.md index cac8e06e2d1a..5c1befa8ac83 100644 --- a/oap-server/analyzer/log-analyzer/CLAUDE.md +++ b/oap-server/analyzer/log-analyzer/CLAUDE.md @@ -196,16 +196,19 @@ Rules with `layer: auto` match logs where `service.layer` is absent (empty). `Lo routes an empty-layer log in up to two passes, stopping at the first pass in which a rule claims it (`LogAnalysisListener.claimed()`: did not abort): -1. The regular auto rules — `LogFilterListener.Factory.autoDsls`, via `create(null)`. -2. The `fallback: true` auto rules — `Factory.fallbackDsls`, via `createFallback()`. Before this pass the - metadata layer is reset to the value received, since the rules share one `LogMetadata` and an aborted - auto rule may have set a layer on it. +1. The regular auto rules. +2. The `fallback: true` auto rules. Before this pass the metadata layer is reset to the value received, + since the rules share one `LogMetadata` and an aborted auto rule may have set a layer on it. + +Both groups live in one immutable `Factory.AutoRules` snapshot, replaced whole on every runtime change, so a +rule whose `fallback` flag flips is never in neither (or both). `LogAnalyzer` takes both passes' listeners +from one snapshot up front via `LogAnalysisListenerFactory.createAuto()` → `AutoListeners`. 3. Nothing claimed it: the log is dropped (debug log). There is no fallback to the `GENERAL` rules; `lal/default.yaml` ships `default-fallback`, which sets `GENERAL`. Auto and fallback rules share one name space and rule key (`ruleKey(null, name)` → `|name`), so `find` / `contains` / `remove` / suspend / runtime-rule keys treat them alike; `addOrReplace` moves a rule -between the two maps when its `fallback` flag changes. `fallback: true` on a non-auto rule fails compile. +between the two groups when its `fallback` flag changes. `fallback: true` on a non-auto rule fails compile. The `autoLayerMode` flag on `ExecutionContext` (set for both passes) triggers a layer check in `FilterSpec.doSink()`: if the extractor didn't set a layer, the log is warned and dropped. Because of it, diff --git a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzer.java b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzer.java index 208143ab1dda..5985829aed20 100644 --- a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzer.java +++ b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzer.java @@ -21,7 +21,9 @@ import java.util.List; import java.util.Objects; import java.util.function.Function; +import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; +import org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener.AutoListeners; import org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener.LogAnalysisListener; import org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener.LogAnalysisListenerFactory; import org.apache.skywalking.oap.server.core.analysis.Layer; @@ -67,15 +69,20 @@ public void doAnalysis(LogMetadata metadata, final Object input) { metadata.setTimestamp(System.currentTimeMillis()); } if (metadata.getLayer() == null || metadata.getLayer().isEmpty()) { - // A log without a layer: the auto rules, then the fallback auto rules, until one claims it. + // A log without a layer: the auto rules, then the fallback auto rules, until one claims it. Both + // passes come from one view of the rules, taken before either runs. + final List passes = factoryManager.getLogAnalysisListenerFactories() + .stream() + .map(LogAnalysisListenerFactory::createAuto) + .collect(Collectors.toList()); final String arrivedLayer = metadata.getLayer(); - if (analyzeUntilClaimed(metadata, input, factory -> factory.create(null))) { + if (analyzeUntilClaimed(metadata, input, passes, AutoListeners::getAuto)) { return; } // The rules share this metadata, and an auto rule may have set a layer before it aborted // (LALBlockCodegen writes `layer` into it); the fallback rules start from the layer as received. metadata.setLayer(arrivedLayer); - if (analyzeUntilClaimed(metadata, input, LogAnalysisListenerFactory::createFallback)) { + if (analyzeUntilClaimed(metadata, input, passes, AutoListeners::getFallback)) { return; } log.debug("No LAL rule claimed the log without a layer of service {}, dropping it", metadata.getService()); @@ -86,16 +93,21 @@ public void doAnalysis(LogMetadata metadata, final Object input) { log.warn("The Layer {} is not recognized, abandon the log.", metadata.getLayer()); return; } - createAnalysisListeners(factory -> factory.create(layer)); + factoryManager.getLogAnalysisListenerFactories() + .stream() + .map(factory -> factory.create(layer)) + .filter(Objects::nonNull) + .forEach(listeners::add); notifyAnalysisListener(metadata, input); notifyAnalysisListenerToBuild(); } private boolean analyzeUntilClaimed(final LogMetadata metadata, final Object input, - final Function create) { + final List passes, + final Function pass) { listeners.clear(); - createAnalysisListeners(create); + passes.stream().map(pass).filter(Objects::nonNull).forEach(listeners::add); if (listeners.isEmpty()) { return false; } @@ -111,12 +123,4 @@ private void notifyAnalysisListener(final LogMetadata metadata, final Object inp private void notifyAnalysisListenerToBuild() { listeners.forEach(LogAnalysisListener::build); } - - private void createAnalysisListeners(final Function create) { - factoryManager.getLogAnalysisListenerFactories() - .stream() - .map(create) - .filter(Objects::nonNull) - .forEach(listeners::add); - } } diff --git a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/AutoListeners.java b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/AutoListeners.java new file mode 100644 index 000000000000..71f701b489c3 --- /dev/null +++ b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/AutoListeners.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +/** + * The two passes a log without a layer runs, taken from one view of the rules so that a rule moving + * between the auto and the fallback auto rules is seen in exactly one of them for the whole log. + */ +@Getter +@RequiredArgsConstructor +public final class AutoListeners { + /** The auto rules' listener; null when there are none. */ + private final LogAnalysisListener auto; + /** The fallback auto rules' listener, run when no auto rule claimed the log; null when there are none. */ + private final LogAnalysisListener fallback; +} diff --git a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogAnalysisListenerFactory.java b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogAnalysisListenerFactory.java index f32648789eac..a0747ff56692 100644 --- a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogAnalysisListenerFactory.java +++ b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogAnalysisListenerFactory.java @@ -25,13 +25,17 @@ */ public interface LogAnalysisListenerFactory { + /** + * @param layer the layer of the log; null for the auto rules of a log without a layer + * @return the listener of the rules declared for the layer; null when there are none. + */ LogAnalysisListener create(Layer layer); /** - * @return the listener of the {@code fallback} auto rules, run on a log without a layer that no other - * auto rule claimed; null when there are none. + * @return the listeners of the two passes a log without a layer runs: the auto rules, then the + * {@code fallback} auto rules when no auto rule claimed it. Both come from one view of the rules. */ - default LogAnalysisListener createFallback() { - return null; + default AutoListeners createAuto() { + return new AutoListeners(create(null), null); } } diff --git a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListener.java b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListener.java index fdd1e783c219..a000c8b3e340 100644 --- a/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListener.java +++ b/oap-server/analyzer/log-analyzer/src/main/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListener.java @@ -173,19 +173,18 @@ public static class Factory implements LogAnalysisListenerFactory, Service { * reference observed by a reader is always fully-populated for its generation. */ private volatile Map> dsls; - private volatile Map autoDsls; /** - * The {@code fallback: true} auto rules. They share the auto rules' name space and rule keys - * ({@link #ruleKey} with a null layer), so a name is in at most one of the two maps. + * The auto rules and the {@code fallback: true} auto rules, published together so a rule whose flag + * a replace flips is in exactly one of the two for every reader. */ - private volatile Map fallbackDsls; + private volatile AutoRules autoRules; /** * Suspended rule keys — encoded as {@code layer.name() + "|" + ruleName} for layer-keyed * rules and {@code "|" + ruleName} for auto-layer rules. When {@link #create} is * asked for a layer, it filters out entries whose key is in this set so samples arriving * during a hot-update Suspend window never hit the prior DSL. Volatile + CoW replace so - * readers stay lock-free — matches the {@link #dsls} / {@link #autoDsls} contract. + * readers stay lock-free — matches the {@link #dsls} / {@link #autoRules} contract. */ private volatile Set suspendedKeys = Collections.emptySet(); @@ -201,7 +200,7 @@ public static class Factory implements LogAnalysisListenerFactory, Service { /** * Two-phase init — the constructor wires fields and runs the SPI scan, leaving the - * {@code dsls} / {@code autoDsls} registry empty so this instance is safe to register + * {@code dsls} / {@code autoRules} registry empty so this instance is safe to register * as a module service from {@code prepare()}. The rule-compile pass — which calls * {@code RecordSinkListener.Factory.} → {@code moduleManager.find()} and is * therefore illegal during the prepare stage — is deferred to @@ -230,8 +229,7 @@ public Factory(final ModuleManager moduleManager, final LogAnalyzerModuleConfig // Empty registry — populated by {@link #loadStaticRules}. this.dsls = new HashMap<>(); - this.autoDsls = new HashMap<>(); - this.fallbackDsls = new HashMap<>(); + this.autoRules = new AutoRules(new HashMap<>(), new HashMap<>()); } /** @@ -282,8 +280,7 @@ public void loadStaticRules() throws Exception { } // Publish: readers from now on see the startup-complete registry. this.dsls = initDsls; - this.autoDsls = initAutoDsls; - this.fallbackDsls = initFallbackDsls; + this.autoRules = new AutoRules(initAutoDsls, initFallbackDsls); } /** @@ -353,13 +350,13 @@ public void addOrReplace(final CompiledLAL compiled) { synchronized (writeLock) { if (compiled.layer == null) { // A replace may move the rule between the auto and the fallback auto rules. - final Map nextAuto = new HashMap<>(autoDsls); - final Map nextFallback = new HashMap<>(fallbackDsls); + final AutoRules current = autoRules; + final Map nextAuto = new HashMap<>(current.auto); + final Map nextFallback = new HashMap<>(current.fallback); nextAuto.remove(compiled.ruleName); nextFallback.remove(compiled.ruleName); (compiled.fallback ? nextFallback : nextAuto).put(compiled.ruleName, compiled.dsl); - autoDsls = nextAuto; - fallbackDsls = nextFallback; + autoRules = new AutoRules(nextAuto, nextFallback); } else { final Map> next = new HashMap<>(dsls); final Map layerMap = @@ -378,7 +375,7 @@ public void addOrReplace(final CompiledLAL compiled) { * *

    Read-only — exposed so the dsl-debugging module can resolve a rule's * {@code GateHolder} via the live DSL right after the LAL apply path commits. - * No write lock; the volatile {@code dsls} / {@code autoDsls} fields are + * No write lock; the volatile {@code dsls} / {@code autoRules} fields are * publication-safe for snapshot reads. */ public DSL find(final Layer layer, final String ruleName) { @@ -386,8 +383,7 @@ public DSL find(final Layer layer, final String ruleName) { return null; } if (layer == null) { - final DSL auto = autoDsls.get(ruleName); - return auto != null ? auto : fallbackDsls.get(ruleName); + return autoRules.get(ruleName); } final Map layerMap = dsls.get(layer); return layerMap == null ? null : layerMap.get(ruleName); @@ -397,15 +393,15 @@ public DSL find(final Layer layer, final String ruleName) { public void remove(final Layer layer, final String ruleName) { synchronized (writeLock) { if (layer == null) { - if (autoDsls.containsKey(ruleName)) { - final Map next = new HashMap<>(autoDsls); - next.remove(ruleName); - autoDsls = next; - } else if (fallbackDsls.containsKey(ruleName)) { - final Map next = new HashMap<>(fallbackDsls); - next.remove(ruleName); - fallbackDsls = next; + final AutoRules current = autoRules; + if (current.get(ruleName) == null) { + return; } + final Map nextAuto = new HashMap<>(current.auto); + final Map nextFallback = new HashMap<>(current.fallback); + nextAuto.remove(ruleName); + nextFallback.remove(ruleName); + autoRules = new AutoRules(nextAuto, nextFallback); } else { final Map layerMap = dsls.get(layer); if (layerMap == null || !layerMap.containsKey(ruleName)) { @@ -428,7 +424,7 @@ public void remove(final Layer layer, final String ruleName) { * detect cross-file collisions before registering. */ public boolean contains(final Layer layer, final String ruleName) { if (layer == null) { - return autoDsls.containsKey(ruleName) || fallbackDsls.containsKey(ruleName); + return autoRules.get(ruleName) != null; } final Map layerMap = dsls.get(layer); return layerMap != null && layerMap.containsKey(ruleName); @@ -483,6 +479,22 @@ public static String ruleKey(final Layer layer, final String ruleName) { return (layer == null ? AUTO_LAYER_PREFIX : layer.name() + "|") + ruleName; } + /** An immutable view of the auto rules and the fallback auto rules; a rule name is in at most one. */ + private static final class AutoRules { + private final Map auto; + private final Map fallback; + + private AutoRules(final Map auto, final Map fallback) { + this.auto = auto; + this.fallback = fallback; + } + + private DSL get(final String ruleName) { + final DSL rule = auto.get(ruleName); + return rule != null ? rule : fallback.get(ruleName); + } + } + /** Compact result of {@link #compile(LALConfig)} so callers don't handle Layer / DSL separately. */ public static final class CompiledLAL { public final Layer layer; @@ -619,7 +631,7 @@ public LogAnalysisListener create(Layer layer) { // mid-iteration. The reference is volatile + copy-on-write so this is lock-free. final Set susp = suspendedKeys; if (layer == null) { - return createAuto(autoDsls); + return autoListener(autoRules.auto, susp); } final Map dsl = dsls.get(layer); if (dsl == null) { @@ -635,15 +647,16 @@ public LogAnalysisListener create(Layer layer) { } @Override - public LogAnalysisListener createFallback() { - return createAuto(fallbackDsls); + public AutoListeners createAuto() { + final AutoRules rules = autoRules; + final Set susp = suspendedKeys; + return new AutoListeners(autoListener(rules.auto, susp), autoListener(rules.fallback, susp)); } - private LogAnalysisListener createAuto(final Map rules) { + private static LogAnalysisListener autoListener(final Map rules, final Set susp) { if (rules.isEmpty()) { return null; } - final Set susp = suspendedKeys; final Collection eligible = susp.isEmpty() ? rules.values() : filterSuspended(rules, susp, null); if (eligible.isEmpty()) { return null; diff --git a/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzerRoutingTest.java b/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzerRoutingTest.java index 421659dbcdb7..0a2696281d8e 100644 --- a/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzerRoutingTest.java +++ b/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/LogAnalyzerRoutingTest.java @@ -19,6 +19,7 @@ package org.apache.skywalking.oap.log.analyzer.v2.provider.log; import java.util.Collections; +import org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener.AutoListeners; import org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener.LogAnalysisListener; import org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener.LogAnalysisListenerFactory; import org.apache.skywalking.oap.server.core.analysis.Layer; @@ -53,8 +54,7 @@ void setUp() { auto = mock(LogAnalysisListener.class); fallback = mock(LogAnalysisListener.class); general = mock(LogAnalysisListener.class); - when(factory.create(null)).thenReturn(auto); - when(factory.createFallback()).thenReturn(fallback); + when(factory.createAuto()).thenReturn(new AutoListeners(auto, fallback)); when(factory.create(Layer.GENERAL)).thenReturn(general); final ILogAnalysisListenerManager manager = mock(ILogAnalysisListenerManager.class); when(manager.getLogAnalysisListenerFactories()).thenReturn(Collections.singletonList(factory)); @@ -72,7 +72,7 @@ void autoRuleClaimingTheLogSkipsTheFallbackRules() { analyzer.doAnalysis(log(""), null); verify(auto).build(); - verify(factory, never()).createFallback(); + verify(fallback, never()).build(); verify(general, never()).build(); } @@ -116,7 +116,6 @@ void logWithLayerRunsOnlyThatLayersRules() { verify(general).parse(any(), any()); verify(general).build(); - verify(factory, never()).create(null); - verify(factory, never()).createFallback(); + verify(factory, never()).createAuto(); } } diff --git a/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListenerLayerTest.java b/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListenerLayerTest.java index a4143ead4c1d..bec919cc832d 100644 --- a/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListenerLayerTest.java +++ b/oap-server/analyzer/log-analyzer/src/test/java/org/apache/skywalking/oap/log/analyzer/v2/provider/log/listener/LogFilterListenerLayerTest.java @@ -18,6 +18,8 @@ package org.apache.skywalking.oap.log.analyzer.v2.provider.log.listener; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import org.apache.skywalking.oap.log.analyzer.v2.dsl.DSL; import org.apache.skywalking.oap.log.analyzer.v2.provider.LALConfig; import org.apache.skywalking.oap.log.analyzer.v2.provider.LogAnalyzerModuleConfig; @@ -99,11 +101,12 @@ void autoAndFallbackRulesAreSeparateGroupsOfOneNameSpace() throws Exception { factory.addOrReplace(new LogFilterListener.Factory.CompiledLAL(null, "a", autoRule, false)); factory.addOrReplace(new LogFilterListener.Factory.CompiledLAL(null, "f", fallbackRule, true)); - evaluate(factory.create(null)); + final AutoListeners passes = factory.createAuto(); + evaluate(passes.getAuto()); verify(autoRule).evaluate(any()); verify(fallbackRule, never()).evaluate(any()); - evaluate(factory.createFallback()); + evaluate(passes.getFallback()); verify(fallbackRule).evaluate(any()); assertSame(fallbackRule, factory.find(null, "f")); @@ -111,11 +114,53 @@ void autoAndFallbackRulesAreSeparateGroupsOfOneNameSpace() throws Exception { // A replace that turns "a" into a fallback rule moves it between the groups. factory.addOrReplace(new LogFilterListener.Factory.CompiledLAL(null, "a", autoRule, true)); - assertNull(factory.create(null)); + assertNull(factory.createAuto().getAuto()); factory.remove(null, "a"); factory.remove(null, "f"); - assertNull(factory.createFallback()); + assertNull(factory.createAuto().getFallback()); + assertFalse(factory.contains(null, "a")); + } + + /** + * A rule whose {@code fallback} flag flips on a runtime replace moves between the two groups; a reader + * running at the same time must always find it in one of them, never in neither. + */ + @Test + void ruleMovingBetweenAutoAndFallbackIsNeverMissing() throws Exception { + final LogFilterListener.Factory factory = factory(); + final DSL rule = mock(DSL.class); + factory.addOrReplace(new LogFilterListener.Factory.CompiledLAL(null, "r", rule, false)); + + final AtomicBoolean stop = new AtomicBoolean(); + final Thread mover = new Thread(() -> { + boolean fallback = false; + while (!stop.get()) { + fallback = !fallback; + factory.addOrReplace(new LogFilterListener.Factory.CompiledLAL(null, "r", rule, fallback)); + } + }); + mover.start(); + int missed = 0; + int notExactlyOnePass = 0; + try { + final long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(2); + while (System.nanoTime() < deadline) { + if (factory.find(null, "r") == null) { + missed++; + } + // The two passes of one log come from one view: the rule is in exactly one of them. + final AutoListeners passes = factory.createAuto(); + if ((passes.getAuto() == null) == (passes.getFallback() == null)) { + notExactlyOnePass++; + } + } + } finally { + stop.set(true); + mover.join(); + } + assertEquals(0, missed, "lookups that found the rule in neither group"); + assertEquals(0, notExactlyOnePass, "createAuto() views with the rule in neither or both passes"); } private static LogFilterListener.Factory factory() throws Exception {