Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhigarg92 committed Jan 16, 2024
1 parent 18f542a commit 7486f62
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
public class SpannerOptions extends ServiceOptions<Spanner, SpannerOptions> {
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;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ public static Collection<Object[]> 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(
Expand Down

0 comments on commit 7486f62

Please sign in to comment.