Java Code Examples for com.amazonaws.services.cloudwatch.model.MetricDatum#setMetricName()

The following examples show how to use com.amazonaws.services.cloudwatch.model.MetricDatum#setMetricName() . 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: PutCloudWatchMetric.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@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());
        datum.setValue(Double.parseDouble(context.getProperty(VALUE).evaluateAttributeExpressions(flowFile).getValue()));

        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);
        }

        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);
    }

}
 
Example 2
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 3
Source File: CloudWatchAlertDispatcher.java    From s3mper with Apache License 2.0 5 votes vote down vote up
private void sendCloudWatchConsistencyAlert() {
    MetricDatum datum = new MetricDatum();
    datum.setMetricName(cloudWatchConsistencyMetric);
    datum.setUnit(StandardUnit.Count);
    datum.setValue(1.0);
    
    PutMetricDataRequest request = new PutMetricDataRequest();
    request.setNamespace(namespace);
    request.setMetricData(Collections.singleton(datum));
    
    cloudWatch.putMetricData(request);
}
 
Example 4
Source File: CloudWatchAlertDispatcher.java    From s3mper with Apache License 2.0 5 votes vote down vote up
private void sendCloudWatchTimeoutAlert() {
    MetricDatum datum = new MetricDatum();
    datum.setMetricName(cloudWatchTimeoutMetric);
    datum.setUnit(StandardUnit.Count);
    datum.setValue(1.0);
    
    PutMetricDataRequest request = new PutMetricDataRequest();
    request.setNamespace(namespace);
    request.setMetricData(Collections.singleton(datum));
    
    cloudWatch.putMetricData(request);
}
 
Example 5
Source File: CloudwatchReporter.java    From bender with Apache License 2.0 4 votes vote down vote up
@Override
public void write(ArrayList<Stat> stats, long invokeTimeMs, Set<Tag> tags) {
  Date dt = new Date();
  dt.setTime(invokeTimeMs);

  Collection<Dimension> parentDims = tagsToDimensions(tags);
  List<MetricDatum> metrics = new ArrayList<MetricDatum>();

  /*
   * Create CW metric objects from bender internal Stat objects
   */
  for (Stat stat : stats) {
    /*
     * Dimension are CW's version of metric tags. A conversion must be done.
     */
    Collection<Dimension> metricDims = tagsToDimensions(stat.getTags());
    metricDims.addAll(parentDims);

    MetricDatum metric = new MetricDatum();
    metric.setMetricName(stat.getName());
    // TODO: add units to Stat object SYSTEMS-870
    metric.setUnit(StandardUnit.None);
    metric.setTimestamp(dt);
    metric.setDimensions(metricDims);
    metric.setValue((double) stat.getValue());

    metrics.add(metric);
  }

  /*
   * Not very well documented in java docs but CW only allows 20 metrics at a time.
   */
  List<List<MetricDatum>> chunks = ListUtils.partition(metrics, 20);
  for (List<MetricDatum> chunk : chunks) {
    PutMetricDataRequest req = new PutMetricDataRequest();
    req.withMetricData(chunk);
    req.setNamespace(namespace);

    this.client.putMetricData(req);
  }
}
 
Example 6
Source File: PutCloudWatchMetric.java    From nifi with Apache License 2.0 4 votes vote down vote up
@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);
    }

}