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

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

middleware/agent-apm-php
========================

Agent APM for PHP

v1.0(1y ago)061[1 PRs](https://github.com/middleware-labs/agent-apm-php/pulls)Apache-2.0PHPPHP ^8.1CI failing

Since Oct 31Pushed 1y ago4 watchersCompare

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

READMEChangelogDependencies (11)Versions (3)Used By (0)

Getting Started
===============

[](#getting-started)

### agent-apm-php

[](#agent-apm-php)

Description: Agent APM for PHP

### Prerequisites

[](#prerequisites)

- To monitor APM data on dashboard, [Middleware Host-agent](https://docs.middleware.io/docs/getting-started) needs to be installed, You can refer [this demo project](https://github.com/middleware-labs/demo-apm/tree/master/php) to refer use cases of APM.
- PHP requires at least PHP 8+ and [OpenTelemetry PHP-Extension](https://opentelemetry.io/docs/instrumentation/php/automatic/#setup) to run this agent.

### Guides

[](#guides)

To use this APM agent, follow below steps:

1. Run `composer require middleware/agent-apm-php` in your project directory.
2. After successful installation, you need to add `require 'vendor/autoload.php';` in your file.
3. Then after, you need to add `use Middleware\AgentApmPhp\MwTracker;` line.
4. Now, add following code to the next line with your Project &amp; Service name: ```
    $tracker = new MwTracker('', '');

    ```
5. Then we have 2 functions, named `preTrack()` &amp; `postTrack()`, your code must be placed between these functions. After preTrack() calls, you need to register your desired classes &amp; functions as follows: ```
    $tracker->preTrack();
    $tracker->registerHook('', '');
    $tracker->registerHook('', '');

    ```
6. You can add your own custom attributes as the third parameter, and checkout many other pre-defined attributes [here](https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/span-general/). ```
    $tracker->registerHook('', '', [
        'custom.attr1' => 'value1',
        'custom.attr2' => 'value2',
    ]);

    ```
7. At the end, just call `postTrack()` function, which will send all the traces to the Middleware Host-agent. ```
    $tracker->postTrack();

    ```
8. If you want to enable Logging feature along with tracing in your project, then you can use below code snippet: ```
    $tracker->warn("this is warning log.");
    $tracker->error("this is error log.");
    $tracker->info("this is info log.");
    $tracker->debug("this is debug log.");

    ```
9. So, final code snippet will look like as: ```
