From 3eca10bcc7af10387014b8b9b35b84c838390f1f Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 22 Feb 2016 14:03:40 +0100 Subject: [PATCH] [#159] Tagging hystrix events when a hystrix command was explicitly created we can tag the following values * command key * command group * thread pool key fixes #159 --- .../springframework/cloud/sleuth/Span.java | 8 ++- .../cloud/sleuth/instrument/TraceKeys.java | 49 ++++++++++++++++++- .../instrument/hystrix/TraceCommand.java | 6 ++- .../instrument/hystrix/TraceCommandTests.java | 17 ++++--- 4 files changed, 70 insertions(+), 10 deletions(-) diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/Span.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/Span.java index 790a3f523..04ceaaee5 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/Span.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/Span.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * Class for gathering and reporting statistics about a block of execution. @@ -148,10 +149,13 @@ public class Span { } /** - * Add a tag or data annotation associated with this span + * Add a tag or data annotation associated with this span. The tag will be + * added only if it has a value. */ public void tag(String key, String value) { - this.tags.put(key, value); + if (StringUtils.hasText(value)) { + this.tags.put(key, value); + } } /** diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/TraceKeys.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/TraceKeys.java index dd5ebed39..18bc2ded9 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/TraceKeys.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/TraceKeys.java @@ -314,10 +314,35 @@ public class TraceKeys { private String prefix = ""; /** - * Name of the command key + * Name of the command key. Describes the name for the given command. + * A key to represent a {@link com.netflix.hystrix.HystrixCommand} for + * monitoring, circuit-breakers, metrics publishing, caching and other such uses. + * + * @see com.netflix.hystrix.HystrixCommandKey */ private String commandKey = "commandKey"; + /** + * Name of the command group. Hystrix uses the command group key to group + * together commands such as for reporting, alerting, dashboards, + * or team/library ownership. + * + * @see com.netflix.hystrix.HystrixCommandGroupKey + */ + private String commandGroup = "commandGroup"; + + /** + * Name of the thread pool key. The thread-pool key represents a {@link com.netflix.hystrix.HystrixThreadPool} + * for monitoring, metrics publishing, caching, and other such uses. A {@link com.netflix.hystrix.HystrixCommand} + * is associated with a single {@link com.netflix.hystrix.HystrixThreadPool} as + * retrieved by the {@link com.netflix.hystrix.HystrixThreadPoolKey} injected into it, + * or it defaults to one created using the {@link com.netflix.hystrix.HystrixCommandGroupKey} + * it is created with. + * + * @see com.netflix.hystrix.HystrixThreadPoolKey + */ + private String threadPoolKey = "threadPoolKey"; + public String getPrefix() { return this.prefix; } @@ -326,6 +351,14 @@ public class TraceKeys { return this.commandKey; } + public String getCommandGroup() { + return this.commandGroup; + } + + public String getThreadPoolKey() { + return this.threadPoolKey; + } + public void setPrefix(String prefix) { this.prefix = prefix; } @@ -334,6 +367,14 @@ public class TraceKeys { this.commandKey = commandKey; } + public void setCommandGroup(String commandGroup) { + this.commandGroup = commandGroup; + } + + public void setThreadPoolKey(String threadPoolKey) { + this.threadPoolKey = threadPoolKey; + } + } /** @@ -348,17 +389,23 @@ public class TraceKeys { /** * Name of the thread that executed the async method + * + * @see org.springframework.scheduling.annotation.Async */ private String threadNameKey = "thread"; /** * Simple name of the class with a method annotated with {@code @Async} * from which the asynchronous process started + * + * @see org.springframework.scheduling.annotation.Async */ private String classNameKey = "class"; /** * Name of the method annotated with {@code @Async} + * + * @see org.springframework.scheduling.annotation.Async */ private String methodNameKey = "method"; diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/hystrix/TraceCommand.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/hystrix/TraceCommand.java index 6c89f8bc6..ab78bffa9 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/hystrix/TraceCommand.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/hystrix/TraceCommand.java @@ -18,9 +18,9 @@ package org.springframework.cloud.sleuth.instrument.hystrix; import org.springframework.cloud.sleuth.Span; import org.springframework.cloud.sleuth.Tracer; +import org.springframework.cloud.sleuth.instrument.TraceKeys; import com.netflix.hystrix.HystrixCommand; -import org.springframework.cloud.sleuth.instrument.TraceKeys; /** * Abstraction over {@code HystrixCommand} that wraps command execution with Trace setting @@ -54,6 +54,10 @@ public abstract class TraceCommand extends HystrixCommand { this.tracer.addTag(Span.SPAN_LOCAL_COMPONENT_TAG_NAME, HYSTRIX_COMPONENT); this.tracer.addTag(this.traceKeys.getHystrix().getPrefix() + this.traceKeys.getHystrix().getCommandKey(), commandKeyName); + this.tracer.addTag(this.traceKeys.getHystrix().getPrefix() + + this.traceKeys.getHystrix().getCommandGroup(), getCommandGroup().name()); + this.tracer.addTag(this.traceKeys.getHystrix().getPrefix() + + this.traceKeys.getHystrix().getThreadPoolKey(), getThreadPoolKey().name()); try { return doRun(); } diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/hystrix/TraceCommandTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/hystrix/TraceCommandTests.java index 569062216..f52ae1cad 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/hystrix/TraceCommandTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/hystrix/TraceCommandTests.java @@ -2,10 +2,6 @@ package org.springframework.cloud.sleuth.instrument.hystrix; import java.util.Random; -import com.netflix.hystrix.HystrixCommandKey; -import com.netflix.hystrix.HystrixCommandProperties; -import com.netflix.hystrix.HystrixThreadPoolProperties; -import com.netflix.hystrix.strategy.HystrixPlugins; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -19,6 +15,11 @@ import org.springframework.cloud.sleuth.trace.DefaultTracer; import org.springframework.cloud.sleuth.trace.TestSpanContextHolder; import org.springframework.context.ApplicationEventPublisher; +import com.netflix.hystrix.HystrixCommandKey; +import com.netflix.hystrix.HystrixCommandProperties; +import com.netflix.hystrix.HystrixThreadPoolProperties; +import com.netflix.hystrix.strategy.HystrixPlugins; + import static com.netflix.hystrix.HystrixCommand.Setter.withGroupKey; import static com.netflix.hystrix.HystrixCommandGroupKey.Factory.asKey; import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then; @@ -70,8 +71,12 @@ public class TraceCommandTests { Span spanFromCommand = whenCommandIsExecuted(command); - then(spanFromCommand).as("Span from the Hystrix Thread").isNotNull(); - then(spanFromCommand.getTraceId()).isEqualTo(EXPECTED_TRACE_ID); + then(spanFromCommand).as("Span from the Hystrix Thread") + .isNotNull() + .hasTraceIdEqualTo(EXPECTED_TRACE_ID) + .hasATag("commandKey", "traceCommandKey") + .hasATag("commandGroup", "group") + .hasATag("threadPoolKey", "group"); } private Span givenATraceIsPresentInTheCurrentThread() {