probeyang / skywalking-python

The Python agent for Apache SkyWalking

Home Page:https://skywalking.apache.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

By default, SkyWalking Python agent uses gRPC protocol to report data to SkyWalking backend, in SkyWalking backend, the port of gRPC protocol is 11800, and the port of HTTP protocol is 12800, you should configure collector_address (or environment variable SW_AGENT_COLLECTOR_BACKEND_SERVICES) according to the protocol you want.

Report data via gRPC protocol (Default)

For example, if you want to use gRPC protocol to report data, configure collector_address (or environment variable SW_AGENT_COLLECTOR_BACKEND_SERVICES) to <oap-ip-or-host>:11800, such as 127.0.0.1:11800:

from skywalking import agent, config

config.init(collector_address='127.0.0.1:11800', service_name='your awesome service')
agent.start()

Report data via HTTP protocol

However, if you want to use HTTP protocol to report data, configure collector_address (or environment variable SW_AGENT_COLLECTOR_BACKEND_SERVICES) to <oap-ip-or-host>:12800, such as 127.0.0.1:12800:

Remember you should install skywalking-python with extra requires http, pip install "apache-skywalking[http].

from skywalking import agent, config

config.init(collector_address='127.0.0.1:12800', service_name='your awesome service')
agent.start()

Report data via Kafka protocol

Finally, if you want to use Kafka protocol to report data, configure kafka_bootstrap_servers (or environment variable SW_KAFKA_REPORTER_BOOTSTRAP_SERVERS) to kafka-brokers, such as 127.0.0.1:9200:

Remember you should install skywalking-python with extra requires kafka, pip install "apache-skywalking[kafka].

from skywalking import agent, config

config.init(kafka_bootstrap_servers='127.0.0.1:9200', service_name='your awesome service')
agent.start()

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

All supported environment variables can be found here

Report logs with Python Agent

The Python agent is capable of reporting collected logs to the backend(SkyWalking OAP), enabling Log & Trace Correlation.

Please refer to the Log Reporter Doc for a detailed guide.

Supported Libraries

There are some built-in plugins (such as http.server, Flask, Django 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

class TagSinger(Tag):
    key = 'Singer'

with context.new_exit_span(op='https://github.com/apache', peer='localhost:8080', component=Component.Flask) as span:
    span.tag(TagSinger('Nakajima'))

with context.new_local_span(op='https://github.com/apache') as span:
    span.tag(TagSinger('Nakajima'))

Decorators

from time import sleep

from skywalking import Component
from skywalking.decorators import trace, runnable
from skywalking.trace.context import SpanContext, get_context

@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()


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

About

The Python agent for Apache SkyWalking

https://skywalking.apache.org/

License:Apache License 2.0


Languages

Language:Python 99.0%Language:Makefile 0.7%Language:Dockerfile 0.3%