关键字:python sanic 微服务 异步 zipkin
所需环境:python3.7, Docker, Linux or WSL
较早的异步框架是aiohttp,它提供了server端和client端,对asyncio做了很好的封装。但是开发方式和最流行的微框架flask不同,flask开发简单,轻量,高效。将两者结合起来就有了sanic。
Sanic框架是和Flask相似异步协程框架,简单轻量,并且性能很高。
微服务解决了复杂性问题,提高开发效率,便于部署等优点。opentracing作为分布式追踪系统应用事实上的标准,适合收集log,分析微服务的瓶颈。
OpenZipkin / Jaeger 都是opentracing具体实现的工具。
在上一篇【基于sanic的微服务框架 - 架构分析】中,使用了完全手写的wrapper,来产生opentracing log。
Python的一个原则:不要重复发明轮子!
Python有许多OpenZipkin的实现,https://zipkin.io/pages/tracers_instrumentation.html:
py_zipkin pyramid_zipkin swagger_zipkin flask_zipkin aiozipkin
既然是异步框架,那我们选择异步的aiozipkin吧。
特性:
- 完全异步,Distributed tracing capabilities to asyncio applications.
- 支持
zipkin v2
协议 - API简洁
- 显式context handling, no thread local variables.
- 支持输入到zipkin, jaeger and stackdriver through zipkin compatible API
aiohttp入门
创建service服务、tracer:
import aiozipkin as az
az.create_endpoint('sanic_app', ipv4=host, port=port)
tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0)
创建新的span:
with tracer.new_trace(sampled=True) as span:
span.name('root_span') # operation 操作名
span.tag('span_type', 'root') # 打标签,key/value格式
span.kind(az.CLIENT) # kind
span.annotate('SELECT * FROM') # 支持log
# imitate long SQL query
await asyncio.sleep(0.1)
span.annotate('start end sql')
创建级联span:
with tracer.new_child(span.context) as nested_span:
nested_span.name('nested_span_1')
nested_span.kind(az.CLIENT)
nested_span.tag('span_type', 'inner1')
nested_span.remote_endpoint('broker', ipv4='127.0.0.1', port=9011)
await asyncio.sleep(0.01)
aiohttp session访问,直接创建request span (GET/POST):
tracer = az.get_tracer(request.app)
span = az.request_span(request)
span Inject (适用于RPC访问其它服务,可以一目了然地看到各微服务之间的调用关系):
headers = span.context.make_headers() # Inject
message = {'payload': 'click', 'headers': headers}
aiohttp.session.post(backend_service, json=message),
span Extract(适用于接收RPC访问):
message = await request.json()
headers = message.get('headers', None)
context = az.make_context(headers) # Extract
with tracer.new_child(context) as span_consumer:
span_consumer.name('consumer event') # operation 操作名
span_consumer.remote_endpoint('broker', ipv4='127.0.0.1', port=9011) # 子服务
span_consumer.kind(az.CONSUMER)
集成到Sanic程序
完整程序:https://github.com/kevinqqnj/sanic-ms/blob/master/develop/sanic_aiozipkin_test.py
- 创建aio_zipkin实例:
@app.listener('before_server_start')
async def init(app, loop):
tracer = await az.create(zipkin_address, endpoint, sample_rate=1.0)
trace_config = az.make_trace_config(tracer)
app.aiohttp_session = aiohttp.ClientSession(trace_configs=[trace_config], loop=loop)
app.tracer = tracer
- 访问
/
地址,会创建一条zipkin trace(含一条子trace)
@app.route("/")
async def test(request):
request['aiozipkin_span'] = request
with app.tracer.new_trace() as span:
span.name(f'HTTP {request.method} {request.path}')
print(span)
url = "https://www.163.com"
with app.tracer.new_child(span.context) as span_producer:
span_producer.kind(az.PRODUCER)
span_producer.name('produce event click')
return response.text('ok')
- 试试看!
安装python依赖(要求Python 3):
pip install sanic aiohttp aiozipkin
使用docker运行zipkin或Jaeger:
docker run -d -p9411:9411 openzipkin/zipkin:latest
or
docker run -d -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 -p5775:5775/udp -p6831:6831/udp -p6832:6832/udp -p5778:5778 -p16686:16686 -p14268:14268 -p9411:9411 jaegertracing/all-in-one
运行app:
python sanic_aiozipkin_test.py
然后打开另一终端:
curl localhost:8000/
-
浏览器里,查看Zipkin UI:
- 创建
/2
地址,把HTTP request抽象出来,包装一下:
def request_span(request):
with app.tracer.new_trace() as span:
span.name(f'HTTP {request.method} {request.path}')
kwargs = {
'http.path':request.path,
'http.method':request.method,
'http.path':request.path,
'http.route':request.url,
'peer.ip':request.remote_addr or request.ip,
'peer.port':request.port,
}
[span.tag(k, v) for k,v in kwargs.items()]
span.kind(az.SERVER)
return span
@app.route("/2")
async def tes2(request):
request['aiozipkin_span'] = request
span = request_span(request)
with app.tracer.new_child(span.context) as span_producer:
span_producer.kind(az.PRODUCER)
span_producer.name('produce event click')
return response.text('ok')
访问:curl localhost:8000/2
这次换用Jaeger来查看:
Next:创建Sanic plugin
aiozipkin集成到Sanic里,是不是很简单?
但大家有没有觉得,每个Http request、每个method call、或者RPC访问(trace inject/extract),都要手动码代码,是不是很low很低效啊?
好,下一篇我们就来创建一个Sanic Plugin,app里直接用装饰器就行了。
原型已写好,暂时取名sanic_zipkin
from sanic_zipkin import SanicZipkin, logger, sz_rpc
app = Sanic(__name__)
sz = SanicZipkin(app, service='service-a')
@app.route("/")
async def index(request):
return response.json({"hello": "from index"})
@logger()
async def db_access(context, data):
await asyncio.sleep(0.1)
print(f'db_access done. data: {data}')
return
@sz.route("/2")
async def method_call(request, context):
await db_access(context, 'this is method_call data')
return response.json({"hello": 'method_call'})
第二篇: python微服务sanic 使用异步zipkin(2)- 创建Sanic plugin:sanic_zipkin
//www.greatytc.com/p/bbb09e46a747