tencent cloud

All product documents
Tencent Cloud Observability Platform
Access of Python Applications via SkyWalking
Last updated: 2025-03-04 22:02:22
Access of Python Applications via SkyWalking
Last updated: 2025-03-04 22:02:22
This document describes how to report data from a Python application via SkyWalking.

Prerequisites

Python applications are of Python version 3.7 or later, so that automatic instrumentation and reporting via SkyWalking can be achieved.
Currently, gRPC is used for reporting by default.
For details of the components that currently support automatic event tracking, see Frameworks Supported by Python Agent.

Directions

Step 1. Getting the Access Point and Token

1. Log in to the TCOP console.
2. In the left sidebar, select Application Performance Management, and then click Application list > Access application.
3. On the page that pops up on the right, select the Python language.
4. On the Access Python Application page, select the Region and Business System.
5. Select SkyWalking as Access protocol type.
6. Select a Reporting method through which you want to report data, and obtain your Access Point and Token.
Note:
Report over private network: This reporting method requires your service to run in the Tencent Cloud VPC. The direct connection through VPC can help avoid the security risks of public network communication and save costs on reporting traffic.
Report over public network: If your service is deployed locally or in a non-Tencent Cloud VPC, you can report data in this method. However, it involves security risks in public network communication and incurs reporting traffic fees.

Step 2: Installing SkyWalking Agent-Related Dependencies

pip install apache-skywalking
# This library is used in the current demo.
pip install tornado

Step 3: Modifying Startup Code

from skywalking import agent, config
config.init(
# Replace with the access point obtained in Step 1.
agent_collector_backend_services='ap-guangzhou.apm.tencentcs.com:11800',
agent_name='python-skywalking', # Customize the application name here.
agent_authentication="xxxxxxxxxxxxxx", # Replace with the token obtained in Step 1.
agent_logging_level="INFO")
agent.start()

The complete demo code is as follows:

import time
import tornado.ioloop
import tornado.web


class MainHandler(tornado.web.RequestHandler):
def get(self):
print("call")
self.write("Hello, world")


class SleepHandler(tornado.web.RequestHandler):
def get(self):
print("call sleep")
time.sleep(0.1)
self.write("sleep 0.1s")


def make_app():
return tornado.web.Application([
(r"/test", MainHandler),
(r"/sleep", SleepHandler),
])


if __name__ == "__main__":
from skywalking import agent, config
config.init(agent_collector_backend_services=''{access point}', # Replace with the access point obtained in Step 1.
agent_name='python-skywalking',
agent_authentication='{token}', # Replace with the token obtained in Step 1.
agent_logging_level='INFO')
agent.start()
app = make_app()
port = 9008
app.listen(port)
print("server in %s" % port)
tornado.ioloop.IOLoop.current().start()


Step 4: Accessing Through Multiple Threads

This step is optional. After completing the above 3 steps, you can view the reported data in the TCOP console, in APM > Application list. If you want to achieve access through multiple threads, you can use the tornado package. Note that the SkyWalking Agent needs to be started after the fork process starts.

Introducing the tornado Dependency

pip install tornado

The complete demo code is as follows:

import time
import os
import tornado.ioloop
import tornado.web
from tornado.httpserver import HTTPServer


class MainHandler(tornado.web.RequestHandler):
def get(self):
print("call")
self.write("Hello, world")


class Sleep01Handler(tornado.web.RequestHandler):
def get(self):
print("call sleep")
time.sleep(0.1)
self.write("sleep 0.1s")


def make_app():
return tornado.web.Application([
(r"/test", MainHandler),
(r"/sleep", Sleep01Handler),
])


if __name__ == "__main__":
from skywalking import agent, config
config.init(agent_collector_backend_services='{access point}', # Replace with the access point obtained in Step 1.
agent_name='python-skywalking-multi',
agent_authentication='{token}', # Replace with the token obtained in Step 1.
agent_logging_level='INFO')
app = make_app()
port = 9009
num_processes = 4
sockets = tornado.netutil.bind_sockets(port)
tornado.process.fork_processes(num_processes)
# For multi-process usage, start the agent after the fork process starts.
agent.start()
pid = os.getpid()
print("Current process ID:%d" % pid)
server = HTTPServer(app)
server.add_sockets(sockets)
print("server in %s" % port)
tornado.ioloop.IOLoop.current().start()


Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support