com.amazonaws.services.cloudwatch.model.MetricDatum Java Examples

The following examples show how to use com.amazonaws.services.cloudwatch.model.MetricDatum. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CloudWatchReporter.java    From metrics-cloudwatch with Apache License 2.0 6 votes vote down vote up
/**
 * @param rescale the submitted sum by this multiplier. 1.0 is the identity (no rescale).
 */
void reportSampling(Map.Entry<String, ? extends Sampling> entry, String typeDimValue, double rescale, List<MetricDatum> data) {
    Sampling metric = entry.getValue();
    Snapshot snapshot = metric.getSnapshot();
    double scaledSum = sum(snapshot.getValues()) * rescale;
    final StatisticSet statisticSet = new StatisticSet()
            .withSum(scaledSum)
            .withSampleCount((double) snapshot.size())
            .withMinimum((double) snapshot.getMin() * rescale)
            .withMaximum((double) snapshot.getMax() * rescale);

    DemuxedKey key = new DemuxedKey(appendGlobalDimensions(entry.getKey()));
    Iterables.addAll(data, key.newDatums(typeDimName, typeDimValue, new Function<MetricDatum, MetricDatum>() {
        @Override
        public MetricDatum apply(MetricDatum datum) {
            return datum.withStatisticValues(statisticSet);
        }
    }));
}
 
Example #2
Source File: MetricDataAggregator.java    From swage with Apache License 2.0 6 votes vote down vote up
/**
 * Flush all the current aggregated MetricDatum and return as a list.
 * This is safe to call concurrently with {@link #add}.
 * All data added prior to a flush call will be included in the returned aggregate.
 * Any data added after the flush call returns will be included in a subsequent flush.
 * Data added while a flush call is processing may be included in the current flush
 * or a subsequent flush, but will not be included twice.
 *
 * The timestamp on the aggregated data will be the time it was flushed,
 * not the time of any of the original metric events.
 *
 * @return list of all data aggregated since the last flush
 */
public List<MetricDatum> flush() {
    if (statisticsMap.size() == 0) {
        return Collections.emptyList();
    }

    // Capture all the current metrics, as represented by the set of keys
    // at this time in the statisticsMap.
    // Note that this iterates over the key set of the underlying map, and
    // removes keys from the map at the same time. It is possible keys may
    // be added during this iteration, or data for keys modified between
    // a key being chosen for iteration and being removed from the map.
    // This is ok.  Any new keys will be picked up on subsequent flushes.
    //TODO: use two maps and swap between, to ensure 'perfect' segmentation?
    List<MetricDatum> metricData = new ArrayList<>();
    for (DatumKey key : statisticsMap.keySet()) {
        StatisticSet value = statisticsMap.remove(key);
        //TODO: better to have no timestamp at all?
        MetricDatum metricDatum = key.getDatum().withTimestamp(Date.from(Instant.now()))
                                                .withStatisticValues(value);
        metricData.add(metricDatum);
    }

    return metricData;
}
 
Example #3
Source File: TestPutCloudWatchMetric.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testMetricExpressionValid() 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, "${metric.value}");
    runner.assertValid();

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("metric.value", "1.23");
    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(1.23d, datum.getValue(), 0.0001d);
}
 
Example #4
Source File: CloudWatchRecorder.java    From swage with Apache License 2.0 6 votes vote down vote up
private void sendAggregatedData() {

        // Grab all the current aggregated attributes, resetting
        // the aggregator to empty in the process
        List<MetricDatum> metricData = aggregator.flush();
        if(metricData.isEmpty()) {
            return;
        }

        // Send the attributes in batches to adhere to CloudWatch limitation on the
        // number of MetricDatum objects per request, see:
        // http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/cloudwatch_limits.html
        int begin = 0;
        while(begin < metricData.size()) {
            int end = begin + BATCH_SIZE;

            sendData(metricData.subList(begin, Math.min(end, metricData.size())));

            begin = end;
        }

    }
 
