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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
import datetime
import traceback
import sys
import uuid
from applicationinsights import channel
NULL_CONSTANT_STRING = 'Null'
class TelemetryClient(object):
"""The telemetry client used for sending all types of telemetry. It serves as the main entry point for
interacting with the Application Insights service.
"""
def __init__(self, instrumentation_key, telemetry_channel=None):
"""Initializes a new instance of the class.
Args:
instrumentation_key (str). the instrumentation key to use for this telemetry client.\n
telemetry_channel (:class:`channel.TelemetryChannel`). the optional telemetry channel to be used instead of
constructing a default one.
"""
if instrumentation_key:
if isinstance(instrumentation_key, channel.TelemetryChannel):
telemetry_channel = instrumentation_key
instrumentation_key = None
else:
raise Exception('Instrumentation key was required but not provided')
self._context = channel.TelemetryContext()
self._context.instrumentation_key = instrumentation_key
self._channel = telemetry_channel or channel.TelemetryChannel()
self._telemetry_processors = []
@property
def context(self):
"""The context associated with this client. All data objects created by this client will be accompanied by
this value.
Returns:
:class:`channel.TelemetryContext`. the context instance.
"""
return self._context
@property
def channel(self):
"""The channel associated with this telemetry client. All data created by this client will be passed along with
the :func:`context` object to :class:`channel.TelemetryChannel`'s :func:`write`.
Returns:
:class:`channel.TelemetryChannel`. the channel instance.
"""
return self._channel
def flush(self):
"""Flushes data in the queue. Data in the queue will be sent either immediately irrespective of what sender is
being used.
"""
self._channel.flush()
def track_pageview(self, name, url, duration=0, properties=None, measurements=None):
"""Send information about the page viewed in the application (a web page for instance).
Args:
name (str). the name of the page that was viewed.\n
url (str). the URL of the page that was viewed.\n
duration (int). the duration of the page view in milliseconds. (defaults to: 0)\n
properties (dict). the set of custom properties the client wants attached to this data item. (defaults to: None)\n
measurements (dict). the set of custom measurements the client wants to attach to this data item. (defaults to: None)
"""
data = channel.contracts.PageViewData()
data.name = name or NULL_CONSTANT_STRING
data.url = url
data.duration = duration
if properties:
data.properties = properties
if measurements:
data.measurements = measurements
self.track(data, self._context)
def track_exception(self, type=None, value=None, tb=None, properties=None, measurements=None):
""" Send information about a single exception that occurred in the application.
Args:
type (Type). the type of the exception that was thrown.\n
value (:class:`Exception`). the exception that the client wants to send.\n
tb (:class:`Traceback`). the traceback information as returned by :func:`sys.exc_info`.\n
properties (dict). the set of custom properties the client wants attached to this data item. (defaults to: None)\n
measurements (dict). the set of custom measurements the client wants to attach to this data item. (defaults to: None)
"""
if not type or not value or not tb:
type, value, tb = sys.exc_info()
if not type or not value or not tb:
try:
raise Exception(NULL_CONSTANT_STRING)
except:
type, value, tb = sys.exc_info()
details = channel.contracts.ExceptionDetails()
details.id = 1
details.outer_id = 0
details.type_name = type.__name__
details.message = str(value)
details.has_full_stack = True
counter = 0
for tb_frame_file, tb_frame_line, tb_frame_function, tb_frame_text in traceback.extract_tb(tb):
frame = channel.contracts.StackFrame()
frame.assembly = 'Unknown'
frame.file_name = tb_frame_file
frame.level = counter
frame.line = tb_frame_line
frame.method = tb_frame_function
details.parsed_stack.append(frame)
counter += 1
details.parsed_stack.reverse()
data = channel.contracts.ExceptionData()
data.handled_at = 'UserCode'
data.exceptions.append(details)
if properties:
data.properties = properties
if measurements:
data.measurements = measurements
self.track(data, self._context)
def track_event(self, name, properties=None, measurements=None):
""" Send information about a single event that has occurred in the context of the application.
Args:
name (str). the data to associate to this event.\n
properties (dict). the set of custom properties the client wants attached to this data item. (defaults to: None)\n
measurements (dict). the set of custom measurements the client wants to attach to this data item. (defaults to: None)
"""
data = channel.contracts.EventData()
data.name = name or NULL_CONSTANT_STRING
if properties:
data.properties = properties
if measurements:
data.measurements = measurements
self.track(data, self._context)
def track_metric(self, name, value, type=None, count=None, min=None, max=None, std_dev=None, properties=None):
"""Send information about a single metric data point that was captured for the application.
Args:
name (str). the name of the metric that was captured.\n
value (float). the value of the metric that was captured.\n
type (:class:`channel.contracts.DataPointType`). the type of the metric. (defaults to: :func:`channel.contracts.DataPointType.aggregation`)\n
count (int). the number of metrics that were aggregated into this data point. (defaults to: None)\n
min (float). the minimum of all metrics collected that were aggregated into this data point. (defaults to: None)\n
max (float). the maximum of all metrics collected that were aggregated into this data point. (defaults to: None)\n
std_dev (float). the standard deviation of all metrics collected that were aggregated into this data point. (defaults to: None)\n
properties (dict). the set of custom properties the client wants attached to this data item. (defaults to: None)
"""
dataPoint = channel.contracts.DataPoint()
dataPoint.name = name or NULL_CONSTANT_STRING
dataPoint.value = value or 0
dataPoint.kind = type or channel.contracts.DataPointType.aggregation
dataPoint.count = count
dataPoint.min = min
dataPoint.max = max
dataPoint.std_dev = std_dev
data = channel.contracts.MetricData()
data.metrics.append(dataPoint)
if properties:
data.properties = properties
self.track(data, self._context)
def track_trace(self, name, properties=None, severity=None):
"""Sends a single trace statement.
Args:
name (str). the trace statement.\n
properties (dict). the set of custom properties the client wants attached to this data item. (defaults to: None)\n
severity (str). the severity level of this trace, one of DEBUG, INFO, WARNING, ERROR, CRITICAL
"""
data = channel.contracts.MessageData()
data.message = name or NULL_CONSTANT_STRING
if properties:
data.properties = properties
if severity is not None:
data.severity_level = channel.contracts.MessageData.PYTHON_LOGGING_LEVELS.get(severity)
self.track(data, self._context)
def track_request(self, name, url, success, start_time=None, duration=None, response_code=None, http_method=None, properties=None, measurements=None, request_id=None):
"""Sends a single request that was captured for the application.
Args:
name (str). the name for this request. All requests with the same name will be grouped together.\n
url (str). the actual URL for this request (to show in individual request instances).\n
success (bool). true if the request ended in success, false otherwise.\n
start_time (str). the start time of the request. The value should look the same as the one returned by :func:`datetime.isoformat()` (defaults to: None)\n
duration (int). the number of milliseconds that this request lasted. (defaults to: None)\n
response_code (str). the response code that this request returned. (defaults to: None)\n
http_method (str). the HTTP method that triggered this request. (defaults to: None)\n
properties (dict). the set of custom properties the client wants attached to this data item. (defaults to: None)\n
measurements (dict). the set of custom measurements the client wants to attach to this data item. (defaults to: None)\n
request_id (str). the id for this request. If None, a new uuid will be generated. (defaults to: None)
"""
data = channel.contracts.RequestData()
data.id = request_id or str(uuid.uuid4())
data.name = name
data.url = url
data.success = success
data.start_time = start_time or datetime.datetime.utcnow().isoformat() + 'Z'
data.duration = self.__ms_to_duration(duration)
data.response_code = str(response_code) or '200'
data.http_method = http_method or 'GET'
if properties:
data.properties = properties
if measurements:
data.measurements = measurements
self.track(data, self._context)
def track_dependency(self, name, data, type=None, target=None, duration=None, success=None, result_code=None, properties=None, measurements=None, dependency_id=None):
"""Sends a single dependency telemetry that was captured for the application.
Args:
name (str). the name of the command initiated with this dependency call. Low cardinality value. Examples are stored procedure name and URL path template.\n
data (str). the command initiated by this dependency call. Examples are SQL statement and HTTP URL with all query parameters.\n
type (str). the dependency type name. Low cardinality value for logical grouping of dependencies and interpretation of other fields like commandName and resultCode. Examples are SQL, Azure table, and HTTP. (default to: None)\n
target (str). the target site of a dependency call. Examples are server name, host address. (default to: None)\n
duration (int). the number of milliseconds that this dependency call lasted. (defaults to: None)\n
success (bool). true if the dependency call ended in success, false otherwise. (defaults to: None)\n
result_code (str). the result code of a dependency call. Examples are SQL error code and HTTP status code. (defaults to: None)\n
properties (dict). the set of custom properties the client wants attached to this data item. (defaults to: None)\n
measurements (dict). the set of custom measurements the client wants to attach to this data item. (defaults to: None)\n
id (str). the id for this dependency call. If None, a new uuid will be generated. (defaults to: None)
"""
dependency_data = channel.contracts.RemoteDependencyData()
dependency_data.id = dependency_id or str(uuid.uuid4())
dependency_data.name = name
dependency_data.data = data
dependency_data.type = type
dependency_data.target = target
dependency_data.duration = self.__ms_to_duration(duration)
dependency_data.success = success
dependency_data.result_code = str(result_code) or '200'
if properties:
dependency_data.properties = properties
if measurements:
dependency_data.measurements = measurements
self.track(dependency_data, self._context)
def track(self, data, context):
if self.run_telemetry_processors(data, context):
self.channel.write(data, context)
def add_telemetry_processor(self, telemetry_processor):
"""Adds telemetry processor to the collection. Telemetry processors will be called one by one
before telemetry item is pushed for sending and in the order they were added.
Args:
telemetry_processor (function). Takes a telemetry item, and context object and returns boolean
that determines if the event is passed to the server (False = Filtered)
"""
if telemetry_processor is None:
raise TypeError('telemetry_processor cannot be None.')
self._telemetry_processors.insert(0, telemetry_processor)
def run_telemetry_processors(self, data, context):
allow_data_through = True
try:
for processor in self._telemetry_processors:
if processor(data, context) == False:
allow_data_through = False
break
except:
allow_data_through = True
return allow_data_through
@staticmethod
def __ms_to_duration(duration_ms):
local_duration = duration_ms or 0
duration_parts = []
for multiplier in [1000, 60, 60, 24]:
duration_parts.append(local_duration % multiplier)
local_duration //= multiplier
duration_parts.reverse()
duration = '%02d:%02d:%02d.%03d' % (duration_parts[0], duration_parts[1], duration_parts[2], duration_parts[3])
if local_duration:
duration = '%d.%s' % (local_duration, duration)
return duration
|