অ্যাপ্লিকেশনের পারফরম্যান্স বুঝতে ও উন্নত করতে, এবং সেইসাথে ত্রুটি ও সিস্টেম-সম্পর্কিত সমস্যা নির্ণয় করতে লগিং ও মনিটরিং একসাথে কাজ করে। আপনার উচিত সমস্ত এপিআই (API) কলের জন্য সামারি লগ এবং ব্যর্থ এপিআই কলের জন্য বিস্তারিত লগ চালু রাখা, যাতে প্রযুক্তিগত সহায়তার প্রয়োজন হলে আপনি এপিআই কল লগগুলো সরবরাহ করতে পারেন।
ক্লায়েন্ট লাইব্রেরি লগিং
গুগল অ্যাডস এপিআই ক্লায়েন্ট লাইব্রেরিগুলোতে বিল্ট-ইন লগিং ব্যবস্থা রয়েছে। প্ল্যাটফর্ম-নির্দিষ্ট লগিংয়ের বিস্তারিত তথ্যের জন্য, আপনার পছন্দের ক্লায়েন্ট লাইব্রেরির মধ্যে থাকা লগিং ডকুমেন্টেশন দেখুন।
| ভাষা | গাইড |
|---|---|
| জাভা | জাভার জন্য লগিং ডকুমেন্টেশন |
| .NET | .NET এর জন্য লগিং ডকুমেন্টেশন |
| পিএইচপি | পিএইচপি-এর জন্য লগিং ডকুমেন্টেশন |
| পাইথন | পাইথনের জন্য লগিং ডকুমেন্টেশন |
| রুবি | রুবির জন্য লগিং ডকুমেন্টেশন |
| পার্ল | পার্লের জন্য লগিং ডকুমেন্টেশন |
লগ ফরম্যাট
গুগল অ্যাডস এপিআই ক্লায়েন্ট লাইব্রেরিগুলো প্রতিটি এপিআই কলের জন্য একটি বিস্তারিত লগ এবং একটি সারসংক্ষেপ লগ তৈরি করে। বিস্তারিত লগে এপিআই কলের সমস্ত বিবরণ থাকে, অন্যদিকে সারসংক্ষেপ লগে এপিআই কলের সংক্ষিপ্ত বিবরণ থাকে। প্রতিটি ধরণের লগের একটি করে উদাহরণ দেখানো হয়েছে, যেখানে পাঠযোগ্যতার জন্য লগগুলো সংক্ষিপ্ত ও ফরম্যাট করা হয়েছে।
সারসংক্ষেপ লগ
GoogleAds.SummaryRequestLogs Warning: 1 : [2023-09-15 19:58:39Z] -
Request made: Host: , Method: /google.ads.googleads.v24.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.v24.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 রেসপন্স হেডারের মানটি লগ করা উচিত, যা পরবর্তীতে প্রয়োজন অনুযায়ী টেকনিক্যাল সাপোর্ট টিমের সাথে শেয়ার করা যেতে পারে।
ক্লাউডে লগিং
আপনার অ্যাপ্লিকেশনের জন্য লগ এবং পারফরম্যান্স মেট্রিক্স সংগ্রহ করতে আপনি অনেক টুল ব্যবহার করতে পারেন। উদাহরণস্বরূপ, আপনি আপনার গুগল ক্লাউড প্রজেক্টে পারফরম্যান্স মেট্রিক্স লগ করার জন্য গুগল ক্লাউড লগিং ব্যবহার করতে পারেন। এর ফলে, লগ করা মেট্রিক্সগুলো কাজে লাগানোর জন্য গুগল ক্লাউড মনিটরিং -এ ড্যাশবোর্ড এবং অ্যালার্টিং সেট আপ করা সম্ভব হয়।
ক্লাউড লগিং পার্ল ছাড়া গুগল অ্যাডস এপিআই-এর সমর্থিত সকল ক্লায়েন্ট লাইব্রেরি ভাষার জন্য ক্লায়েন্ট লাইব্রেরি সরবরাহ করে, তাই বেশিরভাগ ক্ষেত্রে আপনার ক্লায়েন্ট লাইব্রেরি ইন্টিগ্রেশন থেকে সরাসরি ক্লাউড লগিং-এর মাধ্যমে লগ করা সম্ভব। পার্ল সহ অন্যান্য ভাষার জন্য ক্লাউড লগিং একটি REST API- ও সরবরাহ করে।
গুগল অ্যাডস এপিআই ক্লায়েন্ট লাইব্রেরি থেকে ক্লাউড লগিং বা অন্য কোনো টুলে লগ করার জন্য কয়েকটি বিকল্প রয়েছে। প্রতিটি বিকল্পের বাস্তবায়নের সময়, জটিলতা এবং পারফরম্যান্সের নিজস্ব সুবিধা-অসুবিধা রয়েছে। কোন সমাধানটি বাস্তবায়ন করবেন তা সিদ্ধান্ত নেওয়ার আগে এই সুবিধা-অসুবিধাগুলো সম্পর্কে ভালোভাবে চিন্তা করুন।
বিকল্প ১: ব্যাকগ্রাউন্ড প্রসেস থেকে ক্লাউডে স্থানীয় লগ লিখুন
আপনার লগিং কনফিগারেশন পরিবর্তন করে ক্লায়েন্ট লাইব্রেরির লগগুলো আপনার মেশিনের একটি স্থানীয় ফাইলে লেখা যেতে পারে। লগগুলো স্থানীয় ফাইলে আউটপুট হয়ে গেলে, আপনি লগগুলো সংগ্রহ করে ক্লাউডে পাঠানোর জন্য একটি ডেমন সেট আপ করতে পারেন।
এই পদ্ধতির একটি সীমাবদ্ধতা হলো, কিছু পারফরম্যান্স মেট্রিক ডিফল্টরূপে রেকর্ড করা হবে না। ক্লায়েন্ট লাইব্রেরি লগে রিকোয়েস্ট এবং রেসপন্স অবজেক্টের বিবরণ অন্তর্ভুক্ত থাকে, তাই ল্যাটেন্সি মেট্রিকগুলোও অন্তর্ভুক্ত হবে না, যদি না এগুলোকেও লগ করার জন্য অতিরিক্ত পরিবর্তন করা হয়।
বিকল্প ২: আপনার অ্যাপ্লিকেশনটি কম্পিউট ইঞ্জিনে চালান এবং অপস এজেন্ট ইনস্টল করুন।
আপনার অ্যাপ্লিকেশনটি যদি Compute Engine- এ চলে, তাহলে আপনি Ops Agent ইনস্টল করে আপনার লগগুলি Google Cloud Logging-এ পাঠাতে পারেন। ডিফল্টরূপে পাঠানো মেট্রিক্স এবং লগগুলির পাশাপাশি, আপনার অ্যাপ্লিকেশনের লগগুলিও Cloud Logging-এ পাঠানোর জন্য Ops Agent-কে কনফিগার করা যেতে পারে ।
আপনার অ্যাপ্লিকেশনটি যদি ইতিমধ্যেই গুগল ক্লাউড পরিবেশে চালু থাকে, অথবা আপনি যদি আপনার অ্যাপ্লিকেশনটি গুগল ক্লাউডে স্থানান্তরের কথা ভেবে থাকেন, তবে এটি বিবেচনা করার জন্য একটি চমৎকার বিকল্প।
বিকল্প ৩: আপনার অ্যাপ্লিকেশন কোডে লগিং প্রয়োগ করুন
অ্যাপ্লিকেশন কোড থেকে সরাসরি লগিং দুটি উপায়ের একটিতে করা যেতে পারে:
আপনার কোডের প্রতিটি প্রযোজ্য স্থানে মেট্রিক্স গণনা এবং লগ স্টেটমেন্ট অন্তর্ভুক্ত করা। এই বিকল্পটি ছোট কোডবেসের জন্য বেশি কার্যকর, যেখানে এই ধরনের পরিবর্তনের পরিধি এবং রক্ষণাবেক্ষণ খরচ ন্যূনতম হবে।
একটি লগিং ইন্টারফেস ইমপ্লিমেন্ট করা। যদি অ্যাপ্লিকেশন লজিককে এমনভাবে অ্যাবস্ট্রাক্ট করা যায় যে অ্যাপ্লিকেশনের বিভিন্ন অংশ একই বেস ক্লাস থেকে ইনহেরিট করে, তবে সেই বেস ক্লাসেই লগিং লজিক ইমপ্লিমেন্ট করা যেতে পারে। অ্যাপ্লিকেশন কোড জুড়ে লগ স্টেটমেন্ট অন্তর্ভুক্ত করার চেয়ে এই বিকল্পটি সাধারণত বেশি পছন্দনীয়, কারণ এটি রক্ষণাবেক্ষণ এবং স্কেল করা সহজ। বড় কোডবেসের ক্ষেত্রে, এই সমাধানের রক্ষণাবেক্ষণযোগ্যতা এবং স্কেলেবিলিটি আরও বেশি প্রাসঙ্গিক হয়ে ওঠে।
এই পদ্ধতির একটি সীমাবদ্ধতা হলো, অ্যাপ্লিকেশন কোড থেকে সম্পূর্ণ রিকোয়েস্ট এবং রেসপন্স লগ পাওয়া যায় না। সম্পূর্ণ রিকোয়েস্ট এবং রেসপন্স অবজেক্ট gRPC ইন্টারসেপ্টর থেকে অ্যাক্সেস করা যায়; বিল্ট-ইন ক্লায়েন্ট লাইব্রেরি লগিং এভাবেই রিকোয়েস্ট এবং রেসপন্স লগ সংগ্রহ করে। কোনো ত্রুটির ক্ষেত্রে, এক্সেপশন অবজেক্টে অতিরিক্ত তথ্য পাওয়া যেতে পারে, কিন্তু অ্যাপ্লিকেশন লজিকের মধ্যে সফল রেসপন্সের জন্য কম বিবরণ পাওয়া যায়। উদাহরণস্বরূপ, বেশিরভাগ ক্ষেত্রে, একটি সফল রিকোয়েস্টের রিকোয়েস্ট আইডি গুগল অ্যাডস এপিআই রেসপন্স অবজেক্ট থেকে অ্যাক্সেস করা যায় না।
বিকল্প ৪: একটি কাস্টম gRPC লগিং ইন্টারসেপ্টর প্রয়োগ করুন
gRPC ইউনিটারি এবং স্ট্রিমিং ইন্টারসেপ্টর সমর্থন করে, যা ক্লায়েন্ট এবং সার্ভারের মধ্যে আদান-প্রদানের সময় রিকোয়েস্ট এবং রেসপন্স অবজেক্টগুলো অ্যাক্সেস করতে পারে। গুগল অ্যাডস এপিআই ক্লায়েন্ট লাইব্রেরিগুলো বিল্ট-ইন লগিং সাপোর্ট দেওয়ার জন্য gRPC ইন্টারসেপ্টর ব্যবহার করে। একইভাবে, আপনিও একটি কাস্টম gRPC ইন্টারসেপ্টর ইমপ্লিমেন্ট করতে পারেন, যা রিকোয়েস্ট এবং রেসপন্স অবজেক্টগুলো অ্যাক্সেস করবে, লগিং ও মনিটরিংয়ের উদ্দেশ্যে তথ্য সংগ্রহ করবে এবং সেই ডেটা আপনার পছন্দের কোনো স্থানে লিখে রাখবে।
এখানে উপস্থাপিত অন্যান্য কিছু সমাধানের থেকে ভিন্ন, একটি কাস্টম gRPC ইন্টারসেপ্টর প্রয়োগ করলে আপনি প্রতিটি অনুরোধে রিকোয়েস্ট ও রেসপন্স অবজেক্ট ক্যাপচার করার এবং অনুরোধের বিস্তারিত তথ্য সংগ্রহের জন্য অতিরিক্ত লজিক প্রয়োগ করার সুবিধা পাবেন। উদাহরণস্বরূপ, আপনি কাস্টম ইন্টারসেপ্টরের মধ্যেই পারফরম্যান্স টাইমিং লজিক প্রয়োগ করে একটি অনুরোধের অতিবাহিত সময় গণনা করতে পারেন, এবং তারপর সেই মেট্রিকটি গুগল ক্লাউড লগিং-এ লগ করতে পারেন, যাতে এটি গুগল ক্লাউড মনিটরিং-এর মধ্যে ল্যাটেন্সি মনিটরিং-এর জন্য উপলব্ধ হয়।
পাইথনে কাস্টম গুগল ক্লাউড লগিং ইন্টারসেপ্টর
এই সমাধানটি দেখানোর জন্য, আমরা পাইথনে একটি কাস্টম লগিং ইন্টারসেপ্টরের উদাহরণ লিখেছি। কাস্টম ইন্টারসেপ্টরটি তৈরি করে সার্ভিস ক্লায়েন্টে পাঠানো হয়। এরপর এটি প্রতিটি সার্ভিস মেথড কলের মাধ্যমে আসা রিকোয়েস্ট এবং রেসপন্স অবজেক্টগুলো অ্যাক্সেস করে, সেই অবজেক্টগুলো থেকে ডেটা প্রসেস করে এবং ডেটাটি গুগল ক্লাউড লগিং-এ পাঠিয়ে দেয়।
রিকোয়েস্ট এবং রেসপন্স অবজেক্ট থেকে প্রাপ্ত ডেটার পাশাপাশি, এই উদাহরণটিতে রিকোয়েস্টের অতিবাহিত সময় এবং মনিটরিংয়ের জন্য উপযোগী অন্যান্য মেটাডেটা, যেমন রিকোয়েস্টটি সফল হয়েছে কি না, তা ক্যাপচার করার জন্য কিছু অতিরিক্ত লজিক প্রয়োগ করা হয়েছে। এই তথ্যগুলো সাধারণভাবে মনিটরিংয়ের জন্য এবং বিশেষত গুগল ক্লাউড লগিং ও গুগল ক্লাউড মনিটরিং একসাথে ব্যবহার করার ক্ষেত্রে কীভাবে উপযোগী হতে পারে, সে সম্পর্কে আরও জানতে মনিটরিং গাইডটি দেখুন।
# 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