Example #5
Source File: CloudWatchRecorderTest.java    From swage with Apache License 2.0 6 votes vote down vote up
private MetricDatum makeDatum(
        final String id,
        final String name,
        final double sum,
        final double min,
        final double max,
        final int count,
        final StandardUnit unit)
{
    MetricDatum md = new MetricDatum().withMetricName(name).withUnit(unit);

    final StatisticSet statSet = new StatisticSet()
            .withSampleCount(Double.valueOf(count))
            .withSum(sum)
            .withMinimum(min)
            .withMaximum(max);
    md.setStatisticValues(statSet);

    List<Dimension> dimensions = new ArrayList<>(1);
    Dimension trace = new Dimension().withName(ContextData.ID.name).withValue(id);

    dimensions.add(trace);
    md.setDimensions(dimensions);

    return md;
}
 
Example #6
Source File: CloudWatchMeterRegistry.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@Override
protected void publish() {
    boolean interrupted = false;
    try {
        for (List<MetricDatum> batch : MetricDatumPartition.partition(metricData(), config.batchSize())) {
            try {
                sendMetricData(batch);
            } catch (InterruptedException ex) {
                interrupted = true;
            }
        }
    }
    finally {
        if (interrupted) {
            Thread.currentThread().interrupt();
        }
    }
}
 
Example #7
Source File: CloudWatchMeterRegistryTest.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@Test
void batchSizeShouldWorkOnMetricDatum() throws InterruptedException {
    List<Meter> meters = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        Timer timer = Timer.builder("timer." + i).register(this.registry);
        meters.add(timer);
    }
    when(this.registry.getMeters()).thenReturn(meters);
    doNothing().when(this.registry).sendMetricData(any());
    this.registry.publish();
    @SuppressWarnings("unchecked")
    ArgumentCaptor<List<MetricDatum>> argumentCaptor = ArgumentCaptor.forClass(List.class);
    verify(this.registry, times(2)).sendMetricData(argumentCaptor.capture());
    List<List<MetricDatum>> allValues = argumentCaptor.getAllValues();
    assertThat(allValues.get(0)).hasSize(20);
    assertThat(allValues.get(1)).hasSize(20);
}
 
Example #8
Source File: CloudWatchScanCountListener.java    From emodb with Apache License 2.0 6 votes vote down vote up
@Override
protected void runOneIteration() throws Exception {
    try {
        _cloudWatch.putMetricData(
                new PutMetricDataRequest()
                        .withNamespace(NAMESPACE)
                        .withMetricData(
                                new MetricDatum()
                                        .withTimestamp(new Date())
                                        .withMetricName(ACTIVE_AND_PENDING_SCANS)
                                        .withValue((double) (_activeScanCount + _pendingScanCount))
                                        .withUnit(StandardUnit.Count)
                                        .withDimensions(_dimensions)));
    } catch (AmazonClientException e) {
        _log.error("Failed to publish active and pending scans metric", e);
    }
}
 
Example #9
Source File: DynamoDBReplicationEmitter.java    From dynamodb-cross-region-library with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void fail(final List<Record> records) {
    if (isShutdown) {
        if (records.isEmpty()) {
            // This is OK (but not expected)
            log.warn("Emitter fail method called after shutdown method was called. Continuing because list is empty");
            return;
        } else {
            throw new IllegalStateException("Emitter fail method called after shutdown method was called.");
        }
    }
    for (Record record : records) {
        log.error("Could not emit record: " + record);
    }
    final AmazonCloudWatchAsync cloudwatch = CLOUDWATCH.get();
    if (null != cloudwatch) {
        final double failed = records.size();
        final MetricDatum recordsProcessedFailedDatum = new MetricDatum().withMetricName(RECORDS_FAILED).withValue(failed).withUnit(StandardUnit.Count)
            .withTimestamp(new Date());
        final PutMetricDataRequest request = new PutMetricDataRequest().withNamespace(applicationName).withMetricData(recordsProcessedFailedDatum);
        cloudwatch.putMetricDataAsync(request);
    }
}
 
