From 2a38aa124161944f22ba489b6bd326f22300e7f8 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 6 Jun 2016 09:36:35 +0100 Subject: [PATCH] Fix property names for stream binder headers Spring Cloud Stream changed the names of the headers config that we need to propagate headers in kafka (and other binders that do not support headers natively). Fixes gh-284 --- .../StreamEnvironmentPostProcessor.java | 18 ++++-- .../StreamEnvironmentPostProcessorTests.java | 55 +++++++++++++++++++ 2 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 spring-cloud-sleuth-stream/src/test/java/org/springframework/cloud/sleuth/stream/StreamEnvironmentPostProcessorTests.java diff --git a/spring-cloud-sleuth-stream/src/main/java/org/springframework/cloud/sleuth/stream/StreamEnvironmentPostProcessor.java b/spring-cloud-sleuth-stream/src/main/java/org/springframework/cloud/sleuth/stream/StreamEnvironmentPostProcessor.java index 32908625a..a7cca416e 100644 --- a/spring-cloud-sleuth-stream/src/main/java/org/springframework/cloud/sleuth/stream/StreamEnvironmentPostProcessor.java +++ b/spring-cloud-sleuth-stream/src/main/java/org/springframework/cloud/sleuth/stream/StreamEnvironmentPostProcessor.java @@ -63,7 +63,8 @@ public class StreamEnvironmentPostProcessor implements EnvironmentPostProcessor for (Resource resource : resolver .getResources("classpath*:META-INF/spring.binders")) { for (String binderType : parseBinderConfigurations(resource)) { - addHeaders(map, binderType); + int startIndex = findStartIndex(environment, binderType); + addHeaders(map, binderType, startIndex); } } } @@ -80,6 +81,15 @@ public class StreamEnvironmentPostProcessor implements EnvironmentPostProcessor addOrReplace(environment.getPropertySources(), map); } + private int findStartIndex(ConfigurableEnvironment environment, String binder) { + String prefix = "spring.cloud.stream." + binder + ".binder.headers"; + int i = 0; + while (environment.getProperty(prefix + "[" + i + "]")!=null) { + i++; + } + return i; + } + private Collection parseBinderConfigurations(Resource resource) { Collection keys = new HashSet<>(); try { @@ -115,10 +125,10 @@ public class StreamEnvironmentPostProcessor implements EnvironmentPostProcessor } } - private void addHeaders(Map map, String binder) { - String stem = "spring.cloud.stream.binder." + binder + ".headers"; + private void addHeaders(Map map, String binder, int startIndex) { + String stem = "spring.cloud.stream." + binder + ".binder.headers"; for (int i = 0; i < headers.length; i++) { - map.put(stem + "[" + i + "]", headers[i]); + map.put(stem + "[" + (i + startIndex) + "]", headers[i]); } } diff --git a/spring-cloud-sleuth-stream/src/test/java/org/springframework/cloud/sleuth/stream/StreamEnvironmentPostProcessorTests.java b/spring-cloud-sleuth-stream/src/test/java/org/springframework/cloud/sleuth/stream/StreamEnvironmentPostProcessorTests.java new file mode 100644 index 000000000..d7e9031be --- /dev/null +++ b/spring-cloud-sleuth-stream/src/test/java/org/springframework/cloud/sleuth/stream/StreamEnvironmentPostProcessorTests.java @@ -0,0 +1,55 @@ +/* + * Copyright 2015 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. + * 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.springframework.cloud.sleuth.stream; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.cloud.sleuth.Span; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.StandardEnvironment; + +/** + * @author Dave Syer + * + */ +public class StreamEnvironmentPostProcessorTests { + + private StreamEnvironmentPostProcessor processor = new StreamEnvironmentPostProcessor(); + private ConfigurableEnvironment environment = new StandardEnvironment(); + + @Test + public void testHeadersAdded() { + this.processor.postProcessEnvironment(this.environment, + new SpringApplication(StreamEnvironmentPostProcessorTests.class)); + assertThat(this.environment.getProperty("spring.cloud.stream.test.binder.headers[0]")) + .isEqualTo(Span.SPAN_ID_NAME); + } + + @Test + public void headersAppendedIfNecessary() { + EnvironmentTestUtils.addEnvironment(this.environment, "spring.cloud.stream.test.binder.headers[0]=X-Custom", + "spring.cloud.stream.test.binder.headers[1]=X-Mine"); + this.processor.postProcessEnvironment(this.environment, + new SpringApplication(StreamEnvironmentPostProcessorTests.class)); + assertThat(this.environment.getProperty("spring.cloud.stream.test.binder.headers[2]")) + .isEqualTo(Span.SPAN_ID_NAME); + } + +}