Skip to content

Commit d29ce26

Browse files
1 parent 8416b66 commit d29ce26

File tree

6 files changed

+108
-90
lines changed

6 files changed

+108
-90
lines changed

‎README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,20 @@ If you are using Maven without the BOM, add this to your dependencies:
5252
If you are using Gradle 5.x or later, add this to your dependencies:
5353

5454
```Groovy
55-
implementation platform('com.google.cloud:libraries-bom:26.34.0')
55+
implementation platform('com.google.cloud:libraries-bom:26.37.0')
5656
5757
implementation 'com.google.cloud:google-cloud-logging'
5858
```
5959
If you are using Gradle without BOM, add this to your dependencies:
6060

6161
```Groovy
62-
implementation 'com.google.cloud:google-cloud-logging:3.16.1'
62+
implementation 'com.google.cloud:google-cloud-logging:3.17.0'
6363
```
6464

6565
If you are using SBT, add this to your dependencies:
6666

6767
```Scala
68-
libraryDependencies += "com.google.cloud" % "google-cloud-logging" % "3.16.1"
68+
libraryDependencies += "com.google.cloud" % "google-cloud-logging" % "3.17.0"
6969
```
7070
<!-- {x-version-update-end} -->
7171

@@ -452,7 +452,7 @@ Java is a registered trademark of Oracle and/or its affiliates.
452452
[kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-logging/java11.html
453453
[stability-image]: https://img.shields.io/badge/stability-stable-green
454454
[maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-logging.svg
455-
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-logging/3.16.1
455+
[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-logging/3.17.0
456456
[authentication]: https://github.com/googleapis/google-cloud-java#authentication
457457
[auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes
458458
[predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles

‎google-cloud-logging/src/main/java/com/google/cloud/logging/Context.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
import com.google.common.base.Splitter;
2323
import com.google.common.collect.Iterables;
2424
import com.google.errorprone.annotations.CanIgnoreReturnValue;
25+
import io.opentelemetry.api.trace.Span;
2526
import java.util.List;
2627
import java.util.Objects;
2728
import java.util.regex.Matcher;
2829
import java.util.regex.Pattern;
29-
import io.opentelemetry.api.trace.Span;
3030

3131
/** Class to hold context attributes including information about {@see HttpRequest} and tracing. */
3232
public class Context {
@@ -132,9 +132,10 @@ public Builder setTraceSampled(boolean traceSampled) {
132132
}
133133

134134
/**
135-
* Sets the trace id, span id and trace sampled flag values by parsing the string which represents xCloud Trace
136-
* Context. The Cloud Trace Context is passed as {@code x-cloud-trace-context} header (can be in
137-
* Pascal case format). The string format is <code>TRACE_ID/SPAN_ID;o=TRACE_TRUE</code>.
135+
* Sets the trace id, span id and trace sampled flag values by parsing the string which
136+
* represents xCloud Trace Context. The Cloud Trace Context is passed as {@code
137+
* x-cloud-trace-context} header (can be in Pascal case format). The string format is <code>
138+
* TRACE_ID/SPAN_ID;o=TRACE_TRUE</code>.
138139
*
139140
* @see <a href="https://cloud.google.com/trace/docs/setup#force-trace">Cloud Trace header
140141
* format.</a>
@@ -165,10 +166,11 @@ public Builder loadCloudTraceContext(String cloudTrace) {
165166
}
166167

167168
/**
168-
* Sets the trace id, span id and trace sampled flag values by parsing the string which represents the standard W3C
169-
* trace context propagation header. The context propagation header is passed as {@code
170-
* traceparent} header. The method currently supports ONLY version {@code "00"}. The string
171-
* format is <code>00-TRACE_ID-SPAN_ID-FLAGS</code>. field of the {@code version-format} value.
169+
* Sets the trace id, span id and trace sampled flag values by parsing the string which
170+
* represents the standard W3C trace context propagation header. The context propagation header
171+
* is passed as {@code traceparent} header. The method currently supports ONLY version {@code
172+
* "00"}. The string format is <code>00-TRACE_ID-SPAN_ID-FLAGS</code>. field of the {@code
173+
* version-format} value.
172174
*
173175
* @see <a href=
174176
* "https://www.w3.org/TR/trace-context/#traceparent-header-field-values">traceparent header
@@ -202,8 +204,7 @@ public Builder loadW3CTraceParentContext(String traceParent) {
202204
*/
203205
@CanIgnoreReturnValue
204206
public Builder loadOpenTelemetryContext() {
205-
if (Span.current().getSpanContext() != null && Span.current().getSpanContext().isValid())
206-
{
207+
if (Span.current().getSpanContext() != null && Span.current().getSpanContext().isValid()) {
207208
setTraceId(Span.current().getSpanContext().getTraceId());
208209
setSpanId(Span.current().getSpanContext().getSpanId());
209210
setTraceSampled(Span.current().getSpanContext().isSampled());

‎google-cloud-logging/src/main/java/com/google/cloud/logging/ContextHandler.java

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,69 +16,73 @@
1616

1717
package com.google.cloud.logging;
1818

19-
/**
20-
* Class provides a per-thread storage of the {@see Context} instances.
21-
*/
19+
/** Class provides a per-thread storage of the {@see Context} instances. */
2220
public class ContextHandler {
2321

24-
public enum ContextPriority {
25-
NO_INPUT, XCLOUD_HEADER, W3_HEADER, OTEL_EXTRACTED
26-
}
27-
28-
private static final ThreadLocal<Context> contextHolder = initContextHolder();
29-
private static final ThreadLocal<ContextPriority> currentPriority = new ThreadLocal<ContextPriority>();
22+
public enum ContextPriority {
23+
NO_INPUT,
24+
XCLOUD_HEADER,
25+
W3_HEADER,
26+
OTEL_EXTRACTED
27+
}
3028

31-
/**
32-
* Initializes the context holder to {@link InheritableThreadLocal} if {@link LogManager}
33-
* configuration property {@code com.google.cloud.logging.ContextHandler.useInheritedContext} is
34-
* set to {@code true} or to {@link ThreadLocal} otherwise.
35-
*
36-
* @return instance of the context holder.
37-
*/
38-
private static ThreadLocal<Context> initContextHolder() {
39-
LoggingConfig config = new LoggingConfig(ContextHandler.class.getName());
40-
if (config.getUseInheritedContext()) {
41-
return new InheritableThreadLocal<>();
42-
} else {
43-
return new ThreadLocal<>();
44-
}
45-
}
29+
private static final ThreadLocal<Context> contextHolder = initContextHolder();
30+
private static final ThreadLocal<ContextPriority> currentPriority =
31+
new ThreadLocal<ContextPriority>();
4632

47-
public Context getCurrentContext() {
48-
return contextHolder.get();
33+
/**
34+
* Initializes the context holder to {@link InheritableThreadLocal} if {@link LogManager}
35+
* configuration property {@code com.google.cloud.logging.ContextHandler.useInheritedContext} is
36+
* set to {@code true} or to {@link ThreadLocal} otherwise.
37+
*
38+
* @return instance of the context holder.
39+
*/
40+
private static ThreadLocal<Context> initContextHolder() {
41+
LoggingConfig config = new LoggingConfig(ContextHandler.class.getName());
42+
if (config.getUseInheritedContext()) {
43+
return new InheritableThreadLocal<>();
44+
} else {
45+
return new ThreadLocal<>();
4946
}
47+
}
5048

49+
public Context getCurrentContext() {
50+
return contextHolder.get();
51+
}
5152

52-
public void setCurrentContext(Context context) {
53-
contextHolder.set(context);
54-
}
55-
56-
/**
57-
* Sets the context based on the priority. Overrides traceId, spanId and TraceSampled if the passed priority is higher.
58-
* HttpRequest values will be retrieved and combined from existing context if HttpRequest in the new context is empty .
59-
*/
53+
public void setCurrentContext(Context context) {
54+
contextHolder.set(context);
55+
}
6056

61-
public void setCurrentContext(Context context, ContextPriority priority) {
62-
if ((currentPriority.get() == null || priority.compareTo(currentPriority.get()) >= 0) && context != null) {
63-
Context.Builder combinedContextBuilder = Context.newBuilder().setTraceId(context.getTraceId()).setSpanId(context.getSpanId()).setTraceSampled(context.getTraceSampled());
64-
Context currentContext = getCurrentContext();
57+
/**
58+
* Sets the context based on the priority. Overrides traceId, spanId and TraceSampled if the
59+
* passed priority is higher. HttpRequest values will be retrieved and combined from existing
60+
* context if HttpRequest in the new context is empty .
61+
*/
62+
public void setCurrentContext(Context context, ContextPriority priority) {
63+
if ((currentPriority.get() == null || priority.compareTo(currentPriority.get()) >= 0)
64+
&& context != null) {
65+
Context.Builder combinedContextBuilder =
66+
Context.newBuilder()
67+
.setTraceId(context.getTraceId())
68+
.setSpanId(context.getSpanId())
69+
.setTraceSampled(context.getTraceSampled());
70+
Context currentContext = getCurrentContext();
6571

66-
if (context.getHttpRequest() != null)
67-
{
68-
combinedContextBuilder.setRequest(context.getHttpRequest());
69-
}
70-
// Combines HttpRequest from the existing context if HttpRequest in new context is empty.
71-
else if (currentContext != null && currentContext.getHttpRequest() != null ){
72-
combinedContextBuilder.setRequest(currentContext.getHttpRequest());
73-
}
72+
if (context.getHttpRequest() != null) {
73+
combinedContextBuilder.setRequest(context.getHttpRequest());
74+
}
75+
// Combines HttpRequest from the existing context if HttpRequest in new context is empty.
76+
else if (currentContext != null && currentContext.getHttpRequest() != null) {
77+
combinedContextBuilder.setRequest(currentContext.getHttpRequest());
78+
}
7479

75-
contextHolder.set(combinedContextBuilder.build());
76-
currentPriority.set(priority);
77-
}
80+
contextHolder.set(combinedContextBuilder.build());
81+
currentPriority.set(priority);
7882
}
83+
}
7984

80-
81-
public void removeCurrentContext() {
82-
contextHolder.remove();
83-
}
85+
public void removeCurrentContext() {
86+
contextHolder.remove();
87+
}
8488
}

‎google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.google.cloud.MonitoredResourceDescriptor;
4242
import com.google.cloud.PageImpl;
4343
import com.google.cloud.Tuple;
44+
import com.google.cloud.logging.ContextHandler.ContextPriority;
4445
import com.google.cloud.logging.spi.v2.LoggingRpc;
4546
import com.google.common.annotations.VisibleForTesting;
4647
import com.google.common.base.Ascii;
@@ -89,6 +90,7 @@
8990
import com.google.logging.v2.WriteLogEntriesResponse;
9091
import com.google.protobuf.Empty;
9192
import com.google.protobuf.util.Durations;
93+
import io.opentelemetry.api.trace.Span;
9294
import java.text.ParseException;
9395
import java.util.ArrayList;
9496
import java.util.List;
@@ -98,8 +100,6 @@
98100
import java.util.concurrent.ExecutionException;
99101
import java.util.concurrent.TimeUnit;
100102
import java.util.concurrent.TimeoutException;
101-
import io.opentelemetry.api.trace.Span;
102-
import com.google.cloud.logging.ContextHandler.ContextPriority;
103103

104104
class LoggingImpl extends BaseService<LoggingOptions> implements Logging {
105105
protected static final String RESOURCE_NAME_FORMAT = "projects/%s/traces/%s";
@@ -839,8 +839,7 @@ public Iterable<LogEntry> populateMetadata(
839839

840840
ContextHandler contextHandler = new ContextHandler();
841841
// Populate trace/span ID from OpenTelemetry span context to logging context.
842-
if (Span.current().getSpanContext().isValid())
843-
{
842+
if (Span.current().getSpanContext().isValid()) {
844843
Context.Builder contextBuilder = Context.newBuilder().loadOpenTelemetryContext();
845844
contextHandler.setCurrentContext(contextBuilder.build(), ContextPriority.OTEL_EXTRACTED);
846845
}

‎google-cloud-logging/src/test/java/com/google/cloud/logging/AutoPopulateMetadataTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,15 @@ public void teardown() {
111111
new ContextHandler().removeCurrentContext();
112112
}
113113

114-
private void mockCurrentContext(HttpRequest request, String traceId, String spanId, boolean traceSampled) {
114+
private void mockCurrentContext(
115+
HttpRequest request, String traceId, String spanId, boolean traceSampled) {
115116
Context mockedContext =
116-
Context.newBuilder().setRequest(request).setTraceId(traceId).setSpanId(spanId).setTraceSampled(traceSampled).build();
117+
Context.newBuilder()
118+
.setRequest(request)
119+
.setTraceId(traceId)
120+
.setSpanId(spanId)
121+
.setTraceSampled(traceSampled)
122+
.build();
117123
new ContextHandler().setCurrentContext(mockedContext);
118124
}
119125

‎google-cloud-logging/src/test/java/com/google/cloud/logging/ContextTest.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@
2121
import static org.junit.Assert.assertNotEquals;
2222
import static org.junit.Assert.assertNull;
2323

24-
import io.opentelemetry.context.Scope;
25-
import org.junit.Test;
26-
import org.junit.runner.RunWith;
27-
import org.junit.runners.JUnit4;
28-
import org.threeten.bp.Duration;
29-
import io.opentelemetry.api.trace.Span;
3024
import io.opentelemetry.api.trace.*;
25+
import io.opentelemetry.api.trace.Span;
26+
import io.opentelemetry.context.Scope;
3127
import io.opentelemetry.sdk.OpenTelemetrySdk;
3228
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
3329
import io.opentelemetry.sdk.trace.SdkTracerProvider;
3430
import io.opentelemetry.sdk.trace.SpanProcessor;
3531
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.junit.runners.JUnit4;
35+
import org.threeten.bp.Duration;
3636

3737
@RunWith(JUnit4.class)
3838
public class ContextTest {
@@ -127,7 +127,8 @@ public void testParsingCloudTraceContext() {
127127
final String X_CLOUD_TRACE_NO_TRACE = "/SPAN_ID;o=TRACE_TRUE";
128128
final String X_CLOUD_TRACE_ONLY = TEST_TRACE_ID;
129129
final String X_CLOUD_TRACE_WITH_SPAN = TEST_TRACE_ID + "/" + TEST_SPAN_ID;
130-
final String X_CLOUD_TRACE_FULL = TEST_TRACE_ID + "/" + TEST_SPAN_ID + ";o=" + TEST_TRACE_SAMPLED;
130+
final String X_CLOUD_TRACE_FULL =
131+
TEST_TRACE_ID + "/" + TEST_SPAN_ID + ";o=" + TEST_TRACE_SAMPLED;
131132

132133
Context.Builder builder = Context.newBuilder();
133134

@@ -150,7 +151,8 @@ public void testParsingW3CTraceParent() {
150151
final String W3C_TEST_TRACE_ID = "12345678901234567890123456789012";
151152
final String W3C_TEST_SPAN_ID = "1234567890123456";
152153
final String W3C_TEST_TRACE_SAMPLED = "0f";
153-
final String W3C_TRACE_CONTEXT = "00-" + W3C_TEST_TRACE_ID + "-" + W3C_TEST_SPAN_ID + "-" + W3C_TEST_TRACE_SAMPLED;
154+
final String W3C_TRACE_CONTEXT =
155+
"00-" + W3C_TEST_TRACE_ID + "-" + W3C_TEST_SPAN_ID + "-" + W3C_TEST_TRACE_SAMPLED;
154156

155157
Context.Builder builder = Context.newBuilder();
156158

@@ -165,12 +167,10 @@ public void testParsingOpenTelemetryContext() {
165167
InMemorySpanExporter testExporter = InMemorySpanExporter.create();
166168
SpanProcessor inMemorySpanProcessor = SimpleSpanProcessor.create(testExporter);
167169
OpenTelemetrySdk openTelemetrySdk =
168-
OpenTelemetrySdk.builder()
169-
.setTracerProvider(
170-
SdkTracerProvider.builder()
171-
.addSpanProcessor(inMemorySpanProcessor)
172-
.build())
173-
.build();
170+
OpenTelemetrySdk.builder()
171+
.setTracerProvider(
172+
SdkTracerProvider.builder().addSpanProcessor(inMemorySpanProcessor).build())
173+
.build();
174174

175175
Tracer tracer = openTelemetrySdk.getTracer("ContextTest");
176176
Span otelSpan = tracer.spanBuilder("Example Span Attributes").startSpan();
@@ -180,16 +180,24 @@ public void testParsingOpenTelemetryContext() {
180180
otelSpan.setAttribute("Attribute 1", "first attribute value");
181181
currentOtelContext = otelSpan.getSpanContext();
182182
builder.loadOpenTelemetryContext();
183-
assertTraceSpanAndSampled(builder.build(), currentOtelContext.getTraceId(), currentOtelContext.getSpanId(), currentOtelContext.isSampled());
184-
} catch(Throwable t) {
183+
assertTraceSpanAndSampled(
184+
builder.build(),
185+
currentOtelContext.getTraceId(),
186+
currentOtelContext.getSpanId(),
187+
currentOtelContext.isSampled());
188+
} catch (Throwable t) {
185189
otelSpan.recordException(t);
186190
throw t;
187191
} finally {
188192
otelSpan.end();
189193
}
190194
}
191195

192-
private void assertTraceSpanAndSampled(Context context, String expectedTraceId, String expectedSpanId, boolean expectedTraceSampled) {
196+
private void assertTraceSpanAndSampled(
197+
Context context,
198+
String expectedTraceId,
199+
String expectedSpanId,
200+
boolean expectedTraceSampled) {
193201
assertEquals(expectedTraceId, context.getTraceId());
194202
assertEquals(expectedSpanId, context.getSpanId());
195203
assertEquals(expectedTraceSampled, context.getTraceSampled());

0 commit comments

Comments
 (0)