5

Is there any unique request ID in Dancer?

Apache has mod_unique_id: http://httpd.apache.org/docs/current/mod/mod_unique_id.html

PSGI/Plack has a middleware module: http://search.cpan.org/~bayashi/Plack-Middleware-RequestId-0.02/lib/Plack/Middleware/RequestId.pm

But is there anything native in Dancer I missed?

Sebastian
  • 2,472
  • 1
  • 18
  • 31
  • 1
    You can definitely add the Middleware in front of your Dancer program. Where's the problem with that? – simbabque May 08 '16 at 11:03
  • It's not a Debian package and depends on the Dancer app to run via Plack. – Sebastian May 08 '16 at 11:05
  • 1
    The [Dancer::Request object has an _id_ property](https://metacpan.org/source/BIGPRESH/Dancer-1.3202/lib/Dancer/Request.pm#L130) that seems to be an auto-increment number. I will try out how to use it. – simbabque May 08 '16 at 11:05
  • How do you run it? It should be fine in your app.psgi (or .pl) to just wrap it. Plack::Builder will deal with it. Under the hood, it's all PSGI, just the interface to the web is different. That's the whole idea of PSGI. – simbabque May 08 '16 at 11:07
  • Thanks, just tried: Looks like it's the `[hit #1]` counter used in logging. Should be a good ID combined with `$$` and `time` or `$^T`. Dancer apps could be run via `bin/app.pl` using the build-in webserver. You also don't need a `app.psgi` file, the Dancer default `bin/app.pl` also does the job. – Sebastian May 08 '16 at 11:08
  • You can also stealborrow the [idea from Plack::Middleware::RequestId](https://metacpan.org/source/BAYASHI/Plack-Middleware-RequestId-0.02/lib/Plack/Middleware/RequestId.pm#L27) and go with that. But as that is just a UUID that seems to use a random string, I would not rely on it to be honest. Furthermore, in case you are running your Dancer on CGI (you said not via Plack), all those stateful approaches are not going to work anyway. – simbabque May 08 '16 at 11:11
  • 2
    Plack will be used in production, but the Dancer webserver is good for development. `Data::UUID` actually claims: *A UUID is 128 bits long, and is guaranteed to be different from all other UUIDs/GUIDs generated until 3400 CE.* I added a *before* trigger. `var request_id => encode_base64url(Data::UUID->new->create_bin);` – Sebastian May 08 '16 at 11:14
  • 2
    Write it up as an answer. – simbabque May 08 '16 at 11:17

1 Answers1

1

When I have needed unique IDs for use with Mojolicious, I've used Data::UUID which generates long (128bit) numbers in line with RFC 4122

I can't be any more specific without a clearer idea of your use case, but this seems to work nicely:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::UUID;

my $gen = Data::UUID -> new();

my $binary_uuid = $gen -> create ;

print $gen -> to_string ( $binary_uuid ),"\n";
print $gen -> to_hexstring ( $binary_uuid ),"\n";
print $gen -> to_b64string ( $binary_uuid ),"\n";

You have a choice of output formats. You can, if it's useful to your application, create directly, e.g.:

my $gen = Data::UUID -> new();
my $uuid = $gen -> create_str ;
print $uuid, "\n";
#reformat output
print $gen -> to_hexstring ( $uuid ),"\n";
Sobrique
  • 52,974
  • 7
  • 60
  • 101