Skip to content

Commit

Permalink
Protected hystrix-rx-netty-metrics-stream against version mismatches
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Jacobs committed Jan 25, 2016
1 parent 1724284 commit c397273
Showing 1 changed file with 142 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import com.netflix.hystrix.util.HystrixRollingNumberEvent;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.functions.Func0;

import java.io.IOException;
import java.io.StringWriter;
Expand All @@ -39,11 +42,27 @@
final class JsonMappers {

private static final JsonFactory jsonFactory = new JsonFactory();
static final Logger logger = LoggerFactory.getLogger(JsonMappers.class);

private JsonMappers() {
}

static String toJson(HystrixCommandMetrics commandMetrics) throws IOException {
private static void safelyWriteNumberField(JsonGenerator json, String name, Func0<Long> metricGenerator) throws IOException {
try {
json.writeNumberField(name, metricGenerator.call());
} catch (NoSuchFieldError error) {
logger.error("While publishing Hystrix metrics stream, error looking up eventType for : " + name + ". Please check that all Hystrix versions are the same!");
json.writeNumberField(name, 0L);
}
}

/**
* An implementation note. If there's a version mismatch between hystrix-core and hystrix-rx-netty-metrics-stream,
* the code below may reference a HystrixRollingNumberEvent that does not exist in hystrix-core. If this happens,
* a j.l.NoSuchFieldError occurs. Since this data is not being generated by hystrix-core, it's safe to count it as 0
* and we should log an error to get users to update their dependency set.
*/
static String toJson(final HystrixCommandMetrics commandMetrics) throws IOException {
HystrixCommandKey key = commandMetrics.getCommandKey();
HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory.getInstance(key);

Expand All @@ -69,22 +88,102 @@ static String toJson(HystrixCommandMetrics commandMetrics) throws IOException {
json.writeNumberField("requestCount", healthCounts.getTotalRequests());

// rolling counters
json.writeNumberField("rollingCountBadRequests", commandMetrics.getRollingCount(HystrixRollingNumberEvent.BAD_REQUEST));
json.writeNumberField("rollingCountCollapsedRequests", commandMetrics.getRollingCount(HystrixRollingNumberEvent.COLLAPSED));
json.writeNumberField("rollingCountEmit", commandMetrics.getRollingCount(HystrixRollingNumberEvent.EMIT));
json.writeNumberField("rollingCountExceptionsThrown", commandMetrics.getRollingCount(HystrixRollingNumberEvent.EXCEPTION_THROWN));
json.writeNumberField("rollingCountFailure", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FAILURE));
json.writeNumberField("rollingCountFallbackEmit", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_EMIT));
json.writeNumberField("rollingCountFallbackFailure", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_FAILURE));
json.writeNumberField("rollingCountFallbackMissing", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_MISSING));
json.writeNumberField("rollingCountFallbackRejection", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_REJECTION));
json.writeNumberField("rollingCountFallbackSuccess", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_SUCCESS));
json.writeNumberField("rollingCountResponsesFromCache", commandMetrics.getRollingCount(HystrixRollingNumberEvent.RESPONSE_FROM_CACHE));
json.writeNumberField("rollingCountSemaphoreRejected", commandMetrics.getRollingCount(HystrixRollingNumberEvent.SEMAPHORE_REJECTED));
json.writeNumberField("rollingCountShortCircuited", commandMetrics.getRollingCount(HystrixRollingNumberEvent.SHORT_CIRCUITED));
json.writeNumberField("rollingCountSuccess", commandMetrics.getRollingCount(HystrixRollingNumberEvent.SUCCESS));
json.writeNumberField("rollingCountThreadPoolRejected", commandMetrics.getRollingCount(HystrixRollingNumberEvent.THREAD_POOL_REJECTED));
json.writeNumberField("rollingCountTimeout", commandMetrics.getRollingCount(HystrixRollingNumberEvent.TIMEOUT));
safelyWriteNumberField(json, "rollingCountBadRequests", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.BAD_REQUEST);
}
});
safelyWriteNumberField(json, "rollingCountCollapsedRequests", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.COLLAPSED);
}
});
safelyWriteNumberField(json, "rollingCountEmit", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.EMIT);
}
});
safelyWriteNumberField(json, "rollingCountExceptionsThrown", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.EXCEPTION_THROWN);
}
});
safelyWriteNumberField(json, "rollingCountFailure", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.FAILURE);
}
});
safelyWriteNumberField(json, "rollingCountFallbackEmit", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_EMIT);
}
});
safelyWriteNumberField(json, "rollingCountFallbackFailure", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_FAILURE);
}
});
safelyWriteNumberField(json, "rollingCountFallbackMissing", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_MISSING);
}
});
safelyWriteNumberField(json, "rollingCountFallbackRejection", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_REJECTION);
}
});
safelyWriteNumberField(json, "rollingCountFallbackSuccess", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_SUCCESS);
}
});
safelyWriteNumberField(json, "rollingCountResponsesFromCache", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.RESPONSE_FROM_CACHE);
}
});
safelyWriteNumberField(json, "rollingCountSemaphoreRejected", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.SEMAPHORE_REJECTED);
}
});
safelyWriteNumberField(json, "rollingCountShortCircuited", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.SHORT_CIRCUITED);
}
});
safelyWriteNumberField(json, "rollingCountSuccess", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.SUCCESS);
}
});
safelyWriteNumberField(json, "rollingCountThreadPoolRejected", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.THREAD_POOL_REJECTED);
}
});
safelyWriteNumberField(json, "rollingCountTimeout", new Func0<Long>() {
@Override
public Long call() {
return commandMetrics.getRollingCount(HystrixRollingNumberEvent.TIMEOUT);
}
});

