/** Example of getting a metric. */ // [TARGET getMetric(String)] // [VARIABLE "my_metric_name"] public Metric getMetric(String metricName) { // [START getMetric] Metric metric = logging.getMetric(metricName); if (metric == null) { // metric was not found } // [END getMetric] return metric; }
@Override public void run(Logging logging, String metric) { System.out.printf("Metric info: %s%n", logging.getMetric(metric)); } }
/** * Fetches current metric's latest information. Returns {@code null} if the metric does not exist. * * <p>Example of getting the metric's latest information. * * <pre>{@code * Metric latestMetric = metric.reload(); * if (latestMetric == null) { * // the metric was not found * } * }</pre> * * @return a {@code Metric} object with latest information or {@code null} if not found * @throws LoggingException upon failure */ public Metric reload() { return logging.getMetric(getName()); }
@Test public void testReloadNull() { initializeExpectedMetric(1); expect(logging.getOptions()).andReturn(mockOptions); expect(logging.getMetric(NAME)).andReturn(null); replay(logging); initializeMetric(); assertNull(metric.reload()); }
@Test public void testGetMetric_Null() { ApiFuture<LogMetric> response = ApiFutures.immediateFuture(null); GetLogMetricRequest request = GetLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build(); EasyMock.expect(loggingRpcMock.get(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); assertNull(logging.getMetric(METRIC_NAME)); }
@Test public void testReload() { initializeExpectedMetric(2); MetricInfo updatedInfo = METRIC_INFO.toBuilder().setFilter(NEW_FILTER).build(); Metric expectedMetric = new Metric(serviceMockReturnsOptions, new MetricInfo.BuilderImpl(updatedInfo)); expect(logging.getOptions()).andReturn(mockOptions); expect(logging.getMetric(NAME)).andReturn(expectedMetric); replay(logging); initializeMetric(); Metric updatedMetric = metric.reload(); compareMetric(expectedMetric, updatedMetric); }
@Test public void testGetMetric() { LogMetric sinkPb = METRIC_INFO.toPb(); ApiFuture<LogMetric> response = ApiFutures.immediateFuture(sinkPb); GetLogMetricRequest request = GetLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build(); EasyMock.expect(loggingRpcMock.get(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Metric sink = logging.getMetric(METRIC_NAME); assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), sink); }
@Test public void testUpdateNonExistingMetric() { String name = formatForTest("test-update-non-existing-metric"); MetricInfo metricInfo = MetricInfo.newBuilder(name, "severity>=ERROR").setDescription("description").build(); assertNull(logging().getMetric(name)); Metric metric = logging().update(metricInfo); assertEquals(name, metric.getName()); assertEquals("severity>=ERROR", metric.getFilter()); assertEquals("description", metric.getDescription()); assertTrue(metric.delete()); }
@Test public void testCreateGetUpdateAndDeleteMetric() { String name = formatForTest("test-create-get-update-metric"); MetricInfo metricInfo = MetricInfo.newBuilder(name, "severity>=ERROR").setDescription("description").build(); Metric metric = logging().create(metricInfo); assertEquals(name, metric.getName()); assertEquals("severity>=ERROR", metric.getFilter()); assertEquals("description", metric.getDescription()); assertEquals(metric, logging().getMetric(name)); metric = metric .toBuilder() .setDescription("newDescription") .setFilter("severity>=WARNING") .build() .update(); assertEquals(name, metric.getName()); assertEquals("severity>=WARNING", metric.getFilter()); assertEquals("newDescription", metric.getDescription()); assertTrue(metric.delete()); assertFalse(metric.delete()); }
/** * Fetches current metric's latest information. Returns {@code null} if the metric does not exist. * * <p>Example of getting the metric's latest information. * * <pre>{@code * Metric latestMetric = metric.reload(); * if (latestMetric == null) { * // the metric was not found * } * }</pre> * * @return a {@code Metric} object with latest information or {@code null} if not found * @throws LoggingException upon failure */ public Metric reload() { return logging.getMetric(getName()); }