@Test public void testOnTrigger() throws InitializationException { final String metricsUrl = "http: final String applicationId = "nifi"; final String hostName = "localhost"; final String jobName = "nifi_reporting_job"; final boolean jvmMetrics = true; final boolean authentication = false; final Client client = Mockito.mock(Client.class); final WebTarget target = Mockito.mock(WebTarget.class); final Invocation.Builder builder = Mockito.mock(Invocation.Builder.class); final Response response = Mockito.mock(Response.class); Mockito.when(response.getStatus()).thenReturn(200); Mockito.when(client.target(metricsUrl)).thenReturn(target); Mockito.when(target.request()).thenReturn(builder); Mockito.when(builder.post(Matchers.any(Entity.class))).thenReturn(response); final ComponentLog logger = Mockito.mock(ComponentLog.class); final ReportingInitializationContext initContext = Mockito.mock(ReportingInitializationContext.class); Mockito.when(initContext.getIdentifier()).thenReturn(UUID.randomUUID().toString()); Mockito.when(initContext.getLogger()).thenReturn(logger); final ReportingContext context = Mockito.mock(ReportingContext.class); Mockito.when(context.getProperty(PrometheusReportingTask.METRICS_COLLECTOR_URL)) .thenReturn(new MockPropertyValue(metricsUrl)); Mockito.when(context.getProperty(PrometheusReportingTask.APPLICATION_ID)) .thenReturn(new MockPropertyValue(applicationId)); Mockito.when(context.getProperty(PrometheusReportingTask.INSTANCE_ID)) .thenReturn(new MockPropertyValue(hostName)); Mockito.when(context.getProperty(PrometheusReportingTask.PROCESS_GROUP_IDS)) .thenReturn(new MockPropertyValue("1234")); Mockito.when(context.getProperty(PrometheusReportingTask.JOB_NAME)) .thenReturn(new MockPropertyValue(jobName)); Mockito.when(context.getProperty(PrometheusReportingTask.SEND_JVM_METRICS)) .thenReturn(new MockPropertyValue(Boolean.toString(jvmMetrics))); Mockito.when(context.getProperty(PrometheusReportingTask.USE_AUTHENTICATION)) .thenReturn(new MockPropertyValue(Boolean.toString(authentication))); final EventAccess eventAccess = Mockito.mock(EventAccess.class); Mockito.when(context.getEventAccess()).thenReturn(eventAccess); Mockito.when(eventAccess.getControllerStatus()).thenReturn(status); Mockito.when(eventAccess.getGroupStatus("1234")).thenReturn(status); final PrometheusReportingTask task = new PrometheusReportingTask(); task.initialize(initContext); task.onTrigger(context); }
@Override public void onTrigger(final ReportingContext context) { final String metricsCollectorUrl = context.getProperty(METRICS_COLLECTOR_URL) .evaluateAttributeExpressions().getValue() .replace("http: final String applicationId = context.getProperty(APPLICATION_ID).evaluateAttributeExpressions().getValue(); final String jobName = context.getProperty(JOB_NAME).getValue(); final String instance = context.getProperty(INSTANCE_ID).evaluateAttributeExpressions().getValue(); final Map<String,String> groupingKey = Collections.singletonMap("instance", instance); final PushGateway pushGateway = new PushGateway(metricsCollectorUrl); if(context.getProperty(USE_AUTHENTICATION).asBoolean()){ final String username = context.getProperty(AUTH_USERNAME).getValue(); final String password = context.getProperty(AUTH_PASSWORD).getValue(); pushGateway.setConnectionFactory(new BasicAuthHttpConnectionFactory(username, password)); } try { if (context.getProperty(SEND_JVM_METRICS).asBoolean()) { pushGateway.pushAdd(PrometheusMetricsFactory.createJvmMetrics(VirtualMachineMetrics.getInstance()), JVM_JOB_NAME, groupingKey); } } catch (IOException e) { getLogger().error("Failed pushing JVM-metrics to Prometheus PushGateway due to {}; routing to failure", e); } for (ProcessGroupStatus status : searchProcessGroups(context, context.getProperty(PROCESS_GROUP_IDS))) { try { pushGateway.pushAdd(PrometheusMetricsFactory.createNifiMetrics(status, applicationId), jobName, groupingKey); } catch (IOException e) { getLogger().error("Failed pushing Nifi-metrics to Prometheus PushGateway due to {}; routing to failure", e); } } }
PrometheusReportingTask extends AbstractReportingTask { @Override public void onTrigger(final ReportingContext context) { final String metricsCollectorUrl = context.getProperty(METRICS_COLLECTOR_URL) .evaluateAttributeExpressions().getValue() .replace("http: final String applicationId = context.getProperty(APPLICATION_ID).evaluateAttributeExpressions().getValue(); final String jobName = context.getProperty(JOB_NAME).getValue(); final String instance = context.getProperty(INSTANCE_ID).evaluateAttributeExpressions().getValue(); final Map<String,String> groupingKey = Collections.singletonMap("instance", instance); final PushGateway pushGateway = new PushGateway(metricsCollectorUrl); if(context.getProperty(USE_AUTHENTICATION).asBoolean()){ final String username = context.getProperty(AUTH_USERNAME).getValue(); final String password = context.getProperty(AUTH_PASSWORD).getValue(); pushGateway.setConnectionFactory(new BasicAuthHttpConnectionFactory(username, password)); } try { if (context.getProperty(SEND_JVM_METRICS).asBoolean()) { pushGateway.pushAdd(PrometheusMetricsFactory.createJvmMetrics(VirtualMachineMetrics.getInstance()), JVM_JOB_NAME, groupingKey); } } catch (IOException e) { getLogger().error("Failed pushing JVM-metrics to Prometheus PushGateway due to {}; routing to failure", e); } for (ProcessGroupStatus status : searchProcessGroups(context, context.getProperty(PROCESS_GROUP_IDS))) { try { pushGateway.pushAdd(PrometheusMetricsFactory.createNifiMetrics(status, applicationId), jobName, groupingKey); } catch (IOException e) { getLogger().error("Failed pushing Nifi-metrics to Prometheus PushGateway due to {}; routing to failure", e); } } } }
PrometheusReportingTask extends AbstractReportingTask { @Override public void onTrigger(final ReportingContext context) { final String metricsCollectorUrl = context.getProperty(METRICS_COLLECTOR_URL) .evaluateAttributeExpressions().getValue() .replace("http: final String applicationId = context.getProperty(APPLICATION_ID).evaluateAttributeExpressions().getValue(); final String jobName = context.getProperty(JOB_NAME).getValue(); final String instance = context.getProperty(INSTANCE_ID).evaluateAttributeExpressions().getValue(); final Map<String,String> groupingKey = Collections.singletonMap("instance", instance); final PushGateway pushGateway = new PushGateway(metricsCollectorUrl); if(context.getProperty(USE_AUTHENTICATION).asBoolean()){ final String username = context.getProperty(AUTH_USERNAME).getValue(); final String password = context.getProperty(AUTH_PASSWORD).getValue(); pushGateway.setConnectionFactory(new BasicAuthHttpConnectionFactory(username, password)); } try { if (context.getProperty(SEND_JVM_METRICS).asBoolean()) { pushGateway.pushAdd(PrometheusMetricsFactory.createJvmMetrics(VirtualMachineMetrics.getInstance()), JVM_JOB_NAME, groupingKey); } } catch (IOException e) { getLogger().error("Failed pushing JVM-metrics to Prometheus PushGateway due to {}; routing to failure", e); } for (ProcessGroupStatus status : searchProcessGroups(context, context.getProperty(PROCESS_GROUP_IDS))) { try { pushGateway.pushAdd(PrometheusMetricsFactory.createNifiMetrics(status, applicationId), jobName, groupingKey); } catch (IOException e) { getLogger().error("Failed pushing Nifi-metrics to Prometheus PushGateway due to {}; routing to failure", e); } } }  }
PrometheusReportingTask extends AbstractReportingTask { @Override public void onTrigger(final ReportingContext context) { final String metricsCollectorUrl = context.getProperty(METRICS_COLLECTOR_URL) .evaluateAttributeExpressions().getValue() .replace("http: final String applicationId = context.getProperty(APPLICATION_ID).evaluateAttributeExpressions().getValue(); final String jobName = context.getProperty(JOB_NAME).getValue(); final String instance = context.getProperty(INSTANCE_ID).evaluateAttributeExpressions().getValue(); final Map<String,String> groupingKey = Collections.singletonMap("instance", instance); final PushGateway pushGateway = new PushGateway(metricsCollectorUrl); if(context.getProperty(USE_AUTHENTICATION).asBoolean()){ final String username = context.getProperty(AUTH_USERNAME).getValue(); final String password = context.getProperty(AUTH_PASSWORD).getValue(); pushGateway.setConnectionFactory(new BasicAuthHttpConnectionFactory(username, password)); } try { if (context.getProperty(SEND_JVM_METRICS).asBoolean()) { pushGateway.pushAdd(PrometheusMetricsFactory.createJvmMetrics(VirtualMachineMetrics.getInstance()), JVM_JOB_NAME, groupingKey); } } catch (IOException e) { getLogger().error("Failed pushing JVM-metrics to Prometheus PushGateway due to {}; routing to failure", e); } for (ProcessGroupStatus status : searchProcessGroups(context, context.getProperty(PROCESS_GROUP_IDS))) { try { pushGateway.pushAdd(PrometheusMetricsFactory.createNifiMetrics(status, applicationId), jobName, groupingKey); } catch (IOException e) { getLogger().error("Failed pushing Nifi-metrics to Prometheus PushGateway due to {}; routing to failure", e); } } }  @Override void onTrigger(final ReportingContext context); }
PrometheusReportingTask extends AbstractReportingTask { @Override public void onTrigger(final ReportingContext context) { final String metricsCollectorUrl = context.getProperty(METRICS_COLLECTOR_URL) .evaluateAttributeExpressions().getValue() .replace("http: final String applicationId = context.getProperty(APPLICATION_ID).evaluateAttributeExpressions().getValue(); final String jobName = context.getProperty(JOB_NAME).getValue(); final String instance = context.getProperty(INSTANCE_ID).evaluateAttributeExpressions().getValue(); final Map<String,String> groupingKey = Collections.singletonMap("instance", instance); final PushGateway pushGateway = new PushGateway(metricsCollectorUrl); if(context.getProperty(USE_AUTHENTICATION).asBoolean()){ final String username = context.getProperty(AUTH_USERNAME).getValue(); final String password = context.getProperty(AUTH_PASSWORD).getValue(); pushGateway.setConnectionFactory(new BasicAuthHttpConnectionFactory(username, password)); } try { if (context.getProperty(SEND_JVM_METRICS).asBoolean()) { pushGateway.pushAdd(PrometheusMetricsFactory.createJvmMetrics(VirtualMachineMetrics.getInstance()), JVM_JOB_NAME, groupingKey); } } catch (IOException e) { getLogger().error("Failed pushing JVM-metrics to Prometheus PushGateway due to {}; routing to failure", e); } for (ProcessGroupStatus status : searchProcessGroups(context, context.getProperty(PROCESS_GROUP_IDS))) { try { pushGateway.pushAdd(PrometheusMetricsFactory.createNifiMetrics(status, applicationId), jobName, groupingKey); } catch (IOException e) { getLogger().error("Failed pushing Nifi-metrics to Prometheus PushGateway due to {}; routing to failure", e); } } }  @Override void onTrigger(final ReportingContext context);  }