Example #10
Source File: MetricsEmittingBasicClickEventsToKinesis.java    From aws-big-data-blog with Apache License 2.0 6 votes vote down vote up
@Override
protected void runOnce() throws Exception {
    ClickEvent event = inputQueue.take();
    String partitionKey = event.getSessionId();
    ByteBuffer data = ByteBuffer.wrap(
            event.getPayload().getBytes("UTF-8"));
    recordsPut.getAndIncrement();

    PutRecordResult res = kinesis.putRecord(
            STREAM_NAME, data, partitionKey);

    MetricDatum d = new MetricDatum()
        .withDimensions(
            new Dimension().withName("StreamName").withValue(STREAM_NAME),
            new Dimension().withName("ShardId").withValue(res.getShardId()),
            new Dimension().withName("Host").withValue(
                    InetAddress.getLocalHost().toString()))
        .withValue(1.0)
        .withMetricName("RecordsPut");
    cw.putMetricData(new PutMetricDataRequest()
        .withMetricData(d)
        .withNamespace("MySampleProducer"));
}
 
Example #11
Source File: TestPutCloudWatchMetric.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutSimpleMetric() 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.assertValid();

    runner.enqueue(new byte[] {});
    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);
}
 
Example #12
Source File: CloudWatch.java    From javamelody with Apache License 2.0 6 votes vote down vote up
@Override
public void send() throws IOException {
	final List<MetricDatum> datumList;
	synchronized (buffer) {
		datumList = new ArrayList<MetricDatum>(buffer);
		buffer.clear();
	}
	// note: Each PutMetricData request is limited to 40 KB in size for HTTP POST requests.
	// And the collection MetricData must not have a size greater than 20.
	final List<List<MetricDatum>> parts = partition(datumList, 20);
	for (final List<MetricDatum> part : parts) {
		final PutMetricDataRequest request = new PutMetricDataRequest()
				.withNamespace(cloudWatchNamespace).withMetricData(part);
		try {
			awsCloudWatch.putMetricData(request);
		} catch (final Exception e) {
			// pas catch (AmazonCloudWatchException) sinon ClassNotFoundException dans Jenkins par ex
			throw new IOException("Error connecting to AWS CloudWatch", e);
		}
	}
}
 
Example #13
Source File: TestPutCloudWatchMetric.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testMetricExpressionValid() 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, "${metric.value}");
    runner.assertValid();

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("metric.value", "1.23");
    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(1.23d, datum.getValue(), 0.0001d);
}
 
Example #14
Source File: TestPutCloudWatchMetric.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutSimpleMetric() 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.assertValid();

    runner.enqueue(new byte[] {});
    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);
}
 
Example #15
Source File: CloudWatchReporter.java    From metrics-cloudwatch with Apache License 2.0 6 votes vote down vote up
void reportCounter(Map.Entry<String, ? extends Counting> entry, String typeDimValue, List<MetricDatum> data) {
    Counting metric = entry.getValue();
    final long diff = diffLast(metric);
    if (diff == 0) {
        // Don't submit metrics that have not changed. No reason to keep these alive. Also saves on CloudWatch
        // costs.
        return;
    }

    DemuxedKey key = new DemuxedKey(appendGlobalDimensions(entry.getKey()));
    Iterables.addAll(data, key.newDatums(typeDimName, typeDimValue, new Function<MetricDatum, MetricDatum>() {
        @Override
        public MetricDatum apply(MetricDatum datum) {
            return datum.withValue((double) diff).withUnit(StandardUnit.Count);
        }
    }));
}
 
Example #16
Source File: CloudWatchReporter.java    From metrics-cloudwatch with Apache License 2.0 6 votes vote down vote up
void reportGauge(Map.Entry<String, Gauge> gaugeEntry, String typeDimValue, List<MetricDatum> data) {
    Gauge gauge = gaugeEntry.getValue();

    Object valueObj = gauge.getValue();
    if (valueObj == null) {
        return;
    }

    String valueStr = valueObj.toString();
    if (NumberUtils.isNumber(valueStr)) {
        final Number value = NumberUtils.createNumber(valueStr);

        DemuxedKey key = new DemuxedKey(appendGlobalDimensions(gaugeEntry.getKey()));
        Iterables.addAll(data, key.newDatums(typeDimName, typeDimValue, new Function<MetricDatum, MetricDatum>() {
            @Override
            public MetricDatum apply(MetricDatum datum) {
                return datum.withValue(value.doubleValue());
            }
        }));
    }
}
 
