com.mulesoft.agent.monitoring.publisher.CloudwatchMonitorPublisher.java Source code

Java tutorial

Introduction

Here is the source code for com.mulesoft.agent.monitoring.publisher.CloudwatchMonitorPublisher.java

Source

/**
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 *
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

package com.mulesoft.agent.monitoring.publisher;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient;
import com.amazonaws.services.cloudwatch.model.MetricDatum;
import com.amazonaws.services.cloudwatch.model.PutMetricDataRequest;
import com.mulesoft.agent.buffer.BufferedHandler;
import com.mulesoft.agent.configuration.Configurable;
import com.mulesoft.agent.domain.monitoring.Metric;
import com.mulesoft.agent.services.OnOffSwitch;

import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * Handler that publishes JMX information obtained from the Monitoring Service to CloudWatch.
 * </p>
 */
@Named("cloudwatch.agent.monitor.publisher")
@Singleton
public class CloudwatchMonitorPublisher extends BufferedHandler<List<Metric>> {
    // TODO: Implement configurable Internal Handler in the API

    /**
     * <p>
     * Namespace of the metrics as defined in CloudWatch
     * </p>
     */
    @Configurable("com.mulesoft.agent")
    String namespace;

    /**
     * <p>
     * Access Key used for CloudWatch authentication
     * </p>
     */
    @Configurable("missingAccessKey")
    String accessKey;

    /**
     * <p>
     * Secret Key used for CloudWatch authentication
     * </p>
     */
    @Configurable("missingSecretKey")
    String secretKey;

    @Inject
    public CloudwatchMonitorPublisher() {
        super();
    }

    public CloudwatchMonitorPublisher(OnOffSwitch enabledSwitch) {
        super();
        this.enabledSwitch = enabledSwitch;
    }

    @Override
    public boolean canHandle(@NotNull List<Metric> metrics) {
        return true;
    }

    @Override
    public boolean flush(@NotNull Collection<List<Metric>> listOfMetrics) {
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        AmazonCloudWatchAsyncClient cloudWatchClient = new AmazonCloudWatchAsyncClient(credentials);

        for (List<Metric> metrics : listOfMetrics) {
            List<MetricDatum> cloudWatchMetrics = transformMetrics(metrics);
            PutMetricDataRequest putMetricDataRequest = new PutMetricDataRequest();
            putMetricDataRequest.setMetricData(cloudWatchMetrics);
            putMetricDataRequest.setNamespace(namespace);
            cloudWatchClient.putMetricData(putMetricDataRequest);
        }

        return true;
    }

    /**
     * <p>
     * Transforms the metrics from the Metric domain object o the MetricDatum type used by the AWS sdk
     * </p>
     * @param metrics The list of Metric objects
     * @return The converted list of MetricDatum objects
     */
    private static List<MetricDatum> transformMetrics(List<Metric> metrics) {
        List<MetricDatum> cloudWatchMetrics = new LinkedList<>();
        for (Metric metric : metrics) {
            MetricDatum cloudwatchMetric = new MetricDatum();
            cloudwatchMetric.setMetricName(metric.getName());
            cloudwatchMetric.setValue(metric.getValue().doubleValue());
            cloudwatchMetric.setTimestamp(new Date(metric.getTimestamp()));
            cloudWatchMetrics.add(cloudwatchMetric);
        }
        return cloudWatchMetrics;
    }
}