patrickkerrigan / php-xray

A PHP instrumentation library for AWS X-Ray
BSD 3-Clause "New" or "Revised" License
63 stars 26 forks source link

Unable to view trace results in AWS X ray #13

Closed wtfoong closed 1 year ago

wtfoong commented 1 year ago

hello, i am currently working on a php website and would like to use AWS x-ray to for insights, but after implementing and following the guide i am still unable to get any data from AWS x ray.

The code i use for testing is from one of the previous issues and was working in that issue. image

and the result at AWS x ray image

would appreciate your help very much....tqvm....XD

patrickkerrigan commented 1 year ago

Hi,

Looking at the code you've got so far I think I can see a couple of issues that might be preventing the traces showing up properly in x-ray.

Firstly, I'd recommend using the example in one of the later comments in the issue I think you're referring to: https://github.com/patrickkerrigan/php-xray/issues/11#issuecomment-953736831. This should have everything that's needed for x-ray to reliably link the two requests together. If the API you're calling doesn't have its own tracing, then you should remove ->setTraced(true) otherwise x-ray will wait for the API to submit its own trace before showing in the dashboard. That being said, the example you used should result in something being recorded, so I suspect something else must be off.

The next thing I noticed is that $xrayHeader is undefined in the code you posted, whereas in the issue comment it's set correctly to "X-Amzn-Trace-Id".

Finally, and this may be a stupid question, but do you have the AWS x-ray daemon installed and running? By default this library will expect to find the daemon listening on 127.0.0.1:2000, but if you're running it on a remote host or another port then you can pass a hostname/IP address and port to the constructor of DaemonSegmentSubmitter.

If you still have trouble after the above is sorted, then it might be worth testing with the most simple case first to verify that communication with the x-ray daemon and onwards to AWS is working correctly before moving onto cross-service tracing. Something such as this should be the minimum you need to produce a trace and test that out:

use Pkerrigan\Xray\Trace;
use Pkerrigan\Xray\Submission\DaemonSegmentSubmitter;

Trace::getInstance()
    ->setTraceHeader($_SERVER['HTTP_X_AMZN_TRACE_ID'] ?? null)
    ->setName('app.example.com')
    ->setUrl($_SERVER['REQUEST_URI'])
    ->setMethod($_SERVER['REQUEST_METHOD'])
    ->begin(); 

sleep(1); // give x-ray something to actually measure

Trace::getInstance()
    ->end()
    ->setResponseCode(http_response_code())
    ->submit(new DaemonSegmentSubmitter());

Hopefully that helps, but if you don't have any luck then let me know and I'll see if I can put together some further troubleshooting steps.

wtfoong commented 1 year ago

Thank you for your reply, After implementing your simple case i manage to get this response from the xray daemon of my EC2 instance. from what i understand if it runs on EC2, it should be able to get the credentials automatically, but is shows this error.

image

could it be because i am using a learner account?

this is the code that i am currently using.

<?php
require(__DIR__ . '/vendor/autoload.php');
use Pkerrigan\Xray\Trace;
use Pkerrigan\Xray\Submission\DaemonSegmentSubmitter;

Trace::getInstance()
    ->setTraceHeader($_SERVER['HTTP_X_AMZN_TRACE_ID'] ?? null)
    ->setName('app.example.com')
    ->setUrl($_SERVER['REQUEST_URI'])
    ->setMethod($_SERVER['REQUEST_METHOD'])
    ->begin(); 

sleep(1); // give x-ray something to actually measure

Trace::getInstance()
    ->end()
    ->setResponseCode(http_response_code())
    ->submit(new DaemonSegmentSubmitter());
?>
<h1>test</h1>
patrickkerrigan commented 1 year ago

It looks from that log snippet like your code is submitting traces to the daemon now, so once the credentials issue is fixed you should be good.

I'm not familiar with learner accounts and if there are any differences from a full account, but I would assume they should support enough of the functionality for this to work.

from what i understand if it runs on EC2, it should be able to get the credentials automatically

This is correct, at least on a full account, but only if you've granted permission to your EC2 instance to access x-ray. You do this by creating and assigning an IAM role to your EC2 instance with the AWSXRayDaemonWriteAccess permission:

image

If your instance has no IAM role, then it has no credentials to get, and you'd receive the error in your screenshot.

If you already have an IAM role assigned, then you'll need to modify it to include the AWSXRayDaemonWriteAccess permission.

You may need to restart the x-ray daemon after making any changes to the IAM role if it doesn't pick them up automatically.

wtfoong commented 1 year ago

I manage to brute force through it by adding a .aws folder with a credentials file in it to my root user.....pretty sure this is not the correct way, but i manage to get the xray to detect my traces.

image i currently am able to trace my db call and webpage.

i would like to ask would it be possible to trace my AWS service calls? like the time needed to upload an image to S3, cause from what i see, all the traces focus on the web site response time.

Another question will be is it possible to bring traces across webpages? for example i start the trace at index.php and ends it at another.php, cause the last time i tried it did not work, no traces are sent.

tqvm

patrickkerrigan commented 1 year ago

would it be possible to trace my AWS service calls?

This library doesn't currently integrate directly with the AWS SDK, so there's no automatic tracing of your AWS calls, but it should be possible to trace those manually in a similar way to your DB queries, but using a RemoteSegment:

Trace::getInstance()
    ->getCurrentSegment()
    ->addSubsegment(
        (new RemoteSegment())
            ->setName('S3')
            ->begin()    
    );

// Upload image to S3 here

Trace::getInstance()
    ->getCurrentSegment()
    ->end();

is it possible to bring traces across webpages?

Not that I'm aware of. X-ray is more about getting visibility into the performance of individual requests (e.g which DB queries or API calls are slowing down a given page) than following a user as they navigate different pages. You need to end and submit the trace in the same HTTP request that it was started, or as you discovered, it won't be sent.

The x-ray console does allow you to search for traces by properties of the trace. You can use this to, for example, view all traces from a particular IP address, or if you have registered users, you can call setUser on the trace and then search in the console by username/ID. This would give you a rough view of how someone is navigating between pages, but you're probably better off using something like Google Analytics if you need more than that.

wtfoong commented 1 year ago

ok, tqvm for your help, really appreciate it...XD