feat: Add support for library instrumentation by losalex · Pull Request #789 · googleapis/java-logging-logback · GitHub
Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.
Merged
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
6 changes: 3 additions & 3 deletions README.md
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
Comment thread
minherz marked this conversation as resolved.
</dependencies>

<reporting>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.api.core.InternalApi;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.MonitoredResource;
import com.google.cloud.logging.Instrumentation;
import com.google.cloud.logging.LogEntry;
import com.google.cloud.logging.Logging;
import com.google.cloud.logging.Logging.WriteOption;
Expand All @@ -40,7 +41,6 @@
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -100,6 +100,9 @@ public class LoggingAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
"type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent";
private static final List<LoggingEventEnhancer> DEFAULT_LOGGING_EVENT_ENHANCERS =
ImmutableList.<LoggingEventEnhancer>of(new MDCEventEnhancer());
public static final String JAVA_LOGBACK_LIBRARY_NAME = "java-logback";
private static boolean instrumentationAdded = false;
private static Object instrumentationLock = new Object();
Comment thread
losalex marked this conversation as resolved.

private volatile Logging logging;
private LoggingOptions loggingOptions;
Expand Down Expand Up @@ -317,7 +320,16 @@ String getProjectId() {

@Override
protected void append(ILoggingEvent e) {
Iterable<LogEntry> entries = Collections.singleton(logEntryFor(e));
List<LogEntry> entriesList = new ArrayList<LogEntry>();
entriesList.add(logEntryFor(e));
// Check if instrumentation was already added - if not, create a log entry with instrumentation
// data
if (!setInstrumentationStatus(true)) {
entriesList.add(
Instrumentation.createDiagnosticEntry(
JAVA_LOGBACK_LIBRARY_NAME, Instrumentation.getLibraryVersion(LoggingAppender.class)));
}
Iterable<LogEntry> entries = entriesList;
if (autoPopulateMetadata) {
entries =
getLogging()
Expand Down Expand Up @@ -490,4 +502,19 @@ private static Severity severityFor(Level level) {
return Severity.DEFAULT;
}
}

/**
* The package-private helper method used to set the flag which indicates if instrumentation info
* already written or not.
*
* @returns The value of the flag before it was set.
*/
static boolean setInstrumentationStatus(boolean value) {
if (instrumentationAdded == value) return instrumentationAdded;
synchronized (instrumentationLock) {
boolean current = instrumentationAdded;
instrumentationAdded = value;
return current;
}
Comment thread
losalex marked this conversation as resolved.
}
}