All Products
Search
Document Center

Alibaba Cloud Service Mesh:Use Managed Service for OpenTelemetry to trace applications inside and outside an ASM instance

Last Updated:Feb 28, 2024

Service Mesh (ASM) can trace applications that are deployed inside an ASM instance. To trace applications that are deployed inside and outside an ASM instance at the same time, you must use Managed Service for OpenTelemetry to generate call traces. This topic describes how to use Managed Service for OpenTelemetry to enable an external application to call an application inside an ASM instance. This way, Managed Service for OpenTelemetry generates a call trace between the two applications.

Prerequisites

Usage notes

  • In this example, the ExternalProxy application that is compiled by using Python is deployed outside the ASM instance. ExternalProxy uses an HTTP server. When the root path of ExternalProxy is accessed, the HTTP server calls the Productpage microservice of the Bookinfo application that is deployed inside the ASM instance.

  • In this example, Managed Service for OpenTelemetry is used to trace applications. If you use a custom tracing system that is compatible with Zipkin, skip Step 2: Obtain the endpoint that is used to access Managed Service for OpenTelemetry and go to Step 3: Deploy the ExternalProxy application. Set the endpoint for external access to the custom tracing system to the endpoint to which tracing data is reported.

Step 1: Deploy the Bookinfo application inside an ASM instance

  1. Run the following command to deploy the Bookinfo application in the ACK cluster that is added to the ASM instance.

    You can download the YAML file of the Bookinfo application from GitHub.

    kubectl --kubeconfig=${DATA_PLANE_KUBECONFIG} apply -f bookinfo.yaml
  2. Run the following command to deploy virtual services for the Bookinfo application.

    You can download the YAML file of virtual services from GitHub.

    kubectl --kubeconfig=${ASM_KUBECONFIG} apply -f virtual-service-all-v1.yaml
  3. Run the following command to deploy destination rules for the Bookinfo application.

    You can download the YAML file of destination rules from GitHub.

    kubectl --kubeconfig=${ASM_KUBECONFIG} apply -f destination-rule-all.yaml
  4. Run the following command to deploy a gateway for the Bookinfo application.

    You can download the YAML file of the gateway from GitHub.

    kubectl --kubeconfig=${ASM_KUBECONFIG} apply -f bookinfo-gateway.yaml

Step 2: Obtain the endpoint that is used to access Managed Service for OpenTelemetry

  1. Log on to the Managed Service for OpenTelemetry console. In the left-side navigation pane, click Overview.

  2. On the Overview page, click the Access process tab, and then click View access point information.

  3. View the access endpoint.

    1. In the upper part of the page, select the desired region.

      Select the region of the ACK cluster where the Bookinfo application is deployed. If you select another region, the tracing data of the Bookinfo application cannot be reported to Managed Service for OpenTelemetry.

    2. On the Access point information tab, turn on Show Token for the Cluster Information parameter. Select the public or private endpoint based on the place where you want to deploy the ExternalProxy application.

      In this example, the tracing data of ExternalProxy is reported by using the API of Zipkin V1. Therefore, you must use the public or private endpoint of Zipkin V1. 查看接入点地址

Step 3: Deploy the ExternalProxy application

Important
  • The sidecar proxy forwards all content (including the HTTP request headers and request body) received in a request to the upstream service. Therefore, tracing-related headers are also forwarded to the upstream service.

  • To pass tracing information through, the application must pass tracing-related request headers through to the upstream service according to the specifications of the Istio community.

  1. Create an ExternalProxy.py file that contains the following content and save the file in the runtime environment of the ExternalProxy application.

    • Replace {XTRACE_ZIPKIN_V1_ENDPOINT} with the endpoint that you obtained in Step 2. If you need to report the tracing data to a custom tracing system, replace the variable with the access endpoint of the custom tracing system.

    • Replace {INGRESS_GATE_WAY_IP} with the endpoint of the ingress gateway that is deployed for the ACK cluster.

      Show the ExternalProxy.py file

      import requests
      from flask import Flask
      from py_zipkin.zipkin import zipkin_span,create_http_headers_for_new_span
      from py_zipkin.util import generate_random_64bit_string
      from py_zipkin.util import ZipkinAttrs
      import time
      
      app = Flask(__name__)
      
      def do_stuff(trace_id, span_id, parent_span_id):
          time.sleep(2)
          headers = create_http_headers_for_new_span()
          headers["X-B3-TraceId"] = trace_id
          headers["X-B3-SpanId"] = span_id
          headers["X-B3-ParentSpanId"] = parent_span_id
          print "SEND TO INGRESS HEADERS : {0}".format(headers)
          r = requests.get('http://${INGRESS_GATE_WAY_IP}/productpage', headers=headers)
          return 'OK'
      
      
      def http_transport(encoded_span):
          # encoding prefix explained in https://github.com/Yelp/py_zipkin#transport
          body=encoded_span
          zipkin_url="${XTRACE_ZIPKIN_V1_ENDPOINT}"
          headers = {"Content-Type": "application/x-thrift"}
      
          # You'd probably want to wrap this in a try/except in case POSTing fails
          r=requests.post(zipkin_url, data=body, headers=headers)
          print(body)
      
      
      @app.route('/')
      def index():
          with zipkin_span(
              service_name='external-proxy',
              span_name='external-proxy/inbound',
              transport_handler=http_transport,
              port=5000,
              sample_rate=100, 
          ) as inbound_span:
          do_stuff(inbound_span.zipkin_attrs.trace_id, inbound_span.zipkin_attrs.span_id, inbound_span.zipkin_attrs.parent_span_id)
      
          return 'OK', 200
      
      if __name__=='__main__':
          app.run(host="0.0.0.0",port=5000,debug=True)
  2. Run the following command to start ExternalProxy:

    python ExternalProxy.py
     * Serving Flask app "main" (lazy loading)
     * Environment: production
       WARNING: This is a development server. Do not use it in a production deployment.
       Use a production WSGI server instead.
     * Debug mode: on
     * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
     * Restarting with stat
     * Debugger is active!
     * Debugger PIN: 317-792-686
  3. Run the following command to call ExternalProxy:

    curl localhost:5000

    Expected output:

    OK

Step 4: View tracing data

  1. Log on to the Managed Service for OpenTelemetry console.

  2. In the left-side navigation pane, click Applications. In the upper part of the Applications page, select the region where the ACK cluster resides. Then, click ExternalProxy in the application list.

  3. On the page that appears, click Application Details in the left-side navigation pane. On the Application Details page, click the Traces tab. Then, click the trace ID of the call trace that you want to query.

    A call trace is generated between the ExternalProxy application that is deployed outside the ASM instance and the Bookinfo application that is deployed inside the ASM instance.