Example #17
Source File: DemuxedKeyTest.java    From metrics-cloudwatch with Apache License 2.0 5 votes vote down vote up
boolean containsExactly(List<MetricDatum> data, String... names) {
    return Sets.symmetricDifference(Sets.newHashSet(Lists.transform(data, new Function<MetricDatum, String>() {
        @Override
        public String apply(MetricDatum input) {
            return input.getMetricName();
        }
    })), Sets.newHashSet(names)).isEmpty();
}
 
Example #18
Source File: DemuxedKeyTest.java    From metrics-cloudwatch with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testMultiPermDimensions() {
    DemuxedKey key = new DemuxedKey("Name key=value* color=green* machine=localhost");
    List<MetricDatum> data = Lists.newArrayList(key.newDatums(DEF_DIM_NAME_TYPE, "testMultiPermDimensions", Functions.<MetricDatum>identity()));
    Assert.assertEquals(4, data.size());
    Assert.assertTrue(containsExactly(data, "Name"));
    Assert.assertTrue(containsExactly(data,
            d(DEF_DIM_NAME_TYPE, "testMultiPermDimensions", "machine", "localhost"),
            d(DEF_DIM_NAME_TYPE, "testMultiPermDimensions", "key", "value", "machine", "localhost"),
            d(DEF_DIM_NAME_TYPE, "testMultiPermDimensions", "color", "green", "machine", "localhost"),
            d(DEF_DIM_NAME_TYPE, "testMultiPermDimensions", "key", "value", "color", "green", "machine", "localhost")
    ));
}
 
Example #19
Source File: DemuxedKeyTest.java    From metrics-cloudwatch with Apache License 2.0 5 votes vote down vote up
boolean containsExactly(List<MetricDatum> data, Set<Dimension>... dimensions) {
    return Sets.newHashSet(Lists.transform(data, new Function<MetricDatum, Set<Dimension>>() {
        @Override
        public Set<Dimension> apply(MetricDatum input) {
            return Sets.newHashSet(input.getDimensions());
        }
    })).equals(Sets.newHashSet(dimensions));
}
 
Example #20
Source File: TestPutCloudWatchMetric.java    From nifi with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #21
Source File: TestPutCloudWatchMetric.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testStatisticSet() 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.MINIMUM, "${metric.min}");
    runner.setProperty(PutCloudWatchMetric.MAXIMUM, "${metric.max}");
    runner.setProperty(PutCloudWatchMetric.SUM, "${metric.sum}");
    runner.setProperty(PutCloudWatchMetric.SAMPLECOUNT, "${metric.count}");
    runner.assertValid();

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("metric.min", "1");
    attributes.put("metric.max", "2");
    attributes.put("metric.sum", "3");
    attributes.put("metric.count", "2");
    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(1.0d, datum.getStatisticValues().getMinimum(), 0.0001d);
    Assert.assertEquals(2.0d, datum.getStatisticValues().getMaximum(), 0.0001d);
    Assert.assertEquals(3.0d, datum.getStatisticValues().getSum(), 0.0001d);
    Assert.assertEquals(2.0d, datum.getStatisticValues().getSampleCount(), 0.0001d);
}
 
Example #22
Source File: MetricDataAggregator.java    From swage with Apache License 2.0 5 votes vote down vote up
public MetricDatum getDatum() {
    MetricDatum md = new MetricDatum();
    md.setMetricName(name);
    md.setUnit(unit);
    md.setDimensions(dimensions);
    return md;
}
 
Example #23
Source File: CloudWatchReporterTest.java    From chassis with Apache License 2.0 5 votes vote down vote up
void validate(MetricDatum datum) throws Exception {
    if (datum == null) {
        throw new Exception("No Data");
    }
    if (!datum.getMetricName().equals(name)) {
        throw new Exception("invalid metric name " + datum.getMetricName() + ". expected " + name);
    }
    if (value != null && !(Math.round(datum.getValue()) == Math.round(value))) {
        throw new Exception("invalid data value " + Math.round(datum.getValue()) + ". expected " + Math.round(value));
    }
    if (datum.getDimensions().size() != 1) {
        throw new Exception("expected 1 dimension, got " + datum.getDimensions().size());
    }
    if (!datum.getDimensions().get(0).getName().equals("environment")) {
        throw new Exception("invalid dimension name. expected \"environment\", got " + datum.getDimensions().get(0).getName());
    }
    if (!datum.getDimensions().get(0).getValue().equals(environment)) {
        throw new Exception("invalid dimension value " + datum.getDimensions().get(0).getValue() + ". expected " + environment);
    }
    if (datum.getTimestamp() == null) {
        throw new Exception("no timestamp");
    }
    long timeDelta = Math.abs(System.currentTimeMillis() - datum.getTimestamp().getTime());
    if (timeDelta > 5000) {
        throw new Exception("invalid timestamp. expected it to less than 5000 millis old. was " + timeDelta + " millis old.");
    }
}
 
