Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP Request and Response Logging #380

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from abc import ABC, abstractmethod
from typing import Any, Dict

class RequestResponseLogger(ABC):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we look into an alternative that does not expand the public API surface here please?

@abstractmethod
def log_request(self, request_data: Dict[str, Any]) -> None:
pass

@abstractmethod
def log_response(self, response_data: Dict[str, Any]) -> None:
pass
17 changes: 17 additions & 0 deletions packages/http/httpx/kiota_http/httpx_request_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from kiota_abstractions.api_error import APIError
from kiota_abstractions.authentication import AuthenticationProvider
from kiota_abstractions.response_handler import RespondseHandler
from kiota_abstractions.request_adapter import RequestAdapter, ResponseType
from kiota_abstractions.request_information import RequestInformation
from kiota_abstractions.serialization import (
Expand Down Expand Up @@ -42,6 +43,7 @@
from kiota_http.middleware.parameters_name_decoding_handler import ParametersNameDecodingHandler

from ._version import VERSION
from .httpx_request_response_logger import HttpxRequestResponseLogger
from .kiota_client_factory import KiotaClientFactory
from .middleware import ParametersNameDecodingHandler
from .middleware.options import ParametersNameDecodingHandlerOption, ResponseHandlerOption
Expand Down Expand Up @@ -91,6 +93,7 @@ def __init__(
if not observability_options:
observability_options = ObservabilityOptions()
self.observability_options = observability_options
self.logger = HttpxRequestResponseLogger()

@property
def base_url(self) -> str:
Expand Down Expand Up @@ -525,6 +528,20 @@ async def get_http_response_message(

self.set_base_url_for_request_information(request_info)

self.logger.log_request({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is the logging logic here instead of in a middleware handler?

"url": url,
"method": request.method,
"headers": headers,
"query_params": query_params,
"content": content,
})

self.logger.log_response({
"status_code": response.status_code,
"headers": dict(response.headers),
"content": response.text,
})

additional_authentication_context = {}
if claims:
additional_authentication_context[self.CLAIMS_KEY] = claims
Expand Down
13 changes: 13 additions & 0 deletions packages/http/httpx/kiota_http/httpx_request_response_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from kiota_abstractions import RequestResponseLogger
from typing import Any, Dict
import logging

class HttpxRequestResponseLogger(RequestResponseLogger):
def __init__(self):
self.logger = logging.getLogger(__name__)

def log_request(self, request_data: Dict[str, Any]) -> None:
self.logger.info(f"HTTP Request: {request_data}")

def log_response(self, response_data: Dict[str, Any]) -> None:
self.logger.info(f"HTTP Response: {response_data}")
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ def get_serialized_content(self) -> bytes:
stream = json_string.encode('utf-8')
return stream

@staticmethod
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we avoid expanding the API surface and look into an alternative here please?

def serialize_object_to_json_string(obj: Dict[str, Any]) -> str:
return json.dumps(obj, indent=2)

@property
def on_before_object_serialization(self) -> Optional[Callable[[Parsable], None]]:
"""Gets the callback called before the object gets serialized.
Expand Down
Loading