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

Add support printing TID to logs #323

Merged
merged 7 commits into from
Nov 4, 2023

Conversation

CodePrometheus
Copy link
Contributor

@CodePrometheus CodePrometheus commented Oct 11, 2023

@wu-sheng wu-sheng added the feature New feature label Oct 11, 2023
@wu-sheng
Copy link
Member

Please update the docs accordingly.

@CodePrometheus
Copy link
Contributor Author

Please update the docs accordingly.

done

@wu-sheng
Copy link
Member

@Superskyyy Is there any doc about introducing how to use the logger?

@Superskyyy
Copy link
Member

@Superskyyy Is there any doc about introducing how to use the logger?

Not about this particular feature, @CodePrometheus would you mind adding a small doc to this section (by adding a new subsection called something like ``printing traceID to logs".

def getLogger(name=None): # noqa
logger = logging.getLogger(name)
ch = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(name)s [pid:%(process)d] [%(threadName)s] [%(levelname)s] %(message)s')
formatter = logging.Formatter('%(asctime)s %(name)s %(tid)s [pid:%(process)d] [%(threadName)s] [%(levelname)s] %(message)s')
Copy link
Member

Choose a reason for hiding this comment

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

This feature should be optional as consistent with the Java agent (see here), because not all users want tids in their logs. To achieve this behavior in Python, please introduce a new setting to control the insertion of tids and adding the additional filter.

@Superskyyy
Copy link
Member

Superskyyy commented Oct 16, 2023

@CodePrometheus Oh I just realized your understanding of this feature from conversation in #323 was not entirely accurate. Sorry, I should have pointed out earlier when you were confirming it. The place you put this code was wrong, skywalking/loggings.py is the dedicated place to hold logs of the agent itself, while the log where users want to see traceIDs are within their own application logs. You should move these code to the sw_logging module under skywalking/log. Then its all good, the code itself is correct.

@CodePrometheus
Copy link
Contributor Author

Copy that, thanks for the reply, this is my negligence, please give me some time, I will improve this PR as soon as possible.

@Superskyyy
Copy link
Member

Copy that, thanks for the reply, this is my negligence, please give me some time, I will improve this PR as soon as possible.

No problem! I will be waiting patiently, take your time. Ping me for any help you need.

@wu-sheng
Copy link
Member

@Superskyyy As this is a terminal output, we don't have CI to verify this. Could you review and test locally?

@wu-sheng
Copy link
Member

@CodePrometheus Please fix CI.

@CodePrometheus
Copy link
Contributor Author

The latest CI exception does not seem to be caused by this PR change.
case1, case2.

@wu-sheng
Copy link
Member

@Superskyyy Please recheck, and fix the main branch if needed.

@Superskyyy
Copy link
Member

Yes, I have a fix coming, it is due to a upstream dependency bug in flask, I will hold the merge of this pr untill that one is fixed first.

@Superskyyy
Copy link
Member

Superskyyy commented Oct 30, 2023

Thanks! The code LGTM, please regenerate the configuration documentation by running the checklist command below. I will test it locally tmr.

  • I have rebuilt the Configuration.md documentation by running make doc-gen

Comment on lines 80 to 81
if config.agent_log_print_tid:
record.tid = str(context.segment.related_traces[0])
Copy link
Member

Choose a reason for hiding this comment

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

We add the tid info to the record only when agent_log_print_tid == true, but what if I set SW_AGENT_LOG_REPORTER_LAYOUT='%(tid)s %(asctime)s [%(threadName)s] %(levelname)s %(name)s - %(message)s but agent_log_print_tid == false? The implementation looks unreasonable to me, we have two ways to modify the log layout which might be conflict with each other.

Copy link
Contributor Author

@CodePrometheus CodePrometheus Oct 31, 2023

Choose a reason for hiding this comment

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

@kezhenxu94 thanks for your comment, my opinion is:

  1. If and only if agent_log_print_tid=true, the agent will enable the ability to print traceId.
  2. When the user-defined layout has tid, but agent_log_print_tid=false, printing traceId is turned off, so I will determine whether the user's input already exists tid before changing the layout.
  3. Or emphasize in the document that when agent_log_print_tid=true, the layout will change accordingly.

Copy link
Member

Choose a reason for hiding this comment

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

How about we make it more consistent with the Java agent, that is simply introducing the tid mechnism through the custom layout?

Copy link
Member

Choose a reason for hiding this comment

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

How about we make it more consistent with the Java agent, that is simply introducing the tid mechnism through the custom layout?

This makes sense to me

CHANGELOG.md Outdated
@@ -4,6 +4,7 @@

- Feature:
- Users now can specify the `SW_AGENT_ASYNCIO_ENHANCEMENT` environment variable to enable the performance enhancement with asyncio (#316)
- Add support printing TID to logs (#323)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- Add support printing TID to logs (#323)
- Support printing Trace IDs (TID) to collected application logs (#323)


## Print trace ID in your logs
To print out the traceId in the logs, simply add `%(tid)s` to the `agent_log_reporter_layout`.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
To print out the traceId in the logs, simply add `%(tid)s` to the `agent_log_reporter_layout`.
To print out the trace IDs in the logs, simply add `%(tid)s` to the `agent_log_reporter_layout`.

@@ -75,6 +75,9 @@ def build_log_tags() -> LogTags:

context = get_context()

if 'tid' in layout:
Copy link
Member

Choose a reason for hiding this comment

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

Please check for the full %(tid)s instead of this, otherwise one may surpise you with something like tidewave.

@Superskyyy
Copy link
Member

Since tid is injected into the logging record, user could also utilize this to print out the tid in whatever channel they want other than only the reported logs to oap, this can be done using any formatter they use in their own application logic, this should be pointed out in the documentation. @CodePrometheus

Copy link
Member

@Superskyyy Superskyyy left a comment

Choose a reason for hiding this comment

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

LGTM

@wu-sheng wu-sheng added this to the 1.1.0 milestone Nov 4, 2023
@wu-sheng wu-sheng merged commit bea712c into apache:master Nov 4, 2023
91 checks passed
@CodePrometheus CodePrometheus deleted the feature_tid_logs branch November 5, 2023 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants