From 4108ea520ea0b152ea09d4472dbbee74ac11af29 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 25 Jun 2024 16:25:39 -0400 Subject: [PATCH] GH-9260: Fix `IntegrationManagementConfiguration.obtainObservationPatterns()` logic Fixes: #9260 The current `IntegrationManagementConfiguration.obtainObservationPatterns()` is to always have an `*` as a first pattern in the final result. Because of `HashSet` natural order. This would lead to ignore all the patterns, especially those with negation * Fix `IntegrationManagementConfiguration.obtainObservationPatterns()` logic to ignore regular patterns if `*` is present. Optimization wise. * Always put `*` into the end of final array let negative patterns to do their job * Modify `IntegrationGraphServerTests` & `WebFluxObservationPropagationTests` test configurations to ensure that `*` pattern does not have a precedence over negative patterns. **Auto-cherry-pick to `6.3.x` & `6.2.x`** --- .../IntegrationManagementConfiguration.java | 19 +++++++++++++++---- .../graph/IntegrationGraphServerTests.java | 4 ++-- .../WebFluxObservationPropagationTests.java | 4 +++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfiguration.java b/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfiguration.java index 22018753fb..9e430dae7c 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfiguration.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2022 the original author or authors. + * Copyright 2015-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,9 +16,10 @@ package org.springframework.integration.config; +import java.util.Collection; import java.util.HashSet; import java.util.Map; -import java.util.Set; +import java.util.stream.Collectors; import io.micrometer.observation.ObservationRegistry; @@ -88,17 +89,27 @@ public class IntegrationManagementConfiguration implements ImportAware, Environm } private String[] obtainObservationPatterns() { - Set observationPatterns = new HashSet<>(); + Collection observationPatterns = new HashSet<>(); String[] patternsProperties = (String[]) this.attributes.get("observationPatterns"); + boolean hasAsterisk = false; for (String patternProperty : patternsProperties) { String patternValue = this.environment.resolvePlaceholders(patternProperty); String[] patternsToProcess = StringUtils.commaDelimitedListToStringArray(patternValue); for (String pattern : patternsToProcess) { - if (StringUtils.hasText(pattern)) { + hasAsterisk |= "*".equals(pattern); + if (StringUtils.hasText(pattern) && (pattern.startsWith("!") || !hasAsterisk)) { observationPatterns.add(pattern); } } } + if (hasAsterisk) { + observationPatterns = + observationPatterns.stream() + .filter((pattern) -> pattern.startsWith("!")) + .collect(Collectors.toList()); + + observationPatterns.add("*"); + } return observationPatterns.toArray(new String[0]); } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/graph/IntegrationGraphServerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/graph/IntegrationGraphServerTests.java index 6ad0fd94e4..b8376accc7 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/graph/IntegrationGraphServerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/graph/IntegrationGraphServerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 the original author or authors. + * Copyright 2016-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -290,7 +290,7 @@ public class IntegrationGraphServerTests { @Configuration @EnableIntegration - @EnableIntegrationManagement(observationPatterns = "myFilter") + @EnableIntegrationManagement(observationPatterns = { "myFilter", "*" }) @IntegrationComponentScan @ImportResource("org/springframework/integration/graph/integration-graph-context.xml") public static class Config { diff --git a/spring-integration-webflux/src/test/java/org/springframework/integration/webflux/observation/WebFluxObservationPropagationTests.java b/spring-integration-webflux/src/test/java/org/springframework/integration/webflux/observation/WebFluxObservationPropagationTests.java index 8eb0e4476a..4050d0a852 100644 --- a/spring-integration-webflux/src/test/java/org/springframework/integration/webflux/observation/WebFluxObservationPropagationTests.java +++ b/spring-integration-webflux/src/test/java/org/springframework/integration/webflux/observation/WebFluxObservationPropagationTests.java @@ -125,7 +125,7 @@ public class WebFluxObservationPropagationTests { @Configuration @EnableWebFlux @EnableIntegration - @EnableIntegrationManagement(observationPatterns = "*") + @EnableIntegrationManagement(observationPatterns = "*,!notObserved*") public static class ContextConfiguration { @Bean @@ -200,6 +200,8 @@ public class WebFluxObservationPropagationTests { .transformWith(t -> t .transformer(String::toLowerCase) .id("testTransformer")) + .channel("notObservedChannel") + .bridge(e -> e.id("notObservedEndpoint")) .get(); }