[#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
This commit is contained in:
Marcin Grzejszczak
2016-02-22 14:03:40 +01:00
parent 980e2324d7
commit 3eca10bcc7
4 changed files with 70 additions and 10 deletions

View File

@@ -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);
}
}
/**

View File

@@ -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";

View File

@@ -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<R> extends HystrixCommand<R> {
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();
}

View File

@@ -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() {