PutMetricDataRequest createPutRequest(List<Metric> batch) { List<MetricDatum> datumList = batch.stream().map(this::createMetricDatum) .collect(Collectors.toList()); return new PutMetricDataRequest().withNamespace(cloudWatchNamespace) .withMetricData(datumList); }
@Override public void doWrite(Server server, Query query, Iterable<Result> results) throws Exception { PutMetricDataRequest metricDataRequest = new PutMetricDataRequest(); metricDataRequest.setNamespace(namespace); List<MetricDatum> metricDatumList = new ArrayList<>(); // Iterating through the list of query results for (Result result : results) { try { metricDatumList.add(processResult(result)); } catch (IllegalArgumentException iae) { log.error("Could not convert result to double", iae); } } metricDataRequest.setMetricData(metricDatumList); cloudWatchClient.putMetricData(metricDataRequest); }
private PutMetricDataRequest newPutMetricDataRequest( Collection<MetricDatum> data, final String namespace, final Dimension... extraDims) { if (extraDims != null) { // Need to add some extra dimensions. // To do so, we copy the metric data to avoid mutability problems. Collection<MetricDatum> newData = new ArrayList<MetricDatum>(data.size()); for (MetricDatum md: data) { MetricDatum newMD = cloneMetricDatum(md); for (Dimension dim: extraDims) newMD.withDimensions(dim); // add the extra dimensions to the new metric datum newData.add(newMD); } data = newData; } return new PutMetricDataRequest() .withNamespace(namespace) .withMetricData(data) .withRequestMetricCollector(RequestMetricCollector.NONE) ; }
PutMetricDataRequest req = new PutMetricDataRequest(); req.withMetricData(chunk); req.setNamespace(namespace);
private PutMetricDataRequest newRequest() { return new PutMetricDataRequest() .withNamespace(namespace); }
/** * 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); } }
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); }
request.withMetricData(new MetricDatum() .withUnit(StandardUnit.None) .withTimestamp(now)
/** * <p> * The data for the metric. The array can include no more than 20 metrics per call. * </p> * * @param metricData * The data for the metric. The array can include no more than 20 metrics per call. * @return Returns a reference to this object so that method calls can be chained together. */ public PutMetricDataRequest withMetricData(java.util.Collection<MetricDatum> metricData) { setMetricData(metricData); return this; }
/** * <p> * The namespace for the metric data. * </p> * <p> * You cannot specify a namespace that begins with "AWS/". Namespaces that begin with "AWS/" are reserved for use by * Amazon Web Services products. * </p> * * @param namespace * The namespace for the metric data.</p> * <p> * You cannot specify a namespace that begins with "AWS/". Namespaces that begin with "AWS/" are reserved for * use by Amazon Web Services products. * @return Returns a reference to this object so that method calls can be chained together. */ public PutMetricDataRequest withNamespace(String namespace) { setNamespace(namespace); return this; }
private PutMetricDataRequest newPutMetricDataRequest( Collection<MetricDatum> data, final String namespace, final Dimension... extraDims) { if (extraDims != null) { // Need to add some extra dimensions. // To do so, we copy the metric data to avoid mutability problems. Collection<MetricDatum> newData = new ArrayList<MetricDatum>(data.size()); for (MetricDatum md: data) { MetricDatum newMD = cloneMetricDatum(md); for (Dimension dim: extraDims) newMD.withDimensions(dim); // add the extra dimensions to the new metric datum newData.add(newMD); } data = newData; } return new PutMetricDataRequest() .withNamespace(namespace) .withMetricData(data) .withRequestMetricCollector(RequestMetricCollector.NONE) ; }
PutMetricDataRequest req = new PutMetricDataRequest(); req.withMetricData(chunk); req.setNamespace(namespace);
private PutMetricDataRequest createRequest() { return new PutMetricDataRequest().withNamespace(appName); }
/** * <p> * The data for the metric. The array can include no more than 20 metrics per call. * </p> * <p> * <b>NOTE:</b> This method appends the values to the existing list (if any). Use * {@link #setMetricData(java.util.Collection)} or {@link #withMetricData(java.util.Collection)} if you want to * override the existing values. * </p> * * @param metricData * The data for the metric. The array can include no more than 20 metrics per call. * @return Returns a reference to this object so that method calls can be chained together. */ public PutMetricDataRequest withMetricData(MetricDatum... metricData) { if (this.metricData == null) { setMetricData(new com.amazonaws.internal.SdkInternalList<MetricDatum>(metricData.length)); } for (MetricDatum ele : metricData) { this.metricData.add(ele); } return this; }
@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); } } }
@Override public void doWrite(Server server, Query query, Iterable<Result> results) throws Exception { PutMetricDataRequest metricDataRequest = new PutMetricDataRequest(); metricDataRequest.setNamespace(namespace); List<MetricDatum> metricDatumList = new ArrayList<>(); // Iterating through the list of query results for (Result result : results) { try { metricDatumList.add(processResult(result)); } catch (IllegalArgumentException iae) { log.error("Could not convert result to double", iae); } } metricDataRequest.setMetricData(metricDatumList); cloudWatchClient.putMetricData(metricDataRequest); }
/** * <p> * A list of data describing the metric. * </p> * <p> * Returns a reference to this object so that method calls can be chained * together. * * @param metricData <p> * A list of data describing the metric. * </p> * @return A reference to this updated object so that method calls can be * chained together. */ public PutMetricDataRequest withMetricData(java.util.Collection<MetricDatum> metricData) { setMetricData(metricData); return this; }
.withDimensions(dimension); PutMetricDataRequest request = new PutMetricDataRequest() .withNamespace("SITE/TRAFFIC") .withMetricData(datum);
@Override public void publishMetrics(List<MetricDatumWithKey<CWMetricKey>> dataToPublish) { for (int startIndex = 0; startIndex < dataToPublish.size(); startIndex += BATCH_SIZE) { int endIndex = Math.min(dataToPublish.size(), startIndex + BATCH_SIZE); PutMetricDataRequest request = new PutMetricDataRequest(); request.setNamespace(namespace); List<MetricDatum> metricData = new ArrayList<MetricDatum>(); for (int i = startIndex; i < endIndex; i++) { metricData.add(dataToPublish.get(i).datum); } request.setMetricData(metricData); try { cloudWatchClient.putMetricData(request); LOG.debug(String.format("Successfully published %d datums.", endIndex - startIndex)); } catch (AmazonClientException e) { LOG.warn(String.format("Could not publish %d datums to CloudWatch", endIndex - startIndex), e); } } } }
private static boolean putValue(String name, long timestamp, double value) { Date d = new Date(timestamp); MetricDatum m = new MetricDatum().withMetricName(name).withTimestamp(d).withValue(value); PutMetricDataRequest req = new PutMetricDataRequest().withNamespace("TEST") .withMetricData(m); try { CLIENT.putMetricData(req); return true; } catch (Exception e) { System.out.printf("ERROR %e %d - %s: %s%n", value, Math.getExponent(value), e.getClass().getName(), e.getMessage()); return false; } }