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

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

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

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

Language गाइड
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 प्रोजेक्ट में परफ़ॉर्मेंस मेट्रिक लॉग करने के लिए, Google Cloud Logging का इस्तेमाल किया जा सकता है. ऐसा करने से, लॉग की गई मेट्रिक का इस्तेमाल करने के लिए, Google Cloud मॉनिटरिंग में डैशबोर्ड सेट अप किया जा सकता है और सूचना भी दी जा सकती है.

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

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

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

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

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

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

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

अगर आपका ऐप्लिकेशन पहले से ही Google Cloud के प्लैटफ़ॉर्म पर चल रहा है या आपको अपने ऐप्लिकेशन को Google Cloud पर ले जाना है, तो यह एक अच्छा विकल्प है.

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

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

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

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

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

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

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

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

Python में, कस्टम Google Cloud लॉगिंग इंटरसेप्टर

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