Günlük Kaydı

Günlük kaydı ve izleme birlikte çalışarak uygulama performansını anlayıp optimize etmenize ve ayrıca hataları ve sistemle ilgili sorunları teşhis etmenize yardımcı olur. Teknik desteğe ihtiyaç duyduğunuzda API çağrısı günlüklerini sağlayabilmek amacıyla tüm API çağrıları için özet günlüklerini, başarısız API çağrıları için ayrıntılı günlükleri etkinleştirmeniz gerekir.

İstemci kitaplığı günlük kaydı

Google Ads API istemci kitaplıkları, yerleşik günlük kaydı özelliğiyle birlikte gelir. Platforma özgü günlük kaydı ayrıntıları için seçtiğiniz istemci kitaplığındaki günlük kaydı belgelerine bakın.

Dil Kılavuz
Java Java için günlük kaydı belgeleri
.NET .NET için belgeleri günlüğe kaydetme
PHP PHP için günlük kaydı belgeleri
Python Python için günlük kaydı belgeleri
Ruby Ruby için dokümanları günlüğe kaydetme
Perl Perl için günlük kaydı belgeleri

Günlük biçimi

Google Ads API istemci kitaplıkları, her API çağrısı için ayrıntılı bir günlük ve özet günlüğü oluşturur. Ayrıntılı günlük, API çağrısının tüm ayrıntılarını içerir; özet günlüğü ise API çağrısının minimum ayrıntılarını içerir. Her günlük türünün bir örneği gösterilmiştir. Günlükler kısaltılmış ve okunabilirlik için biçimlendirilmiştir.

Özet günlüğü

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.")

Ayrıntılı günlük

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

İstemci kitaplığı kullanmıyorsam ne olacak?

İstemci kitaplığı kullanmıyorsanız giden ve gelen API çağrılarının ayrıntılarını yakalamak için kendi günlük kaydınızı uygulayın. En azından request-id yanıt başlığının değerini günlüğe kaydetmelisiniz. Bu değer daha sonra gerektiğinde teknik destek ekipleriyle paylaşılabilir.

Buluta günlük kaydı

Uygulamanızın günlüklerini ve performans metriklerini kaydetmek için kullanabileceğiniz birçok araç vardır. Örneğin, performans metriklerini Google Cloud Projenize kaydetmek için Google Cloud Logging'i kullanabilirsiniz. Bu sayede, günlüğe kaydedilen metriklerden yararlanmak için Google Cloud Monitoring'de kontrol panelleri ve uyarılar oluşturulabilir.

Cloud Logging, Perl dışında desteklenen tüm Google Ads API istemci kitaplık dilleri için istemci kitaplıkları sunar. Böylece çoğu durumda doğrudan istemci kitaplığı entegrasyonunuzdan Cloud Logging ile giriş yapabilirsiniz. Cloud Logging, Perl dahil diğer diller için REST API de sunar.

Google Ads API istemci kitaplığından Cloud Logging'e veya başka bir araca giriş yapmak için birkaç seçenek vardır. Her seçeneğin uygulama, karmaşıklık ve performans açısından kendine özgü zamanları vardır. Hangi çözümü uygulayacağınıza karar vermeden önce bu dengeleri dikkatlice düşünün.

1. Seçenek: Arka plan işleminden buluta yerel günlükler yazma

İstemci kitaplığı günlükleri, günlük kaydı yapılandırmanızı değiştirerek makinenizdeki yerel bir dosyaya yazılabilir. Günlükler yerel bir dosyaya aktarıldıktan sonra, günlükleri toplayıp buluta göndermesi için bir arka plan programı ayarlayabilirsiniz.

Bu yaklaşımın bir sınırlaması, bazı performans metriklerinin varsayılan olarak yakalanamamasıdır. İstemci kitaplığı günlükleri, istek ve yanıt nesnelerinden alınan ayrıntıları içerir. Bu nedenle, bunları günlüğe kaydetmek için ek değişiklikler yapılmadığı sürece gecikme metrikleri de dahil edilmez.

2. Seçenek: Uygulamanızı Compute Engine'de çalıştırın ve İşlem Aracısı'nı yükleyin

