การบันทึก

การบันทึกและการตรวจสอบทำงานร่วมกันเพื่อช่วยให้คุณเข้าใจและเพิ่มประสิทธิภาพ ของแอปพลิเคชัน รวมถึงเพื่อวินิจฉัยข้อผิดพลาดและปัญหาที่เกี่ยวข้องกับระบบ คุณควรเปิดบันทึกสรุปสำหรับการเรียก API ทั้งหมด และบันทึกแบบละเอียดสำหรับการเรียก API ที่ล้มเหลว เพื่อให้คุณสามารถระบุบันทึกการเรียก API เมื่อต้องการการสนับสนุนด้านเทคนิค

การบันทึกไลบรารีของไคลเอ็นต์

ไลบรารีของไคลเอ็นต์ Google Ads API มาพร้อมกับการบันทึกในตัว โปรดดูรายละเอียดการบันทึกเฉพาะแพลตฟอร์มในเอกสารประกอบการบันทึกภายในไลบรารีของไคลเอ็นต์ที่คุณเลือก

ภาษา คู่มือ
Java เอกสารการบันทึกสำหรับ Java
.NET เอกสารประกอบการบันทึกสำหรับ .NET
PHP เอกสารการบันทึกสำหรับ PHP
Python เอกสารการบันทึกสำหรับ Python
Ruby การบันทึกเอกสารสำหรับ Ruby
Perl การบันทึกเอกสารสำหรับ Perl

รูปแบบบันทึก

ไลบรารีของไคลเอ็นต์ Google Ads API จะสร้างบันทึกแบบละเอียดและบันทึก สรุปสำหรับการเรียก API แต่ละครั้ง บันทึกแบบละเอียดจะมีรายละเอียดทั้งหมดของการเรียก API ในขณะที่บันทึกสรุปจะมีรายละเอียดการเรียก API น้อยที่สุด ตัวอย่างบันทึกแต่ละประเภทจะแสดงโดยมีการตัดทอนและจัดรูปแบบบันทึก เพื่อให้สามารถอ่านได้

บันทึกสรุป

GoogleAds.SummaryRequestLogs Warning: 1 : [2023-09-15 19:58:39Z] -
Request made: Host: , Method: /google.ads.googleads.v14.services.GoogleAdsService/SearchStream,
ClientCustomerID: 5951878031, RequestID: hELhBPNlEDd8mWYcZu7b8g,
IsFault: True, FaultMessage: Status(StatusCode="InvalidArgument",
Detail="Request contains an invalid argument.")

บันทึกโดยละเอียด

GoogleAds.DetailedRequestLogs Verbose: 1 : [2023-11-02 21:09:36Z] -
---------------BEGIN API CALL---------------

Request
-------

Method Name: /google.ads.googleads.v14.services.GoogleAdsService/SearchStream
Host:
Headers: {
  "x-goog-api-client": "gl-dotnet/5.0.0 gapic/17.0.1 gax/4.2.0 grpc/2.46.3 gccl/3.0.1 pb/3.21.5",
  "developer-token": "REDACTED",
  "login-customer-id": "1234567890",
  "x-goog-request-params": "customer_id=4567890123"
}

