Example usage for com.amazonaws.services.cloudwatch.model StatisticSet StatisticSet

List of usage examples for com.amazonaws.services.cloudwatch.model StatisticSet StatisticSet

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudwatch.model StatisticSet StatisticSet.

Prototype

StatisticSet

Source Link

Usage

From source file:com.amazon.kinesis.streaming.agent.metrics.AccumulatingMetricsScope.java

License:Open Source License

@Override
protected void realAddData(String name, double value, StandardUnit unit) {
    MetricDatum datum = data.get(name);/*from   ww w  .  j ava  2s.c  o m*/
    if (datum == null) {
        data.put(name, new MetricDatum().withMetricName(name).withUnit(unit).withStatisticValues(
                new StatisticSet().withMaximum(value).withMinimum(value).withSampleCount(1.0).withSum(value)));
    } else {
        if (!datum.getUnit().equals(unit.name())) {
            throw new IllegalArgumentException("Cannot add to existing metric with different unit");
        }
        StatisticSet statistics = datum.getStatisticValues();
        statistics.setMaximum(Math.max(value, statistics.getMaximum()));
        statistics.setMinimum(Math.min(value, statistics.getMinimum()));
        statistics.setSampleCount(statistics.getSampleCount() + 1);
        statistics.setSum(statistics.getSum() + value);
    }
}

From source file:com.basistech.metrics.CloudWatchReporter.java

License:Open Source License

private void reportTimer(String key, Collection<MetricDatum> data, Map.Entry<String, Timer> met) {
    Timer timer = met.getValue();
    Snapshot snapshot = timer.getSnapshot();
    if (reportAggregates) {
        reportAggregate(key, data, "count", null, timer.getCount());
        reportAggregate(key, data, "rate", "1minute", timer.getOneMinuteRate());
        reportAggregate(key, data, "rate", "5minute", timer.getFiveMinuteRate());
        reportAggregate(key, data, "rate", "15minute", timer.getFifteenMinuteRate());
        reportAggregate(key, data, "rate", "mean", timer.getMeanRate());
        reportSnapshot(data, snapshot, key);
    } else {//  w w w . ja  v  a2s.  c  o  m
        // if no data, don't bother Amazon with it.
        if (snapshot.size() == 0) {
            return;
        }
        double sum = 0;
        for (double val : snapshot.getValues()) {
            sum += val;
        }
        // Metrics works in Nanoseconds, which is not one of Amazon's favorites.
        double max = (double) TimeUnit.NANOSECONDS.toMicros(snapshot.getMax());
        double min = (double) TimeUnit.NANOSECONDS.toMicros(snapshot.getMin());
        double sumMicros = TimeUnit.NANOSECONDS.toMicros((long) sum);
        StatisticSet stats = new StatisticSet().withMaximum(max).withMinimum(min).withSum(sumMicros)
                .withSampleCount((double) snapshot.getValues().length);
        if (LOG.isDebugEnabled()) {
            LOG.debug("timer {}: {}", met.getKey(), stats);
        }
        data.add(new MetricDatum().withMetricName(met.getKey()).withDimensions(dimensions)
                .withStatisticValues(stats).withUnit(StandardUnit.Microseconds));
    }
}

From source file:com.basistech.metrics.CloudWatchReporter.java

License:Open Source License

private void reportHistogram(Collection<MetricDatum> data, Map.Entry<String, Histogram> meh) {
    Snapshot snapshot = meh.getValue().getSnapshot();

    if (reportAggregates) {
        String key = meh.getKey();
        reportSnapshot(data, snapshot, key);
    } else {/*from w  ww . ja  v  a  2 s .com*/
        // if no data, don't bother Amazon with it.
        if (snapshot.size() == 0) {
            return;
        }
        double sum = 0;
        for (double val : snapshot.getValues()) {
            sum += val;
        }
        StatisticSet stats = new StatisticSet().withMaximum((double) snapshot.getMax())
                .withMinimum((double) snapshot.getMin()).withSum(sum)
                .withSampleCount((double) snapshot.getValues().length);
        if (LOG.isDebugEnabled()) {
            LOG.debug("histogram {}: {}", meh.getKey(), stats);
        }
        data.add(new MetricDatum().withMetricName(meh.getKey()).withDimensions(dimensions)
                .withStatisticValues(stats));
    }
}

From source file:com.blacklocus.metrics.CloudWatchReporter.java

License:Apache License

/**
 * @param rescale the submitted sum by this multiplier. 1.0 is the identity (no rescale).
 *///  ww  w  . j  ava  2  s.c  o m
void reportSampling(Map.Entry<String, ? extends Sampling> entry, String type, 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(entry.getKey());
    Iterables.addAll(data, key.newDatums(type, new Function<MetricDatum, MetricDatum>() {
        @Override
        public MetricDatum apply(MetricDatum datum) {
            return datum.withStatisticValues(statisticSet);
        }
    }));
}

From source file:com.github.lpezet.antiope.metrics.aws.BlockingRequestBuilder.java

License:Open Source License

/**
 * Summarizes the given datum into the statistics of the respective unique metric.
 *///  w  w w  .  j  a v a 2s.  c o  m
