Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MySQL Process List redirect to STDOUT
#!/bin/bash
set -ex
# Inject APM Trace ID (part of SQL query comment) to a Log event
# https://docs.datadoghq.com/tracing/advanced/connect_logs_and_traces/?tab=ruby
# Input: {"command": "Query", "current_statement": "SELECT ... /* 4572859165692197980 */"}
# Output: {"process_list": {"current_statement": "SELECT ... /* 4572859165692197980 */"}, "dd": {"trace_id": 4572859165692197980}}
function inject_trace_id() {
trace_id=$(echo "$1" |grep -Eo '/\* [0-9]{16,20} \*/' | awk '{print $2}')
if [ -z "$trace_id" ]
then
echo "{\"process_list\": $1}"
else
echo "{\"process_list\": $1, \"dd\": {\"trace_id\": ${trace_id}}}"
fi
}
# Process List poll to JSON format, duration transform to millisecond unit
# Output multiline: {"command": "Query", "current_statement": "SELECT ... /* 4572859165692197980 */"}
function process_list() {
/usr/bin/mysql -h127.0.0.1 -uroot -p111111 -P6033 -s -r -e "SELECT
JSON_OBJECT(
'thd_id', thd_id,
'conn_id', conn_id,
'command', command,
'state', state,
'current_statement', current_statement,
'statement_latency', statement_latency / 1000,
'progress', progress,
'lock_latency', lock_latency / 1000,
'rows_examined', rows_examined,
'rows_sent', rows_sent,
'rows_affected', rows_affected,
'tmp_tables', tmp_tables,
'tmp_disk_tables', tmp_disk_tables,
'full_scan', full_scan,
'last_statement', last_statement,
'last_statement_latency', last_statement_latency / 1000
)
FROM sys.x\$processlist
WHERE pid IS NOT NULL
AND db != 'sys';"
}
# Read process list output line by line and redirect to STDOUT twice a second, STDOUT -> Datadog Logs
while true; do
process_list | while read -r item; do
inject_trace_id "$item" > /proc/1/fd/1
done
sleep 0.5;
done
# MIT License
@mikhailov

This comment has been minimized.

Copy link
Owner Author

@mikhailov mikhailov commented Oct 28, 2019

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment