halloverden/azure-application-insights

This project extends the Application Insights API surface to support PHP.

0.1.4 2022-06-22 10:22 UTC

This package is auto-updated.

Last update: 2024-10-22 15:53:17 UTC


README

This project was forked from and builds upon the official php sdk from windows, which is no longer mantained. As of now, it only supports exception logging.

About:

Status

This SDK is NOT maintained or supported by Microsoft even though they've contributed to it in the past. Note that Azure Monitor only provides support when using supported SDKs.

Requirements

  • PHP version ^7.1

Installation

Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

Open a command console, enter your project directory and execute:

$ composer require halloverden/application-insights-sdk

Usage

Once installed, you can send exception telemetry to Application Insights. Here are a few samples.

Note: before you can send data to you will need an instrumentation key. Please see the Getting an Application Insights Instrumentation Key section for more information.

Initializing the client and setting the instrumentation key and other optional configurations

$telemetryClient = new \ApplicationInsights\Telemetry_Client();
$context = $telemetryClient->getContext();

// Necessary
$context->setInstrumentationKey('YOUR INSTRUMENTATION KEY');

// Optional
$context->getSessionContext()->setId(session_id());
$context->getUserContext()->setId('YOUR USER ID');
$context->getApplicationContext()->setVer('YOUR VERSION');
$context->getLocationContext()->setIp('YOUR IP');

// Start tracking throwables
$telemetryClient->trackException($throwable);
$telemetryClient->flush();

Setup Operation context

For correct Application Insights reporting you need to setup Operation Context, reference to Request

$telemetryClient->getContext()->getOperationContext()->setId('XX');
$telemetryClient->getContext()->getOperationContext()->setName('GET Index');

Sending an exception telemetry, with custom properties and metrics

try
{
    // Do something to throw an exception
}
catch (\Exception $ex)
{
    $telemetryClient->trackException($ex, ['InlineProperty' => 'test_value'], ['duration_inner' => 42.0]);
    $telemetryClient->flush();
}

Set the Client to gzip the data before sending

$telemetryClient->getChannel()->setSendGzipped(true);

Registering an exception handler

class Handle_Exceptions
{
    private $_telemetryClient;

    public function __construct()
    {
        $this->_telemetryClient = new \ApplicationInsights\Telemetry_Client();
        $this->_telemetryClient->getContext()->setInstrumentationKey('YOUR INSTRUMENTATION KEY');

        set_exception_handler(array($this, 'exceptionHandler'));
    }

    function exceptionHandler(\Exception $exception)
    {
        if ($exception != NULL)
        {
            $this->_telemetryClient->trackException($exception);
            $this->_telemetryClient->flush();
        }
    }
}