লগিং

লগিং এবং মনিটরিং একসাথে কাজ করে আপনাকে অ্যাপ্লিকেশনের কর্মক্ষমতা বুঝতে এবং অপ্টিমাইজ করতে সাহায্য করে, সেইসাথে ত্রুটি এবং সিস্টেম-সম্পর্কিত সমস্যাগুলি নির্ণয় করতে। আপনার সমস্ত API কলের জন্য সারাংশ লগ এবং ব্যর্থ API কলের জন্য বিস্তারিত লগ চালু করা উচিত যাতে প্রযুক্তিগত সহায়তার প্রয়োজন হলে আপনি API কল লগ সরবরাহ করতে পারেন।

ক্লায়েন্ট লাইব্রেরি লগিং

Google Ads API ক্লায়েন্ট লাইব্রেরিগুলিতে বিল্ট-ইন লগিং থাকে। প্ল্যাটফর্ম-নির্দিষ্ট লগিং বিশদের জন্য, আপনার পছন্দের ক্লায়েন্ট লাইব্রেরির মধ্যে লগিং ডকুমেন্টেশন দেখুন।

ভাষা গাইড
জাভা জাভার জন্য লগিং ডক্স
.নেট .NET এর জন্য লগিং ডক্স
পিএইচপি পিএইচপি-র জন্য লগিং ডক্স
পাইথন পাইথনের জন্য লগিং ডক্স
রুবি রুবির জন্য লগিং ডক্স
পার্ল পার্লের জন্য লগিং ডক্স

লগ ফর্ম্যাট

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 প্রতিক্রিয়া শিরোনামের মান লগ করা উচিত, যা প্রয়োজন অনুসারে প্রযুক্তিগত সহায়তা দলের সাথে ভাগ করা যেতে পারে।

ক্লাউডে লগ ইন করা হচ্ছে

আপনার অ্যাপ্লিকেশনের জন্য লগ এবং পারফরম্যান্স মেট্রিক্স ক্যাপচার করার জন্য আপনি অনেক টুল ব্যবহার করতে পারেন। উদাহরণস্বরূপ, আপনি আপনার গুগল ক্লাউড প্রজেক্টে পারফরম্যান্স মেট্রিক্স লগ করতে গুগল ক্লাউড লগিং ব্যবহার করতে পারেন। এর ফলে লগ করা মেট্রিক্স ব্যবহার করার জন্য গুগল ক্লাউড মনিটরিং- এ ড্যাশবোর্ড এবং সতর্কতা সেট আপ করা সম্ভব হয়।

ক্লাউড লগিং পার্ল ব্যতীত সকল সমর্থিত গুগল বিজ্ঞাপন API ক্লায়েন্ট লাইব্রেরি ভাষার জন্য ক্লায়েন্ট লাইব্রেরি অফার করে, তাই বেশিরভাগ ক্ষেত্রেই আপনার ক্লায়েন্ট লাইব্রেরি ইন্টিগ্রেশন থেকে সরাসরি ক্লাউড লগিং দিয়ে লগ করা সম্ভব। পার্ল সহ অন্যান্য ভাষার জন্য, ক্লাউড লগিং একটি REST API ও অফার করে।

Google Ads API ক্লায়েন্ট লাইব্রেরি থেকে ক্লাউড লগিং বা অন্য কোনও টুলে লগ ইন করার জন্য কয়েকটি বিকল্প রয়েছে। প্রতিটি বিকল্পের বাস্তবায়নের সময়, জটিলতা এবং কর্মক্ষমতার নিজস্ব বিনিময় রয়েছে। কোন সমাধান বাস্তবায়ন করবেন তা সিদ্ধান্ত নেওয়ার আগে এই বিনিময়গুলি সম্পর্কে সাবধানে চিন্তা করুন।

বিকল্প ১: ব্যাকগ্রাউন্ড প্রক্রিয়া থেকে ক্লাউডে স্থানীয় লগ লিখুন

আপনার লগিং কনফিগারেশন পরিবর্তন করে ক্লায়েন্ট লাইব্রেরি লগগুলি আপনার মেশিনের একটি স্থানীয় ফাইলে লেখা যেতে পারে। লগগুলি স্থানীয় ফাইলে আউটপুট হয়ে গেলে, আপনি লগগুলি সংগ্রহ করে ক্লাউডে পাঠানোর জন্য একটি ডেমন সেট আপ করতে পারেন।

এই পদ্ধতির একটি সীমাবদ্ধতা হল কিছু পারফরম্যান্স মেট্রিক্স ডিফল্টভাবে ক্যাপচার করা হবে না। ক্লায়েন্ট লাইব্রেরি লগগুলিতে অনুরোধ এবং প্রতিক্রিয়া অবজেক্টের বিবরণ অন্তর্ভুক্ত থাকে, তাই ল্যাটেন্সি মেট্রিক্স অন্তর্ভুক্ত করা হবে না যদি না এগুলি লগ করার জন্য অতিরিক্ত পরিবর্তন করা হয়।

বিকল্প ২: কম্পিউট ইঞ্জিনে আপনার অ্যাপ্লিকেশনটি চালান এবং অপস এজেন্ট ইনস্টল করুন।

যদি আপনার অ্যাপ্লিকেশনটি Compute Engine- এ চলমান থাকে, তাহলে আপনি Ops Agent ইনস্টল করে আপনার লগগুলি Google Cloud Logging-এ পাঠাতে পারেন। Ops Agent আপনার অ্যাপ্লিকেশন লগগুলিকে Cloud Logging-এ পাঠানোর জন্য কনফিগার করা যেতে পারে , ডিফল্টভাবে পাঠানো মেট্রিক্স এবং লগগুলি ছাড়াও।

যদি আপনার অ্যাপ্লিকেশনটি ইতিমধ্যেই গুগল ক্লাউড পরিবেশে চলমান থাকে, অথবা আপনি যদি আপনার অ্যাপ্লিকেশনটি গুগল ক্লাউডে স্থানান্তর করার কথা ভাবছেন, তাহলে এটি বিবেচনা করার জন্য একটি দুর্দান্ত বিকল্প।

বিকল্প ৩: আপনার অ্যাপ্লিকেশন কোডে লগিং বাস্তবায়ন করুন

অ্যাপ্লিকেশন কোড থেকে সরাসরি লগিং দুটি উপায়ের একটিতে করা যেতে পারে:

  1. আপনার কোডের প্রতিটি প্রযোজ্য স্থানে মেট্রিক্স গণনা এবং লগ স্টেটমেন্ট অন্তর্ভুক্ত করা। এই বিকল্পটি ছোট কোডবেসের জন্য আরও কার্যকর, যেখানে এই ধরনের পরিবর্তনের সুযোগ এবং রক্ষণাবেক্ষণ খরচ ন্যূনতম হবে।

  2. লগিং ইন্টারফেস বাস্তবায়ন। যদি অ্যাপ্লিকেশন লজিককে এমনভাবে বিমূর্ত করা যায় যাতে অ্যাপ্লিকেশনের বিভিন্ন অংশ একই বেস ক্লাস থেকে উত্তরাধিকার সূত্রে পায়, তাহলে লগিং লজিকটি সেই বেস ক্লাসে প্রয়োগ করা যেতে পারে। এই বিকল্পটি সাধারণত অ্যাপ্লিকেশন কোড জুড়ে লগ স্টেটমেন্ট অন্তর্ভুক্ত করার চেয়ে পছন্দ করা হয়, কারণ এটি রক্ষণাবেক্ষণ এবং স্কেল করা সহজ। বৃহত্তর কোডবেসের জন্য, এই সমাধানের রক্ষণাবেক্ষণযোগ্যতা এবং স্কেলেবিলিটি আরও প্রাসঙ্গিক।

এই পদ্ধতির একটি সীমাবদ্ধতা হল অ্যাপ্লিকেশন কোড থেকে সম্পূর্ণ অনুরোধ এবং প্রতিক্রিয়া লগ পাওয়া যায় না। সম্পূর্ণ অনুরোধ এবং প্রতিক্রিয়া অবজেক্টগুলি gRPC ইন্টারসেপ্টর থেকে অ্যাক্সেস করা যেতে পারে; এইভাবে বিল্ট-ইন ক্লায়েন্ট লাইব্রেরি লগিং অনুরোধ এবং প্রতিক্রিয়া লগগুলি সংগ্রহ করে। কোনও ত্রুটির ক্ষেত্রে, ব্যতিক্রম অবজেক্টে অতিরিক্ত তথ্য পাওয়া যেতে পারে, তবে অ্যাপ্লিকেশন লজিকের মধ্যে সফল প্রতিক্রিয়াগুলির জন্য কম বিশদ উপলব্ধ থাকে। উদাহরণস্বরূপ, বেশিরভাগ ক্ষেত্রে, একটি সফল অনুরোধের অনুরোধ আইডি Google বিজ্ঞাপন API প্রতিক্রিয়া অবজেক্ট থেকে অ্যাক্সেসযোগ্য নয়।

