लॉग इन हो रहा है

लॉगिंग और मॉनिटरिंग, दोनों एक साथ काम करती हैं. इससे आपको ऐप्लिकेशन की परफ़ॉर्मेंस को समझने और उसे ऑप्टिमाइज़ करने में मदद मिलती है. साथ ही, गड़बड़ियों और सिस्टम से जुड़ी समस्याओं का पता लगाने में भी मदद मिलती है. आपको सभी एपीआई कॉल के लिए खास जानकारी वाले लॉग और पूरे न हो पाने वाले एपीआई कॉल के लिए ज़्यादा जानकारी वाले लॉग चालू करने चाहिए, ताकि तकनीकी सहायता की ज़रूरत पड़ने पर, एपीआई कॉल के लॉग उपलब्ध कराए जा सकें.

क्लाइंट लाइब्रेरी लॉगिंग

Google Ads API की क्लाइंट लाइब्रेरी में, लॉगिंग की सुविधा पहले से मौजूद होती है. प्लेटफ़ॉर्म के हिसाब से लॉगिंग की जानकारी के लिए, अपनी पसंद की क्लाइंट लाइब्रेरी में लॉगिंग से जुड़े दस्तावेज़ देखें.

भाषा गाइड
Java Java के लिए लॉगिंग दस्तावेज़
.NET .NET के लिए लॉगिंग से जुड़े दस्तावेज़
PHP PHP के लिए लॉगिंग से जुड़े दस्तावेज़
Python Python के लिए लॉगिंग से जुड़े दस्तावेज़
Ruby Ruby के लिए लॉगिंग दस्तावेज़
Perl Perl के लिए लॉगिंग दस्तावेज़

लॉग फ़ॉर्मैट

Google Ads 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----------------

अगर मुझे क्लाइंट लाइब्रेरी का इस्तेमाल नहीं करना है, तो क्या होगा?

अगर क्लाइंट लाइब्रेरी का इस्तेमाल नहीं किया जाता है, तो एपीआई कॉल की जानकारी कैप्चर करने के लिए, अपनी लॉगिंग लागू करें. आपको कम से कम request-id रिस्पॉन्स हेडर की वैल्यू लॉग करनी चाहिए. इसके बाद, इसे ज़रूरत के मुताबिक तकनीकी सहायता टीमों के साथ शेयर किया जा सकता है.

क्लाउड में लॉग करना

अपने ऐप्लिकेशन के लिए लॉग और परफ़ॉर्मेंस मेट्रिक कैप्चर करने के लिए, कई टूल इस्तेमाल किए जा सकते हैं. उदाहरण के लिए, Google Cloud Logging का इस्तेमाल करके, परफ़ॉर्मेंस मेट्रिक को अपने Google Cloud प्रोजेक्ट में लॉग किया जा सकता है. इससे, लॉग की गई मेट्रिक का इस्तेमाल करने के लिए, Google Cloud Monitoring में डैशबोर्ड और सूचनाएं सेट अप की जा सकती हैं.

Cloud Logging, Google Ads API क्लाइंट लाइब्रेरी की सभी भाषाओं के लिए क्लाइंट लाइब्रेरी उपलब्ध कराता है. हालांकि, Perl के लिए यह सुविधा उपलब्ध नहीं है. इसलिए, ज़्यादातर मामलों में Cloud Logging की मदद से, सीधे तौर पर क्लाइंट लाइब्रेरी इंटिग्रेशन से लॉग किया जा सकता है. Cloud Logging, Perl के साथ-साथ अन्य भाषाओं के लिए भी REST API उपलब्ध कराता है.

Google Ads API क्लाइंट लाइब्रेरी से Cloud Logging या किसी अन्य टूल में लॉग करने के कुछ विकल्प उपलब्ध हैं. हर विकल्प को लागू करने में लगने वाला समय, जटिलता, और परफ़ॉर्मेंस अलग-अलग होती है. इन फ़ायदों और नुकसानों के बारे में ध्यान से सोचें. इसके बाद, तय करें कि आपको कौनसी सुविधा लागू करनी है.

पहला विकल्प: बैकग्राउंड प्रोसेस से क्लाउड पर स्थानीय लॉग लिखना

लॉगिंग कॉन्फ़िगरेशन में बदलाव करके, क्लाइंट लाइब्रेरी के लॉग को अपनी मशीन पर मौजूद किसी स्थानीय फ़ाइल में लिखा जा सकता है. लॉग को किसी लोकल फ़ाइल में सेव करने के बाद, लॉग इकट्ठा करने और उन्हें क्लाउड पर भेजने के लिए, डेमॉन सेट अप किया जा सकता है.

इस तरीके की एक सीमा यह है कि कुछ परफ़ॉर्मेंस मेट्रिक डिफ़ॉल्ट रूप से कैप्चर नहीं की जाएंगी. क्लाइंट लाइब्रेरी के लॉग में, अनुरोध और जवाब ऑब्जेक्ट की जानकारी शामिल होती है. इसलिए, लेटेन्सी मेट्रिक को तब तक शामिल नहीं किया जाएगा, जब तक इन्हें भी लॉग करने के लिए अतिरिक्त बदलाव नहीं किए जाते.

दूसरा विकल्प: Compute Engine पर अपना ऐप्लिकेशन चलाएं और Ops Agent इंस्टॉल करें

