1616import json
1717import requests
1818
19- from opencensus .common .transports .async_ import AsyncTransport
20- from opencensus .common .schedule import PeriodicTask
2119from opencensus .ext .azure .common import Options
2220from opencensus .ext .azure .common import utils
21+ from opencensus .ext .azure .common .exporter import BaseExporter
2322from opencensus .ext .azure .common .protocol import Data
2423from opencensus .ext .azure .common .protocol import Envelope
2524from opencensus .ext .azure .common .protocol import RemoteDependency
2625from opencensus .ext .azure .common .protocol import Request
2726from opencensus .ext .azure .common .storage import LocalFileStorage
28- from opencensus .trace import base_exporter
2927from opencensus .trace import execution_context
3028from opencensus .trace .span import SpanKind
3129
3432__all__ = ['AzureExporter' ]
3533
3634
37- class AzureExporter (base_exporter . Exporter ):
35+ class AzureExporter (BaseExporter ):
3836 """An exporter that sends traces to Microsoft Azure Monitor.
3937
4038 :type options: dict
@@ -51,17 +49,7 @@ def __init__(self, **options):
5149 maintenance_period = self .options .storage_maintenance_period ,
5250 retention_period = self .options .storage_retention_period ,
5351 )
54- self .transport = AsyncTransport (
55- self ,
56- max_batch_size = 100 ,
57- wait_period = self .options .export_interval ,
58- )
59- self ._transmission_task = PeriodicTask (
60- interval = self .options .storage_maintenance_period ,
61- function = self ._transmission_routine ,
62- )
63- self ._transmission_task .daemon = True
64- self ._transmission_task .start ()
52+ super (AzureExporter , self ).__init__ (** options )
6553
6654 def span_data_to_envelope (self , sd ):
6755 envelope = Envelope (
@@ -124,7 +112,7 @@ def span_data_to_envelope(self, sd):
124112 # TODO: links, tracestate, tags, attrs
125113 return envelope
126114
127- def _transmission_routine (self ):
115+ def _transmit_from_storage (self ):
128116 for blob in self .storage .gets ():
129117 if blob .lease (self .options .timeout + 5 ):
130118 envelopes = blob .get () # TODO: handle error
@@ -142,8 +130,6 @@ def _transmit(self, envelopes):
142130 Return the next retry time in seconds for retryable failure.
143131 This function should never throw exception.
144132 """
145- if not envelopes :
146- return 0
147133 # TODO: prevent requests being tracked
148134 blacklist_hostnames = execution_context .get_opencensus_attr (
149135 'blacklist_hostnames' ,
@@ -236,23 +222,23 @@ def _transmit(self, envelopes):
236222 # server side error (non-retryable)
237223 return - response .status_code
238224
239- def emit (self , span_datas ):
240- """
241- :type span_datas: list of :class:
242- `~opencensus.trace.span_data.SpanData`
243- :param list of opencensus.trace.span_data.SpanData span_datas:
244- SpanData tuples to emit
245- """
246- envelopes = [self .span_data_to_envelope (sd ) for sd in span_datas ]
247- result = self ._transmit (envelopes )
248- if result > 0 :
249- self .storage .put (envelopes , result )
225+ def emit (self , batch , event = None ):
226+ try :
227+ if batch :
228+ envelopes = [self .span_data_to_envelope (sd ) for sd in batch ]
229+ result = self ._transmit (envelopes )
230+ if result > 0 :
231+ self .storage .put (envelopes , result )
232+ if event :
233+ if event is self .EXIT_EVENT :
234+ self ._transmit_from_storage () # send files before exit
235+ event .set ()
236+ return
237+ if len (batch ) < self .options .max_batch_size :
238+ self ._transmit_from_storage ()
239+ except Exception as ex :
240+ logger .exception ('Transmission exception: %s.' , ex )
250241
251- def export (self , span_datas ):
252- """
253- :type span_datas: list of :class:
254- `~opencensus.trace.span_data.SpanData`
255- :param list of opencensus.trace.span_data.SpanData span_datas:
256- SpanData tuples to export
257- """
258- self .transport .export (span_datas )
242+ def _stop (self , timeout = None ):
243+ self .storage .close ()
244+ return self ._worker .stop (timeout )
0 commit comments