@@ -69,6 +69,8 @@ def __init__(self):
6969 self ._runtime_id = generate_runtime_id ()
7070 self ._runtime_worker = None
7171 self ._dogstatsd_client = None
72+ self ._dogstatsd_host = self .DEFAULT_HOSTNAME
73+ self ._dogstatsd_port = self .DEFAULT_DOGSTATSD_PORT
7274
7375 def get_call_context (self , * args , ** kwargs ):
7476 """
@@ -154,12 +156,11 @@ def configure(self, enabled=None, hostname=None, port=None, dogstatsd_host=None,
154156 self ._wrap_executor = wrap_executor
155157
156158 if collect_metrics and self ._runtime_worker is None :
159+ self ._dogstatsd_host = dogstatsd_host or self ._dogstatsd_host
160+ self ._dogstatsd_port = dogstatsd_port or self ._dogstatsd_port
157161 # start dogstatsd client if not already running
158162 if not self ._dogstatsd_client :
159- self ._start_dogstatsd_client (
160- dogstatsd_host or self .DEFAULT_HOSTNAME ,
161- dogstatsd_port or self .DEFAULT_DOGSTATSD_PORT ,
162- )
163+ self ._start_dogstatsd_client ()
163164
164165 self ._start_runtime_worker ()
165166
@@ -271,18 +272,18 @@ def start_span(self, name, child_of=None, service=None, resource=None, span_type
271272 # add it to the current context
272273 context .add_span (span )
273274
275+ # check for new process if runtime metrics worker has already been started
276+ if self ._runtime_worker :
277+ self ._check_new_process ()
278+
274279 # update set of services handled by tracer
275- if service :
280+ if service and service not in self . _services :
276281 self ._services .add (service )
277282
278283 # The constant tags for the dogstatsd client needs to updated with any new
279284 # service(s) that may have been added.
280285 self ._update_dogstatsd_constant_tags ()
281286
282- # check for new process if runtime metrics worker has already been started
283- if self ._runtime_worker :
284- self ._check_new_process ()
285-
286287 return span
287288
288289 def _update_dogstatsd_constant_tags (self ):
@@ -299,12 +300,15 @@ def _update_dogstatsd_constant_tags(self):
299300 log .debug ('Updating constant tags {}' .format (tags ))
300301 self ._dogstatsd_client .constant_tags = tags
301302
302- def _start_dogstatsd_client (self , host , port ):
303+ def _start_dogstatsd_client (self ):
303304 # start dogstatsd as client with constant tags
304- log .debug ('Starting DogStatsd on {}:{}' .format (host , port ))
305+ log .debug ('Connecting to DogStatsd on {}:{}' .format (
306+ self ._dogstatsd_host ,
307+ self ._dogstatsd_port
308+ ))
305309 self ._dogstatsd_client = DogStatsd (
306- host = host ,
307- port = port ,
310+ host = self . _dogstatsd_host ,
311+ port = self . _dogstatsd_port ,
308312 )
309313
310314 def _start_runtime_worker (self ):
@@ -330,6 +334,10 @@ def _check_new_process(self):
330334
331335 self ._start_runtime_worker ()
332336
337+ # force an immediate update constant tags since we have reset services
338+ # and generated a new runtime id
339+ self ._update_dogstatsd_constant_tags ()
340+
333341 def trace (self , name , service = None , resource = None , span_type = None ):
334342 """
335343 Return a span that will trace an operation called `name`. The context that created
0 commit comments