use of io.mantisrx.common.metrics.measurement.Measurements in project mantis by Netflix.
the class JobMasterService method handleMetricEvent.
private Measurements handleMetricEvent(final String ev) {
try {
final Measurements measurements = objectMapper.readValue(ev, Measurements.class);
final String jobId = measurements.getTags().get(MetricStringConstants.MANTIS_JOB_ID);
final int workerIdx = Integer.parseInt(measurements.getTags().get(MetricStringConstants.MANTIS_WORKER_INDEX));
int stage = Integer.parseInt(measurements.getTags().get(MetricStringConstants.MANTIS_STAGE_NUM));
final int workerNum = Integer.parseInt(measurements.getTags().get(MetricStringConstants.MANTIS_WORKER_NUM));
List<GaugeMeasurement> gauges = (List<GaugeMeasurement>) measurements.getGauges();
// Metric is not from current job, it is from the source job
if (jobId != this.jobId) {
// Funnel source job metric into the 1st stage
stage = 1;
if (gauges.isEmpty()) {
gauges = measurements.getCounters().stream().map(counter -> new GaugeMeasurement(counter.getEvent(), counter.getCount())).collect(Collectors.toList());
}
}
metricObserver.onNext(new MetricData(jobId, stage, workerIdx, workerNum, measurements.getName(), gauges));
return measurements;
} catch (JsonProcessingException e) {
logger.error("failed to parse json", e);
} catch (IOException e) {
logger.error("failed to process json", e);
} catch (Exception e) {
logger.error("caught exception", e);
}
return null;
}
use of io.mantisrx.common.metrics.measurement.Measurements in project mantis by Netflix.
the class MetricsServer method start.
public void start() {
final Observable<Measurements> measurements = measurements(publishRateInSeconds);
logger.info("Starting metrics server on port: " + port);
server = RxNetty.createHttpServer(port, new RequestHandler<ByteBuf, ServerSentEvent>() {
@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ServerSentEvent> response) {
final Map<String, List<String>> queryParameters = request.getQueryParameters();
final List<String> namesToFilter = new LinkedList<>();
logger.info("got query params {}", queryParameters);
if (queryParameters != null && queryParameters.containsKey("name")) {
namesToFilter.addAll(queryParameters.get("name"));
}
Observable<Measurements> filteredObservable = measurements.filter(new Func1<Measurements, Boolean>() {
@Override
public Boolean call(Measurements measurements) {
if (!namesToFilter.isEmpty()) {
// check filters
for (String name : namesToFilter) {
if (name.indexOf('*') != -1) {
// check for ends with
if (name.indexOf('*') == 0 && measurements.getName().endsWith(name.substring(1)))
return true;
// check for starts with
if (name.indexOf('*') > 0 && measurements.getName().startsWith(name.substring(0, name.indexOf('*')))) {
return true;
}
}
if (measurements.getName().equals(name)) {
// filter match
return true;
}
}
// not found in filters
return false;
} else {
// no filters provided
return true;
}
}
});
return filteredObservable.flatMap(new Func1<Measurements, Observable<Void>>() {
@Override
public Observable<Void> call(Measurements metrics) {
response.getHeaders().set("Access-Control-Allow-Origin", "*");
response.getHeaders().set("content-type", "text/event-stream");
ServerSentEvent event = null;
try {
ByteBuf data = response.getAllocator().buffer().writeBytes((mapper.writeValueAsString(metrics)).getBytes());
event = new ServerSentEvent(data);
// event = new ServerSentEvent(null, "data", mapper.writeValueAsString(metrics));
} catch (JsonProcessingException e) {
logger.error("Failed to map metrics to JSON", e);
}
if (event != null) {
response.write(event);
return response.writeStringAndFlush("\n");
}
return null;
}
});
}
}, PipelineConfigurators.<ByteBuf>serveSseConfigurator()).start();
}
Aggregations