private void summarize(MetricDatum pDatum, Map<String, MetricDatum> pUniqueMetrics) {
    Double pValue = pDatum.getValue();
    if (pValue == null) {
        return;
    }
    List<Dimension> oDims = pDatum.getDimensions();
    Collections.sort(oDims, DimensionComparator.INSTANCE);
    String oMetricName = pDatum.getMetricName();
    String k = oMetricName + Jackson.toJsonString(oDims);
    MetricDatum oStatDatum = pUniqueMetrics.get(k);
    if (oStatDatum == null) {
        oStatDatum = new MetricDatum().withDimensions(pDatum.getDimensions()).withMetricName(oMetricName)
                .withUnit(pDatum.getUnit()).withStatisticValues(new StatisticSet().withMaximum(pValue)
                        .withMinimum(pValue).withSampleCount(0.0).withSum(0.0));
        pUniqueMetrics.put(k, oStatDatum);
    }
    StatisticSet oStat = oStatDatum.getStatisticValues();
    oStat.setSampleCount(oStat.getSampleCount() + 1.0);
    oStat.setSum(oStat.getSum() + pValue);
    if (pValue > oStat.getMaximum()) {
        oStat.setMaximum(pValue);
    } else if (pValue < oStat.getMinimum()) {
        oStat.setMinimum(pValue);
    }
}

From source file:com.jlhood.metrics.CloudWatchReporter.java

License:Apache License

/**
 * @param rescale the submitted sum by this multiplier. 1.0 is the identity (no rescale).
 *///  w  ww . ja v a  2s  . com
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);
        }
    }));
}

From source file:org.apache.nifi.processors.aws.cloudwatch.PutCloudWatchMetric.java

License:Apache License

@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();/*w ww .ja v  a  2s .  c  o  m*/
    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);
    }

}

From source file:org.web.online.cloudwatch.tomcat.valve.ElapsedTimeAggregator.java

License:Apache License

/**
 * Construct the instance. Sets up the Cloud Watch metric with the given parameters.
 * /*from   ww  w. j a va2s  .c  o  m*/
 * @param namespace namespace value to use to push data to CloudWatch
 * @param region region used to look for instance tags and to push CloudWatch data
 * @param instanceId instanceId to use as dimension
 * @param asgName autoscaling group name to use as dimension
 * @param ec2Client ec2 client to use in querying tags to find ASG name
 * @param cloudWatchClient cloud watch client to use to push CloudWatch data
 */
public ElapsedTimeAggregator(String namespace, Region region, String instanceId, String asgName,
        AmazonEC2 ec2Client, AmazonCloudWatch cloudWatchClient) {

    this.region = region;

    if (instanceId == null) {
        throw new IllegalStateException("unable to find instance id");
    }

    // get the ASG name
    if (asgName == null) {
        ec2Client.setRegion(region);
        List<TagDescription> tagDescriptions = ec2Client.describeTags(new DescribeTagsRequest().withFilters(
                new Filter().withName("resource-id").withValues(instanceId),
                new Filter().withName("key").withValues("aws:autoscaling:groupName"))).getTags();
        if (tagDescriptions.size() == 1) {
            asgName = tagDescriptions.get(0).getValue();
        }
    }

    if (asgName == null) {
        log.warn("unable to determine AutoScalingGroupName for " + instanceId
                + ". No statistics will be published under the AutoScalingGroupName dimension");
    }

    cloudWatchClient.setRegion(region);
    this.cloudWatchClient = cloudWatchClient;

    String metricName = "ElapsedTime";
    Dimension instanceDimension = new Dimension().withName("InstanceId").withValue(instanceId);
    StatisticSet statisticSet = new StatisticSet();

    // set up the static MetricDatum and associate to the PutMetricDataRequest
    MetricDatum instanceMetricDatum = new MetricDatum().withMetricName(metricName)
            .withDimensions(instanceDimension).withStatisticValues(statisticSet)
            .withUnit(StandardUnit.Milliseconds);
    putMetricDataRequest = new PutMetricDataRequest().withNamespace(namespace)
            .withMetricData(instanceMetricDatum);

    // and a special zero value request since statistic set doesn't
    // support zero values
    MetricDatum zeroValueInstanceMetricDatum = new MetricDatum().withMetricName(metricName)
            .withDimensions(instanceDimension).withValue(0d).withUnit(StandardUnit.Milliseconds);
    zeroValuePutMetricDataRequest = new PutMetricDataRequest().withNamespace(namespace)
            .withMetricData(zeroValueInstanceMetricDatum);

    // also push metrics for the ASG dimension if we have an ASG name
    if (asgName != null) {
        Dimension asgDimension = new Dimension().withName("AutoScalingGroupName").withValue(asgName);
        MetricDatum asgMetricDatum = new MetricDatum().withMetricName(metricName).withDimensions(asgDimension)
                .withStatisticValues(statisticSet).withUnit(StandardUnit.Milliseconds);
        putMetricDataRequest.withMetricData(asgMetricDatum);
        MetricDatum zeroValueAsgMetricDatum = new MetricDatum().withMetricName(metricName)
                .withDimensions(asgDimension).withValue(0d).withUnit(StandardUnit.Milliseconds);
        zeroValuePutMetricDataRequest.withMetricData(zeroValueAsgMetricDatum);
    }
}