From 8416b668378d57e6643b4a99d6ab67ba0e936208 Mon Sep 17 00:00:00 2001 From: cindy-peng Date: Tue, 30 Apr 2024 17:42:07 +0800 Subject: [PATCH] Remove comments --- .../com/google/cloud/logging/Context.java | 26 +++++++++---------- .../logging/AutoPopulateMetadataTests.java | 21 ++++++++------- .../com/google/cloud/logging/ContextTest.java | 8 ------ 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/Context.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/Context.java index 666a687b3..78b527aa7 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/Context.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/Context.java @@ -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. */ @@ -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(); @@ -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 TRACE_ID/SPAN_ID;o=TRACE_TRUE. * @@ -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); @@ -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); @@ -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 00-TRACE_ID-SPAN_ID-FLAGS. field of the {@code version-format} value. @@ -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 OpenTelemetry + * SpanContext. + */ @CanIgnoreReturnValue public Builder loadOpenTelemetryContext() { if (Span.current().getSpanContext() != null && Span.current().getSpanContext().isValid()) @@ -209,7 +208,6 @@ public Builder loadOpenTelemetryContext() { setSpanId(Span.current().getSpanContext().getSpanId()); setTraceSampled(Span.current().getSpanContext().isSampled()); } - return this; } diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/AutoPopulateMetadataTests.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/AutoPopulateMetadataTests.java index f415f8c4c..fee5f69c5 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/AutoPopulateMetadataTests.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/AutoPopulateMetadataTests.java @@ -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; @@ -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; @@ -111,15 +111,15 @@ 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)); @@ -127,6 +127,7 @@ public void testAutoPopulationEnabledInLoggingOptions() { 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()); } @@ -136,7 +137,7 @@ 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)); @@ -144,12 +145,13 @@ public void testAutoPopulationEnabledInWriteOptionsAndDisabledInLoggingOptions() 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)); @@ -157,6 +159,7 @@ public void testAutoPopulationDisabledInWriteOptions() { assertNull(actual.getHttpRequest()); assertNull(actual.getTrace()); assertNull(actual.getSpanId()); + assertFalse(actual.getTraceSampled()); assertNull(actual.getResource()); } @@ -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(); @@ -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(); diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/ContextTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/ContextTest.java index d299aeab8..17d06e53a 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/ContextTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/ContextTest.java @@ -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; @@ -35,7 +34,6 @@ import io.opentelemetry.sdk.trace.SpanProcessor; import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; - @RunWith(JUnit4.class) public class ContextTest { @@ -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(); @@ -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());