GithubHelp home page GithubHelp logo

middleware-labs / agent-apm-php Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 40 KB

Agent APM for PHP

Home Page: https://packagist.org/packages/middleware/agent-apm-php

License: Apache License 2.0

PHP 100.00%

agent-apm-php's Introduction

Getting Started

agent-apm-php

Description: Agent APM for PHP

Prerequisites

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 & Service name:
    $tracker = new MwTracker('<PROJECT-NAME>', '<SERVICE-NAME>');
    
  5. Then we have 2 functions, named preTrack() & postTrack(), your code must be placed between these functions. After preTrack() calls, you need to register your desired classes & functions as follows:
    $tracker->preTrack();
    $tracker->registerHook('<CLASS-NAME-1>', '<FUNCTION-NAME-1>');
    $tracker->registerHook('<CLASS-NAME-2>', '<FUNCTION-NAME-2>');
    
  6. You can add your own custom attributes as the third parameter, and checkout many other pre-defined attributes here.
    $tracker->registerHook('<CLASS-NAME-1>', '<FUNCTION-NAME-1>', [
        '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:
    <?php
    require 'vendor/autoload.php';
    use Middleware\AgentApmPhp\MwTracker;
    
    $tracker = new MwTracker('<PROJECT-NAME>', '<SERVICE-NAME>');
    $tracker->preTrack();
    $tracker->registerHook('<CLASS-NAME-1>', '<FUNCTION-NAME-1>', [
        'custom.attr1' => 'value1',
        'custom.attr2' => 'value2',
    ]);
    $tracker->registerHook('<CLASS-NAME-2>', '<FUNCTION-NAME-2>');
    
    $tracker->info("this is info log.");
    
    // ----
    // Your code goes here.
    // ----
    
    $tracker->postTrack();
    

Note: OTEL collector endpoint for all the traces, will be http://localhost:9320/v1/traces by default.

Sample Code

<?php
require 'vendor/autoload.php';
use Middleware\AgentApmPhp\MwTracker;

$tracker = new MwTracker('DemoProject', 'PrintService');
$tracker->preTrack();
$tracker->registerHook('DemoClass', 'runCode', [
    'code.column' => '12',
    'net.host.name' => 'localhost',
    'db.name' => 'users',
    'custom.attr1' => 'value1',
]);
$tracker->registerHook('DoThings', 'printString');

$tracker->info("this is info log.");

class DoThings {
    public static function printString($str): void {
        // sleep(1);
        global $tracker;
        $tracker->warn("this is warning log, but from inner function.");
        
        echo $str . PHP_EOL;
    }
}

class DemoClass {
    public static function runCode(): void {
        DoThings::printString('Hello World!');
    }
}

DemoClass::runCode();

$tracker->postTrack();

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.