Skip to content

Commit c3ed5e8

Browse files
committed
refactor: remove unnecessary overload
1 parent a49d06b commit c3ed5e8

File tree

5 files changed

+41
-28
lines changed

5 files changed

+41
-28
lines changed

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/GoldenSignalsMetricsTracer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ class GoldenSignalsMetricsTracer implements ApiTracer {
4949
private final Stopwatch clientRequestTimer;
5050
private final GoldenSignalsMetricsRecorder metricsRecorder;
5151
private final Map<String, Object> attributes;
52+
private final ApiTracerContext.Transport transport;
5253

5354
GoldenSignalsMetricsTracer(
5455
GoldenSignalsMetricsRecorder metricsRecorder, ApiTracerContext apiTracerContext) {
5556
this.clientRequestTimer = Stopwatch.createStarted();
5657
this.metricsRecorder = metricsRecorder;
5758
this.attributes = apiTracerContext.getMetricsAttributes();
59+
this.transport = apiTracerContext.transport();
5860
}
5961

6062
@VisibleForTesting
@@ -65,6 +67,7 @@ class GoldenSignalsMetricsTracer implements ApiTracer {
6567
this.clientRequestTimer = Stopwatch.createStarted(ticker);
6668
this.metricsRecorder = metricsRecorder;
6769
this.attributes = new HashMap<>(apiTracerContext.getMetricsAttributes());
70+
this.transport = apiTracerContext.transport();
6871
}
6972

7073
/**
@@ -88,7 +91,8 @@ public void operationCancelled() {
8891

8992
@Override
9093
public void operationFailed(Throwable error) {
91-
attributes.put(RPC_RESPONSE_STATUS_ATTRIBUTE, ObservabilityUtils.extractStatus(error));
94+
attributes.put(
95+
RPC_RESPONSE_STATUS_ATTRIBUTE, ObservabilityUtils.extractStatus(error, transport));
9296
metricsRecorder.recordOperationLatency(
9397
clientRequestTimer.elapsed(TimeUnit.NANOSECONDS) / 1_000_000_000.0, attributes);
9498
}

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/MetricsTracer.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ public void operationFailed(Throwable error) {
120120
if (operationFinished.getAndSet(true)) {
121121
throw new IllegalStateException(OPERATION_FINISHED_STATUS_MESSAGE);
122122
}
123-
attributes.put(STATUS_ATTRIBUTE, ObservabilityUtils.extractStatus(error));
123+
attributes.put(
124+
STATUS_ATTRIBUTE,
125+
(String) ObservabilityUtils.extractStatus(error, ApiTracerContext.Transport.GRPC));
124126
metricsRecorder.recordOperationLatency(
125127
operationTimer.elapsed(TimeUnit.MILLISECONDS), attributes);
126128
metricsRecorder.recordOperationCount(1, attributes);
@@ -172,7 +174,9 @@ public void attemptCancelled() {
172174
*/
173175
@Override
174176
public void attemptFailedDuration(Throwable error, java.time.Duration delay) {
175-
attributes.put(STATUS_ATTRIBUTE, ObservabilityUtils.extractStatus(error));
177+
attributes.put(
178+
STATUS_ATTRIBUTE,
179+
(String) ObservabilityUtils.extractStatus(error, ApiTracerContext.Transport.GRPC));
176180
metricsRecorder.recordAttemptLatency(attemptTimer.elapsed(TimeUnit.MILLISECONDS), attributes);
177181
metricsRecorder.recordAttemptCount(1, attributes);
178182
}
@@ -196,7 +200,9 @@ public void attemptFailed(Throwable error, org.threeten.bp.Duration delay) {
196200
*/
197201
@Override
198202
public void attemptFailedRetriesExhausted(Throwable error) {
199-
attributes.put(STATUS_ATTRIBUTE, ObservabilityUtils.extractStatus(error));
203+
attributes.put(
204+
STATUS_ATTRIBUTE,
205+
(String) ObservabilityUtils.extractStatus(error, ApiTracerContext.Transport.GRPC));
200206
metricsRecorder.recordAttemptLatency(attemptTimer.elapsed(TimeUnit.MILLISECONDS), attributes);
201207
metricsRecorder.recordAttemptCount(1, attributes);
202208
}
@@ -210,7 +216,9 @@ public void attemptFailedRetriesExhausted(Throwable error) {
210216
*/
211217
@Override
212218
public void attemptPermanentFailure(Throwable error) {
213-
attributes.put(STATUS_ATTRIBUTE, ObservabilityUtils.extractStatus(error));
219+
attributes.put(
220+
STATUS_ATTRIBUTE,
221+
(String) ObservabilityUtils.extractStatus(error, ApiTracerContext.Transport.GRPC));
214222
metricsRecorder.recordAttemptLatency(attemptTimer.elapsed(TimeUnit.MILLISECONDS), attributes);
215223
metricsRecorder.recordAttemptCount(1, attributes);
216224
}

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/ObservabilityUtils.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@
3939

4040
class ObservabilityUtils {
4141

42-
/** Function to extract the status of the error as a string (defaults to gRPC canonical codes). */
43-
static String extractStatus(@Nullable Throwable error) {
44-
return (String) extractStatus(error, ApiTracerContext.Transport.GRPC);
45-
}
46-
4742
static Object extractStatus(@Nullable Throwable error, ApiTracerContext.Transport transport) {
4843
if (transport == ApiTracerContext.Transport.HTTP) {
4944
return extractHttpStatus(error);
@@ -70,12 +65,16 @@ private static Long extractHttpStatus(@Nullable Throwable error) {
7065
return 200L;
7166
} else if (error instanceof ApiException) {
7267
Object transportCode = ((ApiException) error).getStatusCode().getTransportCode();
68+
// HttpJsonStatusCode.getTransportCode() returns an Integer (HTTP status code).
69+
// GrpcStatusCode returns a Status.Code enum, and FakeStatusCode (in tests) returns
70+
// a StatusCode.Code enum. If it's not an Integer, we fall back to the mapped
71+
// HTTP status code of the canonical code.
7372
if (transportCode instanceof Integer) {
7473
return ((Integer) transportCode).longValue();
7574
} else {
7675
return (long) ((ApiException) error).getStatusCode().getCode().getHttpStatusCode();
7776
}
78-
}
77+
}
7978
StatusCode.Code code = StatusCode.Code.UNKNOWN;
8079
if (error instanceof CancellationException) {
8180
code = StatusCode.Code.CANCELLED;

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/SpanTracer.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
import io.opentelemetry.api.trace.SpanBuilder;
3737
import io.opentelemetry.api.trace.SpanKind;
3838
import io.opentelemetry.api.trace.Tracer;
39-
40-
import java.time.Duration;
4139
import java.util.HashMap;
4240
import java.util.Map;
4341
import java.util.concurrent.CancellationException;
@@ -136,7 +134,7 @@ public void attemptStarted(Object request, int attemptNumber) {
136134

137135
@Override
138136
public void attemptSucceeded() {
139-
endAttempt(null);
137+
endAttempt(null);
140138
}
141139

142140
@Override
@@ -183,33 +181,33 @@ private long extractContentLength(java.util.Map<String, Object> headers) {
183181

184182
@Override
185183
public void attemptCancelled() {
186-
endAttempt(new CancellationException());
184+
endAttempt(new CancellationException());
187185
}
188186

189187
@Override
190188
public void attemptFailedDuration(Throwable error, java.time.Duration delay) {
191-
endAttempt(error);
189+
endAttempt(error);
192190
}
193191

194192
@Override
195193
public void attemptFailedRetriesExhausted(Throwable error) {
196-
endAttempt(error);
194+
endAttempt(error);
197195
}
198196

199197
@Override
200198
public void attemptPermanentFailure(Throwable error) {
201-
endAttempt(error);
199+
endAttempt(error);
202200
}
203201

204-
private void endAttempt(Throwable error) {
202+
private void endAttempt(Throwable error) {
205203
if (attemptSpan != null) {
206-
Map<String, Object> endAttributes = new HashMap<>();
207-
ObservabilityUtils.populateStatusAttributes(
208-
endAttributes, error, this.apiTracerContext.transport());
204+
Map<String, Object> endAttributes = new HashMap<>();
205+
ObservabilityUtils.populateStatusAttributes(
206+
endAttributes, error, this.apiTracerContext.transport());
209207

210-
if (!endAttributes.isEmpty()) {
211-
attemptSpan.setAllAttributes(ObservabilityUtils.toOtelAttributes(endAttributes));
212-
}
208+
if (!endAttributes.isEmpty()) {
209+
attemptSpan.setAllAttributes(ObservabilityUtils.toOtelAttributes(endAttributes));
210+
}
213211

214212
attemptSpan.end();
215213
attemptSpan = null;

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/ObservabilityUtilsTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,26 @@ void testExtractStatus_errorConversion_apiExceptions() {
4747
ApiException error =
4848
new ApiException(
4949
"fake_error", null, new FakeStatusCode(StatusCode.Code.INVALID_ARGUMENT), false);
50-
String errorCode = ObservabilityUtils.extractStatus(error);
50+
String errorCode =
51+
(String) ObservabilityUtils.extractStatus(error, ApiTracerContext.Transport.GRPC);
5152
assertThat(errorCode).isEqualTo(StatusCode.Code.INVALID_ARGUMENT.toString());
5253
}
5354

5455
@Test
5556
void testExtractStatus_errorConversion_noError() {
5657
// test "OK", which corresponds to a "null" error.
57-
String successCode = ObservabilityUtils.extractStatus(null);
58+
String successCode =
59+
(String) ObservabilityUtils.extractStatus(null, ApiTracerContext.Transport.GRPC);
5860
assertThat(successCode).isEqualTo(StatusCode.Code.OK.toString());
5961
}
6062

6163
@Test
6264
void testExtractStatus_errorConversion_unknownException() {
6365
// test "UNKNOWN"
6466
Throwable unknownException = new RuntimeException();
65-
String errorCode2 = ObservabilityUtils.extractStatus(unknownException);
67+
String errorCode2 =
68+
(String)
69+
ObservabilityUtils.extractStatus(unknownException, ApiTracerContext.Transport.GRPC);
6670
assertThat(errorCode2).isEqualTo(StatusCode.Code.UNKNOWN.toString());
6771
}
6872

0 commit comments

Comments
 (0)