Java Code Examples for com.netflix.servo.monitor.DynamicCounter#increment()

The following examples show how to use com.netflix.servo.monitor.DynamicCounter#increment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetricObserverManualTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenMetrics_whenRegisterDynamically_thenMonitored() throws Exception {
    for (int i = 0; i < 2; i++) {
        DynamicCounter.increment("monitor-name", "tag-key", "tag-value");
        SECONDS.sleep(1);
    }

    List<List<Metric>> metrics = observer.getObservations();
    assertThat(metrics, hasSize(greaterThanOrEqualTo(2)));

    Iterator<List<Metric>> metricIterator = metrics.iterator();
    //skip first empty observation
    metricIterator.next();
    while (metricIterator.hasNext()) {
        assertThat(metricIterator.next(), hasItem(hasProperty("value", greaterThanOrEqualTo(1.0))));
    }
}
 
Example 2
Source File: LocalFileSink.java    From suro with Apache License 2.0 6 votes vote down vote up
/**
 * Write all messages in msgList to file writer, sync the file,
 * commit the queue and clear messages
 *
 * @param msgList
 * @throws java.io.IOException
 */
@Override
protected void write(List<Message> msgList) throws IOException {
    for (Message msg : msgList) {
        writer.writeTo(msg);

        String routingKey = normalizeRoutingKey(msg);

        DynamicCounter.increment(
                MonitorConfig.builder("writtenMessages")
                        .withTag(TagKey.DATA_SOURCE, routingKey)
                        .build());
        ++writtenMessages;
        DynamicCounter.increment(
                MonitorConfig.builder("writtenBytes")
                        .withTag(TagKey.DATA_SOURCE, routingKey)
                        .build(), msg.getPayload().length);
        writtenBytes += msg.getPayload().length;

        messageWrittenInRotation = true;
    }

    writer.sync();

    throughput.increment(msgList.size());
}
 
Example 3
Source File: SyncSuroClient.java    From suro with Apache License 2.0 6 votes vote down vote up
public static int incrementMessageCount(String counterName, String app, Iterable<Message> messages, List<AsyncSuroClient.Listener> listeners) {
    int count = 0;
    for (Message message : messages) {
        DynamicCounter.increment(
                MonitorConfig.builder(counterName)
                        .withTag(TagKey.APP, app)
                        .withTag(TagKey.DATA_SOURCE, message.getRoutingKey())
                        .build());
        ++count;
    }

    for (AsyncSuroClient.Listener listener : listeners) {
        listener.sentCallback(count);
    }

    return count;
}
 
Example 4
Source File: CloudTrail.java    From suro with Apache License 2.0 6 votes vote down vote up
@Override
public List<MessageContainer> parse(String data) {
    List<MessageContainer> messages = new ArrayList<MessageContainer>();

    try {
        Map<String, Object> blob = jsonMapper.readValue(data, S3Consumer.typeReference);
        List<Map<String, Object>> records = (List<Map<String, Object>>) blob.get("Records");
        for (Map<String, Object> record : records) {
            messages.add(new DefaultMessageContainer(
                    new Message(routingKey, jsonMapper.writeValueAsBytes(record)),
                    jsonMapper));
        }
    } catch (Exception e) {
        log.error("Exception on parsing: " + e.getMessage(), e);
        DynamicCounter.increment(
                MonitorConfig.builder("recordParseError").withTag("parserType", TYPE).build());
    }

    return messages;
}
 
Example 5
Source File: KafkaSink.java    From suro with Apache License 2.0 6 votes vote down vote up
@Override
public void writeTo(final MessageContainer message) {
    queuedRecords.incrementAndGet();
    DynamicCounter.increment(
        MonitorConfig
            .builder("queuedRecord")
            .withTag(TagKey.ROUTING_KEY, message.getRoutingKey())
            .build());
    runRecordCounterListener();

    if (metadataFetchedTopicSet.contains(message.getRoutingKey())) {
        sendMessage(message);
    } else {
        if(!metadataWaitingQueue.offer(message)) {
            dropMessage(message.getRoutingKey(), "metadataWaitingQueueFull");
        }
    }
}
 
Example 6
Source File: MessageSetProcessor.java    From suro with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void processMessageSet(TMessageSet tMessageSet) {
    MessageSetReader reader = new MessageSetReader(tMessageSet);

    for (final Message message : reader) {
        try {
            router.process(input, new DefaultMessageContainer(message, jsonMapper));
        } catch (Exception e) {
            DynamicCounter.increment(messageProcessErrorMetrics,
                TagKey.APP, tMessageSet.getApp(),
                TagKey.DATA_SOURCE, message.getRoutingKey());

            log.error(String.format("Failed to process message %s: %s", message, e.getMessage()), e);
        }
    }
}
 
