From bff52872e301a59a8e7b695717680d0ba3a95cd7 Mon Sep 17 00:00:00 2001 From: worldtiki Date: Tue, 29 Oct 2019 07:49:33 +0000 Subject: [PATCH] Service name for the remote redis endpoint. (#1475) fixes #1406 --- docs/src/main/asciidoc/_configprops.adoc | 1 + .../instrument/redis/OnRedisEnabled.java | 39 ------------- .../redis/TraceRedisAutoConfiguration.java | 20 +++++-- .../redis/TraceRedisProperties.java | 55 +++++++++++++++++++ ...itional-spring-configuration-metadata.json | 6 -- .../TraceRedisAutoConfigurationTests.java | 18 ++++-- 6 files changed, 85 insertions(+), 54 deletions(-) delete mode 100644 spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/redis/OnRedisEnabled.java create mode 100644 spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/redis/TraceRedisProperties.java diff --git a/docs/src/main/asciidoc/_configprops.adoc b/docs/src/main/asciidoc/_configprops.adoc index c561f204a..b6c967091 100644 --- a/docs/src/main/asciidoc/_configprops.adoc +++ b/docs/src/main/asciidoc/_configprops.adoc @@ -36,6 +36,7 @@ |spring.sleuth.propagation.tag.whitelisted-keys | | A list of keys to be put from extra propagation fields to span tags. |spring.sleuth.reactor.enabled.enabled | true | When true enables instrumentation for reactor. |spring.sleuth.redis.enabled | true | Enable span information propagation when using Redis. +|spring.sleuth.redis.remote-service-name | redis | Service name used for the remote Redis endpoint. |spring.sleuth.rxjava.schedulers.hook.enabled | true | Enable support for RxJava via RxJavaSchedulersHook. |spring.sleuth.rxjava.schedulers.ignoredthreads | [HystrixMetricPoller, ^RxComputation.*$] | Thread names for which spans will not be sampled. |spring.sleuth.sampler.probability | | Probability of requests that should be sampled. E.g. 1.0 - 100% requests should be sampled. The precision is whole-numbers only (i.e. there's no support for 0.1% of the traces). diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/redis/OnRedisEnabled.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/redis/OnRedisEnabled.java deleted file mode 100644 index 1d2d96a46..000000000 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/redis/OnRedisEnabled.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2013-2019 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 - * - * https://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.instrument.redis; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; - -/** - * Verifies if Redis property was enabled. - * - * @author Chao Chang - * @since 2.2.0 - */ -@Retention(RetentionPolicy.RUNTIME) -@Target({ ElementType.TYPE, ElementType.METHOD }) -@Documented -@ConditionalOnProperty(value = "spring.sleuth.redis.enabled", matchIfMissing = true) -@interface OnRedisEnabled { - -} diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/redis/TraceRedisAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/redis/TraceRedisAutoConfiguration.java index e986cc234..204384b4b 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/redis/TraceRedisAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/redis/TraceRedisAutoConfiguration.java @@ -26,6 +26,8 @@ import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -38,9 +40,10 @@ import org.springframework.context.annotation.Configuration; * @since 2.2.0 */ @Configuration(proxyBeanMethods = false) -@OnRedisEnabled +@ConditionalOnProperty(value = "spring.sleuth.redis.enabled", matchIfMissing = true) @ConditionalOnBean({ Tracing.class, ClientResources.class }) @AutoConfigureAfter({ TraceAutoConfiguration.class }) +@EnableConfigurationProperties(TraceRedisProperties.class) public class TraceRedisAutoConfiguration { @Configuration(proxyBeanMethods = false) @@ -48,8 +51,9 @@ public class TraceRedisAutoConfiguration { @Bean static TraceLettuceClientResourcesBeanPostProcessor traceLettuceClientResourcesBeanPostProcessor( - Tracing tracing) { - return new TraceLettuceClientResourcesBeanPostProcessor(tracing); + Tracing tracing, TraceRedisProperties traceRedisProperties) { + return new TraceLettuceClientResourcesBeanPostProcessor(tracing, + traceRedisProperties); } } @@ -63,8 +67,12 @@ class TraceLettuceClientResourcesBeanPostProcessor implements BeanPostProcessor private final Tracing tracing; - TraceLettuceClientResourcesBeanPostProcessor(Tracing tracing) { + private final TraceRedisProperties traceRedisProperties; + + TraceLettuceClientResourcesBeanPostProcessor(Tracing tracing, + TraceRedisProperties traceRedisProperties) { this.tracing = tracing; + this.traceRedisProperties = traceRedisProperties; } @Override @@ -83,7 +91,9 @@ class TraceLettuceClientResourcesBeanPostProcessor implements BeanPostProcessor log.debug( "Lettuce ClientResources bean is auto-configured to enable tracing."); } - return cr.mutate().tracing(BraveTracing.create(this.tracing)).build(); + BraveTracing lettuceTracing = BraveTracing.builder().tracing(this.tracing) + .serviceName(traceRedisProperties.getRemoteServiceName()).build(); + return cr.mutate().tracing(lettuceTracing).build(); } if (log.isDebugEnabled()) { log.debug( diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/redis/TraceRedisProperties.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/redis/TraceRedisProperties.java new file mode 100644 index 000000000..29e0235f3 --- /dev/null +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/redis/TraceRedisProperties.java @@ -0,0 +1,55 @@ +/* + * Copyright 2013-2019 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 + * + * https://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.instrument.redis; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Sleuth Redis properties. + * + * @author Daniel Albuquerque + */ +@ConfigurationProperties("spring.sleuth.redis") +public class TraceRedisProperties { + + /** + * Enable span information propagation when using Redis. + */ + private boolean enabled = true; + + /** + * Service name for the remote Redis endpoint. + */ + private String remoteServiceName = "redis"; + + public boolean isEnabled() { + return this.enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public String getRemoteServiceName() { + return remoteServiceName; + } + + public void setRemoteServiceName(String remoteServiceName) { + this.remoteServiceName = remoteServiceName; + } + +} diff --git a/spring-cloud-sleuth-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-sleuth-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 041306568..68e857166 100644 --- a/spring-cloud-sleuth-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-sleuth-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -48,12 +48,6 @@ "description": "Enable span information propagation when using GRPC.", "defaultValue": true }, - { - "name": "spring.sleuth.redis.enabled", - "type": "java.lang.Boolean", - "description": "Enable span information propagation when using Redis.", - "defaultValue": true - }, { "name": "spring.sleuth.messaging.jms.enabled", "type": "java.lang.Boolean", diff --git a/tests/spring-cloud-sleuth-instrumentation-lettuce-tests/src/test/java/org/springframework/cloud/sleuth/instrument/redis/TraceRedisAutoConfigurationTests.java b/tests/spring-cloud-sleuth-instrumentation-lettuce-tests/src/test/java/org/springframework/cloud/sleuth/instrument/redis/TraceRedisAutoConfigurationTests.java index 08079a584..f245d2506 100644 --- a/tests/spring-cloud-sleuth-instrumentation-lettuce-tests/src/test/java/org/springframework/cloud/sleuth/instrument/redis/TraceRedisAutoConfigurationTests.java +++ b/tests/spring-cloud-sleuth-instrumentation-lettuce-tests/src/test/java/org/springframework/cloud/sleuth/instrument/redis/TraceRedisAutoConfigurationTests.java @@ -62,10 +62,19 @@ public class TraceRedisAutoConfigurationTests { return clientResources; } + @Bean + TraceRedisProperties traceRedisProperties() { + TraceRedisProperties traceRedisProperties = new TraceRedisProperties(); + traceRedisProperties.setEnabled(true); + traceRedisProperties.setRemoteServiceName("redis-foo"); + return traceRedisProperties; + } + @Bean TestTraceLettuceClientResourcesBeanPostProcessor testTraceLettuceClientResourcesBeanPostProcessor( - Tracing tracing) { - return new TestTraceLettuceClientResourcesBeanPostProcessor(tracing); + Tracing tracing, TraceRedisProperties traceRedisProperties) { + return new TestTraceLettuceClientResourcesBeanPostProcessor(tracing, + traceRedisProperties); } } @@ -77,8 +86,9 @@ class TestTraceLettuceClientResourcesBeanPostProcessor boolean tracingCalled = false; - TestTraceLettuceClientResourcesBeanPostProcessor(Tracing tracing) { - super(tracing); + TestTraceLettuceClientResourcesBeanPostProcessor(Tracing tracing, + TraceRedisProperties traceRedisProperties) { + super(tracing, traceRedisProperties); } @Override