Skip to content

Logging exceptions with context #45136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 8, 2025
Merged

Conversation

lmolkova
Copy link
Member

@lmolkova lmolkova commented Apr 25, 2025

Fix #44865

Exception messages don't contain all the useful information we put in logs (e.g. connection-id when connection drops).
This PR allows to create and log exception using the same context in log and inside the exception message:

throw logger.throwableAtWarning()
  .addKeyValue("connectionId""foo")
  .addKeyValue("linkName"1)
  .log("connection dropped", IOException::new);

Results in exception message like :

java.io.IOException: connection dropped; {"connectionId":"foo","linkName":1}

and log message:

{"message":"connection dropped","exception.type":"java.io.IOException","connectionId":"foo","linkName":1}

@github-actions github-actions bot added Azure.Core.V2 Contains issues to consider when desiging Azure Core V2 clientcore labels Apr 25, 2025
@lmolkova lmolkova force-pushed the logging-exceptions-final branch from e0d0a7d to 524a5a7 Compare April 25, 2025 23:14
@azure-sdk
Copy link
Collaborator

azure-sdk commented Apr 25, 2025

API Change Check

APIView identified API level changes in this PR and created the following API reviews

io.clientcore:core

@azure-sdk
Copy link
Collaborator

azure-sdk commented Apr 25, 2025

API change check

API changes are not detected in this pull request.

@lmolkova lmolkova force-pushed the logging-exceptions-final branch from 0b51031 to e96f0c7 Compare May 5, 2025 21:36
@lmolkova lmolkova requested a review from Copilot May 5, 2025 21:43
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request updates exception logging across the codebase to log additional context using the new throwableAtError()/log() methods instead of the previous logThrowableAsError() approach. Key changes include replacing exception throwing and logging patterns with more context-rich variants and removing unnecessary exception wrapping in various client, credential, and test classes.

Reviewed Changes

Copilot reviewed 104 out of 106 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
sdk/clientcore/core/src/main/java/io/clientcore/core/http/pipeline/HttpRetryPolicy.java Updated exception logging in retry loops and interruption handling.
sdk/clientcore/core/src/main/java/io/clientcore/core/http/pipeline/HttpRedirectOptions.java Simplified exception throwing using new logging methods.
sdk/clientcore/core/src/main/java/io/clientcore/core/http/pipeline/HttpPipelineBuilder.java Added key/value context for invalid policy positions and updated logging style.
sdk/clientcore/core/src/main/java/io/clientcore/core/http/paging/PagedIterable.java Changed logging when no more elements are available.
sdk/clientcore/core/src/main/java/io/clientcore/core/http/models/Response.java, HttpRequest.java, HttpRange.java, ETag.java Updated exception logging methods to include contextual information.
sdk/clientcore/core/src/main/java/io/clientcore/core/http/client/JdkHttpClientBuilder.java Refactored unsupported operation exceptions with new logging methods.
sdk/clientcore/core/src/main/java/io/clientcore/core/credentials/* Updated key credential and named credential validations to use the new logging pattern.
sdk/clientcore/annotation-processor(-test)/* Replaced wrapped RuntimeExceptions with more direct exception throwing and improved logging output.
sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/* In test implementations, updated exception handling to use HttpResponseException or UnsupportedOperationException as appropriate.
Files not reviewed (2)
  • sdk/clientcore/core/checkstyle-suppressions.xml: Language not supported
  • sdk/clientcore/core/spotbugs-exclude.xml: Language not supported

Copy link
Member

@alzimmermsft alzimmermsft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per offline discussions, it'd be good to split up T log(String, Throwable, BiFunction<String, Throwable, T>) into a few different methods that better align with calls.

T log(String, BiFunction<String, Throwable, T>) becomes T log(String, Function<String, T) and T log(Throwable, BiFunction<String, Throwable, T>) becomes T log(Throwable, Function<Throwable, T>), so we don't need certain BiFunction values ignored if the constructor called by the BiFunction doesn't support String or Throwable.

This should be fine if we add it as part of release as the compiler should warn ahead of time when a calling pattern is invalid or ambiguous.

@lmolkova lmolkova requested review from joshfree and conniey as code owners May 6, 2025 00:55
@lmolkova lmolkova force-pushed the logging-exceptions-final branch from c5894cf to 17341b6 Compare May 6, 2025 01:09
Copy link
Member

@JonathanGiles JonathanGiles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice work! Thanks for doing this.

@lmolkova lmolkova force-pushed the logging-exceptions-final branch from df60a93 to 1fa740a Compare May 7, 2025 18:26
@lmolkova lmolkova force-pushed the logging-exceptions-final branch from 1fa740a to 98efc13 Compare May 7, 2025 18:58
@lmolkova lmolkova merged commit 2eaa281 into Azure:main May 8, 2025
69 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure.Core.V2 Contains issues to consider when desiging Azure Core V2 clientcore
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clientcore: Exception log vs exception message
5 participants