Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 674 gcp logging spaninfo #677

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collections;
import java.util.logging.ErrorManager;
import java.util.logging.Level;

import org.jboss.logmanager.ExtHandler;
import org.jboss.logmanager.ExtLogRecord;
Expand All @@ -11,6 +12,7 @@
import com.google.cloud.logging.Logging;
import com.google.cloud.logging.Logging.WriteOption;
import com.google.cloud.logging.Payload;
import com.google.cloud.logging.Severity;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;

Expand All @@ -25,6 +27,10 @@

public class LoggingHandler extends ExtHandler {

private static final String LEVEL_NAME_KEY = "levelName";
private static final String LEVEL_VALUE_KEY = "levelValue";
private static final String LOGGER_NAME_KEY = "loggerName";

private final LoggingConfiguration config;

// lazy values, they depend on the gcp config which in turn
Expand Down Expand Up @@ -79,11 +85,24 @@ public void doPublish(ExtLogRecord record) {
private LogEntry transform(ExtLogRecord record, TraceInfo trace) {
Payload<?> payload = internalHandler.transform(record, trace);
if (payload != null) {
Level level = record.getLevel();
Severity severity = LevelTransformer.toSeverity(level);
com.google.cloud.logging.LogEntry.Builder builder = LogEntry.newBuilder(payload)
.setSeverity(LevelTransformer.toSeverity(record.getLevel()))
.setTimestamp(record.getInstant());
.setSeverity(severity)
.setTimestamp(record.getInstant())
.addLabel(LEVEL_NAME_KEY, level.toString())
.addLabel(LEVEL_VALUE_KEY, String.valueOf(level.intValue()));

String loggerName = record.getSourceClassName();
if (!Strings.isNullOrEmpty(loggerName)) {
builder = builder.addLabel(LOGGER_NAME_KEY, loggerName);
}

if (this.config.gcpTracing().enabled() && trace != null && !Strings.isNullOrEmpty(trace.getTraceId())) {
builder = builder.setTrace(composeTraceString(trace.getTraceId()));
builder = builder
.setTrace(composeTraceString(trace.getTraceId()))
.setSpanId(trace.getSpanId())
.setTraceSampled(true);
}
return builder.build();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package io.quarkiverse.googlecloudservices.logging.runtime;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Level;

import org.jboss.logmanager.ExtLogRecord;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import com.google.cloud.logging.Logging;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;

/**
* Unit tests for the {@link LoggingHandler} class.
*/
class LoggingHandlerTest {

private final PrintStream standardOut = System.out;
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();

@BeforeEach
void setUp() {
// This enables us to capture anything written via System.out.println during a test
System.setOut(new PrintStream(outputStreamCaptor));
}

@AfterEach
void tearDown() {
// Reset System.out after test complete
System.setOut(standardOut);
}

@Test
void shouldLogToStdoutWithTraceInfoAndLabels() {
String traceId = UUID.randomUUID().toString();
String spanId = UUID.randomUUID().toString();
String msg = "hello world";
String projectId = "my-project";
int sourceLineNumber = 101;

ArcContainer container = createArcContainer(traceId, spanId);

try (MockedStatic<Arc> arc = Mockito.mockStatic(Arc.class)) {
arc.when(Arc::container).thenReturn(container);

try (LoggingHandler h = new LoggingHandler(createJsonStdoutLoggingConfiguration(projectId))) {
// Publish a log record
h.doPublish(createNewInfoLogRecord(msg, sourceLineNumber));
}

// Assert on what was written to System.out
final String logEntry = outputStreamCaptor.toString();
assertNotNull(logEntry);
JsonObject logEntryJson = JsonParser.parseString(logEntry).getAsJsonObject();
assertAll(
() -> assertEquals("INFO", logEntryJson.get("severity").getAsString()),
() -> assertNotNull(logEntryJson.get("time").getAsString()),
() -> assertNotNull(logEntryJson.get("@timestamp").getAsString()),
() -> assertEquals(msg, logEntryJson.get("message").getAsString()),
() -> assertEquals(String.format("projects/%s/traces/%s", projectId, traceId),
logEntryJson.get("logging.googleapis.com/trace").getAsString()),
() -> assertEquals(spanId, logEntryJson.get("logging.googleapis.com/spanId").getAsString()),
() -> assertTrue(logEntryJson.get("logging.googleapis.com/trace_sampled").getAsBoolean()));

JsonObject labels = logEntryJson.get("logging.googleapis.com/labels").getAsJsonObject();
assertNotNull(labels);
assertAll(
() -> assertEquals("INFO", labels.get("levelName").getAsString()),
() -> assertEquals("800", labels.get("levelValue").getAsString()),
() -> assertEquals(getClass().getName(), labels.get("loggerName").getAsString()));

JsonObject log = logEntryJson.get("log").getAsJsonObject();
assertNotNull(log);
assertAll(
() -> assertEquals("INFO", log.get("level").getAsString()),
() -> assertEquals(getClass().getName(), log.get("logger").getAsString()));

JsonObject origin = log.get("origin").getAsJsonObject();
assertNotNull(origin);
JsonObject classOrigin = origin.get("class").getAsJsonObject();
assertNotNull(classOrigin);
assertEquals(getClass().getName(), classOrigin.get("name").getAsString());
assertEquals(String.format("%s.0", sourceLineNumber), classOrigin.get("line").getAsNumber().toString());
}

}

private ArcContainer createArcContainer(String traceId, String spanId) {
ArcContainer container = Mockito.mock(ArcContainer.class);
InstanceHandle<TraceInfoExtractor> traceInfoInstanceHandler = Mockito.mock(InstanceHandle.class);
when(traceInfoInstanceHandler.get()).thenReturn(x -> new TraceInfo(traceId, spanId));
when(traceInfoInstanceHandler.isAvailable()).thenReturn(true);
when(container.instance(TraceInfoExtractor.class)).thenReturn(traceInfoInstanceHandler);

InstanceHandle<Logging> loggingInstanceHandler = Mockito.mock(InstanceHandle.class);
Logging logging = Mockito.mock(Logging.class);
when(loggingInstanceHandler.get()).thenReturn(logging);
when(container.instance(Logging.class)).thenReturn(loggingInstanceHandler);
return container;
}

private ExtLogRecord createNewInfoLogRecord(String msg, int sourceLineNumber) {
ExtLogRecord extLogRecord = new ExtLogRecord(Level.INFO, msg, getClass().getName());
extLogRecord.setSourceClassName(getClass().getName());
extLogRecord.setSourceLineNumber(sourceLineNumber);
return extLogRecord;
}

private LoggingConfiguration createJsonStdoutLoggingConfiguration(String projectId) {
LoggingConfiguration c = Mockito.mock(LoggingConfiguration.class);

LoggingConfiguration.MDCConfig mdc = Mockito.mock(LoggingConfiguration.MDCConfig.class);
when(mdc.included()).thenReturn(true);

LoggingConfiguration.StackTraceConfig stackTrace = Mockito.mock(LoggingConfiguration.StackTraceConfig.class);
when(stackTrace.included()).thenReturn(true);

LoggingConfiguration.ParametersConfig parameters = Mockito.mock(LoggingConfiguration.ParametersConfig.class);
when(parameters.included()).thenReturn(true);
when(parameters.fieldName()).thenReturn("parameters");

LoggingConfiguration.StructuredConfig structured = Mockito.mock(LoggingConfiguration.StructuredConfig.class);
when(structured.mdc()).thenReturn(mdc);
when(structured.stackTrace()).thenReturn(stackTrace);
when(structured.parameters()).thenReturn(parameters);
when(c.structured()).thenReturn(structured);

LoggingConfiguration.GcpTracingConfig gcpTracingConfig = Mockito.mock(LoggingConfiguration.GcpTracingConfig.class);
when(gcpTracingConfig.enabled()).thenReturn(true);
when(gcpTracingConfig.projectId()).thenReturn(Optional.of(projectId));
when(c.gcpTracing()).thenReturn(gcpTracingConfig);

LoggingConfiguration.ResourceConfig resourceConfig = Mockito.mock(LoggingConfiguration.ResourceConfig.class);
when(resourceConfig.type()).thenReturn("generic_node");
when(c.resource()).thenReturn(resourceConfig);

when(c.format()).thenReturn(LoggingConfiguration.LogFormat.JSON);
when(c.logTarget()).thenReturn(com.google.cloud.logging.LoggingHandler.LogTarget.STDOUT);

return c;
}
}
Loading