|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | +import sys |
| 4 | +import traceback |
| 5 | +from applicationinsights import TelemetryClient |
| 6 | +from botbuilder.core.bot_telemetry_client import BotTelemetryClient, TelemetryDataPointType |
| 7 | +from typing import Dict |
| 8 | + |
| 9 | +class AppinsightsBotTelemetryClient(BotTelemetryClient): |
| 10 | + |
| 11 | + def __init__(self, instrumentation_key:str): |
| 12 | + self._instrumentation_key = instrumentation_key |
| 13 | + |
| 14 | + self._context = TelemetryContext() |
| 15 | + context.instrumentation_key = self._instrumentation_key |
| 16 | + # context.user.id = 'BOTID' # telemetry_channel.context.session. |
| 17 | + # context.session.id = 'BOTSESSION' |
| 18 | + |
| 19 | + # set up channel with context |
| 20 | + self._channel = TelemetryChannel(context) |
| 21 | + # telemetry_channel.context.properties['my_property'] = 'my_value' |
| 22 | + |
| 23 | + self._client = TelemetryClient(self._instrumentation_key, self._channel) |
| 24 | + |
| 25 | + |
| 26 | + def track_pageview(self, name: str, url:str, duration: int = 0, properties : Dict[str, object]=None, |
| 27 | + measurements: Dict[str, object]=None) -> None: |
| 28 | + """ |
| 29 | + Send information about the page viewed in the application (a web page for instance). |
| 30 | + :param name: the name of the page that was viewed. |
| 31 | + :param url: the URL of the page that was viewed. |
| 32 | + :param duration: the duration of the page view in milliseconds. (defaults to: 0) |
| 33 | + :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None) |
| 34 | + :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None) |
| 35 | + """ |
| 36 | + self._client.track_pageview(name, url, duration, properties, measurements) |
| 37 | + |
| 38 | + def track_exception(self, type_exception: type = None, value : Exception =None, tb : traceback =None, |
| 39 | + properties: Dict[str, object]=None, measurements: Dict[str, object]=None) -> None: |
| 40 | + """ |
| 41 | + Send information about a single exception that occurred in the application. |
| 42 | + :param type_exception: the type of the exception that was thrown. |
| 43 | + :param value: the exception that the client wants to send. |
| 44 | + :param tb: the traceback information as returned by :func:`sys.exc_info`. |
| 45 | + :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None) |
| 46 | + :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None) |
| 47 | + """ |
| 48 | + self._client.track_exception(type_exception, value, tb, properties, measurements) |
| 49 | + |
| 50 | + def track_event(self, name: str, properties: Dict[str, object] = None, |
| 51 | + measurements: Dict[str, object] = None) -> None: |
| 52 | + """ |
| 53 | + Send information about a single event that has occurred in the context of the application. |
| 54 | + :param name: the data to associate to this event. |
| 55 | + :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None) |
| 56 | + :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None) |
| 57 | + """ |
| 58 | + self._client.track_event(name, properties, measurements) |
| 59 | + |
| 60 | + def track_metric(self, name: str, value: float, type: TelemetryDataPointType =None, |
| 61 | + count: int =None, min: float=None, max: float=None, std_dev: float=None, |
| 62 | + properties: Dict[str, object]=None) -> NotImplemented: |
| 63 | + """ |
| 64 | + Send information about a single metric data point that was captured for the application. |
| 65 | + :param name: The name of the metric that was captured. |
| 66 | + :param value: The value of the metric that was captured. |
| 67 | + :param type: The type of the metric. (defaults to: TelemetryDataPointType.aggregation`) |
| 68 | + :param count: the number of metrics that were aggregated into this data point. (defaults to: None) |
| 69 | + :param min: the minimum of all metrics collected that were aggregated into this data point. (defaults to: None) |
| 70 | + :param max: the maximum of all metrics collected that were aggregated into this data point. (defaults to: None) |
| 71 | + :param std_dev: the standard deviation of all metrics collected that were aggregated into this data point. (defaults to: None) |
| 72 | + :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None) |
| 73 | + """ |
| 74 | + self._client.track_metric(name, value, type, count, min, max, std_dev, properties) |
| 75 | + |
| 76 | + def track_trace(self, name: str, properties: Dict[str, object]=None, severity=None): |
| 77 | + """ |
| 78 | + Sends a single trace statement. |
| 79 | + :param name: the trace statement.\n |
| 80 | + :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None)\n |
| 81 | + :param severity: the severity level of this trace, one of DEBUG, INFO, WARNING, ERROR, CRITICAL |
| 82 | + """ |
| 83 | + self._client.track_trace(name, properties, severity) |
| 84 | + |
| 85 | + def track_request(self, name: str, url: str, success: bool, start_time: str=None, |
| 86 | + duration: int=None, response_code: str =None, http_method: str=None, |
| 87 | + properties: Dict[str, object]=None, measurements: Dict[str, object]=None, |
| 88 | + request_id: str=None): |
| 89 | + """ |
| 90 | + Sends a single request that was captured for the application. |
| 91 | + :param name: The name for this request. All requests with the same name will be grouped together. |
| 92 | + :param url: The actual URL for this request (to show in individual request instances). |
| 93 | + :param success: True if the request ended in success, False otherwise. |
| 94 | + :param start_time: the start time of the request. The value should look the same as the one returned by :func:`datetime.isoformat()` (defaults to: None) |
| 95 | + :param duration: the number of milliseconds that this request lasted. (defaults to: None) |
| 96 | + :param response_code: the response code that this request returned. (defaults to: None) |
| 97 | + :param http_method: the HTTP method that triggered this request. (defaults to: None) |
| 98 | + :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None) |
| 99 | + :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None) |
| 100 | + :param request_id: the id for this request. If None, a new uuid will be generated. (defaults to: None) |
| 101 | + """ |
| 102 | + self._client.track_request(name, url, success, start_time, duration, response_code, http_method, properties, |
| 103 | + measurements, request_id) |
| 104 | + |
| 105 | + def track_dependency(self, name:str, data:str, type:str=None, target:str=None, duration:int=None, |
| 106 | + success:bool=None, result_code:str=None, properties:Dict[str, object]=None, |
| 107 | + measurements:Dict[str, object]=None, dependency_id:str=None): |
| 108 | + """ |
| 109 | + Sends a single dependency telemetry that was captured for the application. |
| 110 | + :param name: the name of the command initiated with this dependency call. Low cardinality value. Examples are stored procedure name and URL path template. |
| 111 | + :param data: the command initiated by this dependency call. Examples are SQL statement and HTTP URL with all query parameters. |
| 112 | + :param type: 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) |
| 113 | + :param target: the target site of a dependency call. Examples are server name, host address. (default to: None) |
| 114 | + :param duration: the number of milliseconds that this dependency call lasted. (defaults to: None) |
| 115 | + :param success: true if the dependency call ended in success, false otherwise. (defaults to: None) |
| 116 | + :param result_code: the result code of a dependency call. Examples are SQL error code and HTTP status code. (defaults to: None) |
| 117 | + :param properties: the set of custom properties the client wants attached to this data item. (defaults to: None) |
| 118 | + :param measurements: the set of custom measurements the client wants to attach to this data item. (defaults to: None) |
| 119 | + :param id: the id for this dependency call. If None, a new uuid will be generated. (defaults to: None) |
| 120 | + """ |
| 121 | + self._client.track_dependency(name, data, type, target, duration, success, result_code, properties, |
| 122 | + measurements, dependency_id) |
| 123 | + |
0 commit comments