Example #24
Source File: MetricsCloudWatchReporter.java    From chassis with Apache License 2.0 5 votes vote down vote up
private void addDatum(String name, double value, LinkedList<PutMetricDataRequest> requests, Date timestamp) {
    if (logger.isDebugEnabled()) {
        logger.debug("Adding Datum {} with value {} at {}", name, value, timestamp);
    }
    if (requests.isEmpty() || requests.getLast().getMetricData().size() == MAX_CLOUDWATCH_DATUM_PER_REQUEST) {
        requests.add(createRequest());
    }
    PutMetricDataRequest request = requests.getLast();
    MetricDatum datum = new MetricDatum().withTimestamp(timestamp).withValue(value).withMetricName(name).withUnit(StandardUnit.None).withDimensions(createDimensions());
    request.withMetricData(datum);
}
 
Example #25
Source File: MetricsCloudWatchReporter.java    From chassis with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public void report(
        SortedMap<String, Gauge> gauges,
        SortedMap<String, Counter> counters,
        SortedMap<String, Histogram> histograms,
        SortedMap<String, Meter> meters,
        SortedMap<String, Timer> timers) {

    logger.info("Starting metrics publishing to AWS CloudWatch.");

    LinkedList<PutMetricDataRequest> requests = new LinkedList<>();

    addMetricData(gauges, counters, histograms, meters, timers, requests, new Date());

    if (requests.isEmpty()) {
        logger.debug("No metric data to send to AWS.");
        return;
    }

    for (PutMetricDataRequest request : requests) {
        try {
            for (MetricDatum datum : request.getMetricData()) {
                logger.debug("Sending metric " + datum);
            }
            cloudWatch.putMetricData(request);
        } catch (Exception e) {
            logger.error("Failed to log metrics to CloudWatch discarding metrics for this attempt...",e);
            return;
        }
    }
    logger.info("Finished metrics publishing to AWS CloudWatch.");
}
 
Example #26
Source File: DynamoDBBuffer.java    From dynamodb-cross-region-library with Apache License 2.0 5 votes vote down vote up
/**
 * Publish relevant CloudWatch metrics.
 */
protected void emitCloudWatchMetrics() {
    if (null != getCloudwatch()) {
        // TODO Emit CloudWatch metrics about the size of the queue of writes
        MetricDatum recordsProcessedDatum = new MetricDatum().withValue(getProcessedRecords());
        PutMetricDataRequest request = new PutMetricDataRequest().withMetricData(recordsProcessedDatum);
        getCloudwatch().putMetricData(request);
    }
}
 
Example #27
Source File: CloudWatchRecorderTest.java    From swage with Apache License 2.0 5 votes vote down vote up
@Test
public void record_and_shutdown() throws Exception {
    final DimensionMapper mapper = new DimensionMapper.Builder()
            .addGlobalDimension(ContextData.ID)
            .build();

    final String namespace = "spacename";

    final String id = UUID.randomUUID().toString();
    final TypedMap data = ContextData.withId(id).build();
    final double time = 123.45;

    final AmazonCloudWatch client = mock(AmazonCloudWatch.class);

    final CloudWatchRecorder recorder = new CloudWatchRecorder.Builder()
            .client(client)
            .namespace(namespace)
            .publishFrequency(120)
            .maxJitter(60)
            .dimensionMapper(mapper)
            .build();

    final MetricContext context = recorder.context(data);
    context.record(M_TIME, time, Unit.MILLISECOND, Instant.now());
    context.close();

    // shutdown right away, before first scheduled publish
    recorder.shutdown();

    final List<MetricDatum> expected = new ArrayList<>(1);
    expected.add(makeDatum(id, M_TIME.toString(), time, time, time, 1, StandardUnit.Milliseconds));

    verify(client).putMetricData(argThat(new RequestMatcher(namespace, expected)));
}
 
