Logging

Logging e monitoraggio lavorano in tandem per aiutarti a comprendere e ottimizzare le prestazioni dell'applicazione, nonché per diagnosticare errori e che le applicazioni presentino problemi di prestazioni. Devi attivare i log di riepilogo per tutte le chiamate API e log dettagliati per le chiamate API non riuscite, in modo da poter fornire l'API. quando hai bisogno di assistenza tecnica.

Logging della libreria client

Le librerie client dell'API Google Ads includono il logging integrato. Per applicazioni specifiche di logging, consulta la documentazione di logging nella libreria client di scelta.

Lingua Guida
Java Documentazione su Logging per Java
.NET Documentazione su Logging per .NET
PHP Documentazione su Logging per PHP
Python Documentazione di Logging per Python
Ruby Documentazione su Logging per Ruby
Perl Documentazione su Logging per Perl

Formato log

Le librerie client dell'API Google Ads generano un log dettagliato e un riepilogo log per ogni chiamata API. Il log dettagliato contiene tutti i dettagli la chiamata API, mentre il log di riepilogo contiene dettagli minimi sulla chiamata API. Viene mostrato un esempio di ciascun tipo di log, con i log troncati e formattati per una maggiore leggibilità.

Log di riepilogo

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

Log dettagliato

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

Che cosa succede se non utilizzo una libreria client?

Se non utilizzi una libreria client, implementa il tuo logging per acquisire i dettagli delle chiamate API in uscita e in entrata. Devi registrare almeno dell'intestazione della risposta request-id, che può essere condivisa con dei team di assistenza tecnica.

Logging nel cloud

Esistono molti strumenti che puoi usare per acquisire log e metriche delle prestazioni la tua applicazione. Ad esempio, puoi utilizzare Google Cloud Logging per registrare metriche delle prestazioni al tuo progetto Google Cloud. Ciò rende è possibile configurare dashboard e avvisi in Google Cloud Monitoring per utilizzare le metriche registrate.

Cloud Logging offre librerie client per tutti i client dell'API Google Ads supportati. lingue delle librerie tranne Perl, quindi nella maggior parte dei casi è possibile accedere Cloud Logging direttamente dall'integrazione della libreria client. Per altre lingue incluso Perl, Cloud Logging offre anche un'API REST.

Esistono alcune opzioni per il logging in Cloud Logging o in un altro strumento, da un libreria client dell'API Google Ads. Ogni opzione prevede dei compromessi in termini di tempo di implementazione, complessità e prestazioni. Valuta attentamente questi compromessi prima di decidere quale soluzione implementare.

Opzione 1: scrivi log locali nel cloud da un processo in background

I log della libreria client possono essere scritti in un file locale sul tuo computer modificando configurazione del logging. Dopo l'output dei log in un file locale, puoi configurare un daemon per raccogliere i log e inviarli al cloud.

Un limite di questo approccio è che alcune metriche sul rendimento non verranno acquisite per impostazione predefinita. I log della libreria client includono i dettagli della richiesta e risposta, quindi le metriche di latenza non verranno incluse a meno che non vengano apportate ulteriori modifiche vengono effettuate per registrarle.

Opzione 2: esegui l'applicazione su Compute Engine e installa Ops Agent

Se la tua applicazione è in esecuzione su Compute Engine, puoi inviare installando Ops Agent per i log in Google Cloud Logging. Le operazioni L'agente può essere configurato per inviare i log delle tue applicazioni a Cloud Logging, oltre alle metriche e ai log inviati per impostazione predefinita.

Se la tua applicazione è già in esecuzione in un ambiente Google Cloud o stai considerando la possibilità di spostare la tua applicazione su Google Cloud, questa è un'ottima opzione le informazioni da considerare.

Opzione 3: implementa il logging nel codice dell'applicazione

Il logging direttamente dal codice dell'applicazione può essere eseguito in due modi:

  1. Incorporando calcoli delle metriche e istruzioni di log in ogni alla posizione applicabile nel tuo codice. Questa opzione è più fattibile per gli utenti codebase, dove i costi di ambito e manutenzione di una tale modifica sarebbero minimo.

  2. Implementazione di un'interfaccia di logging. Se la logica dell'applicazione può essere astratta in modo che diverse parti dell'applicazione ereditino dalla stessa base , la logica di logging può essere implementata in quella classe base. Questa opzione è generalmente preferito rispetto all'incorporamento di istruzioni di log in poiché è più semplice da mantenere e scalare. Per dimensioni più grandi la manutenibilità e la scalabilità di questa soluzione sono tutti più pertinenti.

Un limite di questo approccio è che i log completi di richieste e risposte non disponibile dal codice dell'applicazione. Gli oggetti di richiesta e risposta completi essere accessibili dagli intercetti gRPC; Ecco come la libreria client integrata Il logging ottiene i log di richieste e risposte. In caso di errore, verranno aggiunte potrebbero essere disponibili informazioni nell'oggetto eccezione, ma sono disponibili meno dettagli disponibili per risposte riuscite all'interno della logica dell'applicazione. Ad esempio, nel nella maggior parte dei casi, l'ID per una richiesta andata a buon fine non è accessibile dal Oggetti di risposta dell'API Google Ads.

Opzione 4: implementa un intercettatore di logging gRPC personalizzato

gRPC supporta intercetti unari e di flussi di dati che possono accedere degli oggetti richiesta e risposta durante il trasferimento tra il client e il server. La Le librerie client dell'API Google Ads utilizzano gli intercetti gRPC per offrire il logging integrato assistenza in tempo reale. Analogamente, puoi implementare un intercettatore gRPC personalizzato per accedere oggetti di richiesta e risposta, estrarre informazioni per il logging e il monitoraggio i tuoi scopi e scrivi tali dati nel percorso che preferisci.

A differenza di alcune delle altre soluzioni presentate qui, l'implementazione di un gRPC personalizzato offre la flessibilità necessaria per acquisire oggetti di richiesta e risposta ogni richiesta e implementare una logica aggiuntiva per acquisire i dettagli della richiesta. Ad esempio, puoi calcolare il tempo trascorso di una richiesta implementando all'interno dell'intercettore personalizzato, quindi registri i in Google Cloud Logging per renderla disponibile per il monitoraggio della latenza in Google Cloud Monitoring.

Intercettore personalizzato di Google Cloud Logging in Python

Per dimostrare questa soluzione, abbiamo scritto un esempio di logging personalizzato l'intercettatore in Python. L'intercettatore personalizzato viene creato e passato di Google Cloud. Accede quindi agli oggetti di richiesta e risposta che passano a ogni chiamata ai metodi di servizio, elabora i dati di questi oggetti e invia i dati a Google Cloud Logging.

Oltre ai dati che provengono dagli oggetti di richiesta e risposta, questo esempio implementa una logica aggiuntiva per acquisire il tempo trascorso del richiesta e alcuni altri metadati utili per il monitoraggio, ad esempio se la richiesta è andata a buon fine. Per ulteriori informazioni le informazioni possono essere utili, sia in generale per il monitoraggio che in particolare quando combinando Google Cloud Logging e Google Cloud Monitoring, consulta la documentazione su 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