Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/knowledge/general-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ automatically by `otel.java-conventions`).
private static final Logger LOGGER = Logger.getLogger(MyClass.class.getName());
```

When logging exceptions, pass the exception as the `Throwable` parameter to the logger rather
than stringifying it via `getMessage()` or concatenation. This ensures logging frameworks can
render the full stack trace.

```java
// Do:
logger.log(Level.WARNING, "Failed to process request", exception);
// Don't:
logger.warning("Failed to process request: " + exception.getMessage());
```

## toString()

Adding `toString()` overrides is encouraged for debugging assistance. All `toString()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,7 @@ private void onError(
Throwable e) {
metricRecording.finishFailed(e);
logger.log(
Level.SEVERE,
"Failed to export "
+ type
+ "s. The request could not be executed. Error message: "
+ e.getMessage(),
e);
Level.SEVERE, "Failed to export " + type + "s. The request could not be executed.", e);
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Failed to export " + type + "s. Details follow:", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,7 @@ private void onError(
Throwable e) {
metricRecording.finishFailed(e);
logger.log(
Level.SEVERE,
"Failed to export "
+ type
+ "s. The request could not be executed. Full error message: "
+ e.getMessage(),
e);
Level.SEVERE, "Failed to export " + type + "s. The request could not be executed.", e);
result.failExceptionally(FailedExportException.httpFailedExceptionally(e));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ public <C> SpanContext extract(Format<C> format, C carrier) {
}
} catch (RuntimeException e) {
logger.log(
Level.WARNING,
"Exception caught while extracting span context; returning null. "
+ "Exception: [{0}] Message: [{1}]",
new String[] {e.getClass().getName(), e.getMessage()});
Level.WARNING, "Exception caught while extracting span context; returning null.", e);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,7 @@ private AutoConfiguredOpenTelemetrySdk buildImpl() {
logger.fine("Closing " + closeable.getClass().getName());
closeable.close();
} catch (IOException ex) {
logger.warning(
"Error closing " + closeable.getClass().getName() + ": " + ex.getMessage());
logger.log(Level.WARNING, "Error closing " + closeable.getClass().getName(), ex);
}
}
if (e instanceof ConfigurationException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ void configurationError_ClosesResources() {
verify(tracerProvider).close();
verify(meterProvider).close();

logs.assertContains("Error closing io.opentelemetry.sdk.trace.SdkTracerProvider: Error!");
logs.assertContains("Error closing io.opentelemetry.sdk.trace.SdkTracerProvider");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,7 @@ static <M, R> R createAndMaybeCleanup(
logger.fine("Closing " + closeable.getClass().getName());
closeable.close();
} catch (IOException ex) {
logger.warning(
"Error closing " + closeable.getClass().getName() + ": " + ex.getMessage());
logger.log(Level.WARNING, "Error closing " + closeable.getClass().getName(), ex);
}
}
if (e instanceof DeclarativeConfigException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,9 @@ private void onResponse(GrpcResponse grpcResponse) {

private static void onError(Throwable e) {
logger.log(
Level.SEVERE,
"Failed to execute "
+ TYPE
+ "s. The request could not be executed. Error message: "
+ e.getMessage(),
e);
Level.SEVERE, "Failed to execute " + TYPE + "s. The request could not be executed.", e);
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Failed to execute " + TYPE + "s. Details follow: " + e);
logger.log(Level.FINEST, "Failed to execute " + TYPE + "s. Details follow:", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public DoubleHistogramBuilder setExplicitBucketBoundariesAdvice(List<Double> buc
Objects.requireNonNull(bucketBoundaries, "bucketBoundaries must not be null");
ExplicitBucketHistogramUtils.validateBucketBoundaries(bucketBoundaries);
} catch (IllegalArgumentException | NullPointerException e) {
logger.warning("Error setting explicit bucket boundaries advice: " + e.getMessage());
logger.log(Level.WARNING, "Error setting explicit bucket boundaries advice", e);
return this;
}
builder.setExplicitBucketBoundaries(bucketBoundaries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public LongHistogramBuilder setExplicitBucketBoundariesAdvice(List<Long> bucketB
boundaries = bucketBoundaries.stream().map(Long::doubleValue).collect(Collectors.toList());
ExplicitBucketHistogramUtils.validateBucketBoundaries(boundaries);
} catch (IllegalArgumentException | NullPointerException e) {
logger.warning("Error setting explicit bucket boundaries advice: " + e.getMessage());
logger.log(Level.WARNING, "Error setting explicit bucket boundaries advice", e);
return this;
}
builder.setExplicitBucketBoundaries(boundaries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private static Stream<Arguments> histogramsWithInvalidAdvice() {
.build();
return histogram::record;
},
"Error setting explicit bucket boundaries advice: Bucket boundaries must be in increasing order: 10.0 >= 9.0"),
"Error setting explicit bucket boundaries advice"),
Arguments.of(
(Function<SdkMeterProvider, Consumer<Long>>)
meterProvider -> {
Expand All @@ -279,7 +279,7 @@ private static Stream<Arguments> histogramsWithInvalidAdvice() {
.build();
return histogram::record;
},
"Error setting explicit bucket boundaries advice: Bucket boundaries must be in increasing order: 10.0 >= 9.0"),
"Error setting explicit bucket boundaries advice"),
Arguments.of(
(Function<SdkMeterProvider, Consumer<Long>>)
meterProvider -> {
Expand All @@ -291,6 +291,6 @@ private static Stream<Arguments> histogramsWithInvalidAdvice() {
.build();
return histogram::record;
},
"Error setting explicit bucket boundaries advice: bucketBoundaries must not be null"));
"Error setting explicit bucket boundaries advice"));
}
}
Loading