Search in sources :

Example 1 with RiemannClient

use of com.aphyr.riemann.client.RiemannClient in project nifi by apache.

the class PutRiemann method onScheduled.

@OnScheduled
public void onScheduled(ProcessContext context) throws ProcessException {
    if (batchSize == -1) {
        batchSize = context.getProperty(BATCH_SIZE).asInteger();
    }
    if (riemannClient == null || !riemannClient.isConnected()) {
        transport = Transport.valueOf(context.getProperty(TRANSPORT_PROTOCOL).getValue());
        String host = context.getProperty(RIEMANN_HOST).getValue().trim();
        int port = context.getProperty(RIEMANN_PORT).asInteger();
        writeTimeout = context.getProperty(TIMEOUT).asLong();
        RiemannClient client = null;
        try {
            switch(transport) {
                case TCP:
                    client = RiemannClient.tcp(host, port);
                    break;
                case UDP:
                    client = RiemannClient.udp(host, port);
                    break;
            }
            client.connect();
            riemannClient = client;
        } catch (IOException e) {
            if (client != null) {
                client.close();
            }
            context.yield();
            throw new ProcessException(String.format("Unable to connect to Riemann [%s:%d] (%s)\n%s", host, port, transport, e.getMessage()));
        }
    }
    if (customAttributes.size() == 0) {
        for (Map.Entry<PropertyDescriptor, String> property : context.getProperties().entrySet()) {
            // only custom defined properties
            if (!getSupportedPropertyDescriptors().contains(property.getKey())) {
                customAttributes.add(property.getKey());
            }
        }
    }
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) RiemannClient(com.aphyr.riemann.client.RiemannClient) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) IOException(java.io.IOException) Map(java.util.Map) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 2 with RiemannClient

use of com.aphyr.riemann.client.RiemannClient in project nifi by apache.

the class TestPutRiemann method getTestRunner.

private TestRunner getTestRunner(final boolean failOnWrite) {
    RiemannClient riemannClient = mock(RiemannClient.class);
    when(riemannClient.sendEvents(anyListOf(Proto.Event.class))).thenAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            List<Proto.Event> events = (List<Proto.Event>) invocationOnMock.getArguments()[0];
            for (Proto.Event event : events) {
                eventStream.add(event);
            }
            IPromise iPromise = mock(IPromise.class);
            if (!failOnWrite) {
                when(iPromise.deref(anyInt(), any(TimeUnit.class))).thenReturn(Proto.Msg.getDefaultInstance());
            } else {
                when(iPromise.deref(anyInt(), any(TimeUnit.class))).thenReturn(null);
            }
            return iPromise;
        }
    });
    when(riemannClient.isConnected()).thenReturn(true);
    PutRiemann riemannProcessor = new PutRiemann();
    riemannProcessor.riemannClient = riemannClient;
    riemannProcessor.transport = PutRiemann.Transport.TCP;
    TestRunner runner = TestRunners.newTestRunner(riemannProcessor);
    runner.setProperty(PutRiemann.RIEMANN_HOST, "localhost");
    runner.setProperty(PutRiemann.RIEMANN_PORT, "5555");
    runner.setProperty(PutRiemann.TRANSPORT_PROTOCOL, "TCP");
    runner.setProperty(PutRiemann.BATCH_SIZE, "100");
    runner.setProperty(PutRiemann.ATTR_SERVICE, "nifi-test-service");
    runner.setProperty(PutRiemann.ATTR_HOST, "${riemann.host}");
    runner.setProperty(PutRiemann.ATTR_TTL, "5");
    runner.setProperty(PutRiemann.ATTR_DESCRIPTION, "test");
    runner.setProperty(PutRiemann.ATTR_TAGS, "tag1, tag2, tag3");
    runner.setProperty(PutRiemann.ATTR_METRIC, "${riemann.metric}");
    runner.setProperty("custom-attribute-1", "${custom.attribute.1}");
    runner.setProperty("custom-attribute-2", "${custom.attribute.2}");
    runner.setProperty("custom-attribute-3", "${custom.attribute.3}");
    return runner;
}
Also used : Answer(org.mockito.stubbing.Answer) IPromise(com.aphyr.riemann.client.IPromise) RiemannClient(com.aphyr.riemann.client.RiemannClient) Proto(com.aphyr.riemann.Proto) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestRunner(org.apache.nifi.util.TestRunner) LinkedList(java.util.LinkedList) List(java.util.List)

Aggregations

RiemannClient (com.aphyr.riemann.client.RiemannClient)2 Proto (com.aphyr.riemann.Proto)1 IPromise (com.aphyr.riemann.client.IPromise)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 OnScheduled (org.apache.nifi.annotation.lifecycle.OnScheduled)1 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1 TestRunner (org.apache.nifi.util.TestRunner)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1