Skip to content

Add contributing guidance for logging when handling exceptions #8228

@jack-berg

Description

@jack-berg

We actually have inconsistent and probably bad patterns around this in other places:

In these cases we log both the exception message and record the the exception itself:

logger.log(
Level.SEVERE,
"Failed to export "
+ type
+ "s. The request could not be executed. Full error message: "
+ e.getMessage(),
e);

logger.log(
Level.SEVERE,
"Failed to execute "
+ TYPE
+ "s. The request could not be executed. Error message: "
+ e.getMessage(),
e);

In these cases we include the stringified caught exception message and don't record the exception, preventing the user from seeing the stacktrace:

throw new ConfigurationException(
"Invalid duration property " + name + "=" + value + ". " + ex.getMessage());

logger.warning(
"Error closing " + closeable.getClass().getName() + ": " + ex.getMessage());

logger.log(
Level.WARNING,
"Exception caught while extracting span context; returning null. "
+ "Exception: [{0}] Message: [{1}]",
new String[] {e.getClass().getName(), e.getMessage()});

logger.warning(
"Error closing " + closeable.getClass().getName() + ": " + ex.getMessage());

logger.warning("Error setting explicit bucket boundaries advice: " + e.getMessage());
return this;

logger.warning("Error setting explicit bucket boundaries advice: " + e.getMessage());

We should add some guidance to https://github.com/open-telemetry/opentelemetry-java/blob/main/CONTRIBUTING.md#best-practices-that-we-follow to recommend not stringifying the exception and recording the exception using dedicated log overloads.

Originally posted by @jack-berg in #8216

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions