Search in sources :

Example 1 with AlertEvent

use of org.apache.druid.java.util.emitter.service.AlertEvent in project druid by druid-io.

the class KafkaEmitterTest method testKafkaEmitter.

// there is 1 seconds wait in kafka emitter before it starts sending events to broker, set a timeout for 5 seconds
@Test(timeout = 5_000)
public void testKafkaEmitter() throws InterruptedException {
    final List<ServiceMetricEvent> serviceMetricEvents = ImmutableList.of(ServiceMetricEvent.builder().build("m1", 1).build("service", "host"));
    final List<AlertEvent> alertEvents = ImmutableList.of(new AlertEvent("service", "host", "description"));
    final List<RequestLogEvent> requestLogEvents = ImmutableList.of(DefaultRequestLogEventBuilderFactory.instance().createRequestLogEventBuilder("requests", RequestLogLine.forSql("", null, DateTimes.nowUtc(), null, new QueryStats(ImmutableMap.of()))).build("service", "host"));
    int totalEvents = serviceMetricEvents.size() + alertEvents.size() + requestLogEvents.size();
    int totalEventsExcludingRequestLogEvents = totalEvents - requestLogEvents.size();
    final CountDownLatch countDownSentEvents = new CountDownLatch(requestTopic == null ? totalEventsExcludingRequestLogEvents : totalEvents);
    final KafkaProducer<String, String> producer = mock(KafkaProducer.class);
    final KafkaEmitter kafkaEmitter = new KafkaEmitter(new KafkaEmitterConfig("", "metrics", "alerts", requestTopic, "test-cluster", null), new ObjectMapper()) {

        @Override
        protected Producer<String, String> setKafkaProducer() {
            // override send interval to 1 second
            sendInterval = 1;
            return producer;
        }
    };
    when(producer.send(any(), any())).then((invocation) -> {
        countDownSentEvents.countDown();
        return null;
    });
    kafkaEmitter.start();
    for (Event event : serviceMetricEvents) {
        kafkaEmitter.emit(event);
    }
    for (Event event : alertEvents) {
        kafkaEmitter.emit(event);
    }
    for (Event event : requestLogEvents) {
        kafkaEmitter.emit(event);
    }
    countDownSentEvents.await();
    Assert.assertEquals(0, kafkaEmitter.getMetricLostCount());
    Assert.assertEquals(0, kafkaEmitter.getAlertLostCount());
    Assert.assertEquals(requestTopic == null ? requestLogEvents.size() : 0, kafkaEmitter.getRequestLostCount());
    Assert.assertEquals(0, kafkaEmitter.getInvalidLostCount());
}
Also used : AlertEvent(org.apache.druid.java.util.emitter.service.AlertEvent) CountDownLatch(java.util.concurrent.CountDownLatch) QueryStats(org.apache.druid.server.QueryStats) RequestLogEvent(org.apache.druid.server.log.RequestLogEvent) AlertEvent(org.apache.druid.java.util.emitter.service.AlertEvent) RequestLogEvent(org.apache.druid.server.log.RequestLogEvent) ServiceMetricEvent(org.apache.druid.java.util.emitter.service.ServiceMetricEvent) Event(org.apache.druid.java.util.emitter.core.Event) ServiceMetricEvent(org.apache.druid.java.util.emitter.service.ServiceMetricEvent) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with AlertEvent

use of org.apache.druid.java.util.emitter.service.AlertEvent in project druid by druid-io.

the class DropwizardEmitter method emit.

@Override
public void emit(Event event) {
    synchronized (started) {
        if (!started.get()) {
            throw new RejectedExecutionException("Dropwizard emitter Service not started.");
        }
    }
    if (event instanceof ServiceMetricEvent) {
        ServiceMetricEvent metricEvent = (ServiceMetricEvent) event;
        String host = metricEvent.getHost();
        String service = metricEvent.getService();
        String metric = metricEvent.getMetric();
        Map<String, Object> userDims = metricEvent.getUserDims();
        Number value = metricEvent.getValue();
        ImmutableList.Builder<String> nameBuilder = new ImmutableList.Builder<>();
        LinkedHashMap<String, String> dims = new LinkedHashMap<>();
        final DropwizardMetricSpec metricSpec = converter.addFilteredUserDims(service, metric, userDims, dims);
        if (metricSpec != null) {
            if (config.getPrefix() != null) {
                nameBuilder.add(config.getPrefix());
            }
            nameBuilder.add(StringUtils.format("metric=%s", metric));
            nameBuilder.add(StringUtils.format("service=%s", service));
            if (config.getIncludeHost()) {
                nameBuilder.add(StringUtils.format("hostname=%s", host));
            }
            dims.forEach((key, value1) -> nameBuilder.add(StringUtils.format("%s=%s", key, value1)));
            String fullName = StringUtils.replaceChar(Joiner.on(",").join(nameBuilder.build()), '/', ".");
            updateMetric(fullName, value, metricSpec);
        } else {
            log.debug("Service=[%s], Metric=[%s] has no mapping", service, metric);
        }
    } else if (event instanceof AlertEvent) {
        for (Emitter emitter : alertEmitters) {
            emitter.emit(event);
        }
    } else {
        throw new ISE("unknown event type [%s]", event.getClass());
    }
}
Also used : Emitter(org.apache.druid.java.util.emitter.core.Emitter) ImmutableList(com.google.common.collect.ImmutableList) AlertEvent(org.apache.druid.java.util.emitter.service.AlertEvent) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) LinkedHashMap(java.util.LinkedHashMap) ServiceMetricEvent(org.apache.druid.java.util.emitter.service.ServiceMetricEvent) ISE(org.apache.druid.java.util.common.ISE)

Aggregations

AlertEvent (org.apache.druid.java.util.emitter.service.AlertEvent)2 ServiceMetricEvent (org.apache.druid.java.util.emitter.service.ServiceMetricEvent)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableList (com.google.common.collect.ImmutableList)1 LinkedHashMap (java.util.LinkedHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 ISE (org.apache.druid.java.util.common.ISE)1 Emitter (org.apache.druid.java.util.emitter.core.Emitter)1 Event (org.apache.druid.java.util.emitter.core.Event)1 QueryStats (org.apache.druid.server.QueryStats)1 RequestLogEvent (org.apache.druid.server.log.RequestLogEvent)1 Test (org.junit.Test)1