Skip to content

Commit 8416b66

Browse files
author
cindy-peng
committed
Remove comments
1 parent 4808c99 commit 8416b66

File tree

3 files changed

+24
-31
lines changed

3 files changed

+24
-31
lines changed

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import java.util.Objects;
2727
import java.util.regex.Matcher;
2828
import java.util.regex.Pattern;
29-
import io.opentelemetry.api.OpenTelemetry;
30-
import io.opentelemetry.api.common.Attributes;
3129
import io.opentelemetry.api.trace.Span;
3230

3331
/** Class to hold context attributes including information about {@see HttpRequest} and tracing. */
@@ -43,10 +41,7 @@ public class Context {
4341
private final HttpRequest request;
4442
private final String traceId;
4543
private final String spanId;
46-
4744
private final boolean traceSampled;
48-
49-
5045
/** A builder for {@see Context} objects. */
5146
public static final class Builder {
5247
private HttpRequest.Builder requestBuilder = HttpRequest.newBuilder();
@@ -137,7 +132,7 @@ public Builder setTraceSampled(boolean traceSampled) {
137132
}
138133

139134
/**
140-
* Sets the trace id and span id values by parsing the string which represents xCloud Trace
135+
* Sets the trace id, span id and trace sampled flag values by parsing the string which represents xCloud Trace
141136
* Context. The Cloud Trace Context is passed as {@code x-cloud-trace-context} header (can be in
142137
* Pascal case format). The string format is <code>TRACE_ID/SPAN_ID;o=TRACE_TRUE</code>.
143138
*
@@ -147,9 +142,10 @@ public Builder setTraceSampled(boolean traceSampled) {
147142
@CanIgnoreReturnValue
148143
public Builder loadCloudTraceContext(String cloudTrace) {
149144
if (cloudTrace != null) {
150-
String traceSampledString = Iterables.get(Splitter.on(";o=").split(cloudTrace), 1);
145+
if (cloudTrace.indexOf("o=") >= 0) {
146+
setTraceSampled(Iterables.get(Splitter.on("o=").split(cloudTrace), 1).equals("1"));
147+
}
151148
cloudTrace = Iterables.get(Splitter.on(';').split(cloudTrace), 0);
152-
153149
int split = cloudTrace.indexOf('/');
154150
if (split >= 0) {
155151
String traceId = cloudTrace.substring(0, split);
@@ -160,10 +156,6 @@ public Builder loadCloudTraceContext(String cloudTrace) {
160156
if (!spanId.isEmpty()) {
161157
setSpanId(spanId);
162158
}
163-
// do not set trace sampled flag without trace Id
164-
if (!traceSampledString.isEmpty()) {
165-
setTraceSampled(traceSampledString.equals("1"));
166-
}
167159
}
168160
} else if (!cloudTrace.isEmpty()) {
169161
setTraceId(cloudTrace);
@@ -173,7 +165,7 @@ public Builder loadCloudTraceContext(String cloudTrace) {
173165
}
174166

175167
/**
176-
* Sets the trace id and span id values by parsing the string which represents the standard W3C
168+
* Sets the trace id, span id and trace sampled flag values by parsing the string which represents the standard W3C
177169
* trace context propagation header. The context propagation header is passed as {@code
178170
* traceparent} header. The method currently supports ONLY version {@code "00"}. The string
179171
* format is <code>00-TRACE_ID-SPAN_ID-FLAGS</code>. field of the {@code version-format} value.
@@ -201,6 +193,13 @@ public Builder loadW3CTraceParentContext(String traceParent) {
201193
return this;
202194
}
203195

196+
/**
197+
* Sets the trace id, span id and trace sampled flag values by parsing detected OpenTelemetry
198+
* span context.
199+
*
200+
* @see <a href="https://opentelemetry.io/docs/specs/otel/trace/api/#spancontext">OpenTelemetry
201+
* SpanContext.</a>
202+
*/
204203
@CanIgnoreReturnValue
205204
public Builder loadOpenTelemetryContext() {
206205
if (Span.current().getSpanContext() != null && Span.current().getSpanContext().isValid())
@@ -209,7 +208,6 @@ public Builder loadOpenTelemetryContext() {
209208
setSpanId(Span.current().getSpanContext().getSpanId());
210209
setTraceSampled(Span.current().getSpanContext().isSampled());
211210
}
212-
213211
return this;
214212
}
215213

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
import static org.easymock.EasyMock.expect;
2323
import static org.easymock.EasyMock.newCapture;
2424
import static org.easymock.EasyMock.replay;
25-
import static org.junit.Assert.assertEquals;
26-
import static org.junit.Assert.assertNull;
25+
import static org.junit.Assert.*;
2726

2827
import com.google.api.core.ApiFutures;
2928
import com.google.cloud.MonitoredResource;
@@ -74,6 +73,7 @@ public class AutoPopulateMetadataTests {
7473
private static final String FORMATTED_TRACE_ID =
7574
String.format(LoggingImpl.RESOURCE_NAME_FORMAT, RESOURCE_PROJECT_ID, TRACE_ID);
7675
private static final String SPAN_ID = "1";
76+
private static final boolean TRACE_SAMPLED = true;
7777

7878
private LoggingRpcFactory mockedRpcFactory;
7979
private LoggingRpc mockedRpc;
@@ -111,22 +111,23 @@ public void teardown() {
111111
new ContextHandler().removeCurrentContext();
112112
}
113113

114-
private void mockCurrentContext(HttpRequest request, String traceId, String spanId) {
114+
private void mockCurrentContext(HttpRequest request, String traceId, String spanId, boolean traceSampled) {
115115
Context mockedContext =
116-
Context.newBuilder().setRequest(request).setTraceId(traceId).setSpanId(spanId).build();
116+
Context.newBuilder().setRequest(request).setTraceId(traceId).setSpanId(spanId).setTraceSampled(traceSampled).build();
117117
new ContextHandler().setCurrentContext(mockedContext);
118118
}
119119

120120
@Test
121121
public void testAutoPopulationEnabledInLoggingOptions() {
122-
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID);
122+
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);
123123

124124
logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY));
125125

126126
LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0));
127127
assertEquals(HTTP_REQUEST, actual.getHttpRequest());
128128
assertEquals(FORMATTED_TRACE_ID, actual.getTrace());
129129
assertEquals(SPAN_ID, actual.getSpanId());
130+
assertEquals(TRACE_SAMPLED, actual.getTraceSampled());
130131
assertEquals(RESOURCE, actual.getResource());
131132
}
132133

