Skip to content
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

Dynatrace registry: Truncate log output #3148

Conversation

arminru
Copy link
Contributor

@arminru arminru commented Apr 28, 2022

Sometimes the response body can be quite large so we should truncate it for logging.

Also logging stack traces from errors during HTTP requests only on debug level should be sufficient.

.onError(response -> logger.error("Failed metric ingestion: Error Code={}, Response Body={}", response.code(), response.body()));
.onError(response -> logger.error("Failed metric ingestion: Error Code={}, Response Body={}",
response.code(),
StringUtils.truncate(response.body(), LOG_RESPONSE_BODY_TRUNCATION_LIMIT)));
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about signaling that the body was truncated?
E.g.: if the length of response.body() is more than the limit, add "[...]" or (truncated).

Copy link
Contributor Author

@arminru arminru May 2, 2022

Choose a reason for hiding this comment

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

I would've used apache/commons/lang3/StringUtils.html#abbreviate if present but unfortunately we don't have that yet in Micrometer's StringUtils.
I could add it there if you think it would make sense.

Copy link
Member

Choose a reason for hiding this comment

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

I think it would make sense so that it is reusable across the code base.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added StringUtils.truncate + tests for it and changed the exporter to use it.

} catch (Throwable throwable) {
logger.error("Failed metric ingestion: " + throwable.getMessage(), throwable);
logger.error("Failed metric ingestion: " + throwable);
logger.debug(throwable);
Copy link
Member

Choose a reason for hiding this comment

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

Is the idea of this coming from a user?
I usually like seeing the stacktrace if an error happened, it makes it easy where the error is coming from. Logging it out on DEBUG level defeats that purpose, you need to know where the log event is coming from so that you can see (and read the code) that a stacktrace is available on a lower level.

Copy link
Member

Choose a reason for hiding this comment

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

Also, the DEBUG event will not contain a message so it will look weird: it will not tell you what happened and nothing guarantees that these events will be next to (or close to) each other in the logs.

If we decide that we don't want the stacktrace on ERROR level (I would advise against it), I would state that the user sees this stacktrace because metric ingestion failed and there is another log event about it on ERROR level.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this was coming from a user.
With code access, the execution context should already be quite clear since the "Failed metric ingestion: " prefix and throwable.toString() afterwards only appear in one single place.
Good point about the events probably not being next to each other necessarily, I did not consider this.

How about this?

            logger.error("Failed metric ingestion: " + throwable);
            logger.debug("Stack trace for previous 'Failed metric ingestion' error log: ", throwable);

Ideally we would have a throttled error log for this but there is no throttled logger available in Micrometer already, right?

Copy link
Member

Choose a reason for hiding this comment

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

There is WarnThenDebugLogger that logs at warn level at first and then logs at debug level for the rest of the events.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it to always print the message as a warning and use the WarnThenDebugLogger for the stack including a reference to the other warning message. This is under the assumption that it will usually be the same error if it would occur multiple times and that the overall context is clear from the log message prefix.

@jonatan-ivanov jonatan-ivanov added type: task A general task registry: dynatrace A Dynatrace Registry related issue waiting for feedback We need additional information before we can continue labels Apr 29, 2022
@jonatan-ivanov jonatan-ivanov removed the waiting for feedback We need additional information before we can continue label May 4, 2022
@jonatan-ivanov jonatan-ivanov added this to the 1.10.0-M2 milestone May 4, 2022
@jonatan-ivanov jonatan-ivanov merged commit ea438e5 into micrometer-metrics:main May 4, 2022
@jonatan-ivanov
Copy link
Member

Thank you! :)

@arminru arminru deleted the dt-log-abbreviate branch May 5, 2022 08:48
@izeye
Copy link
Contributor

izeye commented May 8, 2022

This seems to have been lost while merging #3152.

shakuzen pushed a commit that referenced this pull request May 9, 2022
This got lost in a rebase gone bad with gh-3152. See original commit ea438e5 from gh-3148.
@shakuzen
Copy link
Member

shakuzen commented May 9, 2022

This seems to have been lost while merging #3152.

Thank you for catching that. It was my fault in messing up the rebase on #3152. I believe I've restored these changes now with ccbde40

jonatan-ivanov pushed a commit that referenced this pull request May 10, 2022
@jonatan-ivanov jonatan-ivanov modified the milestones: 1.10.0-M2, 1.9.0 May 10, 2022
@jonatan-ivanov
Copy link
Member

fyi: I backported these to 1.9.

@shakuzen shakuzen added enhancement A general enhancement and removed type: task A general task labels May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A general enhancement registry: dynatrace A Dynatrace Registry related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants