查看應用程式要求的延遲時間

瞭解如何收集及查看應用程式的延遲時間資料:

  1. 使用 Google Cloud CLI 建立 Google Kubernetes Engine (GKE) 叢集。

  2. 下載範例應用程式並部署至叢集。

  3. 將 HTTP 要求傳送至範例應用程式,建立追蹤記錄。

  4. 找出建立的追蹤記錄,即可查看延遲時間資訊。

  5. 清除所用資源。


如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


事前準備

  1. 貴機構定義的安全性限制,可能會導致您無法完成下列步驟。如需疑難排解資訊,請參閱「在受限的 Google Cloud 環境中開發應用程式」。

  2. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  3. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  4. Make sure that billing is enabled for your Google Cloud project.

  5. Enable the Google Kubernetes Engine and Cloud Trace APIs.

    Enable the APIs

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Enable the Google Kubernetes Engine and Cloud Trace APIs.

    Enable the APIs

  9. 建立 GKE 叢集

    本指南將建立標準 GKE 叢集。如果您使用 Google Kubernetes Engine 的 Autopilot 模式,或是啟用 GKE 適用的工作負載身分聯盟,則必須將應用程式設為使用 GKE 適用的工作負載身分聯盟

    1. 按一下工具列中的 「Activate Cloud Shell」(啟用 Cloud Shell),然後在 Cloud Shell 中執行以下步驟。

    2. 建立叢集:

      gcloud container clusters create cloud-trace-demo --zone us-central1-c
      

      上述指令只需幾分鐘即可完成。該指令會在可用區 us-central1-c 中建立名為 cloud-trace-demo 的標準叢集。

    3. kubectl 設為自動重新整理憑證,即可使用與 Google Cloud CLI 相同的身分識別資訊:

      gcloud container clusters get-credentials cloud-trace-demo --zone us-central1-c
      
    4. 驗證叢集存取權:

      kubectl get nodes
      

      這個指令的範例輸出內容如下:

      NAME                                              STATUS   ROLES    AGE   VERSION
      gke-cloud-trace-demo-default-pool-063c0416-113s   Ready    <none>   78s   v1.22.12-gke.2300
      gke-cloud-trace-demo-default-pool-063c0416-1n27   Ready    <none>   79s   v1.22.12-gke.2300
      gke-cloud-trace-demo-default-pool-063c0416-frkd   Ready    <none>   78s   v1.22.12-gke.2300
      

    下載及部署應用程式

    下載並部署使用 Flask 框架和 OpenTelemetry 套件的 Python 應用程式。本頁的「關於應用程式」部分會說明應用程式。

    在 Cloud Shell 中執行下列操作:

    1. 從 GitHub 複製 Python 應用程式:

      git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
      
    2. 執行下列指令來部署範例應用程式:

      cd python-docs-samples/trace/cloud-trace-demo-app-opentelemetry && ./setup.sh
      

      指令碼 setup.sh 需要幾分鐘才會完成。

      這個指令碼會使用預建的映像檔設定三項服務,然後等候系統佈建所有資源。工作負載名稱為 cloud-trace-demo-acloud-trace-demo-bcloud-trace-demo-c

      這個指令的範例輸出內容如下:

      deployment.apps/cloud-trace-demo-a is created
      service/cloud-trace-demo-a is created
      deployment.apps/cloud-trace-demo-b is created
      service/cloud-trace-demo-b is created
      deployment.apps/cloud-trace-demo-c is created
      service/cloud-trace-demo-c is created
      
      Wait for load balancer initialization complete......
      Completed.
      

    建立追蹤記錄資料

    「追蹤記錄」會記錄應用程式完成單次作業所需的時間。

    要建立追蹤記錄,請在 Cloud Shell 中執行下列指令:

    curl $(kubectl get svc -o=jsonpath='{.items[?(@.metadata.name=="cloud-trace-demo-a")].status.loadBalancer.ingress[0].ip}')
    

    上述指令的回應如下所示:

    Hello, I am service A
    And I am service B
    Hello, I am service C
    

    執行 curl 指令數次,即可產生多個追蹤記錄。

    查看延遲時間資料

    1. 前往 Google Cloud 控制台的「Trace Explorer」頁面:

      前往「Trace Explorer」頁面

      您也可以透過搜尋列找到這個頁面。

      下列螢幕截圖顯示多次執行 curl 指令的結果。這張圖表會顯示「spans」的匯總延遲時間資料,這些資料會說明追蹤記錄的子作業。

      快速入門的追蹤記錄探索器視窗。

    2. 如要查看追蹤記錄詳情,請選取圖表中的時距或資料表中任一列。

      系統會開啟追蹤記錄詳細資料對話方塊,如以下螢幕截圖所示:

      詳細資料窗格會顯示每個區段的其他詳細資料。

      甘特圖會顯示所選追蹤記錄的相關資訊。甘特圖的第一列代表追蹤記錄,後續每列則代表追蹤記錄中的一個時距。

    3. 在甘特圖中選取所需時距,即可查看相關詳細資訊。

    關於申請

    本快速入門導覽課程中使用的範例應用程式可在 GitHub 存放區中取得。這個存放區包含如何在 Cloud Shell 以外的環境中使用應用程式。這個應用程式範例是使用 Python 編寫,並使用 Flask 架構和 OpenTelemetry 套件,在 GKE 叢集中執行。

    本指南將建立標準 GKE 叢集。如果您使用 Google Kubernetes Engine 的 Autopilot 模式,或是啟用 GKE 適用的工作負載身分聯盟,則必須將應用程式設為使用 GKE 適用的工作負載身分聯盟

    檢測

    GitHub 存放區中的 app.py 檔案包含擷取追蹤資料並傳送至 Google Cloud 專案所需的檢測工具:

    • 應用程式會匯入多個 OpenTelemetry 套件:

      from opentelemetry import trace
      from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
      from opentelemetry.instrumentation.flask import FlaskInstrumentor
      from opentelemetry.instrumentation.requests import RequestsInstrumentor
      from opentelemetry.propagate import set_global_textmap
      from opentelemetry.propagators.cloud_trace_propagator import CloudTraceFormatPropagator
      from opentelemetry.sdk.trace import TracerProvider
      from opentelemetry.sdk.trace.export import BatchSpanProcessor
      
    • 應用程式會使用追蹤記錄內容檢測網路要求,並自動追蹤 Flask 處理常式和其他服務的要求:

      app = flask.Flask(__name__)
      FlaskInstrumentor().instrument_app(app)
      RequestsInstrumentor().instrument()
    • 應用程式會將 Cloud Trace 匯出工具設為追蹤記錄提供者,以便以 Cloud Trace 格式傳播追蹤記錄內容:

      def configure_exporter(exporter):
          """Configures OpenTelemetry context propagation to use Cloud Trace context
      
          Args:
              exporter: exporter instance to be configured in the OpenTelemetry tracer provider
          """
          set_global_textmap(CloudTraceFormatPropagator())
          tracer_provider = TracerProvider()
          tracer_provider.add_span_processor(BatchSpanProcessor(exporter))
          trace.set_tracer_provider(tracer_provider)
      
      
      configure_exporter(CloudTraceSpanExporter())
      tracer = trace.get_tracer(__name__)
    • 下列程式碼片段說明如何在 Python 中傳送要求。OpenTelemetry 會透過傳出要求,為您隱含地傳播追蹤記錄內容:

      if endpoint is not None and endpoint != "":
          data = {"body": keyword}
          response = requests.get(
              endpoint,
              params=data,
          )
          return keyword + "\n" + response.text
      else:
          return keyword, 200
      
      

    應用程式的運作方式

    為求明確,本節服務名稱中省略了 cloud-trace-demo。例如,服務 cloud-trace-demo-c 會以 c 的形式參照。

    這個應用程式會建立三項服務,分別命名為 abc。服務 a 已設定為呼叫服務 b,服務 b 已設定為呼叫服務 c。如要進一步瞭解服務的設定,請參閱 GitHub 存放區中的 YAML 檔案。

    在本快速入門課程中,您向 a 服務發出 HTTP 要求時,使用了下列 curl 指令:

    curl $(kubectl get svc -o=jsonpath='{.items[?(@.metadata.name=="cloud-trace-demo-a")].status.loadBalancer.ingress[0].ip}')
    

    curl 指令的運作方式如下:

    1. kubectl 會擷取名為 cloud-trace-demo-a 的服務 IP 位址。
    2. curl 指令隨後會將 HTTP 要求傳送至服務 a
    3. 服務 a 會接收 HTTP 要求,並將要求傳送至服務 b
    4. 服務 b 會接收 HTTP 要求,並將要求傳送至服務 c
    5. 服務 c 會接收來自服務 b 的 HTTP 要求,並將字串 Hello, I am service C 傳回至服務 b
    6. 服務 b 會收到來自服務 c 的回應,並將其附加至字串 And I am service B,然後將結果傳回至服務 a
    7. 服務 a 會收到來自服務 b 的回應,並將其附加至字串 Hello, I am service A
    8. Cloud Shell 會列印服務 a 的回應。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取您在本頁所用資源的費用,請按照下列步驟操作。

如果您建立新專案,但現在已不再需要該專案,請刪除專案

如果您使用的是現有專案,請執行下列操作:

  1. 如要刪除叢集,請在 Cloud Shell 中執行下列指令:

    gcloud container clusters delete cloud-trace-demo --zone us-central1-c

後續步驟