Before you can view the trace data of your application in the Tracing Analysis console, you must use a client to submit the trace data to Tracing Analysis. This topic describes how to use Jaeger to report Python application data.
Prerequisites
To obtain an endpoint of Jaeger or Zipkin, perform the following steps:
- Log on to the Tracing Analysis console.
- In the left-side navigation pane, click Cluster Configurations. Then, click the Access point information tab.
- In the top navigation bar, select a region. In the Cluster Information section, turn on Show Token.
- In the Client section, click Jaeger or Zipkin.
Obtain an endpoint of Jaeger or Zipkin in the Related Information column of the table in the lower part.
Note If your application is deployed in an Alibaba Cloud production environment, use an access point of Alibaba Cloud VPC. Otherwise, use a public endpoint. Generally, use the endpoint of v2 for Zipkin. Use the endpoint of v1 only if you know Zipkin well.
Background information
How is data reported?
- The following figure shows how to report data without using the Jaeger agent.
- The following figure shows how to report data by using the Jaeger agent.
Usage notes
- For Python, Jaeger v1.25 allows you to report trace data only by using the Jaeger agent. You cannot report trace data over HTTP. For more information, see Jaeger documentation.
- For Python, Jaeger v1.25 allows you to report trace data from the Jaeger client to the Jaeger agent only over UDP. The UDP protocol does not guarantee the reliability of communication. To ensure reliable transfer of trace data, we recommend that you run the Jaeger client and the Jaeger agent on the same host.
Step 1: Build an environment
This section lists the versions that are required for Docker, the Jaeger agent, the Jaeger client, and Python.
Docker and the Jaeger agent
Docker version: 20.10.7
Jaeger agent version: 1.25
- Run the following command in Docker Hub to pull the image of the Jaeger agent v1.25:
docker pull jaegertracing/jaeger-agent:1.25
- Run the following command to run the Jaeger agent v1.25:
docker run -d --name jaeger-agent -p 5775:5775/udp -p 6831:6831/udp -p 6832:6832/udp -p 5778:5778/tcp jaegertracing/jaeger-agent:1.25 --reporter.type=grpc --reporter.grpc.host-port=<endpoint>(Enter the endpoint) --agent.tags=<auth>(Enter the authentication information)
Note Replace<endpoint>
and<auth>
with the information displayed on the Cluster Configurations page of the Tracing Analysis console. For more information about how to obtain the endpoint, see the Prerequisites section in this topic.
Python and the Jaeger client
Python version: 3.8.5
Jaeger client version: 4.6.0
Install the following Python package in Python to configure the Jaeger client environment:
certifi==2021.5.30
charset-normalizer==2.0.4
idna==3.2
jaeger-client==4.6.0
opentracing==2.4.0
requests==2.26.0
six==1.16.0
threadloop==1.0.2
thrift==0.13.0
tornado==6.1
urllib3==1.26.6
Step 2: Create a Tracer object
- Create a Python file that contains the following content. Run the following code to create a Tracer object and use the Tracer object to create a span to report data to the Tracing Analysis backend:
import logging import time from jaeger_client import Config def construct_span(tracer): with tracer.start_span('AliyunTestSpan') as span: span.log_kv({'event': 'test message', 'life': 42}) print("tracer.tages: ", tracer.tags) with tracer.start_span('AliyunTestChildSpan', child_of=span) as child_span: span.log_kv({'event': 'down below'}) return span if __name__ == "__main__": log_level = logging.DEBUG logging.getLogger('').handlers = [] logging.basicConfig(format='%(asctime)s %(message)s', level=log_level) config = Config( config={ # usually read from some yaml config 'sampler': { 'type': 'const', 'param': 1, }, 'local_agent': { # Specify the hostname and port number of the Jaeger agent. # To ensure data reliability, we recommend that you run the Jaeger client and the Jaeger agent on the same host. Therefore, the reporting_host parameter is set to 127.0.0.1. 'reporting_host': '127.0.0.1', 'reporting_port': 6831, }, 'logging': True, }, # Specify the application name. service_name="mytest3", validate=True ) # this call also sets opentracing.tracer tracer = config.initialize_tracer() span = construct_span(tracer) time.sleep(2) # yield to IOLoop to flush the spans - https://github.com/jaegertracing/jaeger-client-python/issues/50 tracer.close() # flush any buffered spans
- Execute the Python file.
View data in the Tracing Analysis console
- Log on to the Tracing Analysis console. In the left-side navigation pane, click Applications.
- On the Applications page, select a region in the top navigation bar and click the name of the application that you want to manage.
- On the Application Overview page, you can view the key performance metrics and topology of the application.
- In the left-side navigation pane, click Application Details. On the Application Details page, you can view the application information.
- On the Application Details page, click the Traces tab. On the Traces tab, you can view the traces of the application.
References
This section provides common methods for using Jaeger. For more information, see Jaeger documentation.
- Create a Tracer object.:
from jaeger_client import Config def init_jaeger_tracer(service_name='your-app-name'): config = Config(config={}, service_name=service_name) return config.initialize_tracer()
- Create and finish a span:
# Start a span that does not have a parent span. tracer.start_span('TestSpan') # Start a span that has a parent span. tracer.start_span('ChildSpan', child_of=span) # Finish the span. span.finish()
- Pass a SpanContext:
#Serialization: Inject a SpanContext and pass it to the next span. tracer.inject( span_context=span.context, format=Format.TEXT_MAP, carrier=carrier ) # Deserialization: Extract a SpanContext that is passed. span_ctx = tracer.extract(format=Format.TEXT_MAP, carrier={})