本文整理汇总了Python中baseplate.Baseplate.register方法的典型用法代码示例。如果您正苦于以下问题:Python Baseplate.register方法的具体用法?Python Baseplate.register怎么用?Python Baseplate.register使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类baseplate.Baseplate
的用法示例。
在下文中一共展示了Baseplate.register方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TracingTests
# 需要导入模块: from baseplate import Baseplate [as 别名]
# 或者: from baseplate.Baseplate import register [as 别名]
class TracingTests(unittest.TestCase):
def _register_mock(self, context, server_span):
server_span_observer = TraceServerSpanObserver('test-service',
'test-hostname',
server_span,
NullRecorder())
server_span.register(server_span_observer)
self.server_span_observer = server_span_observer
def setUp(self):
configurator = Configurator()
configurator.add_route("example", "/example", request_method="GET")
configurator.add_view(
example_application, route_name="example", renderer="json")
self.observer = TraceBaseplateObserver('test-service')
self.baseplate = Baseplate()
self.baseplate.register(self.observer)
self.baseplate_configurator = BaseplateConfigurator(
self.baseplate,
trust_trace_headers=True,
)
configurator.include(self.baseplate_configurator.includeme)
app = configurator.make_wsgi_app()
self.test_app = webtest.TestApp(app)
def test_trace_on_inbound_request(self):
with mock.patch.object(TraceBaseplateObserver, 'on_server_span_created',
side_effect=self._register_mock) as mocked:
self.test_app.get('/example')
span = self.server_span_observer._serialize()
self.assertEqual(span['name'], 'example')
self.assertEqual(len(span['annotations']), 2)
self.assertEqual(span['parentId'], 0)
示例2: ConfiguratorTests
# 需要导入模块: from baseplate import Baseplate [as 别名]
# 或者: from baseplate.Baseplate import register [as 别名]
class ConfiguratorTests(unittest.TestCase):
def setUp(self):
configurator = Configurator()
configurator.add_route("example", "/example", request_method="GET")
configurator.add_view(
example_application, route_name="example", renderer="json")
self.observer = mock.Mock(spec=BaseplateObserver)
self.baseplate = Baseplate()
self.baseplate.register(self.observer)
self.baseplate_configurator = BaseplateConfigurator(
self.baseplate,
trust_trace_headers=True,
)
configurator.include(self.baseplate_configurator.includeme)
app = configurator.make_wsgi_app()
self.test_app = webtest.TestApp(app)
def test_no_trace_headers(self):
self.test_app.get("/example")
self.assertEqual(self.observer.on_root_span_created.call_count, 1)
context, root_span = self.observer.on_root_span_created.call_args[0]
self.assertIsInstance(context, Request)
self.assertEqual(root_span.trace_id, "no-trace")
self.assertEqual(root_span.parent_id, "no-parent")
self.assertEqual(root_span.id, "no-span")
mock_root_observer = self.observer.on_root_span_created.return_value
self.assertTrue(mock_root_observer.on_start.called)
self.assertTrue(mock_root_observer.on_stop.called)
def test_trace_headers(self):
self.test_app.get("/example", headers={
"X-Trace": "1234",
"X-Parent": "2345",
"X-Span": "3456",
})
self.assertEqual(self.observer.on_root_span_created.call_count, 1)
context, root_span = self.observer.on_root_span_created.call_args[0]
self.assertIsInstance(context, Request)
self.assertEqual(root_span.trace_id, "1234")
self.assertEqual(root_span.parent_id, "2345")
self.assertEqual(root_span.id, "3456")
mock_root_observer = self.observer.on_root_span_created.return_value
self.assertTrue(mock_root_observer.on_start.called)
self.assertTrue(mock_root_observer.on_stop.called)
def test_not_found(self):
self.test_app.get("/nope", status=404)
self.assertFalse(self.observer.on_root_span_created.called)
@mock.patch("random.getrandbits")
def test_distrust_headers(self, getrandbits):
self.baseplate_configurator.trust_trace_headers = False
self.test_app.get("/example", headers={
"X-Trace": "1234",
"X-Parent": "2345",
"X-Span": "3456",
})
context, root_span = self.observer.on_root_span_created.call_args[0]
self.assertEqual(root_span.trace_id, getrandbits.return_value)
self.assertEqual(root_span.parent_id, None)
self.assertEqual(root_span.id, getrandbits.return_value)
示例3: ConfiguratorTests
# 需要导入模块: from baseplate import Baseplate [as 别名]
# 或者: from baseplate.Baseplate import register [as 别名]
class ConfiguratorTests(unittest.TestCase):
def setUp(self):
configurator = Configurator()
configurator.add_route("example", "/example", request_method="GET")
configurator.add_route("trace_context", "/trace_context", request_method="GET")
configurator.add_view(
example_application, route_name="example", renderer="json")
configurator.add_view(
local_tracing_within_context, route_name="trace_context", renderer="json")
self.observer = mock.Mock(spec=BaseplateObserver)
self.server_observer = mock.Mock(spec=ServerSpanObserver)
def _register_mock(context, server_span):
server_span.register(self.server_observer)
self.observer.on_server_span_created.side_effect = _register_mock
self.baseplate = Baseplate()
self.baseplate.register(self.observer)
self.baseplate_configurator = BaseplateConfigurator(
self.baseplate,
trust_trace_headers=True,
)
configurator.include(self.baseplate_configurator.includeme)
app = configurator.make_wsgi_app()
self.test_app = webtest.TestApp(app)
@mock.patch("random.getrandbits")
def test_no_trace_headers(self, getrandbits):
getrandbits.return_value = 1234
self.test_app.get("/example")
self.assertEqual(self.observer.on_server_span_created.call_count, 1)
context, server_span = self.observer.on_server_span_created.call_args[0]
self.assertEqual(server_span.trace_id, 1234)
self.assertEqual(server_span.parent_id, None)
self.assertEqual(server_span.id, 1234)
self.assertTrue(self.server_observer.on_start.called)
self.assertTrue(self.server_observer.on_finish.called)
def test_trace_headers(self):
self.test_app.get("/example", headers={
"X-Trace": "1234",
"X-Parent": "2345",
"X-Span": "3456",
"X-Sampled": "1",
"X-Flags": "1",
})
self.assertEqual(self.observer.on_server_span_created.call_count, 1)
context, server_span = self.observer.on_server_span_created.call_args[0]
self.assertEqual(server_span.trace_id, 1234)
self.assertEqual(server_span.parent_id, 2345)
self.assertEqual(server_span.id, 3456)
self.assertEqual(server_span.sampled, True)
self.assertEqual(server_span.flags, 1)
self.assertTrue(self.server_observer.on_start.called)
self.assertTrue(self.server_observer.on_finish.called)
def test_not_found(self):
self.test_app.get("/nope", status=404)
self.assertFalse(self.observer.on_server_span_created.called)
def test_exception_caught(self):
with self.assertRaises(TestException):
self.test_app.get("/example?error")
self.assertTrue(self.server_observer.on_start.called)
self.assertTrue(self.server_observer.on_finish.called)
_, captured_exc, _ = self.server_observer.on_finish.call_args[0][0]
self.assertIsInstance(captured_exc, TestException)
@mock.patch("random.getrandbits")
def test_distrust_headers(self, getrandbits):
getrandbits.return_value = 1234
self.baseplate_configurator.trust_trace_headers = False
self.test_app.get("/example", headers={
"X-Trace": "1234",
"X-Parent": "2345",
"X-Span": "3456",
})
context, server_span = self.observer.on_server_span_created.call_args[0]
self.assertEqual(server_span.trace_id, getrandbits.return_value)
self.assertEqual(server_span.parent_id, None)
self.assertEqual(server_span.id, getrandbits.return_value)
def test_local_trace_in_context(self):
self.test_app.get('/trace_context')
self.assertEqual(self.server_observer.on_child_span_created.call_count, 1)
child_span = self.server_observer.on_child_span_created.call_args[0][0]
context, server_span = self.observer.on_server_span_created.call_args[0]
self.assertNotEqual(child_span.context, context)
示例4: Globals
# 需要导入模块: from baseplate import Baseplate [as 别名]
# 或者: from baseplate.Baseplate import register [as 别名]
#.........这里部分代码省略.........
The same ``kw`` dictionary used throughout
``config/middleware.py`` namely, the variables from the
section in the config file for your application.
``extra``
The configuration returned from ``load_config`` in
``config/middleware.py`` which may be of use in the setup of
your global variables.
"""
global_conf.setdefault("debug", False)
# reloading site ensures that we have a fresh sys.path to build our
# working set off of. this means that forked worker processes won't get
# the sys.path that was current when the master process was spawned
# meaning that new plugins will be picked up on regular app reload
# rather than having to restart the master process as well.
reload(site)
self.pkg_resources_working_set = pkg_resources.WorkingSet()
self.config = ConfigValueParser(global_conf)
self.config.add_spec(self.spec)
self.plugins = PluginLoader(self.pkg_resources_working_set,
self.config.get("plugins", []))
self.stats = Stats(self.config.get('statsd_addr'),
self.config.get('statsd_sample_rate'))
self.startup_timer = self.stats.get_timer("app_startup")
self.startup_timer.start()
self.baseplate = Baseplate()
self.baseplate.configure_logging()
self.baseplate.register(R2BaseplateObserver())
self.paths = paths
self.running_as_script = global_conf.get('running_as_script', False)
# turn on for language support
self.lang = getattr(self, 'site_lang', 'en')
self.languages, self.lang_name = get_active_langs(
config, default_lang=self.lang)
all_languages = self.lang_name.keys()
all_languages.sort()
self.all_languages = all_languages
# set default time zone if one is not set
tz = global_conf.get('timezone', 'UTC')
self.tz = pytz.timezone(tz)
dtz = global_conf.get('display_timezone', tz)
self.display_tz = pytz.timezone(dtz)
self.startup_timer.intermediate("init")
def __getattr__(self, name):
if not name.startswith('_') and name in self.config:
return self.config[name]
else:
raise AttributeError("g has no attr %r" % name)
def setup(self):
self.env = ''
if (
示例5: TracingTests
# 需要导入模块: from baseplate import Baseplate [as 别名]
# 或者: from baseplate.Baseplate import register [as 别名]
class TracingTests(unittest.TestCase):
def _register_mock(self, context, server_span):
server_span_observer = TraceServerSpanObserver('test-service',
'test-hostname',
server_span,
NullRecorder())
server_span.register(server_span_observer)
self.server_span_observer = server_span_observer
def setUp(self):
configurator = Configurator()
configurator.add_route("example", "/example", request_method="GET")
configurator.add_view(
example_application, route_name="example", renderer="json")
self.client = make_client("test-service")
self.observer = TraceBaseplateObserver(self.client)
self.baseplate = Baseplate()
self.baseplate.register(self.observer)
self.baseplate_configurator = BaseplateConfigurator(
self.baseplate,
trust_trace_headers=True,
)
configurator.include(self.baseplate_configurator.includeme)
app = configurator.make_wsgi_app()
self.test_app = webtest.TestApp(app)
def test_trace_on_inbound_request(self):
with mock.patch.object(TraceBaseplateObserver, 'on_server_span_created',
side_effect=self._register_mock) as mocked:
self.test_app.get('/example')
span = self.server_span_observer._serialize()
self.assertEqual(span['name'], 'example')
self.assertEqual(len(span['annotations']), 2)
self.assertEqual(span['parentId'], 0)
def test_configure_tracing_with_defaults_legacy_style(self):
baseplate = Baseplate()
self.assertEqual(0, len(baseplate.observers))
baseplate.configure_tracing('test')
self.assertEqual(1, len(baseplate.observers))
tracing_observer = baseplate.observers[0]
self.assertEqual('test',tracing_observer.service_name)
def test_configure_tracing_with_defaults_new_style(self):
baseplate = Baseplate()
self.assertEqual(0, len(baseplate.observers))
client = make_client("test")
baseplate.configure_tracing(client)
self.assertEqual(1, len(baseplate.observers))
tracing_observer = baseplate.observers[0]
self.assertEqual('test',tracing_observer.service_name)
def test_configure_tracing_with_args(self):
baseplate = Baseplate()
self.assertEqual(0, len(baseplate.observers))
baseplate.configure_tracing('test',
None,
max_span_queue_size=500,
num_span_workers=5,
span_batch_interval=0.5,
num_conns=100,
sample_rate=0.1)
self.assertEqual(1, len(baseplate.observers))
tracing_observer = baseplate.observers[0]
self.assertEqual('test', tracing_observer.service_name)