PHPackages                             speakol-ads/elastic-apm-php-agent - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. speakol-ads/elastic-apm-php-agent

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

speakol-ads/elastic-apm-php-agent
=================================

A php agent for Elastic APM v2

v7.1.3(6y ago)12.6k21MITPHPPHP &gt;= 7.0

Since Feb 2Pushed 6y ago1 watchersCompare

[ Source](https://github.com/speakol-ads/elastic-apm-php-agent)[ Packagist](https://packagist.org/packages/speakol-ads/elastic-apm-php-agent)[ RSS](/packages/speakol-ads-elastic-apm-php-agent/feed)WikiDiscussions master Synced today

READMEChangelog (4)Dependencies (4)Versions (29)Used By (1)

Elastic APM: PHP Agent
======================

[](#elastic-apm-php-agent)

[![Build Status](https://camo.githubusercontent.com/c7ada8442baac3fbaed09b3ffb6e59796380f253cba396aa18a5b4a30a973aa7/68747470733a2f2f7472617669732d63692e636f6d2f7068696c6b72612f656c61737469632d61706d2d7068702d6167656e742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/philkra/elastic-apm-php-agent)

This is a PHP agent for Elastic.co's APM product: . Laravel &amp; Lumen package

Note
----

[](#note)

This is a fork from the [original](https://github.com/philkra/elastic-apm-php-agent) package to add the support for APM API `v2`

Installation
------------

[](#installation)

The recommended way to install the agent is through [Composer](http://getcomposer.org).

Run the following composer command

```
php composer.phar require speakol-ads/elastic-apm-php-agent
```

After installing, you need to require Composer's autoloader:

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

Usage
-----

[](#usage)

### Initialize the Agent with minimal Config

[](#initialize-the-agent-with-minimal-config)

```
$agent = new \PhilKra\Agent( [ 'appName' => 'demo' ] );
```

When creating the agent, you can directly inject shared contexts such as user, tags and custom.

```
$agent = new \PhilKra\Agent( [ 'appName' => 'with-custom-context' ], [
  'user' => [
    'id'    => 12345,
    'email' => 'email@acme.com',
  ],
  'tags' => [
    // ... more key-values
  ],
  'custom' => [
    // ... more key-values
  ]
] );
```

### Capture Errors and Exceptions

[](#capture-errors-and-exceptions)

The agent can capture all types or errors and exceptions that are implemented from the interface `Throwable` ().

```
// start a new transaction or use an existing one
$transaction = $agent->startTransaction($trxName);

$agent->captureThrowable( new Exception(), [], $transaction );

$agent->send();
```

### Adding spans

[](#adding-spans)

Addings spans () is easy. Please consult the documentation for your exact needs. Below is an example for adding a MySQL span.

```
$trxName = 'GET /some/transaction/name';

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

// start a new transaction
$transaction = $agent->startTransaction($trxName);

// 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
        '