Example 7
Source File: NFHttpMethodRetryHandler.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Override
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "ICAST_INTEGER_MULTIPLY_CAST_TO_LONG")
public boolean retryRequest(
		final IOException exception, 
		int executionCount, 
		HttpContext context
		) {
	if (super.retryRequest(exception, executionCount, context)) {
		HttpRequest request = (HttpRequest)
				context.getAttribute(ExecutionContext.HTTP_REQUEST);
		String methodName = request.getRequestLine().getMethod();
		String path = "UNKNOWN_PATH";
		if(request instanceof HttpUriRequest) {
			HttpUriRequest uriReq = (HttpUriRequest) request;
			path = uriReq.getURI().toString();
		}
		try {
			Thread.sleep(executionCount * this.sleepTimeFactorMs);
		}
		catch (InterruptedException e) {
			logger.warn("Interrupted while sleep before retrying http method " + methodName + " " + path, e);
		}
		DynamicCounter.increment(RETRY_COUNTER + methodName + ":" + path);
		return true;
	}
	return false;
}
 
Example 8
Source File: LocalFileSink.java    From suro with Apache License 2.0 5 votes vote down vote up
private String normalizeRoutingKey(Message msg) {
    String routingKey = msg.getRoutingKey();
    if(routingKey == null || routingKey.trim().length() == 0) {
        emptyRoutingKeyCount += 1;
        DynamicCounter.increment("emptyRoutingKeyCount");
        if(log.isDebugEnabled()) {
            log.debug("Message {} with empty routing key", Arrays.asList(msg.getPayload()));
        }
        return EMPTY_ROUTING_KEY_REPLACEMENT;
    }

    return routingKey;
}
 
Example 9
Source File: AsyncSuroClient.java    From suro with Apache License 2.0 5 votes vote down vote up
@Override
public void send(Message message) {
    if (!messageQueue.offer(message)) {
        lostMessages.incrementAndGet();
        DynamicCounter.increment(
                MonitorConfig.builder(TagKey.LOST_COUNT)
                        .withTag(TagKey.APP, config.getApp())
                        .withTag(TagKey.DATA_SOURCE, message.getRoutingKey())
                        .build());
        for (Listener listener : listeners) {
            listener.lostCallback(1);
        }
    }
}
 
Example 10
Source File: AsyncSuroClient.java    From suro with Apache License 2.0 5 votes vote down vote up
public void restore(Message message) {
    restoredMessages.incrementAndGet();
    DynamicCounter.increment(
            MonitorConfig.builder(TagKey.RESTORED_COUNT)
                    .withTag(TagKey.APP, config.getApp())
                    .withTag(TagKey.DATA_SOURCE, message.getRoutingKey())
                    .build());
    for (Listener listener : listeners) {
        listener.restoredCallback();
    }
    send(message);
}
 
Example 11
Source File: KafkaSink.java    From suro with Apache License 2.0 5 votes vote down vote up
private void dropMessage(final String routingKey, final String reason) {
    DynamicCounter.increment(
        MonitorConfig
            .builder("droppedRecord")
            .withTag(TagKey.ROUTING_KEY, routingKey)
            .withTag(TagKey.DROPPED_REASON, reason)
            .build());
    droppedRecords.incrementAndGet();
    runRecordCounterListener();
}
 
Example 12
Source File: QueuedSink.java    From suro with Apache License 2.0 5 votes vote down vote up
protected void enqueue(Message message) {
    if (!queue4Sink.offer(message)) {
        droppedMessagesCount.incrementAndGet();
        DynamicCounter.increment(
                MonitorConfig.builder(TagKey.DROPPED_COUNT)
                        .withTag("reason", "queueFull")
                        .withTag("sink", sinkId)
                        .build());
    }
}
 
Example 13
Source File: FilterProcessor.java    From s2g-zuul with MIT License 4 votes vote down vote up
@Override
public void notify(ZuulFilter filter, ExecutionStatus status) {
    DynamicCounter.increment(METRIC_PREFIX + filter.getClass().getSimpleName(), "status", status.name(), "filtertype", filter.filterType());
}
 
Example 14
Source File: LocalFileSink.java    From suro with Apache License 2.0 4 votes vote down vote up
/**
 * List all files under the directory. If the file is marked as done, the
 * notice for that file would be sent. Otherwise, it checks the file
 * is not closed properly, the file is marked as done and the notice
 * would be sent. That file would cause EOFException when reading.
 *
 * @param dir
 * @return the number of files found in the directory
 */
