Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #57 from philkra/development
Browse files Browse the repository at this point in the history
Prepareing release 6.5.4
  • Loading branch information
dstepe authored Mar 6, 2019
2 parents 9bce623 + e631cc4 commit 6f43ec1
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 35 deletions.
51 changes: 40 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,48 @@ $agent->getTransaction( $trxName )->setUserContext( [
$agent->getTransaction( $trxName )->setTags( [ 'k1' => 'v1', 'k2' => 'v2' ] );
```

### Example of a Transaction
This example illustrates how you can monitor a call to another web service.
```php
$agent = new \PhilKra\Agent( [ 'appName' => 'example' ] );

$endpoint = 'https://acme.com/api/';
$payload = [ 'foo' => 'bar' ];
$trxName = sprintf('POST %s', $endpoint);
$client = new GuzzleHttp\Client();

// Start the Transaction
$agent->startTransaction( $trxName );

// Do the call via curl/Guzzle e.g.
$response = $client->request('POST', $endpoint, [
'json' => $payload
]);

// Stop the Transaction tracing, attach the Status and the sent Payload
$agent->stopTransaction( $trxName, [
'status' => $response->getStatusCode(),
'payload' => $payload,
] );

// Send the collected Traces to the APM server
$agent->send();
```

### Configuration
```
appName : Name of this application, Required
appVersion : Application version, Default: ''
serverUrl : APM Server Endpoint, Default: 'http://127.0.0.1:8200'
secretToken: Secret token for APM Server, Default: null
hostname : Hostname to transmit to the APM Server, Default: gethostname()
active : Activate the APM Agent, Default: true
timeout : Guzzle Client timeout, Default: 5
apmVersion : APM Server Intake API version, Default: 'v1'
env : $_SERVER vars to send to the APM Server, empty set sends all. Keys are case sensitive, Default: []
cookies : Cookies to send to the APM Server, empty set sends all. Keys are case sensitive, Default: []
httpClient : Extended GuzzleHttp\Client Default: []
appName : Name of this application, Required
appVersion : Application version, Default: ''
serverUrl : APM Server Endpoint, Default: 'http://127.0.0.1:8200'
secretToken : Secret token for APM Server, Default: null
hostname : Hostname to transmit to the APM Server, Default: gethostname()
active : Activate the APM Agent, Default: true
timeout : Guzzle Client timeout, Default: 5
apmVersion : APM Server Intake API version, Default: 'v1'
env : $_SERVER vars to send to the APM Server, empty set sends all. Keys are case sensitive, Default: []
cookies : Cookies to send to the APM Server, empty set sends all. Keys are case sensitive, Default: []
httpClient : Extended GuzzleHttp\Client Default: []
backtraceLimit: Depth of a transaction backtrace, Default: unlimited
```

Detailed `GuzzleHttp\Client` options can be found [here](http://docs.guzzlephp.org/en/stable/request-options.html#request-options).
Expand Down
3 changes: 2 additions & 1 deletion src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Agent
*
* @var string
*/
const VERSION = '6.5.1';
const VERSION = '6.5.4';

/**
* Agent Name
Expand Down Expand Up @@ -158,6 +158,7 @@ public function startTransaction(string $name, array $context = [], float $start
*/
public function stopTransaction(string $name, array $meta = [])
{
$this->getTransaction($name)->setBacktraceLimit($this->config->get('backtraceLimit', 0));
$this->getTransaction($name)->stop();
$this->getTransaction($name)->setMeta($meta);
}
Expand Down
20 changes: 10 additions & 10 deletions src/Events/EventBean.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,34 +170,34 @@ final protected function getMetaResult() : string
*
* @link http://php.net/manual/en/reserved.variables.server.php
* @link https://github.com/philkra/elastic-apm-php-agent/issues/27
* @link https://github.com/philkra/elastic-apm-php-agent/issues/54
*
* @return object
* @return array
*/
final protected function getEnv() : object
final protected function getEnv() : array
{
$envMask = $this->contexts['env'];
$env = empty($envMask)
? $_SERVER
: array_intersect_key($_SERVER, array_flip($envMask));

return (object) $env;
return $env;
}

/**
* Get the cookies
*
* @link https://github.com/philkra/elastic-apm-php-agent/issues/30
* @link https://github.com/philkra/elastic-apm-php-agent/issues/54
*
* @return object
* @return array
*/
final protected function getCookies() : object
final protected function getCookies() : array
{
$cookieMask = $this->contexts['cookies'];
$cookies = empty($cookieMask)
return empty($cookieMask)
? $_COOKIE
: array_intersect_key($_COOKIE, array_flip($cookieMask));

return (object) $cookies;
}

/**
Expand Down Expand Up @@ -250,8 +250,8 @@ final protected function getContext() : array
'user-agent' => $headers['User-Agent'] ?? '',
'cookie' => $this->getCookieHeader($headers['Cookie'] ?? ''),
],
'env' => $this->getEnv(),
'cookies' => $this->getCookies(),
'env' => (object)$this->getEnv(),
'cookies' => (object)$this->getCookies(),
]
];

Expand Down
24 changes: 22 additions & 2 deletions src/Events/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,19 @@ class Transaction extends EventBean implements \JsonSerializable
];

/**
* The spams for the transaction
* The spans for the transaction
*
* @var array
*/
private $spans = [];

/**
* Backtrace Depth
*
* @var int
*/
private $backtraceLimit = 0;

/**
* Create the Transaction
*
Expand Down Expand Up @@ -83,7 +90,7 @@ public function stop(int $duration = null)
// Store Summary
$this->summary['duration'] = $duration ?? round($this->timer->getDurationInMilliseconds(), 3);
$this->summary['headers'] = (function_exists('xdebug_get_headers') === true) ? xdebug_get_headers() : [];
$this->summary['backtrace'] = debug_backtrace();
$this->summary['backtrace'] = debug_backtrace($this->backtraceLimit);
}

/**
Expand Down Expand Up @@ -130,6 +137,19 @@ public function setSpans(array $spans)
$this->spans = $spans;
}

/**
* Set the Max Depth/Limit of the debug_backtrace method
*
* @link http://php.net/manual/en/function.debug-backtrace.php
* @link https://github.com/philkra/elastic-apm-php-agent/issues/55
*
* @param int $limit [description]
*/
public function setBacktraceLimit(int $limit)
{
$this->backtraceLimit = $limit;
}

/**
* Get the spans from the transaction
*
Expand Down
25 changes: 14 additions & 11 deletions src/Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,25 @@ public function asArray() : array
/**
* Get the Default Config of the Agent
*
* @link https://github.com/philkra/elastic-apm-php-agent/issues/55
*
* @return array
*/
private function getDefaultConfig() : array
{
return [
'serverUrl' => 'http://127.0.0.1:8200',
'secretToken' => null,
'hostname' => gethostname(),
'appVersion' => '',
'active' => true,
'timeout' => 5,
'apmVersion' => 'v1',
'env' => [],
'cookies' => [],
'httpClient' => [],
'environment' => 'development',
'serverUrl' => 'http://127.0.0.1:8200',
'secretToken' => null,
'hostname' => gethostname(),
'appVersion' => '',
'active' => true,
'timeout' => 5,
'apmVersion' => 'v1',
'env' => [],
'cookies' => [],
'httpClient' => [],
'environment' => 'development',
'backtraceLimit' => 0,
];
}
}
2 changes: 2 additions & 0 deletions tests/Helper/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function testControlDefaultConfig() {
$this->assertArrayHasKey( 'cookies', $config );
$this->assertArrayHasKey( 'httpClient', $config );
$this->assertArrayHasKey( 'environment', $config );
$this->assertArrayHasKey( 'backtraceLimit', $config );

$this->assertEquals( $config['appName'], $appName );
$this->assertNull( $config['secretToken'] );
Expand All @@ -46,6 +47,7 @@ public function testControlDefaultConfig() {
$this->assertEquals( $config['cookies'], [] );
$this->assertEquals( $config['httpClient'], [] );
$this->assertEquals( $config['environment'], 'development' );
$this->assertEquals( $config['backtraceLimit'], 0 );
}

/**
Expand Down

0 comments on commit 6f43ec1

Please sign in to comment.