רישום

רישום ומעקב אחרי עבודה במקביל כדי לעזור לכם להבין ולשפר את ביצועי האפליקציה, וגם לאבחן שגיאות ובעיות שקשורות למערכת. כדאי להפעיל יומני סיכום לכל הקריאות ל-API ולכל היומנים המפורטים לקריאות ל-API שנכשלו, כדי שתוכלו לספק את יומני הקריאות ל-API כשיש צורך בתמיכה טכנית.

רישום ביומן של ספריית לקוח

ספריות הלקוח של Google Ads API כוללות רישום מובנה ביומן. אם אתם רוצים לקבל פרטים על הרישום ביומן, תוכלו להיעזר במאמרי העזרה לרישום ביומן בספריית הלקוח.

Language הדרכות
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 יש גם API ל-REST.

יש כמה אפשרויות לרישום ביומן ב-Cloud Logging, או בכלי אחר, מספריית לקוח של Google Ads API. לכל אפשרות יש יתרונות משלה של זמן להטמעה, למורכבות ולביצועים. כדאי לחשוב היטב על האיזונים האלה לפני שמחליטים איזה פתרון להטמיע.

אפשרות 1: כתיבת יומנים מקומיים לענן מתהליך ברקע

אפשר לכתוב יומנים של ספריית לקוח בקובץ מקומי במכונה על ידי שינוי הגדרות הרישום ביומן. אחרי הפלט של היומנים לקובץ מקומי, אפשר להגדיר דימון (daemon) כדי לאסוף את היומנים ולשלוח אותם לענן.

מגבלה אחת בגישה הזו היא שמדדי ביצועים מסוימים לא יתועדו כברירת מחדל. היומנים של ספריית הלקוח כוללים פרטים מהאובייקטים של הבקשה והתשובה, כך שמדדי זמן האחזור לא ייכללו אלא אם יבוצעו שינויים נוספים ביומן.

אפשרות 2: מריצים את האפליקציה ב-Compute Engine ומתקינים את סוכן התפעול

אם האפליקציה שלכם פועלת על Compute Engine, תוכלו לשלוח את היומנים ל-Google Cloud Logging על ידי התקנת סוכן התפעול. אפשר להגדיר את סוכן התפעול כך שישלח את יומני האפליקציות ל-Cloud Logging, בנוסף למדדים והיומנים שנשלחים כברירת מחדל.

אם האפליקציה שלכם כבר פועלת בסביבת Google Cloud, או אם אתם שוקלים להעביר אותה ל-Google Cloud, זו אפשרות מצוינת.

אפשרות 3: הטמעת רישום ביומן של קוד האפליקציה

ניתן להתחבר ישירות מקוד האפליקציה באחת משתי דרכים:

  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. המיירט בהתאמה אישית נוצר ומועבר ללקוח השירות. לאחר מכן הוא ניגש לאובייקטים של הבקשה והתשובה שעוברים בכל קריאה ל-method של השירות, מעבד נתונים מהאובייקטים האלה ושולח את הנתונים ל-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