বিকল্প ৪: একটি কাস্টম জিআরপিসি লগিং ইন্টারসেপ্টর বাস্তবায়ন করুন

gRPC ইউনারি এবং স্ট্রিমিং ইন্টারসেপ্টর সমর্থন করে যা ক্লায়েন্ট এবং সার্ভারের মধ্যে যাওয়ার সময় অনুরোধ এবং প্রতিক্রিয়া অবজেক্টগুলিতে অ্যাক্সেস করতে পারে। Google Ads API ক্লায়েন্ট লাইব্রেরিগুলি অন্তর্নির্মিত লগিং সমর্থন প্রদানের জন্য gRPC ইন্টারসেপ্টর ব্যবহার করে। একইভাবে, আপনি অনুরোধ এবং প্রতিক্রিয়া অবজেক্টগুলিতে অ্যাক্সেস করতে, লগিং এবং পর্যবেক্ষণের উদ্দেশ্যে তথ্য আহরণ করতে এবং সেই ডেটা আপনার পছন্দের স্থানে লিখতে একটি কাস্টম gRPC ইন্টারসেপ্টর প্রয়োগ করতে পারেন।

এখানে উপস্থাপিত অন্যান্য সমাধানগুলির থেকে ভিন্ন, একটি কাস্টম gRPC ইন্টারসেপ্টর প্রয়োগ করলে আপনি প্রতিটি অনুরোধে অনুরোধ এবং প্রতিক্রিয়া অবজেক্ট ক্যাপচার করতে পারবেন এবং অনুরোধের বিশদ ক্যাপচার করার জন্য অতিরিক্ত যুক্তি প্রয়োগ করতে পারবেন। উদাহরণস্বরূপ, আপনি কাস্টম ইন্টারসেপ্টরের মধ্যেই পারফরম্যান্স টাইমিং লজিক প্রয়োগ করে একটি অনুরোধের অতিবাহিত সময় গণনা করতে পারেন, তারপর মেট্রিকটি Google Cloud Logging-এ লগ করতে পারেন যাতে এটি Google Cloud Monitoring-এর মধ্যে ল্যাটেন্সি পর্যবেক্ষণের জন্য উপলব্ধ হয়।

পাইথনে কাস্টম গুগল ক্লাউড লগিং ইন্টারসেপ্টর

এই সমাধানটি প্রদর্শনের জন্য, আমরা পাইথনে একটি কাস্টম লগিং ইন্টারসেপ্টরের একটি উদাহরণ লিখেছি। কাস্টম ইন্টারসেপ্টর তৈরি করা হয় এবং পরিষেবা ক্লায়েন্টে প্রেরণ করা হয়। এরপর এটি প্রতিটি পরিষেবা পদ্ধতি কলের মধ্য দিয়ে যাওয়া অনুরোধ এবং প্রতিক্রিয়া অবজেক্টগুলিতে অ্যাক্সেস করে, সেই অবজেক্টগুলি থেকে ডেটা প্রক্রিয়া করে এবং ডেটা গুগল ক্লাউড লগিংয়ে প্রেরণ করে।

অনুরোধ এবং প্রতিক্রিয়া অবজেক্ট থেকে আসা ডেটা ছাড়াও, উদাহরণটি অনুরোধের অতিবাহিত সময় ক্যাপচার করার জন্য কিছু অতিরিক্ত যুক্তি প্রয়োগ করে, এবং কিছু অন্যান্য মেটাডেটা যা পর্যবেক্ষণের উদ্দেশ্যে কার্যকর হবে, যেমন অনুরোধটি সফল হয়েছে কিনা। এই তথ্য কীভাবে কার্যকর হতে পারে সে সম্পর্কে আরও তথ্যের জন্য, সাধারণত পর্যবেক্ষণের জন্য, এবং বিশেষ করে যখন Google ক্লাউড লগিং এবং Google ক্লাউড মনিটরিং একত্রিত করা হয়, মনিটরিং নির্দেশিকাটি দেখুন।

# 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