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.
- Azure Application Insights
- Official Github Repository From Microsoft
- Supported SDKs
- GitHub Announcements
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.
- PHP version ^7.1
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
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.
$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();
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');
try
{
// Do something to throw an exception
}
catch (\Exception $ex)
{
$telemetryClient->trackException($ex, ['InlineProperty' => 'test_value'], ['duration_inner' => 42.0]);
$telemetryClient->flush();
}
$telemetryClient->getChannel()->setSendGzipped(true);
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();
}
}
}