11# Copyright (c) Microsoft Corporation. All rights reserved.
22# Licensed under the MIT License.
3+ import logging
34from typing import Any , Optional
45
56from opentelemetry .context import (
4344from azure .monitor .opentelemetry .exporter ._utils import _ticks_since_dot_net_epoch , PeriodicTask
4445
4546
47+ _logger = logging .getLogger (__name__ )
48+
49+
4650_QUICKPULSE_METRIC_TEMPORALITIES = {
4751 # Use DELTA temporalities because we want to reset the counts every collection interval
4852 Counter : AggregationTemporality .DELTA ,
@@ -77,8 +81,8 @@ def __init__(self, connection_string: Optional[str]) -> None:
7781 self ._live_endpoint = parsed_connection_string .live_endpoint
7882 self ._instrumentation_key = parsed_connection_string .instrumentation_key
7983 # TODO: Support AADaudience (scope)/credentials
80-
81- self ._client = QuickpulseClient (host = self ._live_endpoint )
84+ # Pass `None` for now until swagger definition is fixed
85+ self ._client = QuickpulseClient (credential = None , endpoint = self ._live_endpoint ) # type: ignore
8286 # TODO: Support redirect
8387
8488 MetricExporter .__init__ (
@@ -113,10 +117,11 @@ def export(
113117
114118 token = attach (set_value (_SUPPRESS_INSTRUMENTATION_KEY , True ))
115119 try :
116- post_response = self ._client .post ( # type: ignore
120+ post_response = self ._client .publish ( # type: ignore
121+ endpoint = self ._live_endpoint ,
117122 monitoring_data_points = data_points ,
118123 ikey = self ._instrumentation_key ,
119- x_ms_qps_transmission_time = _ticks_since_dot_net_epoch (),
124+ transmission_time = _ticks_since_dot_net_epoch (),
120125 cls = _Response ,
121126 )
122127 if not post_response :
@@ -128,7 +133,7 @@ def export(
128133 # User leaving the live metrics page will be treated as an unsuccessful
129134 result = MetricExportResult .FAILURE
130135 except Exception : # pylint: disable=broad-except,invalid-name
131- # Errors are not reported and assumed as unsuccessful
136+ _logger . exception ( "Exception occurred while publishing live metrics." )
132137 result = MetricExportResult .FAILURE
133138 finally :
134139 detach (token )
@@ -164,20 +169,25 @@ def shutdown(
164169 """
165170
166171
167- def _ping (self , monitoring_data_point ) -> Optional [_Response ]:
172+ def _ping (self , monitoring_data_point : MonitoringDataPoint ) -> Optional [_Response ]:
168173 ping_response = None
169174 token = attach (set_value (_SUPPRESS_INSTRUMENTATION_KEY , True ))
170175 try :
171- ping_response = self ._client .ping ( # type: ignore
176+ ping_response = self ._client .is_subscribed ( # type: ignore
177+ endpoint = self ._live_endpoint ,
172178 monitoring_data_point = monitoring_data_point ,
173179 ikey = self ._instrumentation_key ,
174- x_ms_qps_transmission_time = _ticks_since_dot_net_epoch (),
180+ transmission_time = _ticks_since_dot_net_epoch (),
181+ machine_name = monitoring_data_point .machine_name ,
182+ instance_name = monitoring_data_point .instance ,
183+ stream_id = monitoring_data_point .stream_id ,
184+ role_name = monitoring_data_point .role_name ,
185+ invariant_version = monitoring_data_point .invariant_version ,
175186 cls = _Response ,
176187 )
177188 return ping_response # type: ignore
178189 except HttpResponseError :
179- # Errors are not reported
180- pass
190+ _logger .exception ("Exception occurred while pinging live metrics." )
181191 detach (token )
182192 return ping_response
183193
@@ -208,41 +218,35 @@ def _ticker(self) -> None:
208218 if _is_ping_state ():
209219 # Send a ping if elapsed number of request meets the threshold
210220 if self ._elapsed_num_seconds % _get_global_quickpulse_state ().value == 0 :
211- print ("pinging..." )
212221 ping_response = self ._exporter ._ping ( # pylint: disable=protected-access
213222 self ._base_monitoring_data_point ,
214223 )
215224 if ping_response :
216225 header = ping_response ._response_headers .get ("x-ms-qps-subscribed" ) # pylint: disable=protected-access
217226 if header and header == "true" :
218- print ("ping succeeded: switching to post" )
219227 # Switch state to post if subscribed
220228 _set_global_quickpulse_state (_QuickpulseState .POST_SHORT )
221229 self ._elapsed_num_seconds = 0
222230 else :
223231 # Backoff after _LONG_PING_INTERVAL_SECONDS (60s) of no successful requests
224232 if _get_global_quickpulse_state () is _QuickpulseState .PING_SHORT and \
225233 self ._elapsed_num_seconds >= _LONG_PING_INTERVAL_SECONDS :
226- print ("ping failed for 60s, switching to pinging every 60s" )
227234 _set_global_quickpulse_state (_QuickpulseState .PING_LONG )
228235 # TODO: Implement redirect
229236 else :
230237 # Erroneous ping responses instigate backoff logic
231238 # Backoff after _LONG_PING_INTERVAL_SECONDS (60s) of no successful requests
232239 if _get_global_quickpulse_state () is _QuickpulseState .PING_SHORT and \
233240 self ._elapsed_num_seconds >= _LONG_PING_INTERVAL_SECONDS :
234- print ("ping failed for 60s, switching to pinging every 60s" )
235241 _set_global_quickpulse_state (_QuickpulseState .PING_LONG )
236242 else :
237- print ("posting..." )
238243 try :
239244 self .collect ()
240245 except _UnsuccessfulQuickPulsePostError :
241246 # Unsuccessful posts instigate backoff logic
242247 # Backoff after _POST_CANCEL_INTERVAL_SECONDS (20s) of no successful requests
243248 # And resume pinging
244249 if self ._elapsed_num_seconds >= _POST_CANCEL_INTERVAL_SECONDS :
245- print ("post failed for 20s, switching to pinging" )
246250 _set_global_quickpulse_state (_QuickpulseState .PING_SHORT )
247251 self ._elapsed_num_seconds = 0
248252
0 commit comments