@Override public void run(Logging logging, MetricInfo metric) { System.out.printf("Created metric %s%n", logging.create(metric)); }
@Override public void run(Logging logging, SinkInfo sink) { System.out.printf("Created sink %s%n", logging.create(sink)); }
/** 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 creating a sink to export logs to a BigQuery dataset (in the {@link * LoggingOptions#getProjectId()} project). */ // [TARGET create(SinkInfo)] // [VARIABLE "my_sink_name"] // [VARIABLE "my_dataset"] public Sink createSink(String sinkName, String datasetName) { // [START logging_create_sink] SinkInfo sinkInfo = SinkInfo.of(sinkName, DatasetDestination.of(datasetName)); Sink sink = logging.create(sinkInfo); // [END logging_create_sink] return sink; }
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); } } } }
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 sink to back log entries to a BigQuery dataset SinkInfo sinkInfo = SinkInfo.newBuilder("test-sink", DatasetDestination.of("test-dataset")) .setFilter("severity >= ERROR") .build(); logging.create(sinkInfo); // List sinks Page<Sink> sinks = logging.listSinks(); for (Sink sink : sinks.iterateAll()) { System.out.println(sink); } } } }
@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 testListSinks() throws InterruptedException { String firstName = formatForTest("test-list-sinks-1"); String secondName = formatForTest("test-list-sinks-2"); Sink firstSink = logging().create(SinkInfo.of(firstName, DatasetDestination.of("dataset"))); Sink secondSink = logging().create(SinkInfo.of(secondName, DatasetDestination.of("dataset"))); Logging.ListOption[] options = {Logging.ListOption.pageSize(1)}; Page<Sink> sinkPage = logging().listSinks(options); Set<Sink> sinks = Sets.newHashSet(sinkPage.iterateAll()); while (!sinks.contains(firstSink) || !sinks.contains(secondSink)) { Thread.sleep(500); sinks = Sets.newHashSet(logging().listSinks(options).iterateAll()); } firstSink.delete(); secondSink.delete(); }
@Test public void testCreateSink() { LogSink sinkPb = SINK_INFO.toPb(PROJECT); ApiFuture<LogSink> response = ApiFutures.immediateFuture(sinkPb); CreateSinkRequest request = CreateSinkRequest.newBuilder().setParent(PROJECT_PB).setSink(sinkPb).build(); EasyMock.expect(loggingRpcMock.create(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Sink sink = logging.create(SINK_INFO); assertEquals(new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)), sink); }
@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 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 testCreateGetUpdateAndDeleteSink() { String name = formatForTest("test-create-get-update-sink"); SinkInfo sinkInfo = SinkInfo.newBuilder(name, DatasetDestination.of("dataset")) .setFilter("severity>=ERROR") .setVersionFormat(SinkInfo.VersionFormat.V2) .build(); Sink sink = logging().create(sinkInfo); assertEquals(name, sink.getName()); assertEquals(SinkInfo.VersionFormat.V2, sink.getVersionFormat()); assertEquals("severity>=ERROR", sink.getFilter()); DatasetDestination datasetDestination = sink.getDestination(); assertEquals(logging().getOptions().getProjectId(), datasetDestination.getProject()); assertEquals("dataset", datasetDestination.getDataset()); assertEquals(sink, logging().getSink(name)); sink = sink.toBuilder().setFilter("severity<=ERROR").build().update(); assertEquals(name, sink.getName()); assertEquals(SinkInfo.VersionFormat.V2, sink.getVersionFormat()); assertEquals("severity<=ERROR", sink.getFilter()); assertTrue(sink.delete()); assertFalse(sink.delete()); }