@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(); }
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); 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; } }
MetricDescriptorCache { 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); 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; } } }
MetricDescriptorCache { 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); 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; } } MetricDescriptorCache(ConfigParams configParams); }
MetricDescriptorCache { 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); 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; } } MetricDescriptorCache(ConfigParams configParams); String idToDescriptorType(Id id); Map<String, String> getExtraTimeSeriesLabels(); void addExtraTimeSeriesLabel(String key, String value); String meterToKind(Registry registry, Meter meter); Map<String, String> tagsToTimeSeriesLabels(String descriptorType, Iterable<Tag> tags); boolean meterIsTimer(Registry registry, Meter meter); String descriptorTypeToKind(String descriptorType, Registry registry, Meter meter); MetricDescriptor addLabel(String descriptorType, String newLabel); }
MetricDescriptorCache { 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); 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; } } MetricDescriptorCache(ConfigParams configParams); String idToDescriptorType(Id id); Map<String, String> getExtraTimeSeriesLabels(); void addExtraTimeSeriesLabel(String key, String value); String meterToKind(Registry registry, Meter meter); Map<String, String> tagsToTimeSeriesLabels(String descriptorType, Iterable<Tag> tags); boolean meterIsTimer(Registry registry, Meter meter); String descriptorTypeToKind(String descriptorType, Registry registry, Meter meter); MetricDescriptor addLabel(String descriptorType, String newLabel); static final String INSTANCE_LABEL; }