
com.netflix.hystrix.contrib.sample.stream.HystrixUtilizationJsonStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hystrix-metrics-event-stream Show documentation
Show all versions of hystrix-metrics-event-stream Show documentation
hystrix-metrics-event-stream developed by Netflix
/**
* Copyright 2016 Netflix, Inc.
*
* 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
*
* http://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 com.netflix.hystrix.contrib.sample.stream;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixThreadPoolKey;
import com.netflix.hystrix.metric.sample.HystrixCommandUtilization;
import com.netflix.hystrix.metric.sample.HystrixThreadPoolUtilization;
import com.netflix.hystrix.metric.sample.HystrixUtilization;
import com.netflix.hystrix.metric.sample.HystrixUtilizationStream;
import rx.Observable;
import rx.functions.Func1;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;
/**
* Links HystrixUtilizationStream and JSON encoding. This may be consumed in a variety of ways:
* -such as-
*
* - {@link HystrixUtilizationSseServlet} for mapping a specific URL to this data as an SSE stream
*
- Consumer of your choice that wants control over where to embed this stream
*
*
*/
public class HystrixUtilizationJsonStream {
private final Func1> streamGenerator;
private static final JsonFactory jsonFactory = new JsonFactory();
private static final Func1 convertToJsonFunc = new Func1() {
@Override
public String call(HystrixUtilization utilization) {
try {
return convertToJson(utilization);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
};
public HystrixUtilizationJsonStream() {
this.streamGenerator = new Func1>() {
@Override
public Observable call(Integer delay) {
return new HystrixUtilizationStream(delay).observe();
}
};
}
public HystrixUtilizationJsonStream(Func1> streamGenerator) {
this.streamGenerator = streamGenerator;
}
private static void writeCommandUtilizationJson(JsonGenerator json, HystrixCommandKey key, HystrixCommandUtilization utilization) throws IOException {
json.writeObjectFieldStart(key.name());
json.writeNumberField("activeCount", utilization.getConcurrentCommandCount());
json.writeEndObject();
}
private static void writeThreadPoolUtilizationJson(JsonGenerator json, HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolUtilization utilization) throws IOException {
json.writeObjectFieldStart(threadPoolKey.name());
json.writeNumberField("activeCount", utilization.getCurrentActiveCount());
json.writeNumberField("queueSize", utilization.getCurrentQueueSize());
json.writeNumberField("corePoolSize", utilization.getCurrentCorePoolSize());
json.writeNumberField("poolSize", utilization.getCurrentPoolSize());
json.writeEndObject();
}
protected static String convertToJson(HystrixUtilization utilization) throws IOException {
StringWriter jsonString = new StringWriter();
JsonGenerator json = jsonFactory.createGenerator(jsonString);
json.writeStartObject();
json.writeStringField("type", "HystrixUtilization");
json.writeObjectFieldStart("commands");
for (Map.Entry entry: utilization.getCommandUtilizationMap().entrySet()) {
final HystrixCommandKey key = entry.getKey();
final HystrixCommandUtilization commandUtilization = entry.getValue();
writeCommandUtilizationJson(json, key, commandUtilization);
}
json.writeEndObject();
json.writeObjectFieldStart("threadpools");
for (Map.Entry entry: utilization.getThreadPoolUtilizationMap().entrySet()) {
final HystrixThreadPoolKey threadPoolKey = entry.getKey();
final HystrixThreadPoolUtilization threadPoolUtilization = entry.getValue();
writeThreadPoolUtilizationJson(json, threadPoolKey, threadPoolUtilization);
}
json.writeEndObject();
json.writeEndObject();
json.close();
return jsonString.getBuffer().toString();
}
public Observable observe(int delay) {
return streamGenerator.call(delay);
}
public Observable observeJson(int delay) {
return streamGenerator.call(delay).map(convertToJsonFunc);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy