PT-243 added --max-hostname-length & max-line-length to pt-query-digest

This commit is contained in:
Carlos Salguero
2018-03-27 14:23:59 -03:00
parent 41c788c680
commit a48a767eb4
11 changed files with 201 additions and 25 deletions

View File

@@ -6745,6 +6745,12 @@ sub BUILDARGS {
ts => 1,
},
};
if (!defined($self->{max_hostname_length})) {
$self->{max_hostname_length} = MAX_STRING_LENGTH;
}
if (!defined($self->{max_line_length})) {
$self->{max_line_length} = LINE_LENGTH;
}
return $self;
}
@@ -7613,16 +7619,19 @@ sub format_string_list {
if ( $str =~ m/(?:\d+\.){3}\d+/ ) {
$print_str = $str; # Do not shorten IP addresses.
}
elsif ( length $str > MAX_STRING_LENGTH ) {
$print_str = substr($str, 0, MAX_STRING_LENGTH) . '...';
}
else {
elsif ( $self->{max_hostname_length} > 0 and length $str > $self->{max_hostname_length} ) {
$print_str = substr($str, 0, $self->{max_hostname_length}) . '...';
} else {
$print_str = $str;
}
my $p = percentage_of($cnt_for->{$str}, $class_cnt);
$print_str .= " ($cnt_for->{$str}/$p%)";
if ( !$show_all->{$attrib} ) {
last if (length $line) + (length $print_str) > LINE_LENGTH - 27;
my $trim_length = LINE_LENGTH;
if ($self->{max_hostname_length} == 0 or $self->{max_hostname_length} > LINE_LENGTH) {
$trim_length = $self->{max_hostname_length};
}
if ( $self->{max_line_length} > 0 and !$show_all->{$attrib} ) {
last if (length $line) + (length $print_str) > $self->{max_line_length} - 27;
}
$line .= "$print_str, ";
$i++;
@@ -14522,13 +14531,15 @@ sub print_reports {
? 'JSONReportFormatter'
: 'QueryReportFormatter';
my $qrf = $report_class->new(
dbh => $ep_dbh,
QueryReview => $args{QueryReview},
QueryRewriter => $args{QueryRewriter},
OptionParser => $args{OptionParser},
QueryParser => $args{QueryParser},
Quoter => $args{Quoter},
show_all => $show_all,
dbh => $ep_dbh,
QueryReview => $args{QueryReview},
QueryRewriter => $args{QueryRewriter},
OptionParser => $args{OptionParser},
QueryParser => $args{QueryParser},
Quoter => $args{Quoter},
show_all => $show_all,
max_hostname_length => $o->get('max-hostname-length'),
max_line_length => $o->get('max-line-length'),
);
$qrf->print_reports(
@@ -15921,6 +15932,18 @@ type: string
Print all output to this file when daemonized.
=item --max-hostname-length
type: int; default: 10
Trim host names in reports to this length. 0=Do not trim host names.
=item --max-line-length
type: int; default: 74
Trim lines to this length. 0=Do not trim lines.
=item --order-by
type: Array; default: Query_time:sum

View File

@@ -138,6 +138,12 @@ sub BUILDARGS {
ts => 1,
},
};
if (!defined($self->{max_hostname_length})) {
$self->{max_hostname_length} = MAX_STRING_LENGTH;
}
if (!defined($self->{max_line_length})) {
$self->{max_line_length} = LINE_LENGTH;
}
return $self;
}
@@ -1160,16 +1166,19 @@ sub format_string_list {
if ( $str =~ m/(?:\d+\.){3}\d+/ ) {
$print_str = $str; # Do not shorten IP addresses.
}
elsif ( length $str > MAX_STRING_LENGTH ) {
$print_str = substr($str, 0, MAX_STRING_LENGTH) . '...';
}
else {
elsif ( $self->{max_hostname_length} > 0 and length $str > $self->{max_hostname_length} ) {
$print_str = substr($str, 0, $self->{max_hostname_length}) . '...';
} else {
$print_str = $str;
}
my $p = percentage_of($cnt_for->{$str}, $class_cnt);
$print_str .= " ($cnt_for->{$str}/$p%)";
if ( !$show_all->{$attrib} ) {
last if (length $line) + (length $print_str) > LINE_LENGTH - 27;
my $trim_length = LINE_LENGTH;
if ($self->{max_hostname_length} == 0 or $self->{max_hostname_length} > LINE_LENGTH) {
$trim_length = $self->{max_hostname_length};
}
if ( $self->{max_line_length} > 0 and !$show_all->{$attrib} ) {
last if (length $line) + (length $print_str) > $self->{max_line_length} - 27;
}
$line .= "$print_str, ";
$i++;

View File

@@ -881,6 +881,84 @@ ok(
"IPs not shortened with more"
);
# Don't shorten hostnames
$events = [
{
cmd => 'Query',
arg => "foo",
Query_time => '8.000652',
host => 'a-really-long-host-name',
},
{
cmd => 'Query',
arg => "foo",
Query_time => '8.000652',
host => '123.123.123.789',
},
];
$ea = new EventAggregator(
groupby => 'arg',
worst => 'Query_time',
ignore_attributes => [qw(arg cmd)],
);
foreach my $event (@$events) {
$ea->aggregate($event);
}
$ea->calculate_statistical_metrics();
my $no_trim_qrf = new QueryReportFormatter(
OptionParser => $o,
QueryRewriter => $qr,
QueryParser => $qp,
Quoter => $q,
ExplainAnalyzer => $ex,
max_hostname_length => 0,
);
$result = $no_trim_qrf->event_report(
ea => $ea,
select => [ qw(Query_time host) ],
item => 'foo',
rank => 1,
orderby => 'Query_time',
);
ok(
no_diff(
$result,
"t/lib/samples/QueryReportFormatter/report014_no_trim.txt",
cmd_output => 1,
),
"Hostnames were not trimmed"
);
$no_trim_qrf = new QueryReportFormatter(
OptionParser => $o,
QueryRewriter => $qr,
QueryParser => $qp,
Quoter => $q,
ExplainAnalyzer => $ex,
max_hostname_length => 12,
max_line_length => 200,
);
$result = $no_trim_qrf->event_report(
ea => $ea,
select => [ qw(Query_time host) ],
item => 'foo',
rank => 1,
orderby => 'Query_time',
);
ok(
no_diff(
$result,
"t/lib/samples/QueryReportFormatter/report014_trim_12.txt",
cmd_output => 1,
),
"Hostnames were not trimmed"
);
$result = $qrf->event_report(
ea => $ea,
select => [ qw(Query_time host) ],

View File

@@ -0,0 +1,8 @@
# Item 1: 0 QPS, 0x concurrency, ID 0xEDEF654FCCC4A4D8 at byte 0 _________
# Scores: V/M = 0.00
# Attribute pct total min max avg 95% stddev median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count 100 2
# Exec time 100 16s 8s 8s 8s 8s 0 8s
# String:
# Hosts 123.123.123.789 (1/50%)... 1 more

View File

@@ -0,0 +1,8 @@
# Item 1: 0 QPS, 0x concurrency, ID 0xEDEF654FCCC4A4D8 at byte 0 _________
# Scores: V/M = 0.00
# Attribute pct total min max avg 95% stddev median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count 100 2
# Exec time 100 16s 8s 8s 8s 8s 0 8s
# String:
# Hosts 123.123.123.789 (1/50%), a-really-lon... (1/50%)

View File

@@ -2,7 +2,7 @@
# Scores: V/M = 0.00
# Attribute pct total min max avg 95% stddev median
# ============ === ======= ======= ======= ======= ======= ======= =======
# Count 100 3
# Exec time 100 24s 8s 8s 8s 8s 0 8s
# Count 100 2
# Exec time 100 16s 8s 8s 8s 8s 0 8s
# String:
# Hosts 123.123.123.456 (1/33%)... 2 more
# Hosts 123.123.123.789 (1/50%), a-really-l... (1/50%)

View File

@@ -0,0 +1,16 @@
/usr/sbin/mysqld, Version: 5.0.77-percona-b13-log (MySQL Percona Edition (GPL)). started with:
Tcp port: 3306 Unix socket: /var/run/mysqld/mysqld.sock
Time Id Command Argument
# Time: 090311 18:11:50
# User@Host: root[root] @ alonghotnamelikelocalhost []
# Thread_id: 47 Schema:
# Query_time: 0.017850 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0 Rows_affected: 0 Rows_read: 0
# QC_Hit: No Full_scan: No Full_join: No Tmp_table: No Tmp_table_on_disk: No
# Filesort: No Filesort_on_disk: No Merge_passes: 0
# administrator command: Refresh;
# User@Host: root[root] @ alonghotnamelikelocalhost []
# Thread_id: 47 Schema:
# Query_time: 0.000002 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0 Rows_affected: 0 Rows_read: 0
# QC_Hit: No Full_scan: No Full_join: No Tmp_table: No Tmp_table_on_disk: No
# Filesort: No Filesort_on_disk: No Merge_passes: 0
# administrator command: Quit;

View File

@@ -0,0 +1,34 @@
#!/usr/bin/env perl
BEGIN {
die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
};
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 1;
use PerconaTest;
my $run_with = "$trunk/bin/pt-query-digest --max-hostname-length 0 --max-line-length 100 --report-format=query_report --limit 10 $trunk/t/lib/samples/slowlogs/";
# #############################################################################
# Issue 232: mk-query-digest does not properly handle logs with an empty Schema:
# #############################################################################
my $output = 'foo'; # clear previous test results
my $cmd = "${run_with}slow-pt-243.txt";
$output = `$cmd 2>&1`;
like(
$output,
qr/Hosts\s+alonghotnamelikelocalhost/,
'Hostname is not being truncated',
);
# #############################################################################
# Done.
# #############################################################################
exit;

View File

@@ -32,7 +32,7 @@
# Tables
# SHOW TABLE STATUS FROM `issue_1196` LIKE 't'\G
# SHOW CREATE TABLE `issue_1196`.`t`\G
# EXPLAIN /*!50100 PARTITIONS */
# EXPLAIN /*!50100 PARTITIONS*/
select t.a, count(*) from t join t t2 using(a) group by 1 order by 2 desc limit 10\G
# *************************** 1. row ***************************
# id: 1

View File

@@ -28,7 +28,7 @@
# Tables
# SHOW TABLE STATUS FROM `food` LIKE 'trees'\G
# SHOW CREATE TABLE `food`.`trees`\G
# EXPLAIN /*!50100 PARTITIONS */
# EXPLAIN /*!50100 PARTITIONS*/
SELECT fruit FROM trees\G
# *************************** 1. row ***************************
# id: 1

View File

@@ -28,7 +28,7 @@
# Tables
# SHOW TABLE STATUS FROM `food` LIKE 'trees'\G
# SHOW CREATE TABLE `food`.`trees`\G
# EXPLAIN /*!50100 PARTITIONS */
# EXPLAIN /*!50100 PARTITIONS*/
SELECT fruit FROM trees\G
# *************************** 1. row ***************************
# id: 1