_context = ($context == NULL) ? new Telemetry_Context() : $context; $this->_channel = ($channel == NULL) ? new Channel\Telemetry_Channel() : $channel; } /** * Returns the Telemetry_Context this Telemetry_Client is using. * @return ApplicationInsights\Telemetry_Context */ public function getContext() { return $this->_context; } /** * Returns the Telemetry_Channel this Telemetry_Client is using. * @return ApplicationInsights\Channel\Telemetry_Channel */ public function getChannel() { return $this->_channel; } /** * Sends an Page_View_Data to the Application Insights service. * @param string $name The friendly name of the page view. * @param string $url The url of the page view. * @param int duration The duration in milliseconds of the page view. * @param array $properties An array of name to value pairs. Use the name as the index and any string as the value. * @param array $measurements An array of name to double pairs. Use the name as the index and any double as the value. */ public function trackPageView($name, $url, $duration = 0, $properties = NULL, $measurements = NULL) { $data = new Channel\Contracts\Page_View_Data(); $data->setName($name); $data->setUrl($url); $data->setDuration($duration); if ($properties != NULL) { $data->setProperties($properties); } if ($measurements != NULL) { $data->setMeasurements($measurements); } $this->_channel->addToQueue($data, $this->_context); } /** * Sends an Metric_Data to the Application Insights service. * @param string $name Name of the metric. * @param double $value Value of the metric. * @param \ApplicationInsights\Channel\Data_Point_Type::Value $type The type of value being sent. * @param int $count The number of samples. * @param double $min The minimum of the samples. * @param double $max The maximum of the samples. * @param double $stdDev The standard deviation of the samples. * @param array $properties An array of name to value pairs. Use the name as the index and any string as the value. */ public function trackMetric($name, $value, $type = NULL, $count = NULL, $min = NULL, $max = NULL, $stdDev = NULL, $properties = NULL) { $dataPoiint = new Channel\Contracts\Data_Point(); $dataPoiint->setName($name); $dataPoiint->setValue($value); $dataPoiint->setKind($type == NULL ? Data_Point_Type::Aggregation : $type); $dataPoiint->setCount($count); $dataPoiint->setMin($min); $dataPoiint->setMax($max); $dataPoiint->setStdDev($stdDev); $data = new Channel\Contracts\Metric_Data(); $data->setMetrics(array($dataPoiint)); if ($properties != NULL) { $data->setProperties($properties); } $this->_channel->addToQueue($data, $this->_context); } /** * Sends an Event_Data to the Application Insights service. * @param string $name * @param array $properties An array of name to value pairs. Use the name as the index and any string as the value. * @param array $measurements An array of name to double pairs. Use the name as the index and any double as the value. */ public function trackEvent($name, $properties = NULL, $measurements = NULL) { $data = new Channel\Contracts\Event_Data(); $data->setName($name); if ($properties != NULL) { $data->setProperties($properties); } if ($measurements != NULL) { $data->setMeasurements($measurements); } $this->_channel->addToQueue($data, $this->_context); } /** * Sends an Message_Data to the Application Insights service. * @param string $message The trace message. * @param array $properties An array of name to value pairs. Use the name as the index and any string as the value. */ public function trackMessage($message, $properties = NULL) { $data = new Channel\Contracts\Message_Data(); $data->setMessage($message); if ($properties != NULL) { $data->setProperties($properties); } $this->_channel->addToQueue($data, $this->_context); } /** * Sends a Request_Data to the Application Insights service. * @param string $name A friendly name of the request. * @param string $url The url of the request. * @param int $startTime The timestamp at which the request started. * @param int $durationInMilliseconds The duration, in milliseconds, of the request. * @param int $httpResponseCode The response code of the request. * @param bool $isSuccessful Whether or not the request was successful. * @param array $properties An array of name to value pairs. Use the name as the index and any string as the value. * @param array $measurements An array of name to double pairs. Use the name as the index and any double as the value. */ public function trackRequest($name, $url, $startTime, $durationInMilliseconds = 0, $httpResponseCode = 200, $isSuccessful = true, $properties = NULL, $measurements = NULL ) { $this->endRequest($this->beginRequest($name, $url, $startTime), $durationInMilliseconds, $httpResponseCode, $isSuccessful, $properties, $measurements ); } /** * Begin a Request_Data to the Application Insights service. This request is not sent until an @see endRequest call is made, passing this request. * * @param string $name A friendly name of the request. * @param string $url The url of the request. * @param int $startTime The timestamp at which the request started. * @return an initialized Request_Data, which can be sent by using @see endRequest */ public function beginRequest($name, $url, $startTime ) { $data = new Channel\Contracts\Request_Data(); $data->setId(mt_rand()); $data->setName($name); $data->setUrl($url); $data->setStartTime(Channel\Contracts\Utils::returnISOStringForTime($startTime)); return $data; } /** * Sends a Request_Data created by @see beginRequest to the Application Insights service. * @param int $durationInMilliseconds The duration, in milliseconds, of the request. * @param int $httpResponseCode The response code of the request. * @param bool $isSuccessful Whether or not the request was successful. * @param array $properties An array of name to value pairs. Use the name as the index and any string as the value. * @param array $measurements An array of name to double pairs. Use the name as the index and any double as the value. */ public function endRequest( Channel\Contracts\Request_Data $request, $durationInMilliseconds = 0, $httpResponseCode = 200, $isSuccessful = true, $properties = NULL, $measurements = NULL ) { $request->setResponseCode($httpResponseCode); $request->setSuccess($isSuccessful); $request->setDuration(Channel\Contracts\Utils::convertMillisecondsToTimeSpan($durationInMilliseconds)); if ($properties != NULL) { $request->setProperties($properties); } if ($measurements != NULL) { $request->setMeasurements($measurements); } $this->_channel->addToQueue($request, $this->_context); } /** * Sends an Exception_Data to the Application Insights service. * @param Exception $ex The exception to send * @param array $properties An array of name to value pairs. Use the name as the index and any string as the value. * @param array $measurements An array of name to double pairs. Use the name as the index and any double as the value. */ public function trackException(\Exception $ex, $properties = NULL, $measurements = NULL) { $details = new Channel\Contracts\Exception_Details(); $details->setId(1); $details->setOuterId(0); $details->setTypeName(get_class($ex)); $details->setMessage($ex->getMessage().' in '.$ex->getFile().' on line '.$ex->getLine()); $details->setHasFullStack(true); $stackFrames = array(); // First stack frame is in the root exception $frameCounter = 0; foreach ($ex->getTrace() as $currentFrame) { $stackFrame = new Channel\Contracts\Stack_Frame(); if (array_key_exists('class', $currentFrame) == true) { $stackFrame->setAssembly($currentFrame['class']); } if (array_key_exists('file', $currentFrame) == true) { $stackFrame->setFileName($currentFrame['file']); } if (array_key_exists('line', $currentFrame) == true) { $stackFrame->setLine($currentFrame['line']); } if (array_key_exists('function', $currentFrame) == true) { $stackFrame->setMethod($currentFrame['function']); } // Make it a string to force serialization of zero $stackFrame->setLevel(''.$frameCounter); array_unshift($stackFrames, $stackFrame); $frameCounter++; } $details->setParsedStack($stackFrames); $data = new Channel\Contracts\Exception_Data(); $data->setHandledAt('UserCode'); $data->setExceptions(array($details)); if ($properties != NULL) { $data->setProperties($properties); } if ($measurements != NULL) { $data->setMeasurements($measurements); } $this->_channel->addToQueue($data, $this->_context); } /** * Flushes the underlying Telemetry_Channel queue. */ public function flush() { $this->_channel->send(); } }