-
Notifications
You must be signed in to change notification settings - Fork 39
feat(logging): OpenTelemetry trace/span ID integration for Java logging library #1596
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
Changes from all commits
da8bd86
ab65496
246c1f5
107144b
b4d3ee7
4808c99
8416b66
7ca6f19
72aa518
db7f163
e67c907
42a49e1
e4c3f06
9f09a85
cefcd73
c600f0d
1d48166
0e61c7d
9cf24ab
e59fd6b
4ff6738
d560806
c8e4eca
ae60311
43d69d0
2d8345e
a8bba3c
e035733
4270847
e4b8de1
fe58cd8
ba86d25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ | |
import com.google.cloud.MonitoredResourceDescriptor; | ||
import com.google.cloud.PageImpl; | ||
import com.google.cloud.Tuple; | ||
import com.google.cloud.logging.ContextHandler.ContextPriority; | ||
import com.google.cloud.logging.spi.v2.LoggingRpc; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Ascii; | ||
|
@@ -89,6 +90,7 @@ | |
import com.google.logging.v2.WriteLogEntriesResponse; | ||
import com.google.protobuf.Empty; | ||
import com.google.protobuf.util.Durations; | ||
import io.opentelemetry.api.trace.Span; | ||
import java.text.ParseException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
@@ -822,7 +824,7 @@ public Iterable<LogEntry> populateMetadata( | |
customResource == null | ||
? MonitoredResourceUtil.getResource(getOptions().getProjectId(), null) | ||
: customResource; | ||
final Context context = new ContextHandler().getCurrentContext(); | ||
|
||
final ArrayList<LogEntry> populatedLogEntries = Lists.newArrayList(); | ||
|
||
// populate empty metadata fields of log entries before calling write API | ||
|
@@ -834,13 +836,23 @@ public Iterable<LogEntry> populateMetadata( | |
if (resourceMetadata != null && entry.getResource() == null) { | ||
entityBuilder.setResource(resourceMetadata); | ||
} | ||
|
||
ContextHandler contextHandler = new ContextHandler(); | ||
// Populate trace/span ID from OpenTelemetry span context to logging context. | ||
if (Span.current().getSpanContext().isValid()) { | ||
Context.Builder contextBuilder = Context.newBuilder().loadOpenTelemetryContext(); | ||
contextHandler.setCurrentContext(contextBuilder.build(), ContextPriority.OTEL_EXTRACTED); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you set the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unlike other logging libraries, java-logging library doesn't call other setCurrentContext() within the library itself. It's usually delegated to customers or integration library. An example is mentioned here (2nd approach to set trace for log entries): RequestContextFilter in java-logging-servlet-initializer repository will call setCurrentContext () and load tracing context from cloud tracing or W3C headers. And we do have a plan mentioned here to switch to the new setCurrentContext(Context context, ContextPriority priority) in java-logging-servlet-initializer after the change in this repository is released. I have also added some unit tests here for |
||
} | ||
|
||
Context context = contextHandler.getCurrentContext(); | ||
if (context != null && entry.getHttpRequest() == null) { | ||
entityBuilder.setHttpRequest(context.getHttpRequest()); | ||
} | ||
if (context != null && Strings.isNullOrEmpty(entry.getTrace())) { | ||
MonitoredResource resource = | ||
entry.getResource() != null ? entry.getResource() : resourceMetadata; | ||
entityBuilder.setTrace(getFormattedTrace(context.getTraceId(), resource)); | ||
entityBuilder.setTraceSampled(context.getTraceSampled()); | ||
} | ||
if (context != null && Strings.isNullOrEmpty(entry.getSpanId())) { | ||
entityBuilder.setSpanId(context.getSpanId()); | ||
|
Uh oh!
There was an error while loading. Please reload this page.