def capture_function_log(func, fmt=DEFAULT_FORMAT): # add stream handler to capture output out = StringIO() sh = logging.StreamHandler(out) try: formatter = logging.Formatter(fmt) sh.setFormatter(formatter) logger.addHandler(sh) result = func() finally: logger.removeHandler(sh) return out.getvalue().strip(), result
def test_send_file(self): """ When calling a patched ``flask.send_file`` We create the expected spans """ fp = StringIO('static file') with self.app.app_context(): with self.app.test_request_context('/'): # DEV: Flask >= (0, 12, 0) tries to infer mimetype, so set explicitly response = flask.send_file(fp, mimetype='text/plain') self.assertTrue(isinstance(response, flask.Response)) self.assertEqual(response.status_code, 200) # 1 for `send_file` # 1 for tearing down the request context we created # 1 for tearing down the app context we created spans = self.get_spans() self.assertEqual(len(spans), 3) self.assertEqual(spans[0].service, 'flask') self.assertEqual(spans[0].name, 'flask.send_file') self.assertEqual(spans[0].resource, 'flask.send_file') self.assertEqual(set(['system.pid']), set(spans[0].meta.keys())) self.assertEqual(spans[1].name, 'flask.do_teardown_request') self.assertEqual(spans[2].name, 'flask.do_teardown_appcontext')
def test_send_file(self): """ When calling a patched ``flask.send_file`` We create the expected spans """ if flask_version >= (2, 0, 0): fp = BytesIO(b"static file") else: fp = StringIO("static file") with self.app.app_context(): with self.app.test_request_context("/"): # DEV: Flask >= (0, 12, 0) tries to infer mimetype, so set explicitly response = flask.send_file(fp, mimetype="text/plain") self.assertTrue(isinstance(response, flask.Response)) self.assertEqual(response.status_code, 200) # 1 for `send_file` # 1 for tearing down the request context we created # 1 for tearing down the app context we created spans = self.get_spans() self.assertEqual(len(spans), 3) self.assertEqual(spans[0].service, "flask") self.assertEqual(spans[0].name, "flask.send_file") self.assertEqual(spans[0].resource, "flask.send_file") assert set(spans[0].meta.keys()) == {"runtime-id"} self.assertEqual(spans[1].name, "flask.do_teardown_request") self.assertEqual(spans[2].name, "flask.do_teardown_appcontext")
def test_render(self): # render t = Template("Hello ${name}!") self.assertEqual(t.render(name="mako"), "Hello mako!") spans = self.pop_spans() self.assertEqual(len(spans), 1) assert_is_measured(spans[0]) self.assertEqual(spans[0].service, "mako") self.assertEqual(spans[0].span_type, "template") self.assertEqual(spans[0].get_tag("mako.template_name"), DEFAULT_TEMPLATE_NAME) self.assertEqual(spans[0].name, "mako.template.render") self.assertEqual(spans[0].resource, DEFAULT_TEMPLATE_NAME) # render_unicode t = Template("Hello ${name}!") self.assertEqual(t.render_unicode(name="mako"), to_unicode("Hello mako!")) spans = self.pop_spans() self.assertEqual(len(spans), 1) assert_is_measured(spans[0]) self.assertEqual(spans[0].service, "mako") self.assertEqual(spans[0].span_type, "template") self.assertEqual(spans[0].get_tag("mako.template_name"), DEFAULT_TEMPLATE_NAME) self.assertEqual(spans[0].name, "mako.template.render_unicode") self.assertEqual(spans[0].resource, DEFAULT_TEMPLATE_NAME) # render_context t = Template("Hello ${name}!") buf = StringIO() c = Context(buf, name="mako") t.render_context(c) self.assertEqual(buf.getvalue(), "Hello mako!") spans = self.pop_spans() self.assertEqual(len(spans), 1) assert_is_measured(spans[0]) self.assertEqual(spans[0].service, "mako") self.assertEqual(spans[0].span_type, "template") self.assertEqual(spans[0].get_tag("mako.template_name"), DEFAULT_TEMPLATE_NAME) self.assertEqual(spans[0].name, "mako.template.render_context") self.assertEqual(spans[0].resource, DEFAULT_TEMPLATE_NAME)
def capture_function_log(func, fmt=DEFAULT_FORMAT, logger_override=None): if logger_override is not None: logger_to_capture = logger_override else: logger_to_capture = logger # add stream handler to capture output out = StringIO() sh = logging.StreamHandler(out) try: formatter = logging.Formatter(fmt) sh.setFormatter(formatter) logger_to_capture.addHandler(sh) result = func() finally: logger_to_capture.removeHandler(sh) return out.getvalue().strip(), result
def test_render(self): # render t = Template('Hello ${name}!') self.assertEqual(t.render(name='mako'), 'Hello mako!') spans = self.tracer.writer.pop() self.assertEqual(len(spans), 1) assert_is_measured(spans[0]) self.assertEqual(spans[0].service, 'mako') self.assertEqual(spans[0].span_type, 'template') self.assertEqual(spans[0].get_tag('mako.template_name'), '<memory>') self.assertEqual(spans[0].name, 'mako.template.render') self.assertEqual(spans[0].resource, '<memory>') # render_unicode t = Template('Hello ${name}!') self.assertEqual(t.render_unicode(name='mako'), to_unicode('Hello mako!')) spans = self.tracer.writer.pop() self.assertEqual(len(spans), 1) assert_is_measured(spans[0]) self.assertEqual(spans[0].service, 'mako') self.assertEqual(spans[0].span_type, 'template') self.assertEqual(spans[0].get_tag('mako.template_name'), '<memory>') self.assertEqual(spans[0].name, 'mako.template.render_unicode') self.assertEqual(spans[0].resource, '<memory>') # render_context t = Template('Hello ${name}!') buf = StringIO() c = Context(buf, name='mako') t.render_context(c) self.assertEqual(buf.getvalue(), 'Hello mako!') spans = self.tracer.writer.pop() self.assertEqual(len(spans), 1) assert_is_measured(spans[0]) self.assertEqual(spans[0].service, 'mako') self.assertEqual(spans[0].span_type, 'template') self.assertEqual(spans[0].get_tag('mako.template_name'), '<memory>') self.assertEqual(spans[0].name, 'mako.template.render_context') self.assertEqual(spans[0].resource, '<memory>')
def test_send_file_pin_disabled(self): """ When calling a patched ``flask.send_file`` When the app's ``Pin`` has been disabled We do not create any spans """ pin = Pin.get_from(self.app) pin.tracer.enabled = False fp = StringIO('static file') with self.app.app_context(): with self.app.test_request_context('/'): # DEV: Flask >= (0, 12, 0) tries to infer mimetype, so set explicitly response = flask.send_file(fp, mimetype='text/plain') self.assertTrue(isinstance(response, flask.Response)) self.assertEqual(response.status_code, 200) self.assertEqual(len(self.get_spans()), 0)