Skip to content

Commit 7ca6f19

Browse files
author
cindy-peng
committed
fix test failures and dependency conflict
1 parent 8416b66 commit 7ca6f19

File tree

6 files changed

+104
-87
lines changed

6 files changed

+104
-87
lines changed

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: 24 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,7 @@ 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 = TEST_TRACE_ID + "/" + TEST_SPAN_ID + ";o=1";
131131

132132
Context.Builder builder = Context.newBuilder();
133133

@@ -150,7 +150,8 @@ public void testParsingW3CTraceParent() {
150150
final String W3C_TEST_TRACE_ID = "12345678901234567890123456789012";
151151
final String W3C_TEST_SPAN_ID = "1234567890123456";
152152
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;
153+
final String W3C_TRACE_CONTEXT =
154+
"00-" + W3C_TEST_TRACE_ID + "-" + W3C_TEST_SPAN_ID + "-" + W3C_TEST_TRACE_SAMPLED;
154155

155156
Context.Builder builder = Context.newBuilder();
156157

@@ -165,12 +166,10 @@ public void testParsingOpenTelemetryContext() {
165166
InMemorySpanExporter testExporter = InMemorySpanExporter.create();
166167
SpanProcessor inMemorySpanProcessor = SimpleSpanProcessor.create(testExporter);
167168
OpenTelemetrySdk openTelemetrySdk =
168-
OpenTelemetrySdk.builder()
169-
.setTracerProvider(
170-
SdkTracerProvider.builder()
171-
.addSpanProcessor(inMemorySpanProcessor)
172-
.build())
173-
.build();
169+
OpenTelemetrySdk.builder()
170+
.setTracerProvider(
171+
SdkTracerProvider.builder().addSpanProcessor(inMemorySpanProcessor).build())
172+
.build();
174173

175174
Tracer tracer = openTelemetrySdk.getTracer("ContextTest");
176175
Span otelSpan = tracer.spanBuilder("Example Span Attributes").startSpan();
@@ -180,16 +179,24 @@ public void testParsingOpenTelemetryContext() {
180179
otelSpan.setAttribute("Attribute 1", "first attribute value");
181180
currentOtelContext = otelSpan.getSpanContext();
182181
builder.loadOpenTelemetryContext();
183-
assertTraceSpanAndSampled(builder.build(), currentOtelContext.getTraceId(), currentOtelContext.getSpanId(), currentOtelContext.isSampled());
184-
} catch(Throwable t) {
182+
assertTraceSpanAndSampled(
183+
builder.build(),
184+
currentOtelContext.getTraceId(),
185+
currentOtelContext.getSpanId(),
186+
currentOtelContext.isSampled());
187+
} catch (Throwable t) {
185188
otelSpan.recordException(t);
186189
throw t;
187190
} finally {
188191
otelSpan.end();
189192
}
190193
}
191194

192-
private void assertTraceSpanAndSampled(Context context, String expectedTraceId, String expectedSpanId, boolean expectedTraceSampled) {
195+
private void assertTraceSpanAndSampled(
196+
Context context,
197+
String expectedTraceId,
198+
String expectedSpanId,
199+
boolean expectedTraceSampled) {
193200
assertEquals(expectedTraceId, context.getTraceId());
194201
assertEquals(expectedSpanId, context.getSpanId());
195202
assertEquals(expectedTraceSampled, context.getTraceSampled());

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
5555
<github.global.server>github</github.global.server>
5656
<site.installationModule>google-cloud-logging-parent</site.installationModule>
57-
<opentelemetry.version>1.36.0</opentelemetry.version>
57+
<opentelemetry.version>1.37.0</opentelemetry.version>
5858
</properties>
5959

6060
<!-- OpenTelemetry -->

0 commit comments

Comments
 (0)