PHPackages                             bskiefer/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. bskiefer/elastic-apm-php-agent

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

bskiefer/elastic-apm-php-agent
==============================

A php 5.6 agent for Elastic APM

7.2.4(6y ago)045MITPHPPHP 5.6.\*

Since Feb 2Pushed 6y agoCompare

[ Source](https://github.com/bskiefer/elastic-apm-php-agent)[ Packagist](https://packagist.org/packages/bskiefer/elastic-apm-php-agent)[ RSS](/packages/bskiefer-elastic-apm-php-agent/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (5)Dependencies (4)Versions (14)Used By (0)

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

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

[![Build Status](https://camo.githubusercontent.com/48ccbe3ff16f5f9aad76bb99f8fa65a7b3fc894b07415d77671133d70efaaa4b/68747470733a2f2f7472617669732d63692e6f72672f62736b69656665722f656c61737469632d61706d2d7068702d6167656e742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/bskiefer/elastic-apm-php-agent)

This is a PHP agent for Elastic.co's APM product: .

*New:* Laravel &amp; Lumen package

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

[](#installation)

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

Run the following composer command

```
php composer.phar require bskiefer/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` (). You may also optionally pass in a line number and file path, which is useful for older PHP and framework versions.

```
$agent->captureThrowable( new Exception(), $line = 0, $path = null );
```

### 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.

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