DEV Community

Cover image for Monitoring a Plack Application with SlapbirdAPM
Rawley Fowler
Rawley Fowler

Posted on

Monitoring a Plack Application with SlapbirdAPM

SlapbirdAPM is a free and open-source application performance monitoring (APM) platform specifically designed for Perl web applications. It provides developers with comprehensive observability tools to monitor and optimize their applications' performance.

SlapbirdAPM is easily installed on your Plack application, here is a minimal example, using a Dancer2 application:

Install with

cpan -I SlapbirdAPM::Agent::Plack
Enter fullscreen mode Exit fullscreen mode
#!/usr/bin/env perl

use Dancer2;
use Plack::Builder;

get '/' => sub {
    'Hello World!';
};

builder {
    enable 'SlapbirdAPM';
    app;
};
Enter fullscreen mode Exit fullscreen mode

Now, you can create an account on SlapbirdAPM, and create your application.

New Application

Then, simply copy the API key output and, add it to your application via the SLAPBIRDAPM_API_KEY environment variable. For example:

SLAPBIRDAPM_API_KEY=<API-KEY> plackup app.pl
Enter fullscreen mode Exit fullscreen mode

or, you can pass your key in to the middleware:

builder {
    enable 'SlapbirdAPM', key => <YOUR API KEY>;
    ...
};
Enter fullscreen mode Exit fullscreen mode

Now when you navigate to /, you will see it logged in your Slapbird dashboard!

Dashboard

Individual transaction

SlapbirdAPM also supports DBI, meaning you can trace your queries, let's edit our application to include a few DBI queries:

#!/usr/bin/env perl

use Dancer2;
use DBI;
use Plack::Builder;

my $dbh = DBI->connect( 'dbi:SQLite:dbname=database.db', '', '' );

$dbh->do('create table if not exists users (id integer primary key, name varchar)');

get '/' => sub {
    send_as html => 'Hello World!';
};

get '/users/:id' => sub {
    my $user_id = route_parameters->get('id');
    my ($user) = 
        $dbh->selectall_array(
          'select * from users where id = ?',
          { Slice => {} }, $user_id );
    send_as JSON => $user;
};

post '/users' => sub {
    my $user_name = body_parameters->get('name');
    my ($user) =
      $dbh->selectall_array(
        'insert into users(name) values ( ? ) returning id, name',
        { Slice => {} }, $user_name );
    send_as JSON => $user;
};

builder {
    enable 'SlapbirdAPM';
    app;
};
Enter fullscreen mode Exit fullscreen mode

Now we can use cURL to add data to our database:

curl -X POST -d 'name=bob' http://127.0.0.1:5000/users
Enter fullscreen mode Exit fullscreen mode

Then, if we go back into Slapbird, we can view our timings for our queries:

Image description

This just breaks the surface of what is possible using SlapbirdAPM. You can also, generate reports, perform health-checks, and get notified if your application is creating too many 5XX responses.

Thanks for reading!

Top comments (0)