json.writeNumberField("currentConcurrentExecutionCount", commandMetrics.getCurrentConcurrentExecutionCount());
json.writeNumberField("rollingMaxConcurrentExecutionCount", commandMetrics.getRollingMaxConcurrentExecutions());
Expand Down Expand Up @@ -157,7 +256,7 @@ static String toJson(HystrixCommandMetrics commandMetrics) throws IOException {
return jsonString.getBuffer().toString();
}

static String toJson(HystrixThreadPoolMetrics threadPoolMetrics) throws IOException {
static String toJson(final HystrixThreadPoolMetrics threadPoolMetrics) throws IOException {
HystrixThreadPoolKey key = threadPoolMetrics.getThreadPoolKey();

StringWriter jsonString = new StringWriter();
Expand All @@ -178,8 +277,12 @@ static String toJson(HystrixThreadPoolMetrics threadPoolMetrics) throws IOExcept
json.writeNumberField("currentTaskCount", threadPoolMetrics.getCurrentTaskCount().longValue());
json.writeNumberField("rollingCountThreadsExecuted", threadPoolMetrics.getRollingCountThreadsExecuted());
json.writeNumberField("rollingMaxActiveThreads", threadPoolMetrics.getRollingMaxActiveThreads());
json.writeNumberField("rollingCountCommandsRejected", threadPoolMetrics.getRollingCount(HystrixRollingNumberEvent.THREAD_POOL_REJECTED));

safelyWriteNumberField(json, "rollingCountCommandsRejected", new Func0<Long>() {
@Override
public Long call() {
return threadPoolMetrics.getRollingCount(HystrixRollingNumberEvent.THREAD_POOL_REJECTED);
}
});
json.writeNumberField("propertyValue_queueSizeRejectionThreshold", threadPoolMetrics.getProperties().queueSizeRejectionThreshold().get());
json.writeNumberField("propertyValue_metricsRollingStatisticalWindowInMilliseconds", threadPoolMetrics.getProperties().metricsRollingStatisticalWindowInMilliseconds().get());

Expand All @@ -191,7 +294,7 @@ static String toJson(HystrixThreadPoolMetrics threadPoolMetrics) throws IOExcept
return jsonString.getBuffer().toString();
}

static String toJson(HystrixCollapserMetrics collapserMetrics) throws IOException {
static String toJson(final HystrixCollapserMetrics collapserMetrics) throws IOException {
HystrixCollapserKey key = collapserMetrics.getCollapserKey();
StringWriter jsonString = new StringWriter();
JsonGenerator json = jsonFactory.createJsonGenerator(jsonString);
Expand All @@ -201,9 +304,24 @@ static String toJson(HystrixCollapserMetrics collapserMetrics) throws IOExceptio
json.writeStringField("name", key.name());
json.writeNumberField("currentTime", System.currentTimeMillis());

json.writeNumberField("rollingCountRequestsBatched", collapserMetrics.getRollingCount(HystrixRollingNumberEvent.COLLAPSER_REQUEST_BATCHED));
json.writeNumberField("rollingCountBatches", collapserMetrics.getRollingCount(HystrixRollingNumberEvent.COLLAPSER_BATCH));
json.writeNumberField("rollingCountResponsesFromCache", collapserMetrics.getRollingCount(HystrixRollingNumberEvent.RESPONSE_FROM_CACHE));
safelyWriteNumberField(json, "rollingCountRequestsBatched", new Func0<Long>() {
@Override
public Long call() {
return collapserMetrics.getRollingCount(HystrixRollingNumberEvent.COLLAPSER_REQUEST_BATCHED);
}
});
safelyWriteNumberField(json, "rollingCountBatches", new Func0<Long>() {
@Override
public Long call() {
return collapserMetrics.getRollingCount(HystrixRollingNumberEvent.COLLAPSER_BATCH);
}
});
safelyWriteNumberField(json, "rollingCountResponsesFromCache", new Func0<Long>() {
@Override
public Long call() {
return collapserMetrics.getRollingCount(HystrixRollingNumberEvent.RESPONSE_FROM_CACHE);
}
});

// batch size percentiles
json.writeNumberField("batchSize_mean", collapserMetrics.getBatchSizeMean());
Expand Down

0 comments on commit c397273

Please sign in to comment.