লগিং

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

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

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

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

লগ বিন্যাস

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 ক্লাউড প্রজেক্টে পারফরম্যান্স মেট্রিক্স লগ করতে Google ক্লাউড লগিং ব্যবহার করতে পারেন। এটি লগ করা মেট্রিকগুলি ব্যবহার করতে Google ক্লাউড মনিটরিং -এ ড্যাশবোর্ড এবং সতর্কতা সেট আপ করা সম্ভব করে তোলে৷

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

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

বিকল্প 1: একটি পটভূমি প্রক্রিয়া থেকে ক্লাউডে স্থানীয় লগ লিখুন

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

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

বিকল্প 2: Compute Engine-এ আপনার অ্যাপ্লিকেশন চালান এবং Ops Agent ইনস্টল করুন

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

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

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

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

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

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

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

বিকল্প 4: একটি কাস্টম gRPC লগিং ইন্টারসেপ্টর প্রয়োগ করুন

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

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

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

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

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