Giter VIP home page Giter VIP logo

inspector-php's Introduction

Inspector | Code Execution Monitoring Tool

Total Downloads Latest Stable Version License Contributor Covenant Build Status

Simple code execution monitoring, built for PHP developers.

Requirements

  • PHP >= 7.2.0

Install

Install the latest version by:

composer require inspector-apm/inspector-php

Use

To start sending data to Inspector you need an Ingestion Key to create an instance of the Configuration class. You can obtain INSPECTOR_API_KEY creating a new project in your Inspector dashboard.

use Inspector\Inspector;
use Inspector\Configuration;

$configuration = new Configuration('YOUR_INGESTION_KEY');
$inspector = new Inspector($configuration);

All start with a transaction. Transaction represent an execution cycle and it can contains one or hundred of segments:

// Start an execution cycle with a transaction
$inspector->startTransaction($_SERVER['PATH_INFO']);

Use addSegment method to monitor a code block in your transaction:

$result = $inspector->addSegment(function ($segment) {
    // Do something here...
	return "Hello World!";
}, 'my-process');

echo $result; // this will print "Hello World!"

Inspector will monitor your code execution in real time alerting you if something goes wrong.

Custom Transport

You can also set up custom transport class to transfer monitoring data from your server to Inspector in a personalized way.

The transport class needs to implement \Inspector\Transports\TransportInterface:

class CustomTransport implements \Inspector\Transports\TransportInterface
{
    protected $configuration;

    protected $queue = [];

    public function __constructor($configuration)
    {
        $this->configuration = $configuration;
    }

    public function addEntry(\Inspector\Models\Arrayable $entry)
    {
        // Add an \Inspector\Models\Arrayable entry in the queue.
        $this->queue[] = $entry;
    }

    public function flush()
    {
        // Performs data transfer.
        $handle = curl_init('https://ingest.inspector.dev');
        curl_setopt($handle, CURLOPT_POST, 1);
        curl_setopt($handle, CURLOPT_HTTPHEADER, [
            'X-Inspector-Key: xxxxxxxxxxxx',
            'Content-Type: application/json',
            'Accept: application/json',
        ]);
        curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($this->queue));
        curl_exec($handle);
        curl_close($handle);
    }
}

Then you can set the new transport in the Inspector instance using a callback the will receive the current configuration state as parameter.

$inspector->setTransport(function ($configuration) {
    return new CustomTransport($configuration);
});

Chek out the official documentation

Contributing

We encourage you to contribute to Inspector! Please check out the Contribution Guidelines about how to proceed. Join us!

LICENSE

This package is licensed under the MIT license.

inspector-php's People

Contributors

bchalier avatar ilvalerione avatar owenvoke avatar seriquynh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

inspector-php's Issues

AbstractApiTransport::sendViaFile incorrect $filePath?

Hello,

Using:

$filepath = __DIR__.DIRECTORY_SEPARATOR.uniqid().'.dat';
file_put_contents($filepath, $json, LOCK_EX);

makes inspector to generate path like:
/var/www/project/vendor/inspector-apm/inspector-php/src/Transports/randomName.dat
which is causing me access issues.
I can of course fix permissions manually, but this made me wondering if this is really the path you want to store the .dat files in?
Wouldn't using say /tmp/ be a better choice perhaps?

And thank you for the product, it's neat.

Fix Inspector::isRecording()

inspector-apm/inspector-symfony#4

Methods canAddSegment() and isRecording() are needed to be replaced with this code:

    /**
     * Determine if a new segment can be added.
     *
     * @return bool
     */
    public function canAddSegments(): bool
    {
        return $this->isRecording();
    }

    /**
     * Check if a transaction was started.
     *
     * @return bool
     */
    public function isRecording(): bool
    {
        return $this->configuration->isEnabled() && $this->hasTransaction();
    }

Bug: Transaction not unset when dropping a flush when sampling

I believe that when returning false on a beforeFlush callback, the transaction should be unset as well (line 300)

/**
* Flush data to the remote platform.
*
* @throws \Exception
*/
public function flush()
{
if (!$this->isRecording() || !$this->hasTransaction()) {
return;
}
if (!$this->transaction->isEnded()) {
$this->transaction->end();
}
foreach (static::$beforeCallbacks as $callback) {
if (call_user_func($callback, $this) === false) {
return;
}
}
$this->transport->flush();
unset($this->transaction);
}

Add possibility to get inspector's segment by label

I need to get Segment by a label to develop https://github.com/inspector-apm/inspector-symfony properly.

I need to end the segment in some methods, but I can't pass the segment instance itself directly to the method.

Use-case: start segment in onKernelRequest() and finish it in onKernelResponse()

So, I need the method public function getSegment(string $type, string $label) I'm not sure which role has the type parameter here, but probably, segments are grouped by types?

disk usage and open_base_dir directive incompatibility

When a base dir restriction is used enabling inspector will cause a 500 due to this line : src/Models/Partials/Host.php:58
round(100 - ((disk_free_space('/') / disk_total_space('/')) * 100), 2);

An option for not checking the free space would be good too (didn't find it if it already exists), I didn't find where I can see the free space in the dashboard either.

Argument 1 passed to Inspector\Models\Segment::__construct() must be an instance of Inspector\Models\Transaction, null given

Describe the bug

The following error is shown in the /storage/logs/laravel.log when opening the nova dashboard on production

In Segment.php line 18:

Argument 1 passed to Inspector\Models\Segment::__construct() must be an instance of Inspector\Models\Transaction, null given, called in inspector-apm/inspector-php/src/Inspector.php on line 172 {"userId":1,"exception":"[object] (TypeError(code: 0): Argument 1 passed to Inspector\\Models\\Segment::__construct() must be an instance of Inspector\\Models\\Transaction, null given, called in inspector-apm/inspector-php/src/Inspector.php on line 172 at inspector-apm/inspector-php/src/Models/Segment.php:18)

Expected behavior

Work fine as it was before the update.

Versions

inspector-apm/inspector-laravel 4.6.21
inspector-apm/inspector-php 3.5.17
laravel/framework ^7.0
laravel/nova "*"

Works downgrading to:

inspector-apm/inspector-laravel 4.6.13
inspector-apm/inspector-php 3.5.10

Is this something on be aware on the nova end?

file_put_contents(/var/www/yn/vendor/inspector-apm/inspector-php/src/Transports/60b01a54b4b83.dat): failed to open stream: Permission denied

I get this error

{
    "class": "ErrorException",
    "message": "file_put_contents(/var/www/yn/vendor/inspector-apm/inspector-php/src/Transports/60b01a54b4b83.dat): failed to open stream: Permission denied",
    "code": 0,
    "file": "/var/www/yn/vendor/inspector-apm/inspector-php/src/Transports/AbstractApiTransport.php:141",
    "trace": [
        "/var/www/yn/vendor/inspector-apm/inspector-php/src/Transports/AbstractApiTransport.php:141",
        "/var/www/yn/vendor/inspector-apm/inspector-php/src/Transports/AbstractApiTransport.php:120",
        "/var/www/yn/vendor/inspector-apm/inspector-php/src/Transports/AbstractApiTransport.php:127",
        "/var/www/yn/vendor/inspector-apm/inspector-php/src/Transports/AbstractApiTransport.php:100",
        "/var/www/yn/vendor/inspector-apm/inspector-php/src/Inspector.php:273",
        "/var/www/yn/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:261",
        "/var/www/yn/vendor/inspector-apm/inspector-laravel/src/Providers/JobServiceProvider.php:126",
        "/var/www/yn/vendor/inspector-apm/inspector-laravel/src/Providers/JobServiceProvider.php:65",
        "/var/www/yn/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:381",
        "/var/www/yn/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:226",
        "/var/www/yn/vendor/laravel/framework/src/Illuminate/Queue/Worker.php:540",
        "/var/www/yn/vendor/laravel/framework/src/Illuminate/Queue/Worker.php:392",
        "/var/www/yn/vendor/laravel/framework/src/Illuminate/Queue/Worker.php:360",
        "/var/www/yn/vendor/laravel/framework/src/Illuminate/Queue/Worker.php:306",
        "/var/www/yn/vendor/laravel/framework/src/Illuminate/Queue/Worker.php:132",
        "/var/www/yn/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php:112",
        "/var/www/yn/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php:96",
        "/var/www/yn/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.p

Do not work correctly on PHP8.1

Hi! I use the latest version 3.7.9 in Laravel 9 project with php8.1.2 onboard.
After I add middleware I get following error:
PHP Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/api/vendor/inspector-apm/inspector-php/src/Models/Arrayable.php on line 165

@ilvalerione Could you please fix it? This issue is a blocker for our team to use Inspector service after the migration to php8.1+ and latest Laravel

Undefined property: Inspector\Laravel\Inspector::$transaction

I'm getting this error with the new updates

Message
Undefined property: Inspector\Laravel\Inspector::$transaction
Level
ERROR
Exception
{
    "class": "ErrorException",
    "message": "Undefined property: Inspector\\Laravel\\Inspector::$transaction",
    "code": 0,
    "file": "/tmp/vendor/inspector-apm/inspector-php/src/Inspector.php:112",
    "trace": [
        "/tmp/vendor/inspector-apm/inspector-php/src/Inspector.php:112",
        "/tmp/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:261",
        "/tmp/vendor/inspector-apm/inspector-laravel/src/Providers/CommandServiceProvider.php:50",
        "/tmp/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:381",
        "/tmp/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:226",
        "/tmp/vendor/laravel/framework/src/Illuminate/Console/Application.php:96",
        "/tmp/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:129",
        "/tmp/vendor/laravel/vapor-core/src/Runtime/Handlers/QueueHandler.php:60",
        "/var/task/cliRuntime.php:75",
        "/tmp/vendor/laravel/vapor-core/src/Runtime/LambdaRuntime.php:53",
        "/var/task/cliRuntime.php:77",
        "/var/task/runtime.php:52",
        "/opt/bootstrap.php:6"
    ]
}

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.