Example #28
Source File: MetricDataAggregatorTest.java    From swage with Apache License 2.0 5 votes vote down vote up
@Test
public void single() throws Exception {
    final DimensionMapper mapper = new DimensionMapper.Builder()
            .addGlobalDimension(ContextData.ID)
            .build();

    final Metric name = Metric.define("SomeMetric");
    final double value = 3.14;
    final StandardUnit unit = StandardUnit.Terabits;

    final MetricRecorder.RecorderContext context = new MetricRecorder.RecorderContext(ContextData.withId(UUID.randomUUID().toString()).build());

    MetricDataAggregator aggregator = new MetricDataAggregator(mapper);
    aggregator.add(context, name, value, unit);

    List<MetricDatum> ags = aggregator.flush();

    assertEquals("One metric datum should aggregate to one entry", 1, ags.size());
    assertEquals("Metric datum has wrong name", name.toString(), ags.get(0).getMetricName());
    assertEquals("Metric datum has wrong unit", unit.toString(), ags.get(0).getUnit());

    StatisticSet stats = ags.get(0).getStatisticValues();
    assertEquals("Metric datum has wrong stats value", Double.valueOf(value), stats.getSum());
    assertEquals("Metric datum has wrong stats value", Double.valueOf(value), stats.getMinimum());
    assertEquals("Metric datum has wrong stats value", Double.valueOf(value), stats.getMaximum());
    assertEquals("Metric datum has wrong stats count", Double.valueOf(1), stats.getSampleCount());

    assertTrue("Flush with no attributes was non-empty", aggregator.flush().isEmpty());
}
 
Example #29
Source File: KouplerMetrics.java    From koupler with MIT License 5 votes vote down vote up
public void run() {
    try {
        PutMetricDataRequest putMetricDataRequest = new PutMetricDataRequest();

        putMetricDataRequest.withMetricData(new MetricDatum()
                .withDimensions(hostDimension)
                .withMetricName("BytesPerEvent")
                .withValue(bytesHistogram.getSnapshot().getMean()));

        putMetricDataRequest.withMetricData(new MetricDatum()
                .withDimensions(hostDimension)
                .withMetricName("KplEventQueueCount")
                .withValue(producer.getKplQueueSize() * 1.0));

        putMetricDataRequest.withMetricData(new MetricDatum()
                .withDimensions(hostDimension)
                .withMetricName("InternalEventQueueCount")
                .withValue(producer.getInternalQueueSize() * 1.0));
        
        putMetricDataRequest.withMetricData(new MetricDatum()
                .withDimensions(hostDimension)
                .withMetricName("QueuedEventsPerSecond")
                .withValue(queuedMeter.getMeanRate()));

        putMetricDataRequest.withMetricData(new MetricDatum()
                .withDimensions(hostDimension)
                .withMetricName("CompletedEventsPerSecond")
                .withValue(completedMeter.getMeanRate()));

        putMetricDataRequest.withNamespace(appName);

        cloudWatch.putMetricData(putMetricDataRequest);

        LOGGER.debug("Published metrics to CloudWatch [{}].", this.toString());

    } catch (Exception e) {
        LOGGER.error("Problem posting or reading cloudwatch metrics.", e);
    }
}
 
Example #30
Source File: PutMetricData.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
            "To run this example, supply a data point:\n" +
            "Ex: PutMetricData <data_point>\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        Double data_point = Double.parseDouble(args[0]);

        final AmazonCloudWatch cw =
            AmazonCloudWatchClientBuilder.defaultClient();

        Dimension dimension = new Dimension()
            .withName("UNIQUE_PAGES")
            .withValue("URLS");

        MetricDatum datum = new MetricDatum()
            .withMetricName("PAGES_VISITED")
            .withUnit(StandardUnit.None)
            .withValue(data_point)
            .withDimensions(dimension);

        PutMetricDataRequest request = new PutMetricDataRequest()
            .withNamespace("SITE/TRAFFIC")
            .withMetricData(datum);

        PutMetricDataResult response = cw.putMetricData(request);

        System.out.printf("Successfully put data point %f", data_point);
    }