{ "customerId": "4567890123", "query": "SELECT ad_group_criterion.type FROM
  ad_group_criterion WHERE ad_group.status IN(ENABLED, PAUSED) AND
  campaign.status IN(ENABLED, PAUSED) ", "summaryRowSetting": "NO_SUMMARY_ROW" }

Response
--------
Headers: {
  "date": "Thu, 02 Nov 2023 21:09:35 GMT",
  "alt-svc": "h3-29=\":443\"; ma=2592000"
}

{
  "results": [ {
    "adGroupCriterion": {
      "resourceName": "customers/4567890123/adGroupCriteria/456789456789~123456123467",
      "type": "KEYWORD"
    } }, {
    "adGroupCriterion": {
      "resourceName": "customers/4567890123/adGroupCriteria/456789456789~56789056788",
      "type": "KEYWORD"
    } } ],
    "fieldMask": "adGroupCriterion.type", "requestId": "VsJ4F00ew6s9heHvAJ-abw"
}
----------------END API CALL----------------

จะเกิดอะไรขึ้นหากฉันไม่ได้ใช้ไลบรารีของไคลเอ็นต์

หากไม่ได้ใช้ไลบรารีของไคลเอ็นต์ ให้ใช้การบันทึกของคุณเองเพื่อบันทึก รายละเอียดของการเรียก API ขาออกและขาเข้า คุณควรบันทึกค่าของส่วนหัวการตอบกลับ request-id อย่างน้อย ซึ่งจะแชร์กับทีมสนับสนุนด้านเทคนิคได้ตามที่จำเป็น

การบันทึกไปยังระบบคลาวด์

คุณใช้เครื่องมือมากมายเพื่อบันทึกเมตริกบันทึกและประสิทธิภาพสำหรับแอปพลิเคชัน ได้ เช่น คุณสามารถใช้ Google Cloud Logging เพื่อบันทึก เมตริกประสิทธิภาพไปยังโปรเจ็กต์ที่อยู่ใน Google Cloud ซึ่งจะช่วยให้ตั้งค่าแดชบอร์ดและการแจ้งเตือนใน Google Cloud Monitoring เพื่อใช้ประโยชน์จากเมตริกที่บันทึกไว้ได้

Cloud Logging มีไลบรารีของไคลเอ็นต์สำหรับภาษาของไลบรารีไคลเอ็นต์ Google Ads API ที่รองรับทั้งหมด ยกเว้น Perl ดังนั้นในกรณีส่วนใหญ่ คุณจะบันทึกด้วย Cloud Logging ได้โดยตรงจากการผสานรวมไลบรารีของไคลเอ็นต์ สำหรับภาษาอื่นๆ รวมถึง Perl Cloud Logging ยังมี REST API ด้วย

คุณมีตัวเลือกในการบันทึกไปยัง Cloud Logging หรือเครื่องมืออื่นจากไลบรารีของไคลเอ็นต์ Google Ads API อยู่ 2-3 ตัวเลือก แต่ละตัวเลือกมีข้อดีข้อเสียของเวลาในการ ติดตั้งใช้งาน ความซับซ้อน และประสิทธิภาพ โปรดพิจารณาข้อดีข้อเสียเหล่านี้อย่างรอบคอบ ก่อนตัดสินใจว่าจะใช้โซลูชันใด

ตัวเลือกที่ 1: เขียนบันทึกในเครื่องไปยังระบบคลาวด์จากกระบวนการเบื้องหลัง

คุณเขียนบันทึกของไลบรารีไคลเอ็นต์ลงในไฟล์ในเครื่องได้โดยการแก้ไข การกำหนดค่าการบันทึก เมื่อส่งออกบันทึกไปยังไฟล์ในเครื่องแล้ว คุณจะ ตั้งค่า Daemon เพื่อรวบรวมบันทึกและส่งไปยังระบบคลาวด์ได้

ข้อจำกัดอย่างหนึ่งของแนวทางนี้คือเมตริกประสิทธิภาพบางอย่างจะไม่ ได้รับการบันทึกโดยค่าเริ่มต้น บันทึกของไลบรารีไคลเอ็นต์มีรายละเอียดจากออบเจ็กต์คำขอและ การตอบกลับ ดังนั้นระบบจะไม่รวมเมตริกเวลาในการตอบสนอง เว้นแต่จะมีการเปลี่ยนแปลงเพิ่มเติม เพื่อบันทึกสิ่งเหล่านี้ด้วย

ตัวเลือกที่ 2: เรียกใช้แอปพลิเคชันใน Compute Engine และติดตั้ง Ops Agent

หากแอปพลิเคชันทำงานใน Compute Engine คุณจะส่งบันทึกไปยัง Google Cloud Logging ได้โดยการติดตั้ง Ops Agent คุณกำหนดค่า Ops Agent ให้ส่งบันทึกของแอปพลิเคชันไปยัง Cloud Logging ได้ นอกเหนือจากเมตริกและบันทึกที่ส่งโดยค่าเริ่มต้น

หากแอปพลิเคชันของคุณทำงานในสภาพแวดล้อม Google Cloud อยู่แล้ว หรือหากคุณ กำลังพิจารณาย้ายแอปพลิเคชันไปยัง Google Cloud นี่เป็นตัวเลือกที่ยอดเยี่ยม ที่ควรพิจารณา

ตัวเลือกที่ 3: ใช้การบันทึกในโค้ดแอปพลิเคชัน

การบันทึกโดยตรงจากโค้ดแอปพลิเคชันทำได้ 2 วิธี ดังนี้

  1. รวมการคำนวณเมตริกและคำสั่งบันทึกในทุกตำแหน่งที่เกี่ยวข้องในโค้ด ตัวเลือกนี้เหมาะกับโค้ดเบสขนาดเล็กมากกว่า เนื่องจากขอบเขตและค่าใช้จ่ายในการบำรุงรักษาของการเปลี่ยนแปลงดังกล่าวจะ น้อยที่สุด

  2. การติดตั้งใช้งานอินเทอร์เฟซการบันทึก หากสามารถแยกตรรกะของแอปพลิเคชัน เพื่อให้ชิ้นส่วนต่างๆ ของแอปพลิเคชันสืบทอดมาจากคลาสฐานเดียวกันได้ ก็สามารถใช้ตรรกะการบันทึกในคลาสฐานนั้นได้ โดยทั่วไปแล้ว เราขอแนะนำให้ใช้ตัวเลือกนี้แทนการรวมคำสั่งบันทึกลงในโค้ดแอปพลิเคชัน เนื่องจากดูแลรักษาและปรับขนาดได้ง่ายกว่า สำหรับโค้ดเบสขนาดใหญ่ ความสามารถในการบำรุงรักษาและความสามารถในการปรับขนาดของโซลูชันนี้จะมีความเกี่ยวข้องมากยิ่งขึ้น

ข้อจำกัดอย่างหนึ่งของแนวทางนี้คือโค้ดแอปพลิเคชันไม่สามารถเข้าถึงบันทึกคำขอและการตอบกลับแบบเต็มได้ เข้าถึงออบเจ็กต์คำขอและการตอบกลับแบบเต็มได้จากตัวสกัดกั้น gRPC ซึ่งเป็นวิธีที่ไลบรารีไคลเอ็นต์ในตัว บันทึกจะรับบันทึกคำขอและการตอบกลับ ในกรณีที่เกิดข้อผิดพลาด อาจมีข้อมูลเพิ่มเติมในออบเจ็กต์ข้อยกเว้น แต่จะมีรายละเอียดน้อยกว่าสำหรับการตอบกลับที่สำเร็จภายในตรรกะของแอปพลิเคชัน เช่น ในกรณีส่วนใหญ่ คุณจะเข้าถึงรหัสคำขอสำหรับคำขอที่สำเร็จจากออบเจ็กต์การตอบกลับของ Google Ads API ไม่ได้

ตัวเลือกที่ 4: ใช้ตัวสกัดกั้นการบันทึก gRPC ที่กำหนดเอง

gRPC รองรับอินเทอร์เซ็ปเตอร์แบบเอกภาคและแบบสตรีมมิงซึ่งเข้าถึงออบเจ็กต์คำขอและการตอบกลับได้ขณะที่ส่งผ่านระหว่างไคลเอ็นต์กับเซิร์ฟเวอร์ ไลบรารีของไคลเอ็นต์ Google Ads API ใช้ตัวสกัดกั้น gRPC เพื่อรองรับการบันทึกในตัว ในทำนองเดียวกัน คุณสามารถใช้ตัวสกัดกั้น gRPC ที่กำหนดเองเพื่อเข้าถึงออบเจ็กต์คำขอและการตอบกลับ ดึงข้อมูลเพื่อวัตถุประสงค์ในการบันทึกและการตรวจสอบ และเขียนข้อมูลนั้นไปยังตำแหน่งที่คุณเลือก

การใช้ตัวสกัดกั้น gRPC แบบกำหนดเองจะช่วยให้คุณมีความยืดหยุ่นในการบันทึกออบเจ็กต์คำขอและการตอบกลับในทุกคำขอ รวมถึงใช้ตรรกะเพิ่มเติมเพื่อบันทึกรายละเอียดของคำขอ ซึ่งแตกต่างจากโซลูชันอื่นๆ บางส่วนที่นำเสนอที่นี่ เช่น คุณสามารถคำนวณเวลาที่ผ่านไปของคำขอได้โดยการใช้ตรรกะการจับเวลาประสิทธิภาพภายในตัวสกัดกั้นที่กำหนดเอง จากนั้นบันทึกเมตริกลงใน Google Cloud Logging เพื่อให้พร้อมใช้งานสำหรับการตรวจสอบเวลาในการตอบสนองภายใน Google Cloud Monitoring

ตัวสกัดกั้น Google Cloud Logging ที่กำหนดเองใน Python

เราได้เขียนตัวอย่างของตัวสกัดกั้นการบันทึกที่กำหนดเองใน Python เพื่อแสดงโซลูชันนี้ ระบบจะสร้าง Interceptor ที่กำหนดเองและส่งไปยังไคลเอ็นต์บริการ จากนั้นจะเข้าถึงออบเจ็กต์คำขอและการตอบกลับที่ส่งผ่าน ในการเรียกใช้เมธอดบริการทุกครั้ง ประมวลผลข้อมูลจากออบเจ็กต์เหล่านั้น และ ส่งข้อมูลไปยัง Cloud Logging

นอกเหนือจากข้อมูลที่มาจากออบเจ็กต์คำขอและการตอบกลับแล้ว ตัวอย่างยังใช้ตรรกะเพิ่มเติมเพื่อบันทึกเวลาที่ผ่านไปของคำขอ และข้อมูลเมตาอื่นๆ ที่จะเป็นประโยชน์ต่อการตรวจสอบ เช่น คำขอสำเร็จหรือไม่ ดูข้อมูลเพิ่มเติมเกี่ยวกับประโยชน์ของข้อมูลนี้ ทั้งในแง่ของการตรวจสอบโดยทั่วไปและในแง่ของการ รวม Google Cloud Logging กับ Google Cloud Monitoring โดยเฉพาะได้ที่คู่มือการตรวจสอบ

# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A custom gRPC Interceptor that logs requests and responses to Cloud Logging.

The custom interceptor object is passed into the get_service method of the
GoogleAdsClient. It intercepts requests and responses, parses them into a
human readable structure and logs them using the logging service instantiated
within the class (in this case, a Cloud Logging client).
"""

import time
from typing import Any, Callable, Dict, Optional

from google.cloud import logging as google_cloud_logging
from grpc._interceptor import _ClientCallDetails

from google.ads.googleads.interceptors import LoggingInterceptor


class CloudLoggingInterceptor(LoggingInterceptor):
    """An interceptor that logs rpc request and response details to Google Cloud Logging.

    This class inherits logic from the LoggingInterceptor, which simplifies the
    implementation here. Some logic is required here in order to make the
    underlying logic work -- comments make note of this where applicable.
    NOTE: Inheriting from the LoggingInterceptor class could yield unexpected side
    effects. For example, if the LoggingInterceptor class is updated, this class would
    inherit the updated logic, which could affect its functionality. One option to avoid
    this is to inherit from the Interceptor class instead, and selectively copy whatever
    logic is needed from the LoggingInterceptor class."""

    def __init__(self, api_version: str):
        """Initializer for the CloudLoggingInterceptor.

        Args:
            api_version: a str of the API version of the request.
        """
        super().__init__(logger=None, api_version=api_version)
        # Instantiate the Cloud Logging client.
        logging_client: google_cloud_logging.Client = google_cloud_logging.Client()
        self.logger: google_cloud_logging.Logger = logging_client.logger("cloud_logging")
        self.rpc_start: float
        self.rpc_end: float

    def log_successful_request(
        self,
        method: str,
        customer_id: Optional[str],
        metadata_json: str,
        request_id: str,
        request: Any,  # google.ads.googleads.vX.services.types.SearchGoogleAdsRequest or SearchGoogleAdsStreamRequest
        trailing_metadata_json: str,
        response: Any,  # grpc.Call or grpc.Future
    ) -> None:
        """Handles logging of a successful request.

        Args:
            method: The method of the request.
            customer_id: The customer ID associated with the request.
            metadata_json: A JSON str of initial_metadata.
            request_id: A unique ID for the request provided in the response.
            request: An instance of a request proto message.
            trailing_metadata_json: A JSON str of trailing_metadata.
            response: A grpc.Call/grpc.Future instance.
        """
        # Retrieve and mask the RPC result from the response future.
        # This method is available from the LoggingInterceptor class.
        # Ensure self._cache is set in order for this to work.
        # The response result could contain up to 10,000 rows of data,
        # so consider truncating this value before logging it, to save
        # on data storage costs and maintain readability.
        result: Any = self.retrieve_and_mask_result(response)

        # elapsed_ms is the approximate elapsed time of the RPC, in milliseconds.
        # There are different ways to define and measure elapsed time, so use
        # whatever approach makes sense for your monitoring purposes.
        # rpc_start and rpc_end are set in the intercept_unary_* methods below.
        elapsed_ms: float = (self.rpc_end - self.rpc_start) * 1000

        debug_log: Dict[str, Any] = {
            "method": method,
            "host": metadata_json,
            "request_id": request_id,
            "request": str(request),
            "headers": trailing_metadata_json,
            "response": str(result),
            "is_fault": False,
            "elapsed_ms": elapsed_ms,
        }
        self.logger.log_struct(debug_log, severity="DEBUG")

        info_log: Dict[str, Any] = {
            "customer_id": customer_id,
            "method": method,
            "request_id": request_id,
            "is_fault": False,
            # Available from the Interceptor class.
            "api_version": self._api_version,
        }
        self.logger.log_struct(info_log, severity="INFO")

    def log_failed_request(
        self,
        method: str,
        customer_id: Optional[str],
        metadata_json: str,
        request_id: str,
        request: Any,  # google.ads.googleads.vX.services.types.SearchGoogleAdsRequest or SearchGoogleAdsStreamRequest
        trailing_metadata_json: str,
        response: Any,  # grpc.Call or grpc.Future
    ) -> None:
        """Handles logging of a failed request.

        Args:
            method: The method of the request.
            customer_id: The customer ID associated with the request.
            metadata_json: A JSON str of initial_metadata.
            request_id: A unique ID for the request provided in the response.
            request: An instance of a request proto message.
            trailing_metadata_json: A JSON str of trailing_metadata.
            response: A JSON str of the response message.
        """
        exception: Any = self._get_error_from_response(response)
        exception_str: str = self._parse_exception_to_str(exception)
        fault_message: str = self._get_fault_message(exception)

        info_log: Dict[str, Any] = {
            "method": method,
            "endpoint": self.endpoint,
            "host": metadata_json,
            "request_id": request_id,
            "request": str(request),
            "headers": trailing_metadata_json,
            "exception": exception_str,
            "is_fault": True,
        }
        self.logger.log_struct(info_log, severity="INFO")

        error_log: Dict[str, Any] = {
            "method": method,
            "endpoint": self.endpoint,
            "request_id": request_id,
            "customer_id": customer_id,
            "is_fault": True,
            "fault_message": fault_message,
        }
        self.logger.log_struct(error_log, severity="ERROR")

    def intercept_unary_unary(
        self,
        continuation: Callable[[_ClientCallDetails, Any], Any], # Any is request type
        client_call_details: _ClientCallDetails,
        request: Any,  # google.ads.googleads.vX.services.types.SearchGoogleAdsRequest
    ) -> Any:  # grpc.Call or grpc.Future
        """Intercepts and logs API interactions.

        Overrides abstract method defined in grpc.UnaryUnaryClientInterceptor.

        Args:
            continuation: a function to continue the request process.
            client_call_details: a grpc._interceptor._ClientCallDetails
                instance containing request metadata.
            request: a SearchGoogleAdsRequest or SearchGoogleAdsStreamRequest
                message class instance.

        Returns:
            A grpc.Call/grpc.Future instance representing a service response.
        """
        # Set the rpc_end value to current time when RPC completes.
        def update_rpc_end(response_future: Any) -> None: # response_future is grpc.Future
            self.rpc_end = time.perf_counter()

        # Capture precise clock time to later calculate approximate elapsed
        # time of the RPC.
        self.rpc_start = time.perf_counter()

        # The below call is REQUIRED.
        response: Any = continuation(client_call_details, request) # response is grpc.Call or grpc.Future

        response.add_done_callback(update_rpc_end)

        self.log_request(client_call_details, request, response)

        # The below return is REQUIRED.
        return response

    def intercept_unary_stream(
        self,
        continuation: Callable[[_ClientCallDetails, Any], Any], # Any is request type
        client_call_details: _ClientCallDetails,
        request: Any,  # google.ads.googleads.vX.services.types.SearchGoogleAdsStreamRequest
    ) -> Any:  # grpc.Call or grpc.Future
        """Intercepts and logs API interactions for Unary-Stream requests.

        Overrides abstract method defined in grpc.UnaryStreamClientInterceptor.

        Args:
            continuation: a function to continue the request process.
            client_call_details: a grpc._interceptor._ClientCallDetails
                instance containing request metadata.
            request: a SearchGoogleAdsRequest or SearchGoogleAdsStreamRequest
                message class instance.

        Returns:
            A grpc.Call/grpc.Future instance representing a service response.
        """

        def on_rpc_complete(response_future: Any) -> None: # response_future is grpc.Future
            self.rpc_end = time.perf_counter()
            self.log_request(client_call_details, request, response_future)

        # Capture precise clock time to later calculate approximate elapsed
        # time of the RPC.
        self.rpc_start = time.perf_counter()

        # The below call is REQUIRED.
        response: Any = continuation(client_call_details, request) # response is grpc.Call or grpc.Future

        # Set self._cache to the cache on the response wrapper in order to
        # access the streaming logs. This is REQUIRED in order to log streaming
        # requests.
        self._cache = response.get_cache()

        response.add_done_callback(on_rpc_complete)

        # The below return is REQUIRED.
        return response