Skip to content

Commit

Permalink
Remove comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cindy-peng committed Apr 30, 2024
1 parent 4808c99 commit 8416b66
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;

/** Class to hold context attributes including information about {@see HttpRequest} and tracing. */
Expand All @@ -43,10 +41,7 @@ public class Context {
private final HttpRequest request;
private final String traceId;
private final String spanId;

private final boolean traceSampled;


/** A builder for {@see Context} objects. */
public static final class Builder {
private HttpRequest.Builder requestBuilder = HttpRequest.newBuilder();
Expand Down Expand Up @@ -137,7 +132,7 @@ public Builder setTraceSampled(boolean traceSampled) {
}

/**
* Sets the trace id and span id values by parsing the string which represents xCloud Trace
* Sets the trace id, span id and trace sampled flag values by parsing the string which represents xCloud Trace
* Context. The Cloud Trace Context is passed as {@code x-cloud-trace-context} header (can be in
* Pascal case format). The string format is <code>TRACE_ID/SPAN_ID;o=TRACE_TRUE</code>.
*
Expand All @@ -147,9 +142,10 @@ public Builder setTraceSampled(boolean traceSampled) {
@CanIgnoreReturnValue
public Builder loadCloudTraceContext(String cloudTrace) {
if (cloudTrace != null) {
String traceSampledString = Iterables.get(Splitter.on(";o=").split(cloudTrace), 1);
if (cloudTrace.indexOf("o=") >= 0) {
setTraceSampled(Iterables.get(Splitter.on("o=").split(cloudTrace), 1).equals("1"));
}
cloudTrace = Iterables.get(Splitter.on(';').split(cloudTrace), 0);

int split = cloudTrace.indexOf('/');
if (split >= 0) {
String traceId = cloudTrace.substring(0, split);
Expand All @@ -160,10 +156,6 @@ public Builder loadCloudTraceContext(String cloudTrace) {
if (!spanId.isEmpty()) {
setSpanId(spanId);
}
// do not set trace sampled flag without trace Id
if (!traceSampledString.isEmpty()) {
setTraceSampled(traceSampledString.equals("1"));
}
}
} else if (!cloudTrace.isEmpty()) {
setTraceId(cloudTrace);
Expand All @@ -173,7 +165,7 @@ public Builder loadCloudTraceContext(String cloudTrace) {
}

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

/**
* Sets the trace id, span id and trace sampled flag values by parsing detected OpenTelemetry
* span context.
*
* @see <a href="https://opentelemetry.io/docs/specs/otel/trace/api/#spancontext">OpenTelemetry
* SpanContext.</a>
*/
@CanIgnoreReturnValue
public Builder loadOpenTelemetryContext() {
if (Span.current().getSpanContext() != null && Span.current().getSpanContext().isValid())
Expand All @@ -209,7 +208,6 @@ public Builder loadOpenTelemetryContext() {
setSpanId(Span.current().getSpanContext().getSpanId());
setTraceSampled(Span.current().getSpanContext().isSampled());
}

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.newCapture;
import static org.easymock.EasyMock.replay;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.*;

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

private LoggingRpcFactory mockedRpcFactory;
private LoggingRpc mockedRpc;
Expand Down Expand Up @@ -111,22 +111,23 @@ public void teardown() {
new ContextHandler().removeCurrentContext();
}

private void mockCurrentContext(HttpRequest request, String traceId, String spanId) {
private void mockCurrentContext(HttpRequest request, String traceId, String spanId, boolean traceSampled) {
Context mockedContext =
Context.newBuilder().setRequest(request).setTraceId(traceId).setSpanId(spanId).build();
Context.newBuilder().setRequest(request).setTraceId(traceId).setSpanId(spanId).setTraceSampled(traceSampled).build();
new ContextHandler().setCurrentContext(mockedContext);
}

@Test
public void testAutoPopulationEnabledInLoggingOptions() {
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID);
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);

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

LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0));
assertEquals(HTTP_REQUEST, actual.getHttpRequest());
assertEquals(FORMATTED_TRACE_ID, actual.getTrace());
assertEquals(SPAN_ID, actual.getSpanId());
assertEquals(TRACE_SAMPLED, actual.getTraceSampled());
assertEquals(RESOURCE, actual.getResource());
}

Expand All @@ -136,27 +137,29 @@ public void testAutoPopulationEnabledInWriteOptionsAndDisabledInLoggingOptions()
LoggingOptions options =
logging.getOptions().toBuilder().setAutoPopulateMetadata(false).build();
logging = options.getService();
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID);
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);

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

LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0));
assertEquals(HTTP_REQUEST, actual.getHttpRequest());
assertEquals(FORMATTED_TRACE_ID, actual.getTrace());
assertEquals(SPAN_ID, actual.getSpanId());
assertEquals(TRACE_SAMPLED, actual.getTraceSampled());
assertEquals(RESOURCE, actual.getResource());
}

@Test
public void testAutoPopulationDisabledInWriteOptions() {
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID);
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);

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

LogEntry actual = LogEntry.fromPb(rpcWriteArgument.getValue().getEntries(0));
assertNull(actual.getHttpRequest());
assertNull(actual.getTrace());
assertNull(actual.getSpanId());
assertFalse(actual.getTraceSampled());
assertNull(actual.getResource());
}

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

@Test
public void testNotFormattedTraceId() {
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID);
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);

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

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

@Test
public void testMonitoredResourcePopulationInWriteOptions() {
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID);
mockCurrentContext(HTTP_REQUEST, TRACE_ID, SPAN_ID, TRACE_SAMPLED);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.cloud.logging;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
Expand All @@ -35,7 +34,6 @@
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;


@RunWith(JUnit4.class)
public class ContextTest {

Expand Down Expand Up @@ -162,7 +160,6 @@ public void testParsingW3CTraceParent() {
assertTraceSpanAndSampled(builder.build(), W3C_TEST_TRACE_ID, W3C_TEST_SPAN_ID, true);
}


@Test
public void testParsingOpenTelemetryContext() {
InMemorySpanExporter testExporter = InMemorySpanExporter.create();
Expand Down Expand Up @@ -192,11 +189,6 @@ public void testParsingOpenTelemetryContext() {
}
}

private void assertTraceAndSpan1(Context context, String expectedTraceId, String expectedSpanId) {
assertEquals(expectedTraceId, context.getTraceId());
assertEquals(expectedSpanId, context.getSpanId());
}

private void assertTraceSpanAndSampled(Context context, String expectedTraceId, String expectedSpanId, boolean expectedTraceSampled) {
assertEquals(expectedTraceId, context.getTraceId());
assertEquals(expectedSpanId, context.getSpanId());
Expand Down

0 comments on commit 8416b66

Please sign in to comment.