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

feat: logging customization #2961

Merged
merged 15 commits into from Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions bentoml/_internal/configuration/containers.py
Expand Up @@ -178,6 +178,12 @@ def _is_ip_address(addr: str) -> bool:
},
},
},
"logging": {
"formatting": {
"trace_id_format": str,
"span_id_format": str,
}
},
}
)

Expand Down Expand Up @@ -535,5 +541,12 @@ def duration_buckets(
)
return DEFAULT_BUCKET

@providers.SingletonFactory
@staticmethod
def logging_formats(
Copy link
Member

Choose a reason for hiding this comment

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

I prefer to make this logging_formatting to be consistent with the config level.

Copy link
Contributor Author

@jiewpeng jiewpeng Sep 5, 2022

Choose a reason for hiding this comment

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

I could change it, but would there be a use case for someone to have one trace id format for the api server and a different format for the runner? When I requested for this feature, the premise was that I would want the trace id format to be consistent across my applications, so I don't really see a use case where a user would want different formats.

Copy link
Member

Choose a reason for hiding this comment

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

Oh I was saying just to change the naming to make it consistent

cfg: dict[str, t.Any] = Provide[config.logging.formatting],
) -> dict[str, str]:
return cfg


BentoMLContainer = _BentoMLContainerClass()
5 changes: 5 additions & 0 deletions bentoml/_internal/configuration/default_configuration.yaml
Expand Up @@ -51,3 +51,8 @@ tracing:
otlp:
protocol: Null
url: Null

logging:
formatting:
trace_id_format: 032x
span_id_format: 016x
sauyon marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 10 additions & 2 deletions bentoml/_internal/log.py
Expand Up @@ -9,6 +9,7 @@
from .context import component_context
from .configuration import get_debug_mode
from .configuration import get_quiet_mode
from .configuration.containers import BentoMLContainer

default_factory = logging.getLogRecordFactory()

Expand Down Expand Up @@ -50,6 +51,10 @@ def filter(self, record: logging.LogRecord) -> bool:
)
DATE_FORMAT = "%Y-%m-%dT%H:%M:%S%z"

LOGGING_FORMATS = BentoMLContainer.logging_formats.get()
TRACE_ID_FORMAT = LOGGING_FORMATS["trace_id_format"]
SPAN_ID_FORMAT = LOGGING_FORMATS["span_id_format"]
aarnphm marked this conversation as resolved.
Show resolved Hide resolved

SERVER_LOGGING_CONFIG: dict[str, t.Any] = {
"version": 1,
"disable_existing_loggers": True,
Expand Down Expand Up @@ -111,10 +116,13 @@ def trace_record_factory(*args: t.Any, **kwargs: t.Any):
record = default_factory(*args, **kwargs)
record.levelname_bracketed = f"[{record.levelname}]" # type: ignore (adding fields to record)
record.component = f"[{_component_name()}]" # type: ignore (adding fields to record)
if trace_context.trace_id == 0:
trace_id = trace_context.trace_id
if trace_id in (0, None):
record.trace_msg = "" # type: ignore (adding fields to record)
else:
record.trace_msg = f" (trace={trace_context.trace_id},span={trace_context.span_id},sampled={trace_context.sampled})" # type: ignore (adding fields to record)
trace_id = format(trace_id, TRACE_ID_FORMAT)
span_id = format(trace_context.span_id, SPAN_ID_FORMAT)
record.trace_msg = f" (trace={trace_id},span={span_id},sampled={trace_context.sampled})" # type: ignore (adding fields to record)
aarnphm marked this conversation as resolved.
Show resolved Hide resolved
record.request_id = trace_context.request_id # type: ignore (adding fields to record)

return record
Expand Down
45 changes: 30 additions & 15 deletions docs/source/guides/logging.rst
Expand Up @@ -59,22 +59,22 @@ logs are logged under the ``"bentoml"`` namespace.
Web Service Request Logging
"""""""""""""""""""""""""""

For web requests, logging can be enabled and disabled using the `logging.access` parameter at the
top level of the ``bentoml_configuration.yml``.
For web requests, logging can be enabled and disabled using the `logging.access` parameter in both the `api_server` and `runner`
sections of the ``bentoml_configuration.yml``.

.. code-block:: yaml

logging:
access:
enabled: False
# whether to log the size of the request body
request_content_length: True
# whether to log the content type of the request
request_content_type: True
# whether to log the content length of the response
response_content_length: True
# whether to log the content type of the response
response_content_type: True
enabled: False
# whether to log the size of the request body
request_content_length: True
# whether to log the content type of the request
request_content_type: True
# whether to log the content length of the response
response_content_length: True
# whether to log the content type of the response
response_content_type: True


Model Runner Request Logging
Expand All @@ -88,15 +88,30 @@ your ``bentoml_configuration.yml``:
.. code-block:: yaml

runners:
logging:
access:
enabled: True
...
logging:
access:
enabled: True
...

The available configuration options are identical to the webserver request logging options above.
These logs are disabled by default in order to prevent double logging of requests.


Logging Formatting
""""""""""""""""""

You may configure the logging formatting options at the top level of your ``bentoml_configuration.yml``.
The default configuration is shown below, where the opentelemetry trace_id and span_id are logged in
hexadecimal format, consistent with opentelemetry logging instrumentation.

.. code-block:: yaml

logging:
formatting:
trace_id_format: 032x
span_id_format: 016x


Library Logging
---------------

Expand Down