use of com.google.api.services.monitoring.v3.model.Metric in project kork by spinnaker.
the class MetricDescriptorCache method addLabel.
/**
* Hack another label into an existing Stackdriver Custom Metric Descriptor.
*
* <p>This may lose historic time series data. It returns the new descriptor (result from create)
* only for testing purposes.
*/
public MetricDescriptor addLabel(String descriptorType, String newLabel) {
String descriptorName = projectResourceName + "/metricDescriptors/" + descriptorType;
MetricDescriptor descriptor;
log.info("Adding label '{}' to stackdriver descriptor '{}'. This may lose existing metric data", newLabel, descriptorName);
try {
descriptor = service.projects().metricDescriptors().get(descriptorName).execute();
} catch (IOException ioex) {
log.error("Could not fetch descriptor " + descriptorType + ": " + ioex);
return null;
}
List<LabelDescriptor> labels = descriptor.getLabels();
if (labels == null) {
labels = new ArrayList<LabelDescriptor>();
descriptor.setLabels(labels);
}
for (LabelDescriptor labelDescriptor : descriptor.getLabels()) {
if (labelDescriptor.getKey().equals(newLabel)) {
log.info("{} already added to descriptor", newLabel);
return descriptor;
}
}
LabelDescriptor labelDescriptor = new LabelDescriptor();
labelDescriptor.setKey(newLabel);
labelDescriptor.setValueType("STRING");
descriptor.getLabels().add(labelDescriptor);
/* Catch but ignore errors. Assume there is another process making mods too.
* We may overwrite theirs, but they'll just have to make them again later.
*/
try {
log.info("Deleting existing stackdriver descriptor {}", descriptorName);
service.projects().metricDescriptors().delete(descriptorName).execute();
} catch (IOException ioex) {
log.info("Ignoring error " + ioex);
}
try {
log.info("Adding new descriptor for {}", descriptorName);
return service.projects().metricDescriptors().create(projectResourceName, descriptor).execute();
} catch (IOException ioex) {
log.error("Failed to update the descriptor definition: " + ioex);
return null;
}
}
use of com.google.api.services.monitoring.v3.model.Metric in project kork by spinnaker.
the class StackdriverWriter method writeRegistryHelper.
/**
* Implementation of writeRegistry wrapped for timing.
*/
private void writeRegistryHelper(Registry registry) {
MonitoredResource resource = determineMonitoredResource();
if (resource == null) {
log.warn("Cannot determine the managed resource - not flushing metrics.");
return;
}
List<TimeSeries> tsList = registryToTimeSeries(registry);
if (tsList.isEmpty()) {
log.debug("No metric data points.");
return;
}
CreateTimeSeriesRequest tsRequest = new CreateTimeSeriesRequest();
int offset = 0;
int failed = 0;
List<TimeSeries> nextN;
log.debug("Writing metrics...");
while (offset < tsList.size()) {
if (offset + MAX_TS_PER_REQUEST < tsList.size()) {
nextN = tsList.subList(offset, offset + MAX_TS_PER_REQUEST);
offset += MAX_TS_PER_REQUEST;
} else {
nextN = tsList.subList(offset, tsList.size());
offset = tsList.size();
}
tsRequest.setTimeSeries(nextN);
try {
service.projects().timeSeries().create(projectResourceName, tsRequest).execute();
} catch (HttpResponseException rex) {
handleTimeSeriesResponseException(rex, "creating time series", nextN);
failed += nextN.size();
} catch (IOException ioex) {
log.error("Caught Exception creating time series " + ioex);
failed += nextN.size();
}
}
log.debug("Wrote {} values", tsList.size() - failed);
}
use of com.google.api.services.monitoring.v3.model.Metric in project kork by spinnaker.
the class StackdriverWriter method measurementToTimeSeries.
/**
* Convert a Spectator metric Meter into a Stackdriver TimeSeries entry.
*
* @param descriptorType The Stackdriver MetricDescriptorType name for the measurement.
* @param measurement The Spectator Measurement to encode.
* @return The Stackdriver TimeSeries equivalent for the measurement.
*/
public TimeSeries measurementToTimeSeries(String descriptorType, Registry registry, Meter meter, Measurement measurement) {
Map<String, String> labels = cache.tagsToTimeSeriesLabels(descriptorType, measurement.id().tags());
long millis = measurement.timestamp();
double value = measurement.value();
TimeInterval timeInterval = new TimeInterval();
Date date = new Date(millis);
timeInterval.setEndTime(rfc3339.format(date));
String descriptorKind = cache.descriptorTypeToKind(descriptorType, registry, meter);
if (descriptorKind == "CUMULATIVE") {
timeInterval.setStartTime(counterStartTimeRfc3339);
}
TypedValue typedValue = new TypedValue();
typedValue.setDoubleValue(value);
Point point = new Point();
point.setValue(typedValue);
point.setInterval(timeInterval);
Metric metric = new Metric();
metric.setType(descriptorType);
metric.setLabels(labels);
TimeSeries ts = new TimeSeries();
ts.setResource(monitoredResource);
ts.setMetric(metric);
ts.setMetricKind(descriptorKind);
ts.setValueType("DOUBLE");
ts.setPoints(Lists.<Point>newArrayList(point));
return ts;
}
Aggregations