@@ -136,27 +137,29 @@ public void testAutoPopulationEnabledInWriteOptionsAndDisabledInLoggingOptions()
136137
LoggingOptions options =
137138
logging.getOptions().toBuilder().setAutoPopulateMetadata(false).build();
138139
logging = options.getService();
139-
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID);
140+
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);
140141

141142
logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY), WriteOption.autoPopulateMetadata(true));
142143

143144
LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0));
144145
assertEquals(HTTP_REQUEST, actual.getHttpRequest());
145146
assertEquals(FORMATTED_TRACE_ID, actual.getTrace());
146147
assertEquals(SPAN_ID, actual.getSpanId());
148+
assertEquals(TRACE_SAMPLED, actual.getTraceSampled());
147149
assertEquals(RESOURCE, actual.getResource());
148150
}
149151

150152
@Test
151153
public void testAutoPopulationDisabledInWriteOptions() {
152-
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID);
154+
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);
153155

154156
logging.write(ImmutableList.of(SIMPLE_LOG_ENTRY), WriteOption.autoPopulateMetadata(false));
155157

156158
LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0));
157159
assertNull(actual.getHttpRequest());
158160
assertNull(actual.getTrace());
159161
assertNull(actual.getSpanId());
162+
assertFalse(actual.getTraceSampled());
160163
assertNull(actual.getResource());
161164
}
162165

@@ -174,7 +177,7 @@ public void testSourceLocationPopulation() {
174177

175178
@Test
176179
public void testNotFormattedTraceId() {
177-
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID);
180+
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);
178181

179182
final MonitoredResource expectedResource = MonitoredResource.newBuilder("custom").build();
180183

@@ -186,7 +189,7 @@ public void testNotFormattedTraceId() {
186189

187190
@Test
188191
public void testMonitoredResourcePopulationInWriteOptions() {
189-
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID);
192+
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);
190193

191194
final MonitoredResource expectedResource = MonitoredResource.newBuilder("custom").build();
192195

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.google.cloud.logging;
1818

19-
2019
import static org.junit.Assert.assertEquals;
2120
import static org.junit.Assert.assertFalse;
2221
import static org.junit.Assert.assertNotEquals;
@@ -35,7 +34,6 @@
3534
import io.opentelemetry.sdk.trace.SpanProcessor;
3635
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
3736

38-
3937
@RunWith(JUnit4.class)
4038
public class ContextTest {
4139

@@ -162,7 +160,6 @@ public void testParsingW3CTraceParent() {
162160
assertTraceSpanAndSampled(builder.build(), W3C_TEST_TRACE_ID, W3C_TEST_SPAN_ID, true);
163161
}
164162

165-
166163
@Test
167164
public void testParsingOpenTelemetryContext() {
168165
InMemorySpanExporter testExporter = InMemorySpanExporter.create();
@@ -192,11 +189,6 @@ public void testParsingOpenTelemetryContext() {
192189
}
193190
}
194191

195-
private void assertTraceAndSpan1(Context context, String expectedTraceId, String expectedSpanId) {
196-
assertEquals(expectedTraceId, context.getTraceId());
197-
assertEquals(expectedSpanId, context.getSpanId());
198-
}
199-
200192
private void assertTraceSpanAndSampled(Context context, String expectedTraceId, String expectedSpanId, boolean expectedTraceSampled) {
201193
assertEquals(expectedTraceId, context.getTraceId());
202194
assertEquals(expectedSpanId, context.getSpanId());

0 commit comments

Comments
 (0)