* Add new flag to Telemetry_Channel to set if data should be gzipped before sending, default to false (#52)

* Update readme with example of setting the gzip field
This commit is contained in:
Jon McElroy 2018-09-05 14:03:59 -07:00 коммит произвёл Sergey Kanzhelev
Родитель 75858b7197
Коммит 966cef465b
2 изменённых файлов: 47 добавлений и 5 удалений

Просмотреть файл

@ -24,6 +24,12 @@ class Telemetry_Channel
*/
protected $_client;
/**
* If true, then the data will be gzipped before sending to application insights.
* @var bool
*/
protected $_sendGzipped;
/**
* Initializes a new Telemetry_Channel.
* @param string $endpointUrl Optional. Allows caller to override which endpoint to send data to.
@ -34,6 +40,7 @@ class Telemetry_Channel
$this->_endpointUrl = $endpointUrl;
$this->_queue = array();
$this->_client = $client;
$this->_sendGzipped = false;
if ($client === null && class_exists('\GuzzleHttp\Client') == true) {
// Standard case if properly pulled in composer dependencies
@ -108,6 +115,24 @@ class Telemetry_Channel
return json_encode($queueToEncode);
}
/**
* @return bool
*/
public function getSendGzipped()
{
return $this->_sendGzipped;
}
/**
* @param bool $sendGzipped
*/
public function setSendGzipped($sendGzipped)
{
$this->_sendGzipped = $sendGzipped;
}
/**
* Writes the item into the sending queue for subsequent processing.
* @param \ApplicationInsights\Channel\Contracts\Data_Interface $data The telemetry item to send.
@ -184,6 +209,7 @@ class Telemetry_Channel
public function send($options = array(), $sendAsync = false)
{
$response = null;
$useGuzzle = $this->_client !== null;
if (count($this->_queue) == 0)
{
return;
@ -191,12 +217,23 @@ class Telemetry_Channel
$serializedTelemetryItem = $this->getSerializedQueue();
$headersArray = array('Accept' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8');
if($this->_sendGzipped && $useGuzzle)
{
$headersArray = array(
'Content-Encoding' => 'gzip',
);
$body = gzencode($serializedTelemetryItem);
}
else
{
$headersArray = array(
'Accept' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8'
);
$body = utf8_encode($serializedTelemetryItem);
}
$body = utf8_encode($serializedTelemetryItem);
if ($this->_client !== null)
if ($useGuzzle)
{
$options = array_merge(
array(

Просмотреть файл

@ -137,6 +137,11 @@ catch (\Exception $ex)
```
**Set the Client to gzip the data before sending**
```php
$telemetryClient->getChannel()->setSendGzipped(true);
```
**Registering an exception handler**
```php
class Handle_Exceptions