This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Telemetry_Channel.php
233 lines (205 loc) · 7.16 KB
/
Telemetry_Channel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
namespace ApplicationInsights\Channel;
/**
* Responsible for managing a queue of telemetry items to send and sending them.
*/
class Telemetry_Channel
{
/**
* The endpoint URL to send data to.
* @var string
*/
protected $_endpointUrl;
/**
* The queue of already serialized JSON objects to send.
* @var array
*/
protected $_queue;
/**
* Client that is used to call out to the endpoint.
* @var \GuzzleHttp\Client
*/
protected $_client;
/**
* Initializes a new Telemetry_Channel.
* @param string $endpointUrl Optional. Allows caller to override which endpoint to send data to.
* @param \GuzzleHttp\Client|null $client - guzzle client if it exists
*/
function __construct($endpointUrl = 'https://dc.services.visualstudio.com/v2/track', $client = null)
{
$this->_endpointUrl = $endpointUrl;
$this->_queue = array();
$this->_client = $client;
if ($client === null && class_exists('\GuzzleHttp\Client') == true) {
// Standard case if properly pulled in composer dependencies
$this->_client = new \GuzzleHttp\Client();
}
}
/**
* Returns the current URL this TelemetrySender will send to.
* @return string
*/
public function getEndpointUrl()
{
return $this->_endpointUrl;
}
/**
* Sets the current URL this TelemetrySender will send to.
* @param string $endpointUrl
*/
public function setEndpointUrl($endpointUrl)
{
$this->_endpointUrl = $endpointUrl;
}
/**
* Returns the current queue.
* @return array
*/
public function getQueue()
{
return $this->_queue;
}
/**
* Sets the current queue.
* @param array $queue
*/
public function setQueue($queue)
{
$this->_queue = $queue;
}
/**
* @return \GuzzleHttp\Client
*/
public function GetClient()
{
return $this->_client;
}
/**
* @param \GuzzleHttp\Client $client
*/
public function SetClient(\GuzzleHttp\Client $client)
{
$this->_client = $client;
}
/**
* Summary of getSerializedQueue
* @return string JSON representation of queue.
*/
public function getSerializedQueue()
{
$queueToEncode = array();
foreach ($this->_queue as $dataItem)
{
array_push($queueToEncode, Contracts\Utils::getUnderlyingData($dataItem->jsonSerialize()));
}
return json_encode($queueToEncode);
}
/**
* Writes the item into the sending queue for subsequent processing.
* @param \ApplicationInsights\Channel\Contracts\Data_Interface $data The telemetry item to send.
* @param \ApplicationInsights\Telemetry_Context $telemetryContext The context to use.
*/
public function addToQueue(
\ApplicationInsights\Channel\Contracts\Data_Interface $data,
\ApplicationInsights\Telemetry_Context $telemetryContext,
$startTime = null)
{
// If no data or context provided, we just return to not cause upstream issues as a result of telemetry
if ($data == NULL || $telemetryContext == NULL)
{
return;
}
$envelope = new Contracts\Envelope();
// Main envelope properties
$envelope->setName($data->getEnvelopeTypeName());
if ($startTime == NULL)
{
$startTime = $data->getTime();
}
$envelope->setTime(Contracts\Utils::returnISOStringForTime($startTime));
// The instrumentation key to use
$envelope->setInstrumentationKey($telemetryContext->getInstrumentationKey());
// Copy all context into the Tags array
$envelope->setTags(array_merge($telemetryContext->getApplicationContext()->jsonSerialize(),
$telemetryContext->getDeviceContext()->jsonSerialize(),
$telemetryContext->getCloudContext()->jsonSerialize(),
$telemetryContext->getLocationContext()->jsonSerialize(),
$telemetryContext->getOperationContext()->jsonSerialize(),
$telemetryContext->getSessionContext()->jsonSerialize(),
$telemetryContext->getUserContext()->jsonSerialize(),
$telemetryContext->getInternalContext()->jsonSerialize()));
// Merge properties from global context to local context
$contextProperties = $telemetryContext->getProperties();
if (method_exists($data, 'getProperties') == true && $contextProperties != NULL && count($contextProperties) > 0)
{
$dataProperties = $data->getProperties();
if ($dataProperties == NULL)
{
$dataProperties = array();
}
foreach ($contextProperties as $key => $value)
{
if (array_key_exists($key, $dataProperties) == false)
{
$dataProperties[$key] = $value;
}
}
$data->setProperties($dataProperties);
}
// Embed the main data object
$envelope->setData(new Contracts\Data());
$envelope->getData()->setBaseType($data->getDataTypeName());
$envelope->getData()->setBaseData($data);
array_push($this->_queue, $envelope);
}
/**
* Summary of send
* @param array $options
* @param bool $sendAsync
* @return \GuzzleHttp\Promise\PromiseInterface|\Psr\Http\Message\ResponseInterface|null|WP_Error
*/
public function send($options = array(), $sendAsync = false)
{
$response = null;
if (count($this->_queue) == 0)
{
return;
}
$serializedTelemetryItem = $this->getSerializedQueue();
$headersArray = array('Accept' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8');
$body = utf8_encode($serializedTelemetryItem);
if ($this->_client !== null)
{
$options = array_merge(
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 */
),
$options
);
if ($sendAsync && method_exists($this->_client, 'sendAsync'))
{
$response = $this->_client->postAsync($this->_endpointUrl, $options);
}
else
{
$response = $this->_client->post($this->_endpointUrl, $options);
}
}
else if (function_exists('wp_remote_post'))
{
// Used in WordPress
$response = wp_remote_post($this->_endpointUrl, array(
'method' => 'POST',
'blocking' => !$sendAsync,
'headers' => $headersArray,
'body' => $body
));
}
return $response;
}
}