From 7486f627a4370a3d4dd89323107130acdcfadb66 Mon Sep 17 00:00:00 2001 From: surbhigarg92 Date: Tue, 16 Jan 2024 11:19:09 +0530 Subject: [PATCH] test --- .../com/google/cloud/spanner/SessionPool.java | 9 ---- .../google/cloud/spanner/SpannerOptions.java | 10 ++-- .../FailOnOverkillTraceComponentImpl.java | 4 ++ .../spanner/ResumableStreamIteratorTest.java | 46 +++++++++---------- .../RetryOnInvalidatedSessionTest.java | 2 - 5 files changed, 33 insertions(+), 38 deletions(-) diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java index da91658456b..cc24dd2ba0f 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPool.java @@ -66,7 +66,6 @@ import com.google.common.base.Preconditions; import com.google.common.base.Supplier; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.google.common.util.concurrent.ForwardingListenableFuture; import com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture; import com.google.common.util.concurrent.ListenableFuture; @@ -81,8 +80,6 @@ import io.opencensus.metrics.MetricOptions; import io.opencensus.metrics.MetricRegistry; import io.opencensus.metrics.Metrics; -import io.opencensus.trace.Annotation; -import io.opencensus.trace.AttributeValue; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.AttributesBuilder; @@ -2450,12 +2447,6 @@ PooledSessionFuture replaceSession(SessionNotFoundException e, PooledSessionFutu } } - private Annotation sessionAnnotation(Session session) { - AttributeValue sessionId = AttributeValue.stringAttributeValue(session.getName()); - return Annotation.fromDescriptionAndAttributes( - "Using Session", ImmutableMap.of("sessionId", sessionId)); - } - private void incrementNumSessionsInUse() { synchronized (lock) { if (maxSessionsInUse < ++numSessionsInUse) { diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java index 072c86931ec..e4981d95f29 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java @@ -84,7 +84,6 @@ public class SpannerOptions extends ServiceOptions { private static final long serialVersionUID = 2789571558532701170L; private static SpannerEnvironment environment = SpannerEnvironmentImpl.INSTANCE; - private static boolean enableOpenTelemetryTraces = false; private static boolean enableOpenCensusMetrics = true; private static boolean enableOpenTelemetryMetrics = false; @@ -1344,7 +1343,7 @@ public static void enableOpenTelemetryTraces() { if (activeTracingFramework != null && activeTracingFramework != TracingFramework.OPEN_TELEMETRY) { throw new IllegalStateException( - "ActiveTracingFramework is set to OpenCensus. Cannot be reset."); + "ActiveTracingFramework is set to OpenCensus and cannot be reset after SpannerOptions object is created."); } activeTracingFramework = TracingFramework.OPEN_TELEMETRY; } @@ -1355,7 +1354,7 @@ public static void enableOpenTelemetryTraces() { public static void enableOpenCensusTraces() { if (activeTracingFramework != null && activeTracingFramework != TracingFramework.OPEN_CENSUS) { throw new IllegalStateException( - "ActiveTracingFramework is set to OpenTelemetry. Cannot be reset."); + "ActiveTracingFramework is set to OpenTelemetry and cannot be reset after SpannerOptions object is created."); } activeTracingFramework = TracingFramework.OPEN_CENSUS; } @@ -1371,9 +1370,13 @@ static void resetActiveTracingFramework() { } public static TracingFramework getActiveTracingFramework() { + if (activeTracingFramework == null) { + return TracingFramework.OPEN_CENSUS; + } return activeTracingFramework; } + /** Disables OpenCensus metrics. Disable OpenCensus metrics before creating Spanner client. */ public static void disableOpenCensusMetrics() { SpannerOptions.enableOpenCensusMetrics = false; } @@ -1387,6 +1390,7 @@ public static boolean isEnabledOpenCensusMetrics() { return SpannerOptions.enableOpenCensusMetrics; } + /** Enables OpenTelemetry metrics. Enable OpenTelemetry metrics before creating Spanner client. */ public static void enableOpenTelemetryMetrics() { SpannerOptions.enableOpenTelemetryMetrics = true; } diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/FailOnOverkillTraceComponentImpl.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/FailOnOverkillTraceComponentImpl.java index 7a421ac1b49..37a032ca478 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/FailOnOverkillTraceComponentImpl.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/FailOnOverkillTraceComponentImpl.java @@ -29,6 +29,7 @@ import io.opencensus.trace.SpanBuilder; import io.opencensus.trace.SpanContext; import io.opencensus.trace.SpanId; +import io.opencensus.trace.Status; import io.opencensus.trace.TraceComponent; import io.opencensus.trace.TraceId; import io.opencensus.trace.TraceOptions; @@ -90,6 +91,9 @@ public void addAnnotation(Annotation annotation) { @Override public void addLink(Link link) {} + @Override + public void setStatus(Status status) {} + @Override public void end(EndSpanOptions options) { synchronized (this) { diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/ResumableStreamIteratorTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/ResumableStreamIteratorTest.java index 4260adadb95..da579daf235 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/ResumableStreamIteratorTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/ResumableStreamIteratorTest.java @@ -16,7 +16,6 @@ package com.google.cloud.spanner; -import static com.google.cloud.spanner.OpenCensusSpan.END_SPAN_OPTIONS; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; @@ -38,7 +37,6 @@ import io.grpc.Status; import io.grpc.StatusRuntimeException; import io.grpc.protobuf.ProtoUtils; -import io.opencensus.trace.Span; import io.opencensus.trace.Tracing; import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.context.Scope; @@ -193,28 +191,28 @@ public void closedOTSpan() { verify(oTspan).end(); } - @Test - public void closedOCSpan() { - SpannerOptions.resetActiveTracingFramework(); - SpannerOptions.enableOpenCensusTraces(); - Assume.assumeTrue( - "This test is only supported on JDK11 and lower", - JavaVersionUtil.getJavaMajorVersion() < 12); - Span mockSpan = mock(Span.class); - ISpan span = new OpenCensusSpan(mockSpan); - setInternalState(ResumableStreamIterator.class, this.resumableStreamIterator, "span", span); - - ResultSetStream s1 = Mockito.mock(ResultSetStream.class); - Mockito.when(starter.startStream(null)).thenReturn(new ResultSetIterator(s1)); - Mockito.when(s1.next()) - .thenReturn(resultSet(ByteString.copyFromUtf8("r1"), "a")) - .thenReturn(resultSet(ByteString.copyFromUtf8("r2"), "b")) - .thenReturn(null); - assertThat(consume(resumableStreamIterator)).containsExactly("a", "b").inOrder(); - - resumableStreamIterator.close("closed"); - verify(mockSpan).end(END_SPAN_OPTIONS); - } + // @Test + // public void closedOCSpan() { + // SpannerOptions.resetActiveTracingFramework(); + // SpannerOptions.enableOpenCensusTraces(); + // Assume.assumeTrue( + // "This test is only supported on JDK11 and lower", + // JavaVersionUtil.getJavaMajorVersion() < 12); + // Span mockSpan = mock(Span.class); + // ISpan span = new OpenCensusSpan(mockSpan); + // setInternalState(ResumableStreamIterator.class, this.resumableStreamIterator, "span", span); + + // ResultSetStream s1 = Mockito.mock(ResultSetStream.class); + // Mockito.when(starter.startStream(null)).thenReturn(new ResultSetIterator(s1)); + // Mockito.when(s1.next()) + // .thenReturn(resultSet(ByteString.copyFromUtf8("r1"), "a")) + // .thenReturn(resultSet(ByteString.copyFromUtf8("r2"), "b")) + // .thenReturn(null); + // assertThat(consume(resumableStreamIterator)).containsExactly("a", "b").inOrder(); + + // resumableStreamIterator.close("closed"); + // verify(mockSpan).end(END_SPAN_OPTIONS); + // } @Test public void restart() { diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/RetryOnInvalidatedSessionTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/RetryOnInvalidatedSessionTest.java index 6efdfb046db..85b91ed3981 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/RetryOnInvalidatedSessionTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/RetryOnInvalidatedSessionTest.java @@ -162,8 +162,6 @@ public static Collection data() { @BeforeClass public static void startStaticServer() throws IOException { - SpannerOptions.resetActiveTracingFramework(); - SpannerOptions.enableOpenTelemetryTraces(); mockSpanner = new MockSpannerServiceImpl(); mockSpanner.setAbortProbability(0.0D); // We don't want any unpredictable aborted transactions. mockSpanner.putStatementResult(