Skip to content

Commit 2266f7e

Browse files
committed
chore: Refactor tests to create Otel objects
1 parent dd59c36 commit 2266f7e

File tree

3 files changed

+16
-37
lines changed

3 files changed

+16
-37
lines changed

java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/telemetry/TelemetryConstants.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818

1919
import com.google.api.core.InternalApi;
2020

21-
/** Internal telemetry constants shared between OpenTelemetry tracing and metrics. */
21+
/**
22+
* Internal telemetry constants shared between OpenTelemetry tracing and metrics.
23+
*
24+
* <p><b>Warning:</b> This is intended to be an internal API and is not intended for external use.
25+
* This is public solely for implementation purposes and does not promise any backwards
26+
* compatibility.
27+
*/
2228
@InternalApi
2329
public class TelemetryConstants {
2430
static final String SERVICE_NAME = "datastore.googleapis.com";

java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/telemetry/MetricsRecorderTest.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,16 @@
2626
import org.junit.runner.RunWith;
2727
import org.junit.runners.JUnit4;
2828

29-
/**
30-
* Tests for {@link MetricsRecorder#getInstance(DatastoreOpenTelemetryOptions)}.
31-
*
32-
* <p>Note: Since {@code setMetricsEnabled()} is package-private on {@link
33-
* DatastoreOpenTelemetryOptions.Builder}, these tests can only verify the default (metrics
34-
* disabled) behavior and the behavior when an explicit OpenTelemetry instance is provided. The
35-
* metrics-enabled paths are exercised through the {@link DatastoreImplMetricsTest} which operates
36-
* in the {@code com.google.cloud.datastore} package.
37-
*/
29+
/** Tests for {@link MetricsRecorder#getInstance(DatastoreOpenTelemetryOptions)}. */
3830
@RunWith(JUnit4.class)
3931
public class MetricsRecorderTest {
4032

33+
// TODO(lawrenceqiu): For now, the default behavior is no-op. Add a test for
34+
// instance being OpenTelemetryMetricsRecorder later (visibility changes)
4135
@Test
4236
public void defaultOptionsReturnNoOp() {
4337
DatastoreOpenTelemetryOptions options = DatastoreOpenTelemetryOptions.newBuilder().build();
44-
4538
MetricsRecorder recorder = MetricsRecorder.getInstance(options);
46-
4739
assertThat(recorder).isInstanceOf(NoOpMetricsRecorder.class);
4840
}
4941

@@ -52,20 +44,12 @@ public void tracingEnabledButMetricsDisabledReturnsNoOp() {
5244
// Enabling tracing alone should not enable metrics
5345
DatastoreOpenTelemetryOptions options =
5446
DatastoreOpenTelemetryOptions.newBuilder().setTracingEnabled(true).build();
55-
5647
MetricsRecorder recorder = MetricsRecorder.getInstance(options);
57-
5848
assertThat(recorder).isInstanceOf(NoOpMetricsRecorder.class);
5949
}
6050

61-
@Test
62-
public void noOpRecorderDoesNotThrow() {
63-
// Verify NoOpMetricsRecorder methods do not throw
64-
NoOpMetricsRecorder recorder = new NoOpMetricsRecorder();
65-
recorder.recordTransactionLatency(100.0, null);
66-
recorder.recordTransactionAttemptCount(1, null);
67-
}
68-
51+
// TODO(lawrenceqiu): Temporary test to ensure that OpenTelemetryMetricsRecorder can
52+
// be created by the DatastoreOpenTelemetryOptions and creates with Otel object
6953
@Test
7054
public void openTelemetryRecorderCreatedWithExplicitOpenTelemetry() {
7155
InMemoryMetricReader metricReader = InMemoryMetricReader.create();

java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/telemetry/OpenTelemetryMetricsRecorderTest.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public void recordTransactionLatency_recordsHistogramWithAttributes() {
6262
Collection<MetricData> metrics = metricReader.collectAllMetrics();
6363
MetricData latencyMetric =
6464
metrics.stream()
65-
.filter(
66-
m -> m.getName().equals(TelemetryConstants.SERVICE_NAME + "/transaction_latency"))
65+
.filter(m -> m.getName().equals(TelemetryConstants.METRIC_NAME_TRANSACTION_LATENCY))
6766
.findFirst()
6867
.orElse(null);
6968

@@ -101,9 +100,7 @@ public void recordTransactionAttemptCount_recordsCounterWithAttributes() {
101100
MetricData attemptMetric =
102101
metrics.stream()
103102
.filter(
104-
m ->
105-
m.getName()
106-
.equals(TelemetryConstants.SERVICE_NAME + "/transaction_attempt_count"))
103+
m -> m.getName().equals(TelemetryConstants.METRIC_NAME_TRANSACTION_ATTEMPT_COUNT))
107104
.findFirst()
108105
.orElse(null);
109106

@@ -148,9 +145,7 @@ public void recordTransactionAttemptCount_multipleAttempts_accumulates() {
148145
MetricData attemptMetric =
149146
metrics.stream()
150147
.filter(
151-
m ->
152-
m.getName()
153-
.equals(TelemetryConstants.SERVICE_NAME + "/transaction_attempt_count"))
148+
m -> m.getName().equals(TelemetryConstants.METRIC_NAME_TRANSACTION_ATTEMPT_COUNT))
154149
.findFirst()
155150
.orElse(null);
156151

@@ -161,16 +156,10 @@ public void recordTransactionAttemptCount_multipleAttempts_accumulates() {
161156
}
162157

163158
@Test
164-
public void recordTransactionLatency_nullAttributes_doesNotThrow() {
165-
// Should not throw even when attributes are null
159+
public void recordTransactionLatency_nullAttributes() {
166160
recorder.recordTransactionLatency(100.0, null);
167161

168162
Collection<MetricData> metrics = metricReader.collectAllMetrics();
169163
assertThat(metrics).isNotEmpty();
170164
}
171-
172-
@Test
173-
public void getOpenTelemetry_returnsConfiguredInstance() {
174-
assertThat(recorder.getOpenTelemetry()).isNotNull();
175-
}
176165
}

0 commit comments

Comments
 (0)