Search in sources :

Example 1 with MetricDescriptor

use of com.google.api.services.monitoring.v3.model.MetricDescriptor 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;
    }
}
Also used : MetricDescriptor(com.google.api.services.monitoring.v3.model.MetricDescriptor) IOException(java.io.IOException) LabelDescriptor(com.google.api.services.monitoring.v3.model.LabelDescriptor)

Example 2 with MetricDescriptor

use of com.google.api.services.monitoring.v3.model.MetricDescriptor in project kork by spinnaker.

the class MetricDescriptorCacheTest method testAddLabelWithCreateFailure.

@Test
public void testAddLabelWithCreateFailure() throws IOException {
    List<String> origTags = Arrays.asList("tagA", "tagB");
    MetricDescriptor origDescriptor = makeDescriptor(idA, origTags, "GAUGE");
    String type = origDescriptor.getType();
    String label = "newTag";
    List<String> updatedTags = Arrays.asList("tagA", "tagB", label);
    MetricDescriptor updatedDescriptor = makeDescriptor(idA, updatedTags, "GAUGE");
    Monitoring.Projects.MetricDescriptors.Get mockGetMethod = Mockito.mock(Monitoring.Projects.MetricDescriptors.Get.class);
    Monitoring.Projects.MetricDescriptors.Delete mockDeleteMethod = Mockito.mock(Monitoring.Projects.MetricDescriptors.Delete.class);
    Monitoring.Projects.MetricDescriptors.Create mockCreateMethod = Mockito.mock(Monitoring.Projects.MetricDescriptors.Create.class);
    String descriptorName = "projects/test-project/metricDescriptors/" + type;
    when(descriptorsApi.get(eq(descriptorName))).thenReturn(mockGetMethod);
    when(descriptorsApi.delete(eq(descriptorName))).thenReturn(mockDeleteMethod);
    when(descriptorsApi.create(eq("projects/test-project"), eq(updatedDescriptor))).thenReturn(mockCreateMethod);
    when(mockGetMethod.execute()).thenReturn(origDescriptor);
    when(mockCreateMethod.execute()).thenThrow(new IOException("Not Found"));
    Assert.assertNull(cache.addLabel(type, label));
    verify(mockGetMethod, times(1)).execute();
    verify(mockDeleteMethod, times(1)).execute();
    verify(mockCreateMethod, times(1)).execute();
}
Also used : IOException(java.io.IOException) Monitoring(com.google.api.services.monitoring.v3.Monitoring) Test(org.junit.Test)

Example 3 with MetricDescriptor

use of com.google.api.services.monitoring.v3.model.MetricDescriptor in project kork by spinnaker.

the class MetricDescriptorCacheTest method testAddLabelWithDeleteFailure.

@Test
public void testAddLabelWithDeleteFailure() throws IOException {
    List<String> origTags = Arrays.asList("tagA", "tagB");
    MetricDescriptor origDescriptor = makeDescriptor(idA, origTags, "GAUGE");
    String type = origDescriptor.getType();
    String label = "newTag";
    List<String> updatedTags = Arrays.asList("tagA", "tagB", label);
    MetricDescriptor updatedDescriptor = makeDescriptor(idA, updatedTags, "GAUGE");
    Monitoring.Projects.MetricDescriptors.Get mockGetMethod = Mockito.mock(Monitoring.Projects.MetricDescriptors.Get.class);
    Monitoring.Projects.MetricDescriptors.Delete mockDeleteMethod = Mockito.mock(Monitoring.Projects.MetricDescriptors.Delete.class);
    Monitoring.Projects.MetricDescriptors.Create mockCreateMethod = Mockito.mock(Monitoring.Projects.MetricDescriptors.Create.class);
    String descriptorName = "projects/test-project/metricDescriptors/" + type;
    when(descriptorsApi.get(eq(descriptorName))).thenReturn(mockGetMethod);
    when(descriptorsApi.delete(eq(descriptorName))).thenReturn(mockDeleteMethod);
    when(descriptorsApi.create(eq("projects/test-project"), eq(updatedDescriptor))).thenReturn(mockCreateMethod);
    when(mockGetMethod.execute()).thenReturn(origDescriptor);
    when(mockDeleteMethod.execute()).thenThrow(new IOException("Not Found"));
    when(mockCreateMethod.execute()).thenReturn(updatedDescriptor);
    Assert.assertEquals(updatedDescriptor, cache.addLabel(type, label));
    verify(mockGetMethod, times(1)).execute();
    verify(mockDeleteMethod, times(1)).execute();
    verify(mockCreateMethod, times(1)).execute();
}
Also used : IOException(java.io.IOException) Monitoring(com.google.api.services.monitoring.v3.Monitoring) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)3 Monitoring (com.google.api.services.monitoring.v3.Monitoring)2 Test (org.junit.Test)2 LabelDescriptor (com.google.api.services.monitoring.v3.model.LabelDescriptor)1 MetricDescriptor (com.google.api.services.monitoring.v3.model.MetricDescriptor)1