def zipkin_tween(handler, registry):
def tween(request):<EOL><INDENT>zipkin_settings = _get_settings_from_request(request)<EOL>tracer = get_default_tracer()<EOL>tween_kwargs = dict(<EOL>service_name=zipkin_settings.service_name,<EOL>span_name=zipkin_settings.span_name,<EOL>zipkin_attrs=zipkin_settings.zipkin_attrs,<EOL>transport_handler=zipkin_settings.transport_handler,<EOL>host=zipkin_settings.host,<EOL>port=zipkin_settings.port,<EOL>add_logging_annotation=zipkin_settings.add_logging_annotation,<EOL>report_root_timestamp=zipkin_settings.report_root_timestamp,<EOL>context_stack=zipkin_settings.context_stack,<EOL>max_span_batch_size=zipkin_settings.max_span_batch_size,<EOL>encoding=zipkin_settings.encoding,<EOL>kind=Kind.SERVER,<EOL>)<EOL>if zipkin_settings.firehose_handler is not None:<EOL><INDENT>tween_kwargs['<STR_LIT>'] = zipkin_settings.firehose_handler<EOL><DEDENT>with tracer.zipkin_span(**tween_kwargs) as zipkin_context:<EOL><INDENT>response = handler(request)<EOL>if zipkin_settings.use_pattern_as_span_name and request.matched_route:<EOL><INDENT>zipkin_context.override_span_name('<STR_LIT>'.format(<EOL>request.method,<EOL>request.matched_route.pattern,<EOL>))<EOL><DEDENT>zipkin_context.update_binary_annotations(<EOL>get_binary_annotations(request, response),<EOL>)<EOL>if zipkin_settings.post_handler_hook:<EOL><INDENT>zipkin_settings.post_handler_hook(request, response)<EOL><DEDENT>return response<EOL><DEDENT><DEDENT>return tween<EOL>
Factory for pyramid tween to handle zipkin server logging. Note that even
if the request isn't sampled, Zipkin attributes are generated and pushed
into threadlocal storage, so `create_http_headers_for_new_span` and
`zipkin_span` will have access to the proper Zipkin state.

Consumes custom create_zipkin_attr function if one is set in the pyramid
registry.

:param handler: pyramid request handler
:param registry: pyramid app registry

:returns: pyramid tween