Uygulamanız Compute Engine'de çalışıyorsa İşlem Aracısı'nı yükleyerek günlüklerinizi Google Cloud Logging'e gönderebilirsiniz. İşlem Aracısı, varsayılan olarak gönderilen metrik ve günlüklerin yanı sıra uygulama günlüklerinizi de Cloud Logging'e gönderecek şekilde yapılandırılabilir.

Uygulamanız zaten bir Google Cloud ortamında çalışıyorsa veya uygulamanızı Google Cloud'a taşımayı düşünüyorsanız bu mükemmel bir seçenektir.

3. Seçenek: Günlük kaydını uygulama kodunuzda uygulayın

Doğrudan uygulama kodundan günlük kaydı iki şekilde yapılabilir:

  1. Metrik hesaplamalarını ve günlük ifadelerini kodunuzdaki her geçerli konuma dahil edin. Bu seçenek, kapsam ve bakım maliyetlerinin minimum düzeyde olacağı daha küçük kod tabanları için daha uygundur.

  2. Günlük kaydı arayüzü uygulama. Uygulama mantığı, uygulamanın farklı parçalarının aynı temel sınıftan devralacağı şekilde soyutlanabiliyorsa söz konusu temel sınıfa günlük kaydı mantığı uygulanabilir. Bu seçenek, bakımı ve ölçeklendirmesi daha kolay olduğu için günlük ifadelerinin uygulama kodunun tamamına dahil edilmesi yerine genellikle tercih edilir. Bu çözümün sürdürülebilirliği ve ölçeklenebilirliği, daha büyük kod tabanları için son derece önemlidir.

Bu yaklaşımın bir sınırlaması, tam istek ve yanıt günlüklerine uygulama kodundan erişilememesidir. Tam istek ve yanıt nesnelerine gRPC engelleyicilerden erişilebilir. Yerleşik istemci kitaplığı günlük kaydı, istek ve yanıt günlüklerini bu şekilde alır. Bir hata olması durumunda, istisna nesnesinde ek bilgiler bulunabilir, ancak uygulama mantığında başarılı yanıtlar için daha az ayrıntı kullanılabilir. Örneğin, çoğu durumda başarılı bir isteğin istek kimliğine Google Ads API yanıt nesnelerinden erişilemez.

4. Seçenek: Özel bir gRPC günlük kaydı engelleyici uygulama

gRPC, istemci ile sunucu arasında geçiş yaparken istek ve yanıt nesnelerine erişebilen tekli ve akış adayıcılarını destekler. Google Ads API istemci kitaplıkları, yerleşik günlük kaydı desteği sunmak için gRPC önleyicilerini kullanır. Benzer şekilde, istek ve yanıt nesnelerine erişmek, günlük kaydı ve izleme amaçları için bilgileri ayıklamak ve bu verileri istediğiniz konuma yazmak için özel bir gRPC engelleyici uygulayabilirsiniz.

Burada sunulan diğer çözümlerin bazılarının aksine, özel bir gRPC önleme aracı uygulamak size her istekteki istek ve yanıt nesnelerini yakalama ve isteğin ayrıntılarını yakalamak için ek mantık uygulama esnekliği sağlar. Örneğin, özel önleyicinin kendi içinde performans zamanlama mantığını uygulayarak bir istek için geçen süreyi hesaplayabilir ve ardından metriği Google Cloud Logging'e kaydederek Google Cloud Monitoring'deki gecikme izleme için kullanılabilir hale getirebilirsiniz.

Python'da özel Google Cloud Logging müdahale aracı

Bu çözümü göstermek için Python'da özel bir günlük kaydı engelleyici örneği hazırladık. Özel önleyici oluşturulur ve hizmet istemcisine iletilir. Ardından her hizmet yöntemi çağrısında geçirilen istek ve yanıt nesnelerine erişir, bu nesnelerden gelen verileri işler ve verileri Google Cloud Logging'e gönderir.

Örnekte, istek ve yanıt nesnelerinden gelen verilere ek olarak, isteğin geçen süresini yakalamak için bazı ek mantıklar ve isteğin başarılı olup olmadığı gibi izleme amacıyla yararlı olabilecek diğer meta veriler uygulanır. Bu bilgilerin hem izleme açısından hem de özellikle Google Cloud Logging ile Google Cloud Monitoring'i birleştirirken nasıl faydalı olabileceği hakkında daha fazla bilgi edinmek için İzleme kılavuzuna bakın.

# 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