Search in sources :

Example 1 with Dimension

use of com.amazonaws.services.cloudwatch.model.Dimension in project camel by apache.

the class CwProducer method setDimension.

private void setDimension(MetricDatum metricDatum, Exchange exchange) {
    String name = exchange.getIn().getHeader(CwConstants.METRIC_DIMENSION_NAME, String.class);
    String value = exchange.getIn().getHeader(CwConstants.METRIC_DIMENSION_VALUE, String.class);
    if (name != null && value != null) {
        metricDatum.withDimensions(new Dimension().withName(name).withValue(value));
    } else {
        Map<String, String> dimensions = exchange.getIn().getHeader(CwConstants.METRIC_DIMENSIONS, Map.class);
        if (dimensions != null) {
            Collection<Dimension> dimensionCollection = new ArrayList<Dimension>();
            for (Map.Entry<String, String> dimensionEntry : dimensions.entrySet()) {
                Dimension dimension = new Dimension().withName(dimensionEntry.getKey()).withValue(dimensionEntry.getValue());
                dimensionCollection.add(dimension);
            }
            metricDatum.withDimensions(dimensionCollection);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Dimension(com.amazonaws.services.cloudwatch.model.Dimension) Map(java.util.Map)

Example 2 with Dimension

use of com.amazonaws.services.cloudwatch.model.Dimension in project camel by apache.

the class CwComponentTest method setsMeticDimensions.

@Test
public void setsMeticDimensions() throws Exception {
    template.send("direct:start", ExchangePattern.InOnly, new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(CwConstants.METRIC_NAME, "errorCount");
            Map<String, String> dimensionsMap = new LinkedHashMap<String, String>();
            dimensionsMap.put("keyOne", "valueOne");
            dimensionsMap.put("keyTwo", "valueTwo");
            exchange.getIn().setHeader(CwConstants.METRIC_DIMENSIONS, dimensionsMap);
        }
    });
    ArgumentCaptor<PutMetricDataRequest> argument = ArgumentCaptor.forClass(PutMetricDataRequest.class);
    verify(cloudWatchClient).putMetricData(argument.capture());
    List<Dimension> dimensions = argument.getValue().getMetricData().get(0).getDimensions();
    Dimension dimension = dimensions.get(0);
    assertThat(dimensions.size(), is(2));
    assertEquals("keyOne", dimension.getName());
    assertEquals("valueOne", dimension.getValue());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) PutMetricDataRequest(com.amazonaws.services.cloudwatch.model.PutMetricDataRequest) Dimension(com.amazonaws.services.cloudwatch.model.Dimension) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with Dimension

use of com.amazonaws.services.cloudwatch.model.Dimension in project nifi by apache.

the class PutCloudWatchMetric method onTrigger.

@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    MetricDatum datum = new MetricDatum();
    try {
        datum.setMetricName(context.getProperty(METRIC_NAME).evaluateAttributeExpressions(flowFile).getValue());
        final String valueString = context.getProperty(VALUE).evaluateAttributeExpressions(flowFile).getValue();
        if (valueString != null) {
            datum.setValue(Double.parseDouble(valueString));
        } else {
            StatisticSet statisticSet = new StatisticSet();
            statisticSet.setMaximum(Double.parseDouble(context.getProperty(MAXIMUM).evaluateAttributeExpressions(flowFile).getValue()));
            statisticSet.setMinimum(Double.parseDouble(context.getProperty(MINIMUM).evaluateAttributeExpressions(flowFile).getValue()));
            statisticSet.setSampleCount(Double.parseDouble(context.getProperty(SAMPLECOUNT).evaluateAttributeExpressions(flowFile).getValue()));
            statisticSet.setSum(Double.parseDouble(context.getProperty(SUM).evaluateAttributeExpressions(flowFile).getValue()));
            datum.setStatisticValues(statisticSet);
        }
        final String timestamp = context.getProperty(TIMESTAMP).evaluateAttributeExpressions(flowFile).getValue();
        if (timestamp != null) {
            datum.setTimestamp(new Date(Long.parseLong(timestamp)));
        }
        final String unit = context.getProperty(UNIT).evaluateAttributeExpressions(flowFile).getValue();
        if (unit != null) {
            datum.setUnit(unit);
        }
        // add dynamic properties as dimensions
        if (!dynamicPropertyNames.isEmpty()) {
            final List<Dimension> dimensions = new ArrayList<>(dynamicPropertyNames.size());
            for (String propertyName : dynamicPropertyNames) {
                final String propertyValue = context.getProperty(propertyName).evaluateAttributeExpressions(flowFile).getValue();
                if (StringUtils.isNotBlank(propertyValue)) {
                    dimensions.add(new Dimension().withName(propertyName).withValue(propertyValue));
                }
            }
            datum.withDimensions(dimensions);
        }
        final PutMetricDataRequest metricDataRequest = new PutMetricDataRequest().withNamespace(context.getProperty(NAMESPACE).evaluateAttributeExpressions(flowFile).getValue()).withMetricData(datum);
        putMetricData(metricDataRequest);
        session.transfer(flowFile, REL_SUCCESS);
        getLogger().info("Successfully published cloudwatch metric for {}", new Object[] { flowFile });
    } catch (final Exception e) {
        getLogger().error("Failed to publish cloudwatch metric for {} due to {}", new Object[] { flowFile, e });
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) PutMetricDataRequest(com.amazonaws.services.cloudwatch.model.PutMetricDataRequest) ArrayList(java.util.ArrayList) MetricDatum(com.amazonaws.services.cloudwatch.model.MetricDatum) Dimension(com.amazonaws.services.cloudwatch.model.Dimension) StatisticSet(com.amazonaws.services.cloudwatch.model.StatisticSet) Date(java.util.Date) ProcessException(org.apache.nifi.processor.exception.ProcessException) AmazonClientException(com.amazonaws.AmazonClientException)

Example 4 with Dimension

use of com.amazonaws.services.cloudwatch.model.Dimension in project nifi by apache.

the class TestPutCloudWatchMetric method testDimensions.

@Test
public void testDimensions() throws Exception {
    MockPutCloudWatchMetric mockPutCloudWatchMetric = new MockPutCloudWatchMetric();
    final TestRunner runner = TestRunners.newTestRunner(mockPutCloudWatchMetric);
    runner.setProperty(PutCloudWatchMetric.NAMESPACE, "TestNamespace");
    runner.setProperty(PutCloudWatchMetric.METRIC_NAME, "TestMetric");
    runner.setProperty(PutCloudWatchMetric.VALUE, "1.0");
    runner.setProperty(PutCloudWatchMetric.UNIT, "Count");
    runner.setProperty(PutCloudWatchMetric.TIMESTAMP, "1476296132575");
    runner.setProperty(new PropertyDescriptor.Builder().dynamic(true).name("dim1").build(), "${metric.dim1}");
    runner.setProperty(new PropertyDescriptor.Builder().dynamic(true).name("dim2").build(), "val2");
    runner.assertValid();
    final Map<String, String> attributes = new HashMap<>();
    attributes.put("metric.dim1", "1");
    runner.enqueue(new byte[] {}, attributes);
    runner.run();
    runner.assertAllFlowFilesTransferred(PutCloudWatchMetric.REL_SUCCESS, 1);
    Assert.assertEquals(1, mockPutCloudWatchMetric.putMetricDataCallCount);
    Assert.assertEquals("TestNamespace", mockPutCloudWatchMetric.actualNamespace);
    MetricDatum datum = mockPutCloudWatchMetric.actualMetricData.get(0);
    Assert.assertEquals("TestMetric", datum.getMetricName());
    Assert.assertEquals(1d, datum.getValue(), 0.0001d);
    List<Dimension> dimensions = datum.getDimensions();
    Collections.sort(dimensions, (d1, d2) -> d1.getName().compareTo(d2.getName()));
    Assert.assertEquals(2, dimensions.size());
    Assert.assertEquals("dim1", dimensions.get(0).getName());
    Assert.assertEquals("1", dimensions.get(0).getValue());
    Assert.assertEquals("dim2", dimensions.get(1).getName());
    Assert.assertEquals("val2", dimensions.get(1).getValue());
}
Also used : HashMap(java.util.HashMap) TestRunner(org.apache.nifi.util.TestRunner) MetricDatum(com.amazonaws.services.cloudwatch.model.MetricDatum) Dimension(com.amazonaws.services.cloudwatch.model.Dimension) Test(org.junit.Test)

Example 5 with Dimension

use of com.amazonaws.services.cloudwatch.model.Dimension in project Synapse-Stack-Builder by Sage-Bionetworks.

the class RdsAlarmSetup method createDefaultPutMetricRequest.

/**
 * @param instances
 * @param topicArn
 * @param alarmRequest
 */
static PutMetricAlarmRequest createDefaultPutMetricRequest(DBInstance instances, String topicArn) {
    PutMetricAlarmRequest alarmRequest = new PutMetricAlarmRequest();
    alarmRequest.setAlarmDescription("Setup by: " + RdsAlarmSetup.class.getName());
    alarmRequest.setActionsEnabled(true);
    alarmRequest.withAlarmActions(topicArn);
    alarmRequest.setNamespace(NAME_SPACES_AWS_RDS);
    alarmRequest.withDimensions(new Dimension().withName(DB_INSTANCE_IDENTIFIER).withValue(instances.getDBInstanceIdentifier()));
    return alarmRequest;
}
Also used : PutMetricAlarmRequest(com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest) Dimension(com.amazonaws.services.cloudwatch.model.Dimension)

Aggregations

Dimension (com.amazonaws.services.cloudwatch.model.Dimension)19 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 PutMetricAlarmRequest (com.amazonaws.services.cloudwatch.model.PutMetricAlarmRequest)5 MetricDatum (com.amazonaws.services.cloudwatch.model.MetricDatum)4 PutMetricDataRequest (com.amazonaws.services.cloudwatch.model.PutMetricDataRequest)4 AmazonCloudWatch (com.amazonaws.services.cloudwatch.AmazonCloudWatch)2 GetMetricStatisticsRequest (com.amazonaws.services.cloudwatch.model.GetMetricStatisticsRequest)2 GetMetricStatisticsResult (com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult)2 Date (java.util.Date)2 Map (java.util.Map)2 AmazonClientException (com.amazonaws.AmazonClientException)1 PutMetricAlarmResult (com.amazonaws.services.cloudwatch.model.PutMetricAlarmResult)1 PutMetricDataResult (com.amazonaws.services.cloudwatch.model.PutMetricDataResult)1 StatisticSet (com.amazonaws.services.cloudwatch.model.StatisticSet)1 Stat (com.nextdoor.bender.monitoring.Stat)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1