Skip to content

Commit fa00f0d

Browse files
author
Emanuele Palazzetti
committed
[core] add backward compatibility for pin.service
1 parent 7d06be5 commit fa00f0d

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

ddtrace/pin.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ class Pin(object):
2424
>>> pin = Pin.override(conn, service="user-db")
2525
>>> conn = sqlite.connect("/tmp/image.db")
2626
"""
27-
__slots__ = ['app', 'app_type', 'service', 'tags', 'tracer', '_target', '_config', '_initialized']
27+
__slots__ = ['app', 'app_type', 'tags', 'tracer', '_target', '_config', '_initialized']
2828

2929
def __init__(self, service, app=None, app_type=None, tags=None, tracer=None, _config=None):
3030
tracer = tracer or ddtrace.tracer
31-
self.service = service
3231
self.app = app
3332
self.app_type = app_type
3433
self.tags = tags
@@ -37,8 +36,17 @@ def __init__(self, service, app=None, app_type=None, tags=None, tracer=None, _co
3736
# keep the configuration attribute internal because the
3837
# public API to access it is not the Pin class
3938
self._config = _config or {}
39+
# [Backward compatibility]: service argument updates the `Pin` config
40+
self._config['service_name'] = service
4041
self._initialized = True
4142

43+
@property
44+
def service(self):
45+
"""Backward compatibility: accessing to `pin.service` returns the underlying
46+
configuration value.
47+
"""
48+
return self._config['service_name']
49+
4250
def __setattr__(self, name, value):
4351
if getattr(self, '_initialized', False) and name is not '_target':
4452
raise AttributeError("can't mutate a pin, use override() or clone() instead")

tests/test_instance_config.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest import TestCase
22

3-
from nose.tools import ok_
3+
from nose.tools import eq_, ok_
44

55
from ddtrace import config
66
from ddtrace.pin import Pin
@@ -47,3 +47,33 @@ def test_configuration_override_instance(self):
4747
cfg['distributed_tracing'] = False
4848
ok_(config.get_from(self.Klass)['distributed_tracing'] is True)
4949
ok_(config.get_from(instance)['distributed_tracing'] is False)
50+
51+
def test_service_name_for_pin(self):
52+
# ensure for backward compatibility that changing the service
53+
# name via the Pin object also updates integration config
54+
Pin(service='intake').onto(self.Klass)
55+
instance = self.Klass()
56+
cfg = config.get_from(instance)
57+
eq_(cfg['service_name'], 'intake')
58+
59+
def test_service_attribute_priority(self):
60+
# ensure the `service` arg has highest priority over configuration
61+
# for backward compatibility
62+
global_config = {
63+
'service_name': 'primary_service',
64+
}
65+
Pin(service='service', _config=global_config).onto(self.Klass)
66+
instance = self.Klass()
67+
cfg = config.get_from(instance)
68+
eq_(cfg['service_name'], 'service')
69+
70+
def test_configuration_copy(self):
71+
# ensure when a Pin is created, it copies the given configuration
72+
global_config = {
73+
'service_name': 'service',
74+
}
75+
Pin(service='service', _config=global_config).onto(self.Klass)
76+
instance = self.Klass()
77+
cfg = config.get_from(instance)
78+
cfg['service_name'] = 'metrics'
79+
eq_(global_config['service_name'], 'service')

0 commit comments

Comments
 (0)