Search in sources :

Example 1 with Measurements

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;
}
Also used : Measurements(io.mantisrx.common.metrics.measurement.Measurements) List(java.util.List) IOException(java.io.IOException) GaugeMeasurement(io.mantisrx.common.metrics.measurement.GaugeMeasurement) JsonProcessingException(io.mantisrx.shaded.com.fasterxml.jackson.core.JsonProcessingException) JsonProcessingException(io.mantisrx.shaded.com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 2 with Measurements

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();
}
Also used : Measurements(io.mantisrx.common.metrics.measurement.Measurements) ServerSentEvent(mantis.io.reactivex.netty.protocol.http.sse.ServerSentEvent) ByteBuf(io.netty.buffer.ByteBuf) Observable(rx.Observable) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Func1(rx.functions.Func1) Map(java.util.Map) JsonProcessingException(io.mantisrx.shaded.com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

Measurements (io.mantisrx.common.metrics.measurement.Measurements)2 JsonProcessingException (io.mantisrx.shaded.com.fasterxml.jackson.core.JsonProcessingException)2 List (java.util.List)2 GaugeMeasurement (io.mantisrx.common.metrics.measurement.GaugeMeasurement)1 ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 ServerSentEvent (mantis.io.reactivex.netty.protocol.http.sse.ServerSentEvent)1 Observable (rx.Observable)1 Func1 (rx.functions.Func1)1