अगर आपका ऐप्लिकेशन Compute Engine पर चल रहा है, तो Ops Agent इंस्टॉल करके, अपने लॉग Google Cloud Logging को भेजे जा सकते हैं. Ops Agent को कॉन्फ़िगर किया जा सकता है, ताकि वह आपके ऐप्लिकेशन के लॉग को Cloud Logging पर भेज सके. ऐसा डिफ़ॉल्ट रूप से भेजी जाने वाली मेट्रिक और लॉग के अलावा किया जा सकता है.

अगर आपका ऐप्लिकेशन पहले से ही Google Cloud एनवायरमेंट में चल रहा है या आपको अपने ऐप्लिकेशन को Google Cloud पर माइग्रेट करना है, तो यह एक बेहतरीन विकल्प है.

तीसरा विकल्प: अपने ऐप्लिकेशन कोड में लॉगिंग लागू करना

ऐप्लिकेशन कोड से सीधे लॉग इन करने के लिए, इनमें से कोई एक तरीका इस्तेमाल किया जा सकता है:

  1. अपने कोड में, मेट्रिक कैलकुलेशन और लॉग स्टेटमेंट को हर उस जगह पर शामिल करें जहां लागू हो. यह विकल्प छोटे कोडबेस के लिए ज़्यादा सही है. ऐसा इसलिए, क्योंकि इस तरह के बदलाव का दायरा और रखरखाव की लागत कम होगी.

  2. लॉगिंग इंटरफ़ेस लागू करना. अगर ऐप्लिकेशन लॉजिक को इस तरह से ऐब्स्ट्रैक्ट किया जा सकता है कि ऐप्लिकेशन के अलग-अलग हिस्से एक ही बेस क्लास से इनहेरिट करें, तो उस बेस क्लास में लॉगिंग लॉजिक लागू किया जा सकता है. आम तौर पर, इस विकल्प को पूरे ऐप्लिकेशन कोड में लॉग स्टेटमेंट शामिल करने के बजाय प्राथमिकता दी जाती है. ऐसा इसलिए, क्योंकि इसे बनाए रखना और स्केल करना आसान होता है. बड़े कोडबेस के लिए, इस समाधान को बनाए रखना और इसे स्केल करना ज़्यादा ज़रूरी है.

इस तरीके की एक सीमा यह है कि ऐप्लिकेशन कोड से, अनुरोध और जवाब के पूरे लॉग उपलब्ध नहीं होते. gRPC इंटरसेप्टर से अनुरोध और जवाब के पूरे ऑब्जेक्ट ऐक्सेस किए जा सकते हैं. इस तरह, क्लाइंट लाइब्रेरी में लॉगिंग की सुविधा, अनुरोध और जवाब के लॉग हासिल करती है. गड़बड़ी होने पर, अपवाद ऑब्जेक्ट में ज़्यादा जानकारी उपलब्ध हो सकती है. हालांकि, ऐप्लिकेशन लॉजिक में सही जवाबों के लिए कम जानकारी उपलब्ध होती है. उदाहरण के लिए, ज़्यादातर मामलों में, अनुरोध पूरा होने पर मिलने वाले अनुरोध आईडी को Google Ads API के जवाब ऑब्जेक्ट से ऐक्सेस नहीं किया जा सकता.

चौथा विकल्प: कस्टम gRPC लॉगिंग इंटरसेप्टर लागू करना

gRPC, यूनरी और स्ट्रीमिंग इंटरसेप्टर के साथ काम करता है. ये इंटरसेप्टर, अनुरोध और जवाब वाले ऑब्जेक्ट को ऐक्सेस कर सकते हैं. ये ऑब्जेक्ट, क्लाइंट और सर्वर के बीच ट्रांसफ़र होते हैं. Google Ads API की क्लाइंट लाइब्रेरी, gRPC इंटरसेप्टर का इस्तेमाल करती हैं. इससे, लॉगिंग की सुविधा पहले से मौजूद होती है. इसी तरह, अनुरोध और जवाब ऑब्जेक्ट को ऐक्सेस करने के लिए, कस्टम gRPC इंटरसेप्टर लागू किया जा सकता है. साथ ही, लॉगिंग और मॉनिटरिंग के लिए जानकारी निकाली जा सकती है. इसके अलावा, उस डेटा को अपनी पसंद की जगह पर लिखा जा सकता है.

यहां दिए गए कुछ अन्य समाधानों के उलट, कस्टम gRPC इंटरसेप्टर को लागू करने से आपको हर अनुरोध पर अनुरोध और जवाब ऑब्जेक्ट कैप्चर करने की सुविधा मिलती है. साथ ही, अनुरोध की जानकारी कैप्चर करने के लिए अतिरिक्त लॉजिक लागू करने की सुविधा मिलती है. उदाहरण के लिए, कस्टम इंटरसेप्टर में परफ़ॉर्मेंस टाइमिंग लॉजिक लागू करके, किसी अनुरोध के पूरे होने में लगे समय का हिसाब लगाया जा सकता है. इसके बाद, इस मेट्रिक को Google Cloud Logging में लॉग किया जा सकता है, ताकि इसे Google Cloud Monitoring में लेटेन्सी मॉनिटरिंग के लिए उपलब्ध कराया जा सके.

Python में Google Cloud Logging का कस्टम इंटरसेप्टर

इस समाधान को दिखाने के लिए, हमने 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 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