public int cleanUp(String dir, boolean fetchAll) {
    if (!dir.endsWith("/")) {
        dir += "/";
    }

    int count = 0;

    try {
        FileSystem fs = writer.getFS();
        FileStatus[] files = fs.listStatus(new Path(dir));
        for (FileStatus file: files) {
            if (file.getLen() > 0) {
                String fileName = file.getPath().getName();
                String fileExt = getFileExt(fileName);
                if (fileExt != null && fileExt.equals(done)) {
                    notice.send(dir + fileName);
                    ++count;
                } else if (fileExt != null) {
                    long lastPeriod =
                            new DateTime().minus(rotationPeriod).minus(rotationPeriod).getMillis();
                    if (file.getModificationTime() < lastPeriod) {
                        ++errorClosedFiles;
                        DynamicCounter.increment("closedFileError");
                        log.error(dir + fileName + " is not closed properly!!!");
                        String doneFile = fileName.replace(fileExt, done);
                        writer.setDone(dir + fileName, dir + doneFile);
                        notice.send(dir + doneFile);
                        ++count;
                    } else if (fetchAll) {
                        ++count;
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error("Exception while on cleanUp: " + e.getMessage(), e);
        return Integer.MAX_VALUE; // return non-zero value
    }

    return count;
}
 
Example 15
Source File: MessageSetProcessor.java    From suro with Apache License 2.0 4 votes vote down vote up
@Override
public Result process(TMessageSet messageSet) throws TException {
    Result result = new Result();
    try {
        // Stop adding chunks if it's no running
        if ( !isRunning) {
            DynamicCounter.increment(rejectedMessageCountMetrics,
                TagKey.APP, messageSet.getApp(),
                TagKey.REJECTED_REASON, "SURO_STOPPED");

            log.warn("Message processor is not running. Message rejected");
            result.setMessage("Suro server stopped");
            result.setResultCode(ResultCode.STOPPED);
            return result;
        }

        if ( !isTakingTraffic ) {
            DynamicCounter.increment(rejectedMessageCountMetrics,
                TagKey.APP, messageSet.getApp(),
                TagKey.REJECTED_REASON, "SURO_THROTTLING");

            log.warn("Suro is not taking traffic. Message rejected. ");
            result.setMessage("Suro server is not taking traffic");
            result.setResultCode(ResultCode.OTHER_ERROR);
            return result;
        }

        MessageSetReader reader = new MessageSetReader(messageSet);
        if (!reader.checkCRC()) {
            DynamicCounter.increment(dataCorruptionCountMetrics, TagKey.APP, messageSet.getApp());

            result.setMessage("data corrupted");
            result.setResultCode(ResultCode.CRC_CORRUPTED);
            return result;
        }

        if (queue.offer(messageSet)) {
            DynamicCounter.increment(
                MonitorConfig.builder(messageCountMetrics)
                    .withTag(TagKey.APP, messageSet.getApp())
                    .build(),
                messageSet.getNumMessages());

            result.setMessage(Long.toString(messageSet.getCrc()));
            result.setResultCode(ResultCode.OK);
        } else {
            DynamicCounter.increment(retryCountMetrics, TagKey.APP, messageSet.getApp());

            result.setMessage(Long.toString(messageSet.getCrc()));
            result.setResultCode(ResultCode.QUEUE_FULL);
        }

        return result;
    } catch (Exception e) {
        log.error("Exception when processing message set " + e.getMessage(), e);
    }

    return result;
}
 
Example 16
Source File: MessageRouter.java    From suro with Apache License 2.0 4 votes vote down vote up
public void process(SuroInput input, MessageContainer msg) throws Exception {
    if (Strings.isNullOrEmpty(msg.getRoutingKey())) {
        DynamicCounter.increment(
                MonitorConfig.builder(TagKey.DROPPED_COUNT).withTag("reason", "emptyRoutingKey").build());
        return; // discard message
    }

    DynamicCounter.increment(
        MonitorConfig
            .builder(TagKey.RECV_COUNT)
            .withTag("routingKey", msg.getRoutingKey())
            .build());

    RoutingMap.RoutingInfo info = routingMap.getRoutingInfo(msg.getRoutingKey());

    if (info == null) {
        Sink defaultSink = sinkManager.getSink("default");
        input.setPause(defaultSink.checkPause());
        defaultSink.writeTo(msg);
    } else if (info.doFilter(msg)) {
        List<Route> routes = info.getWhere();
        for (Route route : routes) {
            if (route.doFilter(msg)) {
                Sink sink = sinkManager.getSink(route.getSink());
                input.setPause(sink.checkPause());
                if (!Strings.isNullOrEmpty(route.getAlias())) {
                    sink.writeTo(
                            new DefaultMessageContainer(
                                    new Message(route.getAlias(), msg.getMessage().getPayload()), jsonMapper));
                } else {
                    sink.writeTo(msg);
                }

                DynamicCounter.increment(
                    MonitorConfig
                        .builder(TagKey.ATTEMPTED_COUNT)
                        .withTag("routingKey", msg.getRoutingKey())
                        .withTag("sinkId", route.getSink())
                        .build());
            }
        }
    }
}