AWS-XRay

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

Generate a Trace ID. (e.g. "1-581cf771-a006649127e371903a2de979")

[Document](https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids)

## capture($name, $code)

capture() executes $code->($segment) and send the segment document to X-Ray daemon.

$segment is a AWS::XRay::Segment object.

When $AWS::XRay::TRACE\_ID is not set, generates TRACE\_ID automatically.

When capture() called from other capture(), $segment is a sub segment document.

See also [AWS X-Ray Segment Documents](https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html).

## capture\_from($header, $name, $code)

capture\_from() parses the trace header and capture the $code with sub segment of header's segment.

## parse\_trace\_header($header)

README.md  view on Meta::CPAN

When $mode is 1 (default), segment data will be sent to xray daemon immediately after capture() called.

When $mode is 0, segment data are buffered in memory. You should call AWS::XRay->sock->flush() to send the buffered segment data or call AWS::XRay->sock->close() to discard the buffer.

## AWS\_XRAY\_DAEMON\_ADDRESS environment variable

Set the host and port of the X-Ray daemon. Default 127.0.0.1:2000

## $AWS::XRay::CROAK\_INVALID\_NAME

When set to 1 (default 0), capture() will raise exception if a segment name is invalid.

See https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html

> name – The logical name of the service that handled the request, up to 200 characters.
> For example, your application's name or domain name.
> Names can contain Unicode letters, numbers, and whitespace, and the following symbols: \_, ., :, /, %, &, #, =, +, \\, -, @

# LICENSE

Copyright (C) FUJIWARA Shunichiro.

lib/AWS/XRay.pm  view on Meta::CPAN

Generate a Trace ID. (e.g. "1-581cf771-a006649127e371903a2de979")

L<Document|https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids>

=head2 capture($name, $code)

capture() executes $code->($segment) and send the segment document to X-Ray daemon.

$segment is a AWS::XRay::Segment object.

When $AWS::XRay::TRACE_ID is not set, generates TRACE_ID automatically.

When capture() called from other capture(), $segment is a sub segment document.

See also L<AWS X-Ray Segment Documents|https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html>.

=head2 capture_from($header, $name, $code)

capture_from() parses the trace header and capture the $code with sub segment of header's segment.

=head2 parse_trace_header($header)

lib/AWS/XRay.pm  view on Meta::CPAN

When $mode is 1 (default), segment data will be sent to xray daemon immediately after capture() called.

When $mode is 0, segment data are buffered in memory. You should call AWS::XRay->sock->flush() to send the buffered segment data or call AWS::XRay->sock->close() to discard the buffer.

=head2 AWS_XRAY_DAEMON_ADDRESS environment variable

Set the host and port of the X-Ray daemon. Default 127.0.0.1:2000

=head2 $AWS::XRay::CROAK_INVALID_NAME

When set to 1 (default 0), capture() will raise exception if a segment name is invalid.

See https://docs.aws.amazon.com/xray/latest/devguide/xray-api-segmentdocuments.html

=over

name – The logical name of the service that handled the request, up to 200 characters.
For example, your application's name or domain name.
Names can contain Unicode letters, numbers, and whitespace, and the following symbols: _, ., :, /, %, &, #, =, +, \, -, @

=back

t/01_trace.t  view on Meta::CPAN

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../";

use AWS::XRay qw/ capture /;
use Test::More;
use Time::HiRes qw/ sleep /;
use t::Util qw/ reset segments /;

capture "myApp", sub {
    my $seg = shift;
    sleep 0.1;
    capture "remote1", sub { sleep 0.1 };
    capture "remote2", sub {
        sleep 0.1;
        capture "remote3", sub { sleep 0.1 };
    };
    $seg->{annotations}->{foo} = "bar";

t/02_from.t  view on Meta::CPAN

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../";

use AWS::XRay qw/ capture capture_from /;
use Test::More;
use t::Util qw/ reset segments /;

my $header = capture "from", sub {
    my $segment = shift;
    return $segment->trace_header;
};
diag $header;

capture_from $header, "to", sub {
};

t/03_miss_from.t  view on Meta::CPAN

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../";

use AWS::XRay qw/ capture capture_from /;
use Test::More;
use t::Util qw/ reset segments /;

my $header;
capture_from $header, "first", sub {
};

my @seg = segments;
ok @seg == 1;

my $root = shift @seg;
is $root->{name}, "first";

t/04_sampling.t  view on Meta::CPAN

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../";

use AWS::XRay qw/ capture capture_from /;
use Test::More;
use t::Util qw/ reset segments /;

srand(1); # fix seed

subtest "disable", sub {
    reset();
    AWS::XRay->sampling_rate(0);
    capture "root", sub {
        capture "sub $_", sub { }
            for (1 .. 100);
    };
    my @seg = segments();
    ok scalar(@seg) == 0;
};

subtest "enable", sub {
    reset();
    AWS::XRay->sampling_rate(1);
    capture "root", sub {
        capture "sub $_", sub { }
            for (1 .. 100);
    };
    my @seg = segments();
    ok scalar(@seg) == 101;
};

subtest "50%", sub {
    reset();
    AWS::XRay->sampling_rate(0.5);
    for (1 .. 1000) {
        capture "root $_", sub { };
    }
    my @seg = segments();
    ok scalar(@seg) > 400;
    ok scalar(@seg) <= 600;
};

subtest "50% sub", sub {
    for (1 .. 10) {
        reset();
        AWS::XRay->sampling_rate(0.5);
        capture "root", sub {
            capture "sub $_", sub { }
                for (1 .. 100);
        };
        my @seg = segments();
        ok scalar(@seg) == 101 || scalar(@seg) == 0;
        diag @seg if @seg < 100 && @seg > 1;
    }
};

t/05_sampled_header.t  view on Meta::CPAN

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../";

use AWS::XRay qw/ capture capture_from /;
use Test::More;
use t::Util qw/ reset segments /;

AWS::XRay->sampling_rate(0);

my $trace_id   = AWS::XRay::new_trace_id;
my $segment_id = AWS::XRay::new_id;

my $header1 = "Root=$trace_id;Parent=$segment_id;Sampled=1";
diag $header1;
my $header2 = capture_from $header1, "from", sub {
    my $segment = shift;

t/06_sampler.t  view on Meta::CPAN

use 5.12.0;
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../";

use AWS::XRay qw/ capture capture_from /;
use Test::More;
use t::Util qw/ reset segments /;

subtest "disable", sub {
    reset();
    AWS::XRay->sampler(sub { 0 });
    capture "root", sub {
        capture "sub $_", sub { }
            for (1 .. 100);
    };
    my @seg = segments();
    ok scalar(@seg) == 0;
};

subtest "enable", sub {
    reset();
    AWS::XRay->sampler(sub { 1 });
    capture "root", sub {
        capture "sub $_", sub { }
            for (1 .. 100);
    };
    my @seg = segments();
    ok scalar(@seg) == 101;
};

subtest "odd", sub {
    reset();
    AWS::XRay->sampler(sub { state $count = 0; $count++ % 2 == 0 });
    for (1 .. 1000) {
        capture "root $_", sub { };
    }
    my @seg = segments();
    ok scalar(@seg) == 500;
};

subtest "odd_from", sub {
    reset();
    AWS::XRay->sampler(sub { state $count = 0; $count++ % 2 == 0 });
    for (1 .. 1000) {
        capture_from "", "root $_", sub { };
    }
    my @seg = segments();
    ok scalar(@seg) == 500;
};

done_testing;

t/08_add.t  view on Meta::CPAN

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../";

use AWS::XRay qw/ capture /;
use Test::More;
use Time::HiRes qw/ sleep /;
use t::Util qw/ reset segments /;

sub myApp {
    sleep 0.1;
    capture "remote1", sub { sleep 0.1 };
    capture "remote2", sub {
        sleep 0.1;
        capture "remote3", sub { sleep 0.1 };
    };
}

t/10_plugin_ec2_v1.t  view on Meta::CPAN

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../";

use Test::TCP;
use HTTP::Server::PSGI;
use AWS::XRay qw/ capture /;
use Test::More;
use t::Util qw/ reset segments /;

# mock server of IMDSv1
my $app_server = Test::TCP->new(
    listen => 1,
    code   => sub {
        my $sock   = shift;
        my $server = HTTP::Server::PSGI->new(
            listen_sock => $sock,
        );
        $server->run(

t/10_plugin_ec2_v2.t  view on Meta::CPAN

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../";

use Test::TCP;
use HTTP::Server::PSGI;
use AWS::XRay qw/ capture /;
use Test::More;
use t::Util qw/ reset segments /;

# mock server of IMDSv2
my $token      = "very-secret";
my $app_server = Test::TCP->new(
    listen => 1,
    code   => sub {
        my $sock   = shift;
        my $server = HTTP::Server::PSGI->new(
            listen_sock => $sock,
        );

t/Util.pm  view on Meta::CPAN

package t::Util;

use strict;
use warnings;

use IO::Scalar;
use JSON::XS;
use Exporter 'import';
our @EXPORT_OK = qw/ reset segments /;

my $buf;
no warnings 'redefine';

*AWS::XRay::sock = sub {
    IO::Scalar->new(\$buf);
};
1;

sub reset {
    undef $buf;
}

sub segments {
    return unless $buf;
    $buf =~ s/{"format":"json","version":1}//g;
    my @seg = split /\n/, $buf;
    shift @seg; # despose first ""
    return map { decode_json($_) } @seg;
}



( run in 0.797 second using v1.01-cache-2.11-cpan-49f99fa48dc )