Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Bug fixes, client ip context and PHP 5.3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubOleksy committed Jan 14, 2015
1 parent 05cff12 commit 1922ef7
Show file tree
Hide file tree
Showing 24 changed files with 86 additions and 47 deletions.
4 changes: 2 additions & 2 deletions ApplicationInsights/Channel/Contracts/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Application.
*/
class Application implements \JsonSerializable
class Application
{
/**
* Data array that will store all the values.
Expand All @@ -16,7 +16,7 @@ class Application implements \JsonSerializable
*/
function __construct()
{
$this->_data = [];
$this->_data = array();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ApplicationInsights/Channel/Contracts/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Data.
*/
class Data implements \JsonSerializable
class Data
{
/**
* Data array that will store all the values.
Expand Down
2 changes: 1 addition & 1 deletion ApplicationInsights/Channel/Contracts/Data_Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Data_Point.
*/
class Data_Point implements \JsonSerializable
class Data_Point
{
/**
* Data array that will store all the values.
Expand Down
4 changes: 2 additions & 2 deletions ApplicationInsights/Channel/Contracts/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Device.
*/
class Device implements \JsonSerializable
class Device
{
/**
* Data array that will store all the values.
Expand All @@ -16,7 +16,7 @@ class Device implements \JsonSerializable
*/
function __construct()
{
$this->_data = [];
$this->_data = array();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ApplicationInsights/Channel/Contracts/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Envelope.
*/
class Envelope implements \JsonSerializable
class Envelope
{
/**
* Data array that will store all the values.
Expand Down
2 changes: 1 addition & 1 deletion ApplicationInsights/Channel/Contracts/Event_Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Event_Data.
*/
class Event_Data implements \JsonSerializable
class Event_Data
{
/**
* Data array that will store all the values.
Expand Down
2 changes: 1 addition & 1 deletion ApplicationInsights/Channel/Contracts/Exception_Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Exception_Data.
*/
class Exception_Data implements \JsonSerializable
class Exception_Data
{
/**
* Data array that will store all the values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Exception_Details.
*/
class Exception_Details implements \JsonSerializable
class Exception_Details
{
/**
* Data array that will store all the values.
Expand Down
4 changes: 2 additions & 2 deletions ApplicationInsights/Channel/Contracts/Internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Internal.
*/
class Internal implements \JsonSerializable
class Internal
{
/**
* Data array that will store all the values.
Expand All @@ -16,7 +16,7 @@ class Internal implements \JsonSerializable
*/
function __construct()
{
$this->_data = [];
$this->_data = array();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions ApplicationInsights/Channel/Contracts/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Location.
*/
class Location implements \JsonSerializable
class Location
{
/**
* Data array that will store all the values.
Expand All @@ -16,7 +16,7 @@ class Location implements \JsonSerializable
*/
function __construct()
{
$this->_data = [];
$this->_data = array();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ApplicationInsights/Channel/Contracts/Message_Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Message_Data.
*/
class Message_Data implements \JsonSerializable
class Message_Data
{
/**
* Data array that will store all the values.
Expand Down
2 changes: 1 addition & 1 deletion ApplicationInsights/Channel/Contracts/Metric_Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Metric_Data.
*/
class Metric_Data implements \JsonSerializable
class Metric_Data
{
/**
* Data array that will store all the values.
Expand Down
4 changes: 2 additions & 2 deletions ApplicationInsights/Channel/Contracts/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Operation.
*/
class Operation implements \JsonSerializable
class Operation
{
/**
* Data array that will store all the values.
Expand All @@ -16,7 +16,7 @@ class Operation implements \JsonSerializable
*/
function __construct()
{
$this->_data = [];
$this->_data = array();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ApplicationInsights/Channel/Contracts/Page_View_Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Page_View_Data.
*/
class Page_View_Data implements \JsonSerializable
class Page_View_Data
{
/**
* Data array that will store all the values.
Expand Down
2 changes: 1 addition & 1 deletion ApplicationInsights/Channel/Contracts/Request_Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Request_Data.
*/
class Request_Data implements \JsonSerializable
class Request_Data
{
/**
* Data array that will store all the values.
Expand Down
4 changes: 2 additions & 2 deletions ApplicationInsights/Channel/Contracts/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Session.
*/
class Session implements \JsonSerializable
class Session
{
/**
* Data array that will store all the values.
Expand All @@ -16,7 +16,7 @@ class Session implements \JsonSerializable
*/
function __construct()
{
$this->_data = [];
$this->_data = array();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ApplicationInsights/Channel/Contracts/Stack_Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type Stack_Frame.
*/
class Stack_Frame implements \JsonSerializable
class Stack_Frame
{
/**
* Data array that will store all the values.
Expand Down
4 changes: 2 additions & 2 deletions ApplicationInsights/Channel/Contracts/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Data contract class for type User.
*/
class User implements \JsonSerializable
class User
{
/**
* Data array that will store all the values.
Expand All @@ -16,7 +16,7 @@ class User implements \JsonSerializable
*/
function __construct()
{
$this->_data = [];
$this->_data = array();
}

/**
Expand Down
31 changes: 29 additions & 2 deletions ApplicationInsights/Channel/Contracts/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class Utils
*/
public static function removeEmptyValues($sourceArray)
{
$newArray = [];
$newArray = array();
foreach ($sourceArray as $key => $value)
{
if ($value == [] || $value == NULL)
if ((is_array($value) && sizeof($value) == 0) || ($value == NULL && is_bool($value) == false))
{
continue;
}
Expand All @@ -26,6 +26,33 @@ public static function removeEmptyValues($sourceArray)
return $newArray;
}

/**
* Serialization helper.
* @param array Items to serialize
* @return array JSON serialized items, nested
*/
public static function getUnderlyingData($dataItems)
{
$queueToEncode = array();
foreach ($dataItems as $key => $dataItem)
{
if (method_exists($dataItem, 'jsonSerialize') == true)
{
$queueToEncode[$key] = Utils::getUnderlyingData($dataItem->jsonSerialize());
}
else if (is_array($dataItem))
{
$queueToEncode[$key] = Utils::getUnderlyingData($dataItem);
}
else
{
$queueToEncode[$key] = $dataItem;
}
}

return $queueToEncode;
}

/**
* Converts milliseconds to a timespan string as accepted by the backend
* @param int $milliseconds
Expand Down
24 changes: 15 additions & 9 deletions ApplicationInsights/Channel/Telemetry_Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Telemetry_Channel
function __construct($endpointUrl = 'https://dc.services.visualstudio.com/v2/track')
{
$this->_endpointUrl = $endpointUrl;
$this->_queue = [];
$this->_queue = array();
}

/**
Expand Down Expand Up @@ -70,7 +70,13 @@ public function setQueue($queue)
*/
public function getSerializedQueue()
{
return json_encode($this->_queue);
$queueToEncode = array();
foreach ($this->_queue as $dataItem)
{
array_push($queueToEncode, Contracts\Utils::getUnderlyingData($dataItem->jsonSerialize()));
}

return json_encode($queueToEncode);
}

/**
Expand Down Expand Up @@ -115,7 +121,7 @@ public function addToQueue($data, \ApplicationInsights\Telemetry_Context $teleme
$dataProperties = $data->getProperties();
if ($dataProperties == NULL)
{
$dataProperties = [];
$dataProperties = array();
}
foreach ($contextProperties as $key => $value)
{
Expand Down Expand Up @@ -148,8 +154,8 @@ public function send()

$serializedTelemetryItem = $this->getSerializedQueue();

$headersArray = ['Accept' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8'];
$headersArray = array('Accept' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8');

if (array_key_exists('HTTP_USER_AGENT', $_SERVER) == true)
{
Expand All @@ -164,22 +170,22 @@ public function send()

$client = new \GuzzleHttp\Client();

$client->post($this->_endpointUrl, [
$client->post($this->_endpointUrl, array(
'headers' => $headersArray,
'body' => $body,
'verify' => false /* If you want to verify, you can, but you will need to provide proper CA bundle. See http://guzzle.readthedocs.org/en/latest/clients.html#verify-option */
//,'proxy' => '127.0.0.1:8888' /* For Fiddler debugging */
]);
));
}
else if (function_exists('wp_remote_post'))
{
// Used in WordPress
wp_remote_post($this->_endpointUrl, [
wp_remote_post($this->_endpointUrl, array(
'method' => 'POST',
'blocking' => true,
'headers' => $headersArray,
'body' => $body
]);
));
}
}
}
2 changes: 1 addition & 1 deletion ApplicationInsights/Telemetry_Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function trackException(\Exception $ex, $properties = NULL, $measurements
$details->setMessage($ex->getMessage().' in '.$ex->getFile().' on line '.$ex->getLine());
$details->setHasFullStack(true);

$stackFrames = [];
$stackFrames = array();

// First stack frame is in the root exception
$frameCounter = 0;
Expand Down
8 changes: 7 additions & 1 deletion ApplicationInsights/Telemetry_Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function __construct()
$this->_locationContext = new Channel\Contracts\Location();
$this->_operationContext = new Channel\Contracts\Operation();
$this->_sessionContext = new Channel\Contracts\Session();
$this->_properties = [];
$this->_properties = array();

// Initialize user id
$currentUser = new Current_User();
Expand All @@ -74,6 +74,12 @@ function __construct()
// Initialize session id
$currentSession = new Current_Session();
$this->_sessionContext->setId($currentSession->id);

// Initialize client ip
if (array_key_exists('REMOTE_ADDR', $_SERVER) && sizeof(explode('.', $_SERVER['REMOTE_ADDR'])) >= 4)
{
$this->_deviceContext->setIp($_SERVER['REMOTE_ADDR']);
}
}

/**
Expand Down
Loading

0 comments on commit 1922ef7

Please sign in to comment.