Skip to content

Commit 1fa0bbd

Browse files
committed
otel: add null checks for retry instrumentation
1 parent cdf0eb0 commit 1fa0bbd

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

opentelemetry/src/main/java/io/grpc/opentelemetry/GrpcOpenTelemetry.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public Stopwatch get() {
6565
};
6666

6767
@VisibleForTesting
68-
static boolean ENABLE_OTEL_TRACING = GrpcUtil.getFlag("GRPC_EXPERIMENTAL_ENABLE_OTEL_TRACING",
69-
false);
68+
static boolean ENABLE_OTEL_TRACING =
69+
GrpcUtil.getFlag("GRPC_EXPERIMENTAL_ENABLE_OTEL_TRACING", false);
7070

7171
private final OpenTelemetry openTelemetrySdk;
7272
private final MeterProvider meterProvider;
@@ -242,8 +242,7 @@ static OpenTelemetryMetricsResource createMetricInstruments(Meter meter,
242242
.build());
243243
}
244244

245-
if (isMetricEnabled("grpc.client.call.retries", enableMetrics,
246-
disableDefault)) {
245+
if (isMetricEnabled("grpc.client.call.retries", enableMetrics, disableDefault)) {
247246
builder.clientCallRetriesCounter(
248247
meter.histogramBuilder(
249248
"grpc.client.call.retries")
@@ -254,14 +253,13 @@ static OpenTelemetryMetricsResource createMetricInstruments(Meter meter,
254253
.build());
255254
}
256255

257-
if (isMetricEnabled("grpc.client.call.retry_delay", enableMetrics,
258-
disableDefault)) {
256+
if (isMetricEnabled("grpc.client.call.retry_delay", enableMetrics, disableDefault)) {
259257
builder.clientCallRetryDelayCounter(
260258
meter.histogramBuilder(
261259
"grpc.client.call.retry_delay")
262260
.setUnit("s")
263-
.setDescription("Total time of delay while there is no active attempt during the " +
264-
"client call")
261+
.setDescription("Total time of delay while there is no active attempt during the "
262+
+ "client call")
265263
.setExplicitBucketBoundariesAdvice(LATENCY_BUCKETS)
266264
.build());
267265
}

opentelemetry/src/main/java/io/grpc/opentelemetry/OpenTelemetryMetricsModule.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -416,29 +416,32 @@ void recordFinishedCall() {
416416
}
417417
callLatencyNanos = callStopWatch.elapsed(TimeUnit.NANOSECONDS);
418418

419-
if (module.resource.clientCallDurationCounter() != null) {
420-
long retriesPerCall = 0;
421-
long attempts = attemptsPerCall.get();
422-
if (attempts > 0) {
423-
retriesPerCall = attempts - 1;
424-
}
425-
426-
// Base attributes
427-
io.opentelemetry.api.common.Attributes baseAttributes =
428-
io.opentelemetry.api.common.Attributes.of(
429-
METHOD_KEY, fullMethodName,
430-
TARGET_KEY, target
431-
);
419+
// Base attributes
420+
io.opentelemetry.api.common.Attributes baseAttributes =
421+
io.opentelemetry.api.common.Attributes.of(
422+
METHOD_KEY, fullMethodName,
423+
TARGET_KEY, target
424+
);
432425

433-
// Duration
426+
// Duration
427+
if (module.resource.clientCallDurationCounter() != null) {
434428
module.resource.clientCallDurationCounter().record(
435429
callLatencyNanos * SECONDS_PER_NANO,
436430
baseAttributes.toBuilder()
437431
.put(STATUS_KEY, status.getCode().toString())
438432
.build()
439433
);
434+
}
435+
436+
// Retry counts
437+
if (module.resource.clientCallRetriesCounter() != null) {
438+
439+
long retriesPerCall = 0;
440+
long attempts = attemptsPerCall.get();
441+
if (attempts > 0) {
442+
retriesPerCall = attempts - 1;
443+
}
440444

441-
// Retry counts
442445
module.resource.clientCallRetriesCounter().record(
443446
retriesPerCall,
444447
baseAttributes.toBuilder()
@@ -452,8 +455,10 @@ void recordFinishedCall() {
452455
.put(RETRY_TYPE_KEY, OpenTelemetryConstants.RetryType.TRANSPARENT.getValue())
453456
.build()
454457
);
458+
}
455459

456-
// Retry delay
460+
// Retry delay
461+
if (module.resource.clientCallRetryDelayCounter() != null) {
457462
module.resource.clientCallRetryDelayCounter().record(
458463
retryDelayNanos / NANOS_PER_SEC,
459464
baseAttributes

0 commit comments

Comments
 (0)