Before you Begin

The support for OpenTracing, an API and vendor-neutral instrumentation framework, allows application developers to instrument their application code and analyze the trace data in Oracle Cloud Infrastructure (OCI) Application Performance Monitoring (APM). This is an example of instrumentation of Python code.

Background

Distributed tracing is a method used to profile and monitor applications. This method is especially used to monitor applications built using a microservices architecture. OpenTracing is an open-source project that provides vendor-neutral APIs and instrumentation for distributed tracing. It allows developers to add instrumentation to their application code using APIs.

Application Performance Monitoring consumes telemetry data collected by OpenTracing and provides information on requests, through various microservices or functions. Therefore, APM allows real end user monitoring and complete analysis of the impact of services on the overall end-user digital experience.

Our Scenario

In this example, we have two Python programs deployed on Kubernetes. One program gets a video from the OCI Object Storage and puts it into Kafka, the other one gets the video from Kafka and analyzes the video. APM supports the Zipkin distributed tracing system, so our Python code is instrumented using Zipkin. Let's see how it is done!

Step 1: Import the Tracer Initialization Code

For Python applications, py_zipkin provides a context manager/decorator along with some utilities to facilitate the usage of Zipkin. First, you import a customized py_zipkin, the tracer initialization code, into your Python code. Note the changes for this scenario are shown in bold below:

              
from py_zipkin import Encoding #import Zipkin package 
from py_zipkin.zipkin import zipkin_span #import Zipkin package 
from transport import http_transport #import Zipkin transport 
def send_video():
#Initiate Zipkin tracer client on your python code that you want to trace
with zipkin_span(  
      service_name="start download file from object storage", #You can change it as you need
      span_name="loop download file", #You can change it as you need
      transport_handler=http_transport, #zipkin transport, will use it to upload trace data to OCI APM
      encoding = Encoding.V2_JSON,
      binary_annotations = {"send_video":"customer message"}, #Custom tag
      sample_rate=100 # this is optional and can be used to set custom sample rates
  ):
    result = download_file(namespace, "bucket-version", "sample_video.mp4")

Step 2: Add the Zipkin Span Annotations

  1. Add the first annotation. The annotation is shown in bold for our example:

    from py_zipkin import Encoding #import Zipkin package 
    from py_zipkin.zipkin import zipkin_span #import Zipkin package 
    from transport import http_transport #import Zipkin transport 
    #This is the Zipkin span annotation used to trace “download file from object storage” and upload this trace data to OCI APM
    @zipkin_span(service_name='start download file from object storage', span_name='download file method')
    def download_file(self, namespace_name, bucket_name, object_name):
        try:
            objectStorageClient = self.connect_oracleObjectStorage()
           #download video from object storage
            download_file = objectStorageClient.get_object(namespace_name, bucket_name, object_name)
            params = download_file.data.content
            #send video to kafaka
            self.producer.senddata(params)
        except Exception:
            return False
            return True
          
  2. Add the second annotation:

    from py_zipkin import Encoding #import Zipkin package 
    from py_zipkin.zipkin import zipkin_span #import Zipkin package 
    from transport import http_transport #import Zipkin transport 
    #This is the Zipkin span annotation used to trace “send video to kafka” and upload this trace data to OCI APM
    @zipkin_span(service_name='start download file from object storage', span_name='send video to kakfa')
    def senddata(self, params):
        try:
            parmas_message = params
            producer = self.producer
            producer.send(self.kafkaTopic, value=parmas_message)
            producer.flush()
        except KafkaError as e:
            print(e) 

Step 3: Create an APM Domain

  1. Sign in to the Oracle Cloud Infrastructure console, open the navigation menu, and click Observability and Management. Under Application Performance Monitoring, click Administration:

    OCI Main Menu
    Description of the illustration apm.png
  2. Click Create APM Domain:

    Create Domain
    Description of the illustration domain.png
  3. Enter the domain details:
    Create Menu
    Description of the illustration create.png
  4. Click on the domain you just created and make a note of the domain values Data Upload Endpoint, Private Key and Public Key:

    Domain Details
    Description of the illustration domain_details.png

Step 4: Configure the Zipkin Transport for Python

  1. Construct the URL required to send data to APM. The format of the URL is:

    apm-domain-short-id.apm-collector-domain.apm-domain-short-id.apm-collector-domain/API version/observations/observationType?dataFormat=dataFormat&dataFormatVersion=dataFormatVersion&dataKey=dataKey

    For this example, the root of the URL is:

    https://xyz.apm-agt.ap-abc.oci.oraclecloud.com

    and the datakey is:

    XXXXXXXXTZOZBR3XE4GJRXYZ
  2. Add the complete URL to your code:
    import requests
    def http_transport(encoded_span):
          requests.post(
                 #Construct a URL that communicate with Application Performance Monitoring
                 'https://xyz.apm-agt.ap-abc.oci.oraclecloud.com/20200101/observations/public-span?dataFormat=zipkin&dataFormatVersion=2&dataKey=XXXXXXXXTZOZBR3XE4GJRXYZ',
            data=encoded_span,
            headers={'Content-Type': 'application/json'},
        )

Step 5: View Data in Trace Explorer

You are now ready to view and analyze your trace data in APM Trace Explorer. When your Python program runs, the Zipkin Client will start sending trace data that will in turn be uploaded to APM. Take a look at how this data looks in APM.

  1. From the OCI main menu, navigate to Trace Explorer:
    OCI Main Menu
    Description of the illustration trace.png
  2. Select your compartment and domain and view all trace data, under the Traces tab:

    Trace Explorer
    Description of the illustration traceexplorer.png
  3. Click on any trace to view more details about it:

    Trace Details
    Description of the illustration tracedetails.png
  4. In this example, the trace data shows that the Python application spent 7.21 seconds in getting the video file, 7.21 seconds downloading the file and 5.5 seconds in sending the video file to Kafka. Based on this data, you can determine if this is the best experience for your user.

  5. Click on Span to view more details and tagged values:

    Trace Details
    Description of the illustration span.png
  6. Note the send_video tag that was defined on Zipkin tracer Client. (binary_annotations)

Learn More