Logging

การบันทึกและการตรวจสอบจะทำงานควบคู่กันไปเพื่อช่วยให้คุณเข้าใจและเพิ่มประสิทธิภาพ ประสิทธิภาพของแอปพลิเคชัน รวมถึงเพื่อวินิจฉัยข้อผิดพลาด ปัญหา คุณควรเปิดบันทึกสรุปสำหรับการเรียก 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 แต่ละตัวเลือกจะมาพร้อมกับข้อดี และเวลาในการแตกต่างกันไป การใช้งาน ความซับซ้อน และประสิทธิภาพ โปรดคิดให้รอบคอบเกี่ยวกับข้อดีข้อเสียเหล่านี้ ก่อนตัดสินใจเลือกโซลูชันที่จะใช้

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

ในการสาธิตโซลูชันนี้ เราได้เขียนตัวอย่างของการบันทึกที่กำหนดเอง ใน Python เครื่องมือดักจับที่กำหนดเองจะสร้างขึ้นและส่งผ่านไปยัง บริการ จากนั้นจะเข้าถึงออบเจ็กต์คำขอและการตอบกลับที่ส่งผ่าน ผ่านการเรียกใช้เมธอดบริการทั้งหมด ประมวลผลข้อมูลจากออบเจ็กต์เหล่านั้น และ ส่งข้อมูลไปยัง Google 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 logging
import time

from google.cloud import logging
from grpc import UnaryUnaryClientInterceptor, UnaryStreamClientInterceptor

from google.ads.googleads.interceptors import LoggingInterceptor, mask_message


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):
        """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 = logging.Client()
        self.logger = logging_client.logger("cloud_logging")

    def log_successful_request(
        self,
        method,
        customer_id,
        metadata_json,
        request_id,
        request,
        trailing_metadata_json,
        response,
    ):
        """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 = 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 = (self.rpc_end - self.rpc_start) * 1000

        debug_log = {
            "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 = {
            "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,
        customer_id,
        metadata_json,
        request_id,
        request,
        trailing_metadata_json,
        response,
    ):
        """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 = self._get_error_from_response(response)
        exception_str = self._parse_exception_to_str(exception)
        fault_message = self._get_fault_message(exception)

        info_log = {
            "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 = {
            "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, client_call_details, request):
        """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):
            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 = continuation(client_call_details, request)

        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, client_call_details, request
    ):
        """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):
            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 = continuation(client_call_details, request)

        # 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