Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Trace on captureMessage #859

Closed
nadar opened this issue Jul 29, 2019 · 12 comments
Closed

Add Trace on captureMessage #859

nadar opened this issue Jul 29, 2019 · 12 comments

Comments

@nadar
Copy link

nadar commented Jul 29, 2019

Hey there, as i could not find anything in the code, nor in the docs and no community resources are provided (slack, gitter), i try to ask my question here (as an issue, which is actually never a good place for questions):

I have a situation where an error module acts as proxy in order to gather error informations from several sites at one place. So i have a json with all the php exception informations including trace.

As i have found out that i can send the error message like this:

Sentry\init(['dsn' => $dsn]);
Sentry\captureMessage("This is my awesome error message."));

So far so good. But what really makes the error message meaning full are all the other information like trace. How can i send trace information? How does the error handler internally send the trace information? Is it a "Breadcrumb" object i can attache to the Scope object? Is there any easy to read and understand example in how the internal error handler converts the exception and sends it to the sentry API?

All the information about "capture message" is could find is this one: https://docs.sentry.io/enriching-error-data/context/?platform=php

(also i could not find an api endpoint in the sentry docs i could use)

@Jean85
Copy link
Collaborator

Jean85 commented Jul 30, 2019

What you're searching for is the attach_stacktrace option. You can read about it here: https://docs.sentry.io/error-reporting/configuration/?platform=php#attach-stacktrace

It's the doc of the unified API.

@nadar
Copy link
Author

nadar commented Jul 30, 2019

Thanks for the hint @Jean85 , i will take a deeper look into this. As far as i can see from the docs you provided, this allows me to turn it off, but does not explain how to provided stack trace informations? Maybe you could show me the part in the php sdk where the stack trace is provided?

Thank you very much for the help! Here you could see how i currently try to send the proper exception: https://github.com/luyadev/luya-module-errorapi/blob/sentry-adapter/src/adapters/SentryAdapter.php

@Jean85
Copy link
Collaborator

Jean85 commented Jul 30, 2019

Nope, that option is false by default, you should turn it on with true.

@nadar
Copy link
Author

nadar commented Jul 30, 2019

And how can i then provide the trace informations? (thanks for your help!)

@stayallive
Copy link
Collaborator

stayallive commented Jul 30, 2019

When enabled, stack traces are automatically attached to all messages logged. 
Note that stack traces are always attached to exceptions but when this is set stack traces are also sent with messages. 
This, for instance, means that stack traces appear next to all log messages.

As mentioned in the documentation when setting attach_stacktrace to true the trace is attached automatically to all logged messages. So you won't have to do anything to provide the trace information, the SDK handles it for you! 😄

Or is it something specific you would like to attach that we're not understanding?

@nadar
Copy link
Author

nadar commented Jul 30, 2019

Hey @stayallive thanks for helping me on this. Yes, i want to send the trace informations by myself because the error api module we have made (https://github.com/luyadev/luya-module-errorapi) already recieves the error informations from the different websites, including stack trace, ip, etc. - so now we don't want to use the PHP SDK's strack trace, as we don't have exceptions.

So basically our MODULE recives the json including the data (this one for example => https://github.com/luyadev/luya-module-errorapi/blob/sentry-adapter/tests/adapters/SentryAdapterTest.php#L25-L82) And now we want to pass this informations to senty - including stack trace, etc.Therfore we use Sentry\captureMessage() but we don't know how to attache trace informations ;-)

@nadar
Copy link
Author

nadar commented Jul 30, 2019

If i inspect the json payload from a sentry error message i can see there is a json key backtrace with a key frames - but i could not find:

  • an option to define (override?) this in the php sdk
  • an api endpoint description to create "new issue" / "new report" / "new event" => https://docs.sentry.io/api/events/

So basically what i am asking my self, how does the php sdk create the event? :D What payload to which endpoint?

Update:

So i was trying to use the captureEvent with strack trace payload, but this won't work as well:

Sentry\captureEvent([
            "message" => 'NEW ERROR: ' . time(),
            "stacktrace" => [
                "frames" => [
                    [
                        "function" => 'fn()',
                        "abs_path" => 'path/',
                        "pre_context" => [],
                        "post_context" => [],
                        "filename" => 'cih.php',
                        "lineno" => 12,
                        "in_app" => true,
                        "context_line" => 'path/cli()',
                    ]
                ]
            ]
        ]);

@Jean85
Copy link
Collaborator

Jean85 commented Jul 30, 2019

You should take a peek into our source code starting from here: https://github.com/getsentry/sentry-php/blob/master/src/EventFactory.php#L67-L71

@nadar
Copy link
Author

nadar commented Jul 30, 2019

I guess what i was looking for was this: https://docs.sentry.io/development/sdk-dev/overview/

Because i have to do everything by myself i assume.

Thank you @Jean85 @stayallive for pointing me into the right direction!

For anyone else maybe googling into this issue:

This is what my example payload array lookes like:

$examplePayLoad = [
            'transaction' => $data->getRequestUri(),
            'server_name' => $data->getServerName(),
            'fingerprint' => [
                $data->getRequestUri(),
                $data->getServerName(),
            ],
            'logger' => 'luya.errorapi',
            'platform' => 'php',
            'sdk' => [
                'name' => 'luya-errorapi',
                'version' => '1.0.0',
            ],
            'environment' => 'prod',
            'level' => 'error',
            'tags' => [
                'luya_version' => '1.0',
            ],
            'exception' => [
                'values' => [
                    [
                        'type' => 'Exception',
                        'value' => $data->getErrorMessage(),
                        'stacktrace' => [
                            'frames' => [
                                 'filename' => $trace->file,
                                 'function' => $trace->function,
                                 'lineno' => $trace->line,
                                 'module' => $trace->class,
                           ]
                        ]
                    ]
                ]
            ]
];

@nadar nadar closed this as completed Jul 30, 2019
@Jean85
Copy link
Collaborator

Jean85 commented Jul 30, 2019

@nadar you don't normally need to do it all by yourself, the SDK should be used to do that. Point is that you have a strange setup, where a separate piece of software of your own is acting as a middleman between the project having errors and Sentry itself. If you install Sentry directly in your project, you can cut out the middleman and use the automatic stacktrace detection.

@Jean85 Jean85 reopened this Jul 30, 2019
@Jean85 Jean85 closed this as completed Jul 30, 2019
@nadar
Copy link
Author

nadar commented Jul 30, 2019

Yes @Jean85, this is exactly the point, i thought the PHP SDK could be configured to take those certain data - but it does not, i understand that.

I would not call it a "strange" setup, as the solution allows use the integrate error handling without even define a DNS for every project. Configure and Setup hundreds of Websites would be a pain this way 😄

Thanks anyhow for helping me! And maybe i could solve this questions for others.

@ste93cry
Copy link
Collaborator

Even though it's a bit more verbose, you can actually do it with the SDK by creating an event processor. Those will be executed when the scope's information are added to the event and before the event itself is sent, so it's the perfect place to modify its data. A rough example of the code could be:

Scope::addGlobalEventProcessor(static function (Event $event, array $payload): Event {
    if (isset($payload['exceptions'])) {
        $event->setExceptions($payload['exceptions']);
    }

    return $event;
});

captureEvent([
    'exceptions' => [
        ...,
    ],
]);

However, I don't really see the point in using the SDK if you just want to collect data from a third party service and proxy it to Sentry as it could be done more easily by just using an HTTP client, cURL or whatever else you prefer and send the raw data. This of course has the drawbacks that you have to implement the authentication handling yourself and build the entire payload to send

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants