Search in sources :

Example 1 with MetricsBuilder

use of org.apache.nifi.reporting.ambari.api.MetricsBuilder in project nifi by apache.

the class AmbariReportingTask method onTrigger.

@Override
public void onTrigger(final ReportingContext context) {
    final String metricsCollectorUrl = context.getProperty(METRICS_COLLECTOR_URL).evaluateAttributeExpressions().getValue();
    final String applicationId = context.getProperty(APPLICATION_ID).evaluateAttributeExpressions().getValue();
    final String hostname = context.getProperty(HOSTNAME).evaluateAttributeExpressions().getValue();
    final boolean pgIdIsSet = context.getProperty(PROCESS_GROUP_ID).isSet();
    final String processGroupId = pgIdIsSet ? context.getProperty(PROCESS_GROUP_ID).evaluateAttributeExpressions().getValue() : null;
    final long start = System.currentTimeMillis();
    // send the metrics from last execution
    if (previousMetrics != null) {
        final WebTarget metricsTarget = client.target(metricsCollectorUrl);
        final Invocation.Builder invocation = metricsTarget.request();
        final Entity<String> entity = Entity.json(previousMetrics.toString());
        getLogger().debug("Sending metrics {} to Ambari", new Object[] { entity.getEntity() });
        final Response response = invocation.post(entity);
        if (response.getStatus() == Response.Status.OK.getStatusCode()) {
            final long completedMillis = TimeUnit.NANOSECONDS.toMillis(System.currentTimeMillis() - start);
            getLogger().info("Successfully sent metrics to Ambari in {} ms", new Object[] { completedMillis });
        } else {
            final String responseEntity = response.hasEntity() ? response.readEntity(String.class) : "unknown error";
            getLogger().error("Error sending metrics to Ambari due to {} - {}", new Object[] { response.getStatus(), responseEntity });
        }
    }
    // calculate the current metrics, but store them to be sent next time
    final ProcessGroupStatus status = processGroupId == null ? context.getEventAccess().getControllerStatus() : context.getEventAccess().getGroupStatus(processGroupId);
    if (status != null) {
        final Map<String, String> statusMetrics = metricsService.getMetrics(status, pgIdIsSet);
        final Map<String, String> jvmMetrics = metricsService.getMetrics(virtualMachineMetrics);
        final MetricsBuilder metricsBuilder = new MetricsBuilder(factory);
        final JsonObject metricsObject = metricsBuilder.applicationId(applicationId).instanceId(status.getId()).hostname(hostname).timestamp(start).addAllMetrics(statusMetrics).addAllMetrics(jvmMetrics).build();
        previousMetrics = metricsObject;
    } else {
        getLogger().error("No process group status with ID = {}", new Object[] { processGroupId });
        previousMetrics = null;
    }
}
Also used : Response(javax.ws.rs.core.Response) ProcessGroupStatus(org.apache.nifi.controller.status.ProcessGroupStatus) Invocation(javax.ws.rs.client.Invocation) JsonObject(javax.json.JsonObject) WebTarget(javax.ws.rs.client.WebTarget) MetricsBuilder(org.apache.nifi.reporting.ambari.api.MetricsBuilder)

Aggregations

JsonObject (javax.json.JsonObject)1 Invocation (javax.ws.rs.client.Invocation)1 WebTarget (javax.ws.rs.client.WebTarget)1 Response (javax.ws.rs.core.Response)1 ProcessGroupStatus (org.apache.nifi.controller.status.ProcessGroupStatus)1 MetricsBuilder (org.apache.nifi.reporting.ambari.api.MetricsBuilder)1