Code Monkey home page Code Monkey logo

elastic-apm-php-agent's Introduction

Elastic APM: PHP Agent

Total Downloads


⚠️ WARNING: This project is no longer maintained! The official Elastic PHP APM agent has superseeded this project.


Contributors

A big, big thank you goes out to every contributor of this repo, special thanks goes out to:

elastic-apm-php-agent's People

Contributors

alash3al avatar arkaitzgarro avatar arron0 avatar dstepe avatar eddycjy avatar flexer23 avatar freezewarp avatar georgeboot avatar gueroverde avatar iainmckay avatar jblotus avatar jcalonso avatar kankje avatar l00cyph3r avatar matthieutirelli avatar mattriddell avatar nishimura avatar philkra avatar ricco24 avatar strayobject avatar tepeds avatar thinkspill avatar tvbeek avatar xon avatar yuzhenxie avatar zobo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

elastic-apm-php-agent's Issues

How to handle kafka messages?

Hi, what is th best practice to handle traces of Kafka, lets say we have some producer who is producing messages and probably sending some trace-id in header. My application is producer and I want to know this information and use it in other calls I need to create... Do we have some example for that?

for Kafka messages is used rdkafka library

Can't Get Agent to Work

Hey,

So im new to this APM overall, so im starting with a small php file to test things out but even that is not working, here is what i have:

index.php file:

<?php
 
$config = [
    'appName'     => 'APM Test Server',
    'appVersion'  => '1.0',
    'serverUrl'   => 'http://serverip:8200',
    'hostname'    => 'APM Server',
    'env'         => ['DOCUMENT_ROOT'],
];
$agent = new PhilKra\Agent($config);

require 'vendor/autoload.php';
echo "hello";
sleep(5);
echo "goodbye";
?>

and this is the error that i get whenever i try to run the php script:

PHP Fatal error: Uncaught Error: Class 'PhilKra\Agent' not found in /home/ubuntu/test/index.php:10 Stack trace: #0 {main} thrown in /home/ubuntu/test/index.php on line 10

this is how my directory looks like:

PhilKra/ composer.json composer.lock elastic-apm-php-agent/ index.php vendor/

what am i missing here?

Any help or guidance is greatly appreciated.

Cheers!

Agent setup

Hello everyone, I already installed the package via composer, but I have some doubts:

1 - Where I get "appName" within APM?
2 - Where do I set the url to point to my APM server?

Symfony support

Does this agent has Symfony integration in its roadmap?

I'm quite new to Elastic APM, but I saw on online docs for Node.JS and Python agents, that they have out-of-the-box support for most common frameworks, and this mean that it automatically instruments the app automatically.

From what I saw, this PHP agent does not have any automatic framework instrumentation, yet. Right?

413 Request Entity Too Large

We are experiencing an issue when reporting to APM from our laravel app.

POST https://APP_ID.apm.eu-central-1.aws.cloud.es.io/v1/transactions` resulted in a `413 Request Entity Too Large`

I see there's an config option for limiting the backtraceDepth, but can I be sure that limiting this is sufficient?

philkra/elastic-apm-laravel#36

/cc @Froelund

HTTP_HOST error on php-cli

When using php cli ( for instance, while running tests ) we may have the ErrorHandler loaded.
It then makes an ErrorException because php-cli does not have any HTTP_HOST defined.

ErrorException  : Undefined index: HTTP_HOST
at /.../vendor/philkra/elastic-apm-php-agent/src/Events/EventBean.php:213
    209|                     'hostname' => $_SERVER['SERVER_NAME'] ?? '',
    210|                     'port'     => $_SERVER['SERVER_PORT'] ?? '',
    211|                     'pathname' => $_SERVER['SCRIPT_NAME'] ?? '',
    212|                     'search'   => '?' . (($_SERVER['QUERY_STRING'] ?? '') ?? ''),
  > 213|                     'full' => $http_or_https . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
    214|                 ],
    215|                 'headers' => [
    216|                     'user-agent' => $headers['User-Agent'] ?? '',
    217|                     'cookie'     => $headers['Cookie'] ?? ''

Possible to convert debug_backtrace() into span stack trace

Hi,

Quick question. I see the example which creates a stack trace manually for a span.

Is there any way to convert a stack trace created by debug_backtrace into a format that can be used in a span?

I've had a bit of a search around and there doesn't seem to be any questions or answers along this line.

If I do:

$spanSt->setStackTrace(debug_backtrace());

it causes:

[Sat Nov 09 19:19:07.447727 2019] [php7:error] [pid 24024] [client 127.0.0.1:52194] PHP Fatal error: Uncaught GuzzleHttp\\Exception\\ClientException: Client error: POST http://apm:8200/intake/v2/events` resulted in a 400 Bad Request response:\n{\n "accepted": 5,\n "errors": [\n {\n "message": "error validating JSON document against schema: I[#] S[#] doesn' (truncated...)\n in /Users/matt/Sites/x/private/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113\nStack trace:\n#0 /Users/matt/Sites/x/private/vendor/guzzlehttp/guzzle/src/Middleware.php(65): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response))\n#1 /Users/matt/Sites/x/private/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))\n#2 /Users/matt/Sites/x/private/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array)\n#3 /Users/matt/Sites/x/pri in /Users/matt/Sites/x/private/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113, referer: http://pv.com/admin/system_reports/unfulfillable.php`

Span context

Hello

Just wondering why the PhilKra\Events\Span object uses context, (https://github.com/philkra/elastic-apm-php-agent/blob/master/src/Events/Span.php#L46) where as the base PhilKra\Events\EventBean uses contexts (https://github.com/philkra/elastic-apm-php-agent/blob/master/src/Events/EventBean.php#L67)

This means that if I try and set some tags on my span, eg $span->setTags(['foo' => 'bar']); they do not actually get sent to the Elastic APM agent, as when the Span is serialized it uses context, rather than $this->getContext().
https://github.com/philkra/elastic-apm-php-agent/blob/master/src/Events/Span.php#L159
https://github.com/philkra/elastic-apm-php-agent/blob/master/src/Events/Transaction.php#L148

Not sure if this is a bug, or deliberate?

Perhaps if the Span object is not meant to set tags, then setTags should be moved to a different object / trait?

When you open a span in Kibana, it seems to support tags?
image

Class 'PhilKra\Middleware\Agent' not found

In /philkra/elastic-apm-php-agent/src/Middleware/Connector.php:

Message: Class 'PhilKra\Middleware\Agent' not found
File: /var/www/api/vendor/philkra/elastic-apm-php-agent/src/Middleware/Connector.php
Line: 83

Changing the line to 'User-Agent' => sprintf( 'elasticapm-php/%s', \PhilKra\Agent::VERSION ), fixed it.

nested span

hi, does someone have working example of nested spans?

thanks

-ks-

How can I get the data of apm-services?【apm server7.4】

How can I get the data of apm-services?

environment:

kibana7.4
es7
apm server7.4
elastic-apm-php-agent 7.0.0-rc3


$config = [
'appName' => 'test',
'appVersion' => '1.0.0',
'serverUrl' => 'http://10.2.114.101:8200',
'secretToken' => '',
'hostname' => 'APM Server',
'env' => ['DOCUMENT_ROOT'],
];
try {
$agent = new \PhilKra\Agent($config);
echo "hello";
sleep(5);
echo "goodbye";
$info = $agent->info();
var_dump($info->getStatusCode());
var_dump($info->getBody());
} catch (\GuzzleHttp\Exception\ConnectException $e){
// disable the agent agent
$config['active'] = false;
$agent = new \PhilKra\Agent($config);
}
$transaction = $agent->startTransaction('APM Test');


Never get data
The result of the request result is 202
I don't know if it is a problem with apm server, or a problem with code, or a problem with Intake API. It is difficult to detect. I can't see the problem when I read the source code.

I think the documentation is not comprehensive enough, only at the code level.


supplement

Versions v1 and v2 are confusing

Elastic APM > 7.0 support

I can't use my APM server version 7.0.

I am getting this error:

[2019-05-08 18:17:18] local.ERROR: GuzzleHttp\Exception\ClientException: Client error: POST https://myserver.apm.us-east-1.aws.cloud.es.io/v1/transactions resulted in a 404 Not Found response: 404 page not found

According to this doc this endpoint was deprecated: https://www.elastic.co/guide/en/apm/server/7.0/transaction-endpoint.html

We need to change this endpoint request to event API: https://www.elastic.co/guide/en/apm/server/7.0/events-api.html

explanation how agent send to apm server

Hi,
as i read inside code seems that agent send data to apm with guzzle.
This are synchronous calls?
If APM Server goes down, php script that include agent become slowly?
Or data are sent maybe when register_shutdown_function is called to simulate an async call?

Feature: Redact / sanitize cookies

$context['request']['cookies'] = $_COOKIE;

Ran into issues where I have jwt tokens stored using cookies and that info is being sent off to the APM. While hunting around to see how other agents handled this I came across this pull request for the go agent. Also I had to make sure I wasn't sending HTTP_AUTHORIZATION and HTTP_COOKIE via 'env' configuration.

elastic/apm-agent-go@fad1cec

At the very least it would be cool to prevent sending cookies at all, based on user configuration. I do like the approach they took to use a user-configurable regex as well.

Thoughts?

APM v2 plan

Thank you for your efforts in this package, I want to ask you about your plans for APM v2 API, and if you need any help in the development so we can accelerate it as possible as we can, I know that there is a v2 branch, but I don't know what is the current state of it now.

404 on POST /v2/transactions

Hi all,

Im trying to make local tests for the apm server. I setup a elastic + kibana stack on docker and now i want to make my application start sending requests to the apm server.

For some reason, i am getting a 404 error when the POST is made to the /v2/transactions url.

The application itself is also running on a docker host. My config is:

$agent = new Agent( [ 'appName' => 'api-checkout', 'serverUrl' => 'http://192.168.122.1:8200', 'apmVersion' => 'v2' ] );

If i go inside the docker host and try to simulate the same POST that is made from the application:
curl -v -XPOST -H 'Content-Type: application/json' http://192.168.122.1:8200/intake/v2/events

i get the error:
{ "accepted": 0, "errors": [ { "message": "EOF while reading metadata" } ] }

But, inside the application the error is 404.
Does anyone know if i am doing something wrong?

APM-SERVER vesion: 7.4.2
Application PHP version: 7.1

The apm server and the elastic are OK! I can see the connection messages in kibana from the apm server and the apm-server test on the apm configuration in kibana is green.

Thank you

Add custom span detail

Hello ,

I need to monitor my web services calls.
It seems to me that we have to use spans? Can you confirm it?

And how can I do it if it is?

I use your libraries (elastic-apm-php-agent & elastic-apm-laravel)

Thank you !

Duration is 1000 times the value it should be

When stopping a transaction, you use a Timer to calculate the duration of the transaction. You have a toMicro function that does:

/**
     * Convert the Duration from Seconds to Micro-Seconds
     *
     * @param  float $num
     *
     * @return float
     */
    private function toMicro(float $num) : float
    {
        return $num * 1000000;
    }

This is 1000 to much and it makes the kibana apm think my transactions take 1000seconds.

Is this related to kibana updating something? Or intended?
I'm whiling to open a MR for this.

Send test data to APM Server

Hello everyone,

So i got the apm server up and running, but im just not sure how to send data to it. This is an test code that i have but i feel like im missing something but i dont know what it is:

<?php
require 'vendor/autoload.php';

$config = [
    'appName'     => 'APM Test Server',
    'appVersion'  => '1.0',
    'serverUrl'   => 'http://apm-server-ip:8200',
    'hostname'    => 'APM Test Server',
    'env'         => ['DOCUMENT_ROOT'],
];
$agent = new PhilKra\Agent($config);
$transaction = $agent->startTransaction('Simple Transaction');

echo "hello";
sleep(5);
echo "goodbye";
$agent->stopTransaction($transaction->getTransactionName());

i assume that after the transaction is stopped the agent should send the data to the apm server and then ES would index it, no?

if not how can i check that the data was actually sent?

Thanks in advance for any help!

Allow Timer::__construct to accept a start time

The Timer class only starts the timer when Timer::start() is explicitly called and uses the current microtime(). Allowing the caller to give a suitable start time in the constructor and start the timer will facilitate using the Timer class when the desired start time precedes the creation of the Timer object. For example, this would allow using the PHP provided REQUEST_TIME_FLOAT value.

The Transaction and Agent classes would also be updated to support this optional parameter.

I'm going to work on this in support of possible changes in the Laravel APM timing.

Transaction sampling rate

There doesn't seem to be a way to set transaction sampling rate like you can with the official agents (e.g., https://github.com/elastic/apm-agent-python/blob/master/docs/configuration.asciidoc#transaction_sample_rate). Something similar to this would be rather useful for high-volume services.

Are there any plans of adding support for this? The only way I've found to influence the agent currently is the active key in the configuration, but that would mean errors won't get submitted either which would be suboptimal.

Typo in

Hey, great start on this (I was actually going to roll my own, but you beat me to it). Found a typo in /philkra/elastic-apm-php-agent/src/Middleware/Connector.php

private function getRequestHeaders() : array {
// Default Headers Set
$headers = [
'Content-Type' => 'application/json',
'Content-Encoding' => 'deflate',
'User-Agent' => spritnf( 'elasticapm-php/%s', Agent::VERSION ),
];

// Add Secret Token to Header
if( $this->config->get( 'secretToken' ) !== null ) {
  $headers['Authorization'] = sprintf( 'Bearer %s', $this->config['secretToken'] ); 
}

return $headers;

}

Upgrade version of dependency ralouphie/getallheaders

Im trying to install this package using Laravel 5.8 and I get the following error:

philkra/elastic-apm-php-agent 6.5.3 requires ralouphie/getallheaders 2.0.5 -> satisfiable by ralouphie/getallheaders[2.0.5] but these conflict with your requirements or minimum-stability

Spans / Traces?

Hi! Thanks for starting this project!

I'm curious if you have plans to handle Spans / Traces?

Check for APM server status

Hello everyone.

First thanks for developing this it's really helpful.

Second I have an issue I need to fix and I can't get around it, the problem is that if the APM server fails, the whole application simply stops, and this is unbearable, I need to place a check on the server before hand so that _destruct is not sent and crashes the application with an exception.

Any help or tips for this is greatly appreciated.

I think this issue relates to what I need but I'm not capable of doing anything with it

(philkra/elastic-apm-laravel#9)

[version 6.6.1] using "Adding spans" Demo, something went wrong

Hello, I am currently using the 6.6.1 version. When trying “Adding spans” Demo, Kibana showed a blank and reported an error. Maybe I need your help.

image

Thank you!


Code:

        // create the agent
        $agent = new \PhilKra\Agent(['appName' => 'Demo with Spans']);

        // start a new transaction
        $transaction = $agent->startTransaction('GET /some/transaction/name');

        // create a span
        $spans = [];
        $spans[] = [
            'name' => 'Your Span Name. eg: ORM Query',
            'type' => 'db.mysql.query',
            'start' => 300, // when did tht query start, relative to the transaction start, in milliseconds
            'duration' => 23, // duration, in milliseconds
            'stacktrace' => [
                [
                    'function' => "\\YourOrMe\\Library\\Class::methodCall()",
                    'abs_path' => '/full/path/to/file.php',
                    'filename' => 'file.php',
                    'lineno' => 30,
                    'library_frame' => false, // indicated whether this code is 'owned' by an (external) library or not
                    'vars' => [
                        'arg1' => 'value',
                        'arg2' => 'value2',
                    ],
                    'pre_context' => [ // lines of code leading to the context line
                        '<?php',
                        '',
                        '// executing query below',
                    ],
                    'context_line' => '$result = mysql_query("select * from non_existing_table")', // source code of context line
                    'post_context' => [// lines of code after to the context line
                        '',
                        '$table = $fakeTableBuilder->buildWithResult($result);',
                        'return $table;',
                    ],
                ],
            ],
            'context' => [
                'db' => [
                    'instance' => 'my_database', // the database name
                    'statement' => 'select * from non_existing_table', // the query being executed
                    'type' => 'sql',
                    'user' => 'root', // the user executing the query (don't use root!)
                ],
            ],
        ];

        // add the array of spans to the transaction
        $transaction->setSpans($spans);

        // send our transactions to te apm
        $agent->send();

getEnv() must be an instance of PhilKra\Events\object error

I'm getting the following error when 'env' is an empty array.

NOTICE: PHP message: TypeError: Return value of PhilKra\Events\EventBean::getEnv() must be an instance of PhilKra\Events\object, instance of stdClass returned in /app/vendor/philkra/elastic-apm-php-agent/src/Events/EventBean.php on line 183

The same error happens for cookies as well.

Agent doesn't capture body

Problem is: generateRequest function doesn't collect payload information at all.

is there any possibility to log payload as well?
It is important to know, which data was sended to the PHP service.

Elastic Breakdown graph requiring aggregation metrics

It looks like Elastic has added a "Breakdown graph" which may require some additional metric data to be sent along with the transaction and spans. Without it the first graph on all the pages just sits there blank.

I have had difficulty figuring out what is going on, and have submitted a request for documentation on the matter: elastic/apm#195

If I am understanding it correctly, the Agent would need to go through all the spans and calculate some aggregate statistics. (# of spans, sum of all timers)

I am looking forward to elastic clarifying this process, but then it may be useful to add some of that functionality.

Thanks

[SUGGESTION] Allow cookies to be completely omitted

In addition to being able to specify an inclusive list of keys to send to ELK from $_COOKIE, it would be appreciated to implement a mechanism that excludes all cookies

Currently, as a workaround, I must do the following:

new \PhilKra\Agent([
...
	'cookies' => [''],
...

Due to the empty() state being used as an 'all available' option.

Why xdebug is required?

i understand that this library is not for production, but using xdebug functions is really making this work only locally

EDIT:
i saw that there is really only 1 use of it, which is "xdebug_get_headers"
i can make a PR to replace it with "headers_list" i think they are pretty much doing the same, then you can remove xdebug dependency

How to Configure APM Agent to work

Hey i asked a prev question but for some reason it was closed before i got an answer,

i just want to know how to configure the PHP APM Agent to send the data/stats to the APM Server. This is what i have for the config but its not working, but i cant see anything in my ELK Server (whichhas APM installed and enabled).

`<?php
require 'vendor/autoload.php';

$config = [
'appName' => 'APM Test Server',
'appVersion' => '1.0',
'serverUrl' => 'http://172.31.24.70:5066',
'hostname' => 'APM Test Server',
'env' => ['DOCUMENT_ROOT'],
];
$agent = new PhilKra\Agent($config);
echo "hello";
sleep(5);
echo "goodbye";
?>`

am i doing anything wrong here?

thank you

gzip: invalid header to APM server 7.3.1

I tried version 7.0.0-beta3 with APM server 7.3.1 but following error happened. Any idea ..?

ERROR   [request]       beater/intake_handler.go :94     error handling request  
{ 
   "request_id":"a25a2ee9-dc0a-4a91-8308-29d8cbdc8738",
   "method":"POST",
   "URL":"/intake/v2/events",
   "content_length":2680,
   "remote_address":"127.0.0.1",
   "user-agent":"elasticapm-php/7.0.0-beta3",
   "error":"gzip: invalid header",
   "response_code":400
}

setMeta, setTags, ...

If you try to use EventBean->setMeta or any of the context setters that use array_merge, it will fail if the previous value wasn't set. i.e.:
if $this->contexts['user'] is null,

$this->contexts['user'] = array_merge($this->contexts['user'], $userContext);

// results in null

I believe this affects all of the contexts setters (tags, meta, user, etc...) if they are not set with the sharedContext from the Agent.

Also, is the SharedContext from the agent supposed to be shared with the spans inside the transaction? or only the transaction?

Thanks!

Hello from Elastic

Hi @philkra

My name is Thomas and I'm a developer of the Node.js agent for Elastic APM. I just wanted to reach out and say that I really appreciate the work you've put into this agent.

I just added you to this list we're maintaining of community developed agents. I hope that's ok with you 🙂

If you ever run into any issues with the APM Server API or have any questions regarding these things, don't hesitate to reach out. For example regarding supporting the new v2 API. You can always open an issue in https://github.com/elastic/apm or even just tag me in PR's or issues in this repo and I'll do my best to help.

Also, if you DM me on Twitter with your postal address, I want to send you a little something 📦☺️ If you don't have Twitter, let me know.

Installation without composer

Hi,

First of all, thank you for your time with this agent.

Second, is there a way to install the agent without compose?

Thank you

Enable http client configuration

We're using an internal APM server with an self signed cert. I need to be able to configure the http client to not validate the cert. I've already tested a change and should have a PR soon.

DateTime::createFromFormat returns FALSE sometimes

So, this one is interesting. It's not a bug in your code, per se, but more of a PHP issue. When calling line 54 in /Events/EventBean.php, sometimes \DateTime::createFromFormat() returns FALSE and will throw an exception into the errors logs with the message "Call to a member function setTimeZone() on boolean".

After doing some research on it I came across https://bugs.php.net/bug.php?id=64414 (Bug #64414 | DateTime::createFromFormat() fails randomly when using microtime(true) as input). The reason I'm seeing is that sometimes microtime(true) will return a float with only an integer part.

It looks like it's still an open bug in PHP7.x as well. I changed my code to the following to see if this fixes it:

$timestamp = \DateTime::createFromFormat( 'U.u', sprintf("%.6F", microtime(true)));
$timestamp->setTimeZone( new \DateTimeZone( 'UTC' ) );
$this->timestamp = $timestamp->format( 'Y-m-d\TH:i:s.u\Z' );

Just a heads up in case you run into this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.