Skip to content

This project is built upon the abandoned Microsoft SDK for Azure Application Insights

License

Notifications You must be signed in to change notification settings

halloverden/azure-application-insights

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Azure Application Insights

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();
        }
    }
}

About

This project is built upon the abandoned Microsoft SDK for Azure Application Insights

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages