-
Notifications
You must be signed in to change notification settings - Fork 13
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
Jvr2022
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
Jvr2022:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
packages/abstractions/kiota_abstractions/request_response_logger.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
@abstractmethod | ||
def log_request(self, request_data: Dict[str, Any]) -> None: | ||
pass | ||
|
||
@abstractmethod | ||
def log_response(self, response_data: Dict[str, Any]) -> None: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ( | ||
|
@@ -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 | ||
|
@@ -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: | ||
|
@@ -525,6 +528,20 @@ async def get_http_response_message( | |
|
||
self.set_base_url_for_request_information(request_info) | ||
|
||
self.logger.log_request({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
13 changes: 13 additions & 0 deletions
13
packages/http/httpx/kiota_http/httpx_request_response_logger.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -387,6 +387,10 @@ def get_serialized_content(self) -> bytes: | |
stream = json_string.encode('utf-8') | ||
return stream | ||
|
||
@staticmethod | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?