Skip to content

Add a new system variable tidb_session_alias to print some custom trace info in log #46071

@lcwangchao

Description

@lcwangchao

Feature Request

Add a new system variable tidb_session_alias to print some custom trace info in log. After setting this var by:

> set @@tidb_session_alias='custom_session1'

The log entries in the session will include a session_alias field:

[2023/08/14 17:48:50.766 +08:00] [WARN] [session.go:2255] ["compile SQL failed"] [conn=2097154] [session_alias=custom_session1] [error="[schema:1146]Table 'test.tnoexist' doesn't exist"] [SQL="select * from tnoexist"]
[2023/08/14 17:48:50.766 +08:00] [INFO] [conn.go:1096] ["command dispatched failed"] [conn=2097154] [session_alias=custom_session1] [connInfo="id:2097154, addr:127.0.0.1:61498 status:10, collation:utf8_general_ci, user:root"] [command=Query] [status="inTxn:0, autocommit:1"] [sql="select * from tnoexist"] [txn_mode=PESSIMISTIC] [timestamp=0] [err="[schema:1146]Table 'test.tnoexist' doesn't exist"]

Classifying logs with some custom dimensions (connection, statement, or business) will be helpful. The value of @@tidb_session_alias should also be propagated across nodes such as TiKV. For the above example, if the TiKV coprocessor execution is slow, a log with session_alias field will be logged like:

[2022/06/14 16:22:15.516 +08:00] [INFO] [tracker.rs:255] [slow-query] [connection_id=4132488064717029791] [session_alias=custom_session1] [perf_stats.internal_delete_skipped_count=0] [perf_stats.internal_key_skipped_count=0] [perf_stats.block_read_byte=48736] [perf_stats.block_read_count=2] [perf_stats.block_cache_hit_count=21] [scan.range.first="Some(start: 7480000000000002485F72800000008FEC1E34 end: 7480000000000002485F72800000008FEC1E35)"] [scan.ranges=2] [scan.total=2] [scan.processed=2] [scan.is_desc=false] [tag=select] [table_id=584] [txn_start_ts=433899413354053884] [total_suspend_time=0ns] [total_process_time=77ms] [handler_build_time=0ns] [wait_time.snapshot=0ns] [wait_time.schedule=2.101s] [wait_time=2.101s] [total_lifetime=2.178s] [remote_host=ipv4:192.168.1.20:54728] [region_id=1335237651]

connection id and session alias entry should be added to some kind of logs:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions