@@ -46,6 +46,10 @@ def __init__(self):
4646 # in production.
4747 self .debug_logging = False
4848
49+ # a buffer for service info so we dont' perpetually send the same
50+ # things.
51+ self ._services = {}
52+
4953 def configure (self , enabled = None , hostname = None , port = None , sampler = None ):
5054 """Configure an existing Tracer the easy way.
5155
@@ -169,20 +173,28 @@ def set_service_info(self, service, app, app_type):
169173 :param str app: the off the shelf name of the application (e.g. rails, postgres, custom-app)
170174 :param str app_type: the type of the application (e.g. db, web)
171175 """
172-
173- services = {
174- service : {
175- "app" : app ,
176- "app_type" : app_type ,
177- }
178- }
179-
180- if self .debug_logging :
181- log .debug ("set_service_info: service:%s app:%s type:%s" ,
182- service , app , app_type )
183-
184- if self .enabled and self .writer :
185- self .writer .write (services = services )
176+ try :
177+ # don't bother sending the same services over and over.
178+ info = (service , app , app_type )
179+ if self ._services .get (service , None ) == info :
180+ return
181+ self ._services [service ] = info
182+
183+ if self .debug_logging :
184+ log .debug ("set_service_info: service:%s app:%s type:%s" , service , app , app_type )
185+
186+ # If we had changes, send them to the writer.
187+ if self .enabled and self .writer :
188+
189+ # translate to the form the server understands.
190+ services = {}
191+ for service , app , app_type in self ._services .values ():
192+ services [service ] = {"app" : app , "app_type" : app_type }
193+
194+ # queue them for writes.
195+ self .writer .write (services = services )
196+ except Exception :
197+ log .exception ("error setting service info" )
186198
187199 def wrap (self , name = None , service = None , resource = None , span_type = None ):
188200 """A decorator used to trace an entire function.
0 commit comments