Skip to content

Commit 2693292

Browse files
committed
Merge pull request Netflix#769 from mattrjacobs/add-threadpool-to-metrics-stream-output
Add threadpool to hystrix-metrics-event-stream for each command
2 parents 5bb931a + c17ff4f commit 2693292

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

hystrix-contrib/hystrix-metrics-event-stream/src/main/java/com/netflix/hystrix/contrib/metrics/eventstream/HystrixMetricsPoller.java

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ private String getCommandJson(HystrixCommandMetrics commandMetrics) throws IOExc
186186
json.writeStringField("type", "HystrixCommand");
187187
json.writeStringField("name", key.name());
188188
json.writeStringField("group", commandMetrics.getCommandGroup().name());
189+
json.writeStringField("threadPool", commandMetrics.getThreadPoolKey().name());
189190
json.writeNumberField("currentTime", System.currentTimeMillis());
190191

191192
// circuit breaker

hystrix-contrib/hystrix-rx-netty-metrics-stream/src/test/java/com/netflix/hystrix/HystrixCommandMetricsSamples.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public String name() {
4242
}
4343
}
4444

45+
private static class MyHystrixThreadPoolKey implements HystrixThreadPoolKey {
46+
@Override
47+
public String name() {
48+
return "hystrixThreadPoolKey";
49+
}
50+
}
51+
4552
private static class MyHystrixCommandProperties extends HystrixCommandProperties {
4653
protected MyHystrixCommandProperties(HystrixCommandKey key) {
4754
super(key);
@@ -50,7 +57,7 @@ protected MyHystrixCommandProperties(HystrixCommandKey key) {
5057

5158
static {
5259
HystrixCommandKey key = new MyHystrixCommandKey();
53-
SAMPLE_1 = new HystrixCommandMetrics(key, new MyHystrixCommandGroupKey(),
60+
SAMPLE_1 = new HystrixCommandMetrics(key, new MyHystrixCommandGroupKey(), new MyHystrixThreadPoolKey(),
5461
new MyHystrixCommandProperties(key), HystrixEventNotifierDefault.getInstance());
5562
}
5663
}

hystrix-core/src/main/java/com/netflix/hystrix/AbstractCommand.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ protected AbstractCommand(HystrixCommandGroupKey group, HystrixCommandKey key, H
207207
* Metrics initialization
208208
*/
209209
if (metrics == null) {
210-
this.metrics = HystrixCommandMetrics.getInstance(this.commandKey, this.commandGroup, this.properties);
210+
this.metrics = HystrixCommandMetrics.getInstance(this.commandKey, this.commandGroup, this.threadPoolKey, this.properties);
211211
} else {
212212
this.metrics = metrics;
213213
}

hystrix-core/src/main/java/com/netflix/hystrix/HystrixCommandMetrics.java

+40-3
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,38 @@ public class HystrixCommandMetrics extends HystrixMetrics {
5757
* @return {@link HystrixCommandMetrics}
5858
*/
5959
public static HystrixCommandMetrics getInstance(HystrixCommandKey key, HystrixCommandGroupKey commandGroup, HystrixCommandProperties properties) {
60+
return getInstance(key, commandGroup, null, properties);
61+
}
62+
63+
/**
64+
* Get or create the {@link HystrixCommandMetrics} instance for a given {@link HystrixCommandKey}.
65+
* <p>
66+
* This is thread-safe and ensures only 1 {@link HystrixCommandMetrics} per {@link HystrixCommandKey}.
67+
*
68+
* @param key
69+
* {@link HystrixCommandKey} of {@link HystrixCommand} instance requesting the {@link HystrixCommandMetrics}
70+
* @param commandGroup
71+
* Pass-thru to {@link HystrixCommandMetrics} instance on first time when constructed
72+
* @param properties
73+
* Pass-thru to {@link HystrixCommandMetrics} instance on first time when constructed
74+
* @return {@link HystrixCommandMetrics}
75+
*/
76+
public static HystrixCommandMetrics getInstance(HystrixCommandKey key, HystrixCommandGroupKey commandGroup, HystrixThreadPoolKey threadPoolKey, HystrixCommandProperties properties) {
6077
// attempt to retrieve from cache first
6178
HystrixCommandMetrics commandMetrics = metrics.get(key.name());
6279
if (commandMetrics != null) {
6380
return commandMetrics;
6481
}
6582
// it doesn't exist so we need to create it
66-
commandMetrics = new HystrixCommandMetrics(key, commandGroup, properties, HystrixPlugins.getInstance().getEventNotifier());
83+
84+
//now check to see if we need to create a synthetic threadPoolKey
85+
HystrixThreadPoolKey nonNullThreadPoolKey;
86+
if (threadPoolKey == null) {
87+
nonNullThreadPoolKey = HystrixThreadPoolKey.Factory.asKey(commandGroup.name());
88+
} else {
89+
nonNullThreadPoolKey = threadPoolKey;
90+
}
91+
commandMetrics = new HystrixCommandMetrics(key, commandGroup, nonNullThreadPoolKey, properties, HystrixPlugins.getInstance().getEventNotifier());
6792
// attempt to store it (race other threads)
6893
HystrixCommandMetrics existing = metrics.putIfAbsent(key.name(), commandMetrics);
6994
if (existing == null) {
@@ -107,13 +132,15 @@ public static Collection<HystrixCommandMetrics> getInstances() {
107132
private final HystrixRollingPercentile percentileTotal;
108133
private final HystrixCommandKey key;
109134
private final HystrixCommandGroupKey group;
135+
private final HystrixThreadPoolKey threadPoolKey;
110136
private final AtomicInteger concurrentExecutionCount = new AtomicInteger();
111137
private final HystrixEventNotifier eventNotifier;
112138

113-
/* package */HystrixCommandMetrics(HystrixCommandKey key, HystrixCommandGroupKey commandGroup, HystrixCommandProperties properties, HystrixEventNotifier eventNotifier) {
139+
/* package */HystrixCommandMetrics(HystrixCommandKey key, HystrixCommandGroupKey commandGroup, HystrixThreadPoolKey threadPoolKey, HystrixCommandProperties properties, HystrixEventNotifier eventNotifier) {
114140
super(new HystrixRollingNumber(properties.metricsRollingStatisticalWindowInMilliseconds(), properties.metricsRollingStatisticalWindowBuckets()));
115141
this.key = key;
116142
this.group = commandGroup;
143+
this.threadPoolKey = threadPoolKey;
117144
this.properties = properties;
118145
this.percentileExecution = new HystrixRollingPercentile(properties.metricsRollingPercentileWindowInMilliseconds(), properties.metricsRollingPercentileWindowBuckets(), properties.metricsRollingPercentileBucketSize(), properties.metricsRollingPercentileEnabled());
119146
this.percentileTotal = new HystrixRollingPercentile(properties.metricsRollingPercentileWindowInMilliseconds(), properties.metricsRollingPercentileWindowBuckets(), properties.metricsRollingPercentileBucketSize(), properties.metricsRollingPercentileEnabled());
@@ -131,13 +158,23 @@ public HystrixCommandKey getCommandKey() {
131158

132159
/**
133160
* {@link HystrixCommandGroupKey} of the {@link HystrixCommand} these metrics represent.
134-
*
161+
*
135162
* @return HystrixCommandGroupKey
136163
*/
137164
public HystrixCommandGroupKey getCommandGroup() {
138165
return group;
139166
}
140167

168+
/**
169+
* {@link HystrixThreadPoolKey} used by {@link HystrixCommand} these metrics represent.
170+
*
171+
* @return HystrixThreadPoolKey
172+
*/
173+
public HystrixThreadPoolKey getThreadPoolKey() {
174+
return threadPoolKey;
175+
}
176+
177+
141178
/**
142179
* {@link HystrixCommandProperties} of the {@link HystrixCommand} these metrics represent.
143180
*

hystrix-core/src/test/java/com/netflix/hystrix/HystrixCircuitBreakerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ public void testLowVolumeDoesNotTripCircuit() {
494494
* Utility method for creating {@link HystrixCommandMetrics} for unit tests.
495495
*/
496496
private static HystrixCommandMetrics getMetrics(HystrixCommandProperties.Setter properties) {
497-
return new HystrixCommandMetrics(CommandKeyForUnitTest.KEY_ONE, CommandOwnerForUnitTest.OWNER_ONE, HystrixCommandPropertiesTest.asMock(properties), HystrixEventNotifierDefault.getInstance());
497+
return new HystrixCommandMetrics(CommandKeyForUnitTest.KEY_ONE, CommandOwnerForUnitTest.OWNER_ONE, ThreadPoolKeyForUnitTest.THREAD_POOL_ONE, HystrixCommandPropertiesTest.asMock(properties), HystrixEventNotifierDefault.getInstance());
498498
}
499499

500500
/**

hystrix-core/src/test/java/com/netflix/hystrix/HystrixCommandMetricsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ protected Boolean getFallback() {
129129
* Utility method for creating {@link HystrixCommandMetrics} for unit tests.
130130
*/
131131
private static HystrixCommandMetrics getMetrics(HystrixCommandProperties.Setter properties) {
132-
return new HystrixCommandMetrics(InspectableBuilder.CommandKeyForUnitTest.KEY_ONE, InspectableBuilder.CommandGroupForUnitTest.OWNER_ONE, HystrixCommandPropertiesTest.asMock(properties), HystrixEventNotifierDefault.getInstance());
132+
return new HystrixCommandMetrics(InspectableBuilder.CommandKeyForUnitTest.KEY_ONE, InspectableBuilder.CommandGroupForUnitTest.OWNER_ONE, InspectableBuilder.ThreadPoolKeyForUnitTest.THREAD_POOL_ONE, HystrixCommandPropertiesTest.asMock(properties), HystrixEventNotifierDefault.getInstance());
133133
}
134134

135135
}

0 commit comments

Comments
 (0)