skywalking 通過python探針監控Python 微服務應用性能


來源:https://skywalking.apache.org/zh/2020-11-30-pycon/

Python 作為一門功能強大的編程語言,被廣泛的應用於計算機行業之中; 在微服務系統架構盛行的今天,Python 以其豐富的軟件生態和靈活的語言特性在服務端編程領域也占有重要的一席之地。 本次分享將闡述 Apache SkyWalking 在微服務架構中要解決的問題,展示如何使用 Apache SkyWalking 來近乎自動化地監控 Python 后端應用服務,並對 Apache SkyWalking 的 Python 語言探針的實現技術進行解讀。

python探針源碼:https://github.com/apache/skywalking-python/tree/v0.6.0#create-spans

SkyWalking Python Agent

Sky Walking logo

SkyWalking-Python: The Python Agent for Apache SkyWalking, which provides the native tracing abilities for Python project.

SkyWalking: an APM(application performance monitor) system, especially designed for microservices, cloud native and container-based (Docker, Kubernetes, Mesos) architectures.

GitHub stars Twitter Follow

Build

Install

From Pypi

The Python agent module is published to Pypi, from where you can use pip to install:

# Install the latest version, using the default gRPC protocol to report data to OAP pip install "apache-skywalking" # Install the latest version, using the http protocol to report data to OAP pip install "apache-skywalking[http]" # Install the latest version, using the kafka protocol to report data to OAP pip install "apache-skywalking[kafka]" # Install a specific version x.y.z # pip install apache-skywalking==x.y.z pip install apache-skywalking==0.1.0 # For example, install version 0.1.0 no matter what the latest version is

From Source Codes

Refer to the FAQ.

Set up Python Agent

SkyWalking Python SDK requires SkyWalking 8.0+ and Python 3.5+.

If you want to try out the latest features that are not released yet, please refer to the guide to build from sources.

from skywalking import agent, config config.init(collector='127.0.0.1:11800', service='your awesome service') agent.start()

Alternatively, you can also pass the configurations via environment variables (such as SW_AGENT_NAMESW_AGENT_COLLECTOR_BACKEND_SERVICES, etc.) so that you don't need to call config.init.

All supported environment variables can be found here

Supported Libraries

There are some built-in plugins (such as http.serverFlaskDjango etc.) that support automatic instrumentation of Python libraries, the complete lists can be found here

API

Apart from the libraries that can be instrumented automatically, we also provide some APIs to enable manual instrumentation.

Create Spans

The code snippet below shows how to create entry span, exit span and local span.

from skywalking import Component from skywalking.trace.context import SpanContext, get_context from skywalking.trace.tags import Tag context: SpanContext = get_context() # get a tracing context # create an entry span, by using `with` statement, # the span automatically starts/stops when entering/exiting the context with context.new_entry_span(op='https://github.com/apache') as span: span.component = Component.Flask # the span automatically stops when exiting the `with` context with context.new_exit_span(op='https://github.com/apache', peer='localhost:8080') as span: span.component = Component.Flask with context.new_local_span(op='https://github.com/apache') as span: span.tag(Tag(key='Singer', val='Nakajima'))

Decorators

from time import sleep from skywalking import Component from skywalking.decorators import trace, runnable from skywalking.trace.context import SpanContext, get_context from skywalking.trace.ipc.process import SwProcess @trace() # the operation name is the method name('some_other_method') by default def some_other_method(): sleep(1) @trace(op='awesome') # customize the operation name to 'awesome' def some_method(): some_other_method() @trace(op='async_functions_are_also_supported') async def async_func(): return 'asynchronous' @trace() async def async_func2(): return await async_func() @runnable() # cross thread propagation def some_method(): some_other_method() from threading import Thread t = Thread(target=some_method) t.start() # When another process is started, agents will also be started in other processes, # supporting only the process mode of spawn. p1 = SwProcess(target=some_method) p1.start() p1.join() context: SpanContext = get_context() with context.new_entry_span(op=str('https://github.com/apache/skywalking')) as span: span.component = Component.Flask some_method()

Contact Us

Contributing

Before submitting a pull request or push a commit, please read our contributing and developer guide.

FAQs

Check the FAQ page or add the FAQs there.

License

Apache 2.0


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM