金字塔应用的opentracing支持
pyramid-opentracing的Python项目详细描述
此包允许通过The OpenTracing Project在金字塔项目中进行分布式跟踪。一旦生产系统与真正的并发性竞争或分成许多服务,关键的(以前很容易的)任务就会变得困难:面向用户的延迟优化、后端错误的根本原因分析、关于现在分布式系统的不同部分的通信等。分布式跟踪从一开始就跟随一个请求。从移动/浏览器一直到微服务。
由于核心服务和库采用opentracing,应用程序构建器不再承担向自己的代码添加基本跟踪工具的任务。通过这种方式,开发人员可以使用他们喜欢的工具构建他们的应用程序,并从内置的跟踪工具中获益。OpenCACKIN实现存在于主要的分布式跟踪系统中,并且可以通过一行配置更改来绑定或交换。
如果您想了解有关底层python api的更多信息,请访问pythonsource code。
如果要从0.x系列迁移,可能需要读取breaking changes的列表。
安装
运行以下命令:
$ pip install pyramid_opentracing
为所有请求设置跟踪
为了在您的系统中实现跟踪(对于所有请求),请将以下代码行添加到站点的配置部分,以启用跟踪之间的功能:
# OpenTracing settings# defaults to Trueconfig.add_attributes({'ot.trace_all':True})# defaults to []# only valid if 'opentracing_trace_all' == Trueconfig.add_attributes({'ot.traced_attributes':['host','method',...]})# an optional module-level callable invoked after Span is created, taking# span and request as parameters.config.add_attributes({'ot.start_span_cb':'my_main_module.start_span_cb'})# One valid underlying OpenTracing implementation as# one either ONE of these three values:# 1. A PyramidTracing object.config.add_attributes({'ot.tracing':PyramidTracing(my_ot_tracer)})# 2. A module-level callable, invoked once, returning a PyramidTracing# and receiving the settings, such as: create_tracing(**settings).config.add_attributes({'ot.tracing_callable','my_main_module.utils.create_tracing')# 3. OR a module-level callable, invoked once, returning an opentracing# compliant Tracer with optional parameters.config.add_attributes({'ot.tracer_callable','opentracing.Tracer'})config.add_attributes({'ot.tracer_parameters',...})# enable the tweenconfig.include('pyramid_opentracing')
或者,您可以通过ini文件配置tween:
[app:myapp]ot.trace_all=trueot.start_span_cb=my_main_module.start_span_cbot.traced_attributes=host methodot.tracing_callable=my_main_module.utils.create_tracingpyramid.includes=pyramid_opentracing< >一旦TWEN被包含,^ {STR 1 } $如果没有/Sturi>^ {CD1>}没有被直接设置,将创建一个新实例,并且将存在于{TT1} $中,用于进一步的消费。
注意:要跟踪的有效请求属性列在[此处](http://docs.pylonsproject.org/projects/pyramid/en/latest/api/request.html#pyramid.request.Request)。当您跟踪一个属性时,这意味着创建的跨距将具有带有属性名和请求值的标记。
跟踪单个请求
如果不想跟踪到站点的所有请求,可以使用函数装饰器跟踪各个视图函数。这可以通过自己管理一个全局唯一的PyramidTracing对象,然后将以下代码行添加到任何具有视图功能的文件中来完成:
# get_tracer() should return a globally-unique PyramidTracing object.frommy_tracing_modimportget_tracingtracing=get_tracing()# put the decorator after @view_config, if used@tracing.trace(optional_args)defsome_view_func(request):...#do some stuff
这个跟踪方法不使用tween,所以不需要包含那个。
可选参数允许跟踪请求属性。例如,如果要跟踪元数据,可以传入@tracing.trace('headers')
,并且request.headers将被设置为此视图函数的所有跨距上的标记。
示例
下面是一个金字塔应用程序的tween example,它使用金字塔tween来记录所有 请求:
这里是一个同时充当客户机和服务器的应用程序的client server example,
使用手动管理的跟踪程序(需要安装waitress
模块)。
其他示例包含在examples directrory下。
从0.x中断更改
从1.0版开始,与以前的版本相比发生了一些更改:
- PyramidTracer已重命名为PyramidTracing,尽管PyramidTracer 仍可用作已弃用的名称。
- ot.base_tracer和ot.base_tracer_func仍然有效,但已被弃用。
- 使用中间层时,ot.trace_all默认为True。
- 当没有提供opentracing.Tracer时,PyramidTracing将依赖于 全球追踪。
更多信息
如果您有兴趣进一步了解opentracing标准,请访问opentracing.io或join the mailing list。如果您想在您的项目中实现opentracing并需要帮助,请随时在community@opentracing.io向我们发送一条消息。