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.
Directions
Step 1. Getting the Access Point and Token
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
pip install tornado
Step 3: Modifying Startup Code
from skywalking import agent, config
config.init(
agent_collector_backend_services='ap-guangzhou.apm.tencentcs.com:11800',
agent_name='python-skywalking',
agent_authentication="xxxxxxxxxxxxxx",
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}',
agent_name='python-skywalking',
agent_authentication='{token}',
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
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}',
agent_name='python-skywalking-multi',
agent_authentication='{token}',
agent_logging_level='INFO')
app = make_app()
port = 9009
num_processes = 4
sockets = tornado.netutil.bind_sockets(port)
tornado.process.fork_processes(num_processes)
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()