AWS-XRay

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

    - Fix typo (#5 @moznion, #9 @shogo82148)
    - Fix wantarray in capture (#7 @shogo82148)

0.08 2018-10-26T13:56:35Z
    - Segment name created by add_capture becomes to package name.

0.07 2018-10-26T08:32:01Z
    - Add add_capture method.

0.06 2018-07-05T06:58:37Z
    - Fix segment data lost in 0.05 and auto_flush(0).

0.05 2018-07-05T01:50:46Z
    - Add auto_flush()

0.04 2018-06-22T15:05:03Z
    - Add sampler()

0.03 2018-06-19T16:34:09Z
    - Add sampling_rate()

README.md  view on Meta::CPAN

    };
    capture_from $header, "dest", sub {
        my $segment = shift;  # is a child of "source" segment
        # ...
    };

# DESCRIPTION

AWS::XRay is a tracing library with AWS X-Ray.

AWS::XRay sends segment data to [AWS X-Ray Daemon](https://docs.aws.amazon.com/xray/latest/devguide/xray-daemon.html).

# FUNCTIONS

## new\_trace\_id

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.

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

    no warnings 'redefine';
    no strict 'refs';
    for my $method (@methods) {
        my $orig = $package->can($method) or next;
        *{"${package}::${method}"} = sub {
            my @args = @_;
            capture(
                $package,
                sub {
                    my $segment = shift;
                    $segment->{metadata}->{method}  = $method;
                    $segment->{metadata}->{package} = $package;
                    $orig->(@args);
                },
            );
        };
    }
}

if ($ENV{LAMBDA_TASK_ROOT}) {
    # AWS::XRay is loaded in AWS Lambda worker.
    # notify the Lambda Runtime that initialization is complete.

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

package AWS::XRay::Plugin::EC2;
use strict;
use warnings;

use HTTP::Tiny;

# for test
our $_base_url = "http://169.254.169.254/latest";

sub ID_ADDR() {
    return "$_base_url/meta-data/instance-id";
}

sub AZ_ADDR() {
    return "$_base_url/meta-data/placement/availability-zone";
}

our $METADATA;

sub apply_plugin {
    my ($class, $segment) = @_;

    $METADATA ||= do {
        my $ua = HTTP::Tiny->new(timeout => 1);

        # get token for IMDSv2
        my $token = do {
            my $res = $ua->request(
                "PUT",
                "$_base_url/api/token", {
                    headers => {
                        'X-aws-ec2-metadata-token-ttl-seconds' => '60',
                    },
                }
            );
            $res->{success} ? $res->{content} : '';
        };
        my $opt = {};
        if ($token ne '') {
            $opt->{headers}->{'X-aws-ec2-metadata-token'} = $token;
        }

        my $instance_id = do {
            my $res = $ua->get(ID_ADDR, $opt);
            $res->{success} ? $res->{content} : '';
        };
        my $az = do {
            my $res = $ua->get(AZ_ADDR, $opt);
            $res->{success} ? $res->{content} : '';
        };

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


AWS::XRay->add_capture("main", "myApp");

myApp();

my @seg = segments();
ok @seg == 4;

my $root = pop @seg;
is $root->{name}, "main";
is $root->{metadata}->{method},  "myApp";
is $root->{metadata}->{package}, "main";
like $root->{trace_id} => qr/\A1-[0-9a-fA-F]{8}-[0-9a-fA-F]{24}\z/, "trace_id format";
like $root->{id}       => qr/\A[0-9a-fA-F]{16}\z/;
is $root->{type}, undef;
ok $root->{start_time} < $root->{end_time};

my $trace_id = $root->{trace_id};
my $root_id  = $root->{id};

# remote1
my $seg1 = shift @seg;

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

        my $server = HTTP::Server::PSGI->new(
            listen_sock => $sock,
        );
        $server->run(
            sub {
                my $env = shift;
                if ($env->{REQUEST_METHOD} ne 'GET') {
                    return [405, [], ['Method Not Allowed']];
                }
                my $path = $env->{PATH_INFO};
                if ($path eq '/meta-data/instance-id') {
                    return [200, [], ['i-1234567890abcdef0']];
                }
                if ($path eq '/meta-data/placement/availability-zone') {
                    return [200, [], ['ap-northeast-1a']];
                }
                return [404, [], ['Not Found']];
            }
        );
    },
    max_wait => 10, # seconds
);
use AWS::XRay::Plugin::EC2;
$AWS::XRay::Plugin::EC2::_base_url = "http://127.0.0.1:" . $app_server->port;

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

                    }
                    if ($path ne '/api/token') {
                        return [404, [], ['Not Found']];
                    }
                    return [200, [], [$token]];
                }
                if ($method eq 'GET') {
                    if (($env->{HTTP_X_AWS_EC2_METADATA_TOKEN} || '') ne $token) {
                        return [401, [], ['Unauthorized']];
                    }
                    if ($path eq '/meta-data/instance-id') {
                        return [200, [], ['i-1234567890abcdef0']];
                    }
                    if ($path eq '/meta-data/placement/availability-zone') {
                        return [200, [], ['ap-northeast-1a']];
                    }
                    return [404, [], ['Not Found']];
                }
                return [405, [], ['Method Not Allowed']];
            }
        );
    },
    max_wait => 10, # seconds
);

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.705 second using v1.00-cache-2.02-grep-82fe00e-cpan-4673cadbf75 )