use of com.google.api.services.monitoring.v3.model.LabelDescriptor 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;
}
}
Aggregations