Skip to content

Commit

Permalink
Add stackcollapse for java error stacks in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
pcdv committed Jan 25, 2018
1 parent a8d807a commit 8dad2d3
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions stackcollapse-java-exceptions.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/perl -w
#
# stackcolllapse-java-exceptions.pl collapse java exceptions (found in logs) into single lines.
#
# Parses Java error stacks found in a log file and outputs them as
# single lines, with methods separated by semicolons, and then a space and an
# occurrence count. Inspired by stackcollapse-jstack.pl except that it does
# not act as a performance profiler.
#
# It can be useful if a Java process dumps a lot of different stacks in its logs
# and you want to quickly identify the biggest culprits.
#
# USAGE: ./stackcollapse-java-exceptions.pl infile > outfile
#
# Copyright 2018 Paul de Verdiere. All rights reserved.

use strict;
use Getopt::Long;

# tunables
my $shorten_pkgs = 0; # shorten package names
my $no_pkgs = 0; # really shorten package names!!
my $help = 0;

sub usage {
die <<USAGE_END;
USAGE: $0 [options] infile > outfile\n
--shorten-pkgs : shorten package names
--no-pkgs : suppress package names (makes SVG much more readable)
USAGE_END
}

GetOptions(
'shorten-pkgs!' => \$shorten_pkgs,
'no-pkgs!' => \$no_pkgs,
'help' => \$help,
) or usage();
$help && usage();

my %collapsed;

sub remember_stack {
my ($stack, $count) = @_;
$collapsed{$stack} += $count;
}

my @stack;

foreach (<>) {
chomp;

if (/^\s*at ([^\(]*)/) {
my $func = $1;
if ($shorten_pkgs || $no_pkgs) {
my ($pkgs, $clsFunc) = ( $func =~ m/(.*\.)([^.]+\.[^.]+)$/ );
$pkgs =~ s/(\w)\w*/$1/g;
$func = $no_pkgs ? $clsFunc: $pkgs . $clsFunc;
}
unshift @stack, $func;
} elsif (@stack ) {
remember_stack(join(";", @stack), 1) if @stack;
undef @stack;
}
}

remember_stack(join(";", @stack), 1) if @stack;

foreach my $k (sort { $a cmp $b } keys %collapsed) {
print "$k $collapsed{$k}\n";
}

0 comments on commit 8dad2d3

Please sign in to comment.