Search in sources :

Example 1 with MonitoredResource

use of com.google.cloud.MonitoredResource in project google-cloud-java by GoogleCloudPlatform.

the class LoggingImpl method writeLogEntriesRequest.

private static WriteLogEntriesRequest writeLogEntriesRequest(LoggingOptions serviceOptions, Iterable<LogEntry> logEntries, Map<Option.OptionType, ?> options) {
    String projectId = serviceOptions.getProjectId();
    WriteLogEntriesRequest.Builder builder = WriteLogEntriesRequest.newBuilder();
    String logName = LOG_NAME.get(options);
    if (logName != null) {
        builder.setLogName(LogName.create(projectId, logName).toString());
    }
    MonitoredResource resource = RESOURCE.get(options);
    if (resource != null) {
        builder.setResource(resource.toPb());
    }
    Map<String, String> labels = LABELS.get(options);
    if (labels != null) {
        builder.putAllLabels(labels);
    }
    builder.addAllEntries(Iterables.transform(logEntries, LogEntry.toPbFunction(projectId)));
    return builder.build();
}
Also used : WriteLogEntriesRequest(com.google.logging.v2.WriteLogEntriesRequest) MonitoredResource(com.google.cloud.MonitoredResource)

Example 2 with MonitoredResource

use of com.google.cloud.MonitoredResource in project google-cloud-java by GoogleCloudPlatform.

the class LoggingHandlerTest method testPublishCustomResource.

@Test
public void testPublishCustomResource() {
    expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
    expect(options.getService()).andReturn(logging);
    logging.setFlushSeverity(Severity.ERROR);
    expectLastCall().once();
    logging.setWriteSynchronicity(Synchronicity.ASYNC);
    expectLastCall().once();
    MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.<String, String>of());
    logging.write(ImmutableList.of(FINEST_ENTRY), WriteOption.logName(LOG_NAME), WriteOption.resource(resource), WriteOption.labels(BASE_SEVERITY_MAP));
    expectLastCall().once();
    replay(options, logging);
    Handler handler = new LoggingHandler(LOG_NAME, options, resource);
    handler.setLevel(Level.ALL);
    handler.setFormatter(new TestFormatter());
    handler.publish(newLogRecord(Level.FINEST, MESSAGE));
}
Also used : MonitoredResource(com.google.cloud.MonitoredResource) Handler(java.util.logging.Handler) Test(org.junit.Test)

Example 3 with MonitoredResource

use of com.google.cloud.MonitoredResource in project google-cloud-java by GoogleCloudPlatform.

the class LoggingHandlerTest method testEnhancedLogEntry.

@Test
public void testEnhancedLogEntry() {
    expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
    expect(options.getService()).andReturn(logging);
    MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.<String, String>of());
    logging.setFlushSeverity(Severity.ERROR);
    expectLastCall().once();
    logging.setWriteSynchronicity(Synchronicity.ASYNC);
    expectLastCall().once();
    logging.write(ImmutableList.of(FINEST_ENHANCED_ENTRY), WriteOption.logName(LOG_NAME), WriteOption.resource(resource), WriteOption.labels(BASE_SEVERITY_MAP));
    expectLastCall().once();
    replay(options, logging);
    LoggingEnhancer enhancer = new LoggingEnhancer() {

        @Override
        public void enhanceLogEntry(Builder builder) {
            builder.addLabel("enhanced", "true");
        }
    };
    Handler handler = new LoggingHandler(LOG_NAME, options, resource, Collections.singletonList(enhancer));
    handler.setLevel(Level.ALL);
    handler.setFormatter(new TestFormatter());
    handler.publish(newLogRecord(Level.FINEST, MESSAGE));
}
Also used : Builder(com.google.cloud.logging.LogEntry.Builder) MonitoredResource(com.google.cloud.MonitoredResource) Handler(java.util.logging.Handler) Test(org.junit.Test)

Example 4 with MonitoredResource

use of com.google.cloud.MonitoredResource in project google-cloud-java by GoogleCloudPlatform.

the class MonitoredResourceUtil method getResource.

/* Return a self-configured monitored Resource. */
public static MonitoredResource getResource(String projectId, String resourceTypeParam) {
    String resourceType = resourceTypeParam;
    if (Strings.isNullOrEmpty(resourceType)) {
        Resource detectedResourceType = getAutoDetectedResourceType();
        resourceType = detectedResourceType.getKey();
    }
    // Currently, "gae_app" is the supported logging Resource type, but we distinguish
    // between "gae_app_flex", "gae_app_standard" to support zone id, instance name logging on flex VMs.
    // Hence, "gae_app_flex", "gae_app_standard" are trimmed to "gae_app"
    String resourceName = resourceType.startsWith("gae_app") ? "gae_app" : resourceType;
    MonitoredResource.Builder builder = MonitoredResource.newBuilder(resourceName).addLabel(Label.ProjectId.getKey(), projectId);
    Label[] resourceLabels = resourceTypeWithLabels.get(resourceType);
    if (resourceLabels != null) {
        for (Label label : resourceLabels) {
            String value = getValue(label);
            if (value != null) {
                builder.addLabel(label.getKey(), value);
            }
        }
    }
    return builder.build();
}
Also used : MonitoredResource(com.google.cloud.MonitoredResource) MonitoredResource(com.google.cloud.MonitoredResource)

Example 5 with MonitoredResource

use of com.google.cloud.MonitoredResource in project google-cloud-java by GoogleCloudPlatform.

the class BaseSystemTest method testSyncLoggingHandler.

@Test
public void testSyncLoggingHandler() throws InterruptedException {
    String logName = formatForTest("test-sync-logging-handler");
    LoggingOptions options = logging().getOptions();
    MonitoredResource resource = MonitoredResource.of("gce_instance", ImmutableMap.of("project_id", options.getProjectId(), "instance_id", "instance", "zone", "us-central1-a"));
    LoggingHandler handler = new LoggingHandler(logName, options, resource);
    handler.setLevel(Level.WARNING);
    handler.setSynchronicity(Synchronicity.SYNC);
    Logger logger = Logger.getLogger(getClass().getName());
    logger.addHandler(handler);
    logger.setLevel(Level.WARNING);
    logger.warning("Message");
    Iterator<LogEntry> iterator = logging().listLogEntries(EntryListOption.filter("logName:" + logName)).iterateAll().iterator();
    while (!iterator.hasNext()) {
        Thread.sleep(500L);
        iterator = logging().listLogEntries(EntryListOption.filter("logName:" + logName)).iterateAll().iterator();
    }
    assertTrue(iterator.hasNext());
    LogEntry entry = iterator.next();
    assertTrue(entry.getPayload() instanceof StringPayload);
    assertTrue(entry.<StringPayload>getPayload().getData().contains("Message"));
    assertEquals(logName, entry.getLogName());
    assertEquals(ImmutableMap.of("levelName", "WARNING", "levelValue", String.valueOf(Level.WARNING.intValue())), entry.getLabels());
    assertEquals(resource, entry.getResource());
    assertNull(entry.getHttpRequest());
    assertEquals(Severity.WARNING, entry.getSeverity());
    assertNull(entry.getOperation());
    assertNotNull(entry.getInsertId());
    assertNotNull(entry.getTimestamp());
    assertFalse(iterator.hasNext());
    logger.removeHandler(handler);
    logging().deleteLog(logName);
}
Also used : MonitoredResource(com.google.cloud.MonitoredResource) StringPayload(com.google.cloud.logging.Payload.StringPayload) Logger(java.util.logging.Logger) Test(org.junit.Test)

Aggregations

MonitoredResource (com.google.cloud.MonitoredResource)6 Test (org.junit.Test)3 Handler (java.util.logging.Handler)2 Builder (com.google.cloud.logging.LogEntry.Builder)1 LoggingEnhancer (com.google.cloud.logging.LoggingEnhancer)1 StringPayload (com.google.cloud.logging.Payload.StringPayload)1 WriteLogEntriesRequest (com.google.logging.v2.WriteLogEntriesRequest)1 Logger (java.util.logging.Logger)1