@Override MetricInfo parse(String... args) throws Exception { String message; if (args.length == 2) { return MetricInfo.of(args[0], args[1]); } else if (args.length > 2) { message = "Too many arguments."; } else { message = "Missing required metric name or filter."; } throw new IllegalArgumentException(message); }
/** 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; }
/** * 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; }
@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(); }
@Test public void testToAndFromPb() { compareMetricInfo(METRIC_INFO, MetricInfo.fromPb(METRIC_INFO.toPb())); MetricInfo metricInfo = MetricInfo.of(NAME, FILTER); compareMetricInfo(metricInfo, MetricInfo.fromPb(metricInfo.toPb())); }
@Test public void testOf() { MetricInfo metricInfo = MetricInfo.of(NAME, FILTER); assertEquals(NAME, metricInfo.getName()); assertEquals(FILTER, metricInfo.getFilter()); assertNull(metricInfo.getDescription()); }