/** Example of updating a metric. */ // [TARGET update(MetricInfo)] // [VARIABLE "my_metric_name"] public Metric updateMetric(String metricName) { // [START updateMetric] MetricInfo metricInfo = MetricInfo.newBuilder(metricName, "severity>=ERROR") .setDescription("new description") .build(); Metric metric = logging.update(metricInfo); // [END updateMetric] return metric; }
private void compareMetricInfo(MetricInfo expected, MetricInfo value) { assertEquals(expected, value); assertEquals(expected.getName(), value.getName()); assertEquals(expected.getDescription(), value.getDescription()); assertEquals(expected.getFilter(), value.getFilter()); assertEquals(expected.hashCode(), value.hashCode()); assertEquals(expected.toString(), value.toString()); } }
@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()); }
@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 testListMetrics() throws InterruptedException { String firstName = formatForTest("test-list-metrics-1"); String secondName = formatForTest("test-list-metrics-2"); Metric firstMetric = logging().create(MetricInfo.of(firstName, "severity>=ERROR")); Metric secondMetric = logging().create(MetricInfo.of(secondName, "severity>=ERROR")); Logging.ListOption[] options = {Logging.ListOption.pageSize(1)}; Page<Metric> metricPage = logging().listMetrics(options); Set<Metric> metrics = Sets.newHashSet(metricPage.iterateAll()); while (!metrics.contains(firstMetric) || !metrics.contains(secondMetric)) { Thread.sleep(500); metrics = Sets.newHashSet(logging().listMetrics(options).iterateAll()); } firstMetric.delete(); secondMetric.delete(); }
public static void main(String... args) throws Exception { // Create a service object // Credentials are inferred from the environment try (Logging logging = LoggingOptions.getDefaultInstance().getService()) { // Create a metric MetricInfo metricInfo = MetricInfo.newBuilder("test-metric", "severity >= ERROR") .setDescription("Log entries with severity higher or equal to ERROR") .build(); logging.create(metricInfo); // List metrics Page<Metric> metrics = logging.listMetrics(); for (Metric metric : metrics.iterateAll()) { System.out.println(metric); } } } }
@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 testUpdateMetric() { LogMetric sinkPb = METRIC_INFO.toPb(); ApiFuture<LogMetric> response = ApiFutures.immediateFuture(sinkPb); UpdateLogMetricRequest request = UpdateLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).setMetric(sinkPb).build(); EasyMock.expect(loggingRpcMock.update(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Metric sink = logging.update(METRIC_INFO); assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), sink); }
@Test public void testGetMetricAsync() throws ExecutionException, InterruptedException { 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.getMetricAsync(METRIC_NAME).get(); assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), sink); }
@Test public void testUpdateMetricAsync() throws ExecutionException, InterruptedException { LogMetric sinkPb = METRIC_INFO.toPb(); ApiFuture<LogMetric> response = ApiFutures.immediateFuture(sinkPb); UpdateLogMetricRequest request = UpdateLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).setMetric(sinkPb).build(); EasyMock.expect(loggingRpcMock.update(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Metric sink = logging.updateAsync(METRIC_INFO).get(); assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), sink); }
/** Example of asynchronously updating a metric. */ // [TARGET updateAsync(MetricInfo)] // [VARIABLE "my_metric_name"] public Metric updateMetricAsync(String metricName) throws ExecutionException, InterruptedException { // [START updateMetricAsync] MetricInfo metricInfo = MetricInfo.newBuilder(metricName, "severity>=ERROR") .setDescription("new description") .build(); Future<Metric> future = logging.updateAsync(metricInfo); // ... Metric metric = future.get(); // [END updateMetricAsync] return metric; }
@Override public ApiFuture<Metric> updateAsync(MetricInfo metric) { UpdateLogMetricRequest request = UpdateLogMetricRequest.newBuilder() .setMetricName( ProjectMetricName.of(getOptions().getProjectId(), metric.getName()).toString()) .setMetric(metric.toPb()) .build(); return transform(rpc.update(request), Metric.fromPbFunction(this)); }
@Test public void testOf() { MetricInfo metricInfo = MetricInfo.of(NAME, FILTER); assertEquals(NAME, metricInfo.getName()); assertEquals(FILTER, metricInfo.getFilter()); assertNull(metricInfo.getDescription()); }
@Test public void testCreateMetric() { LogMetric metricPb = METRIC_INFO.toPb(); ApiFuture<LogMetric> response = ApiFutures.immediateFuture(metricPb); CreateLogMetricRequest request = CreateLogMetricRequest.newBuilder().setParent(PROJECT_PB).setMetric(metricPb).build(); EasyMock.expect(loggingRpcMock.create(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Metric metric = logging.create(METRIC_INFO); assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), metric); }
@Test public void testCreateMetricAsync() throws ExecutionException, InterruptedException { LogMetric metricPb = METRIC_INFO.toPb(); ApiFuture<LogMetric> response = ApiFutures.immediateFuture(metricPb); CreateLogMetricRequest request = CreateLogMetricRequest.newBuilder().setParent(PROJECT_PB).setMetric(metricPb).build(); EasyMock.expect(loggingRpcMock.create(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Metric metric = logging.createAsync(METRIC_INFO).get(); assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), metric); }
/** Example of creating a metric for logs with severity higher or equal to ERROR. */ // [TARGET create(MetricInfo)] // [VARIABLE "my_metric_name"] public Metric createMetric(String metricName) { // [START createMetric] MetricInfo metricInfo = MetricInfo.of(metricName, "severity>=ERROR"); Metric metric = logging.create(metricInfo); // [END createMetric] return metric; }
@Test public void testToBuilder() { compareMetricInfo(METRIC_INFO, METRIC_INFO.toBuilder().build()); MetricInfo metricInfo = METRIC_INFO .toBuilder() .setName(NEW_NAME) .setDescription(NEW_DESCRIPTION) .setFilter(NEW_FILTER) .build(); assertEquals(NEW_NAME, metricInfo.getName()); assertEquals(NEW_FILTER, metricInfo.getFilter()); assertEquals(NEW_DESCRIPTION, metricInfo.getDescription()); metricInfo = metricInfo.toBuilder().setName(NAME).setDescription(DESCRIPTION).setFilter(FILTER).build(); compareMetricInfo(METRIC_INFO, metricInfo); }
/** * Example of asynchronously creating a metric for logs with severity higher or equal to ERROR. */ // [TARGET createAsync(MetricInfo)] // [VARIABLE "my_metric_name"] public Metric createMetricAsync(String metricName) throws ExecutionException, InterruptedException { // [START createMetricAsync] MetricInfo metricInfo = MetricInfo.of(metricName, "severity>=ERROR"); Future<Metric> future = logging.createAsync(metricInfo); // ... Metric metric = future.get(); // [END createMetricAsync] return metric; }