Skip to content

Commit

Permalink
BREAKING: feat: otel trace format customization (#2961)
Browse files Browse the repository at this point in the history
Adds customization for the opentelemetry trace id and span id formats.

Also changes the default format to hexadecimal. This is the standard format of
trace and span IDs, and while it may break some workflows, hopefully it should
be very minor.

Co-authored-by: Jinsung Lee <goflrkqk@naver.com>
Co-authored-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com>
Co-authored-by: Sauyon Lee <2347889+sauyon@users.noreply.github.com>
Co-authored-by: Benjamin Tan Wei Hao <benjamintanweihao@gmail.com>
Co-authored-by: Huib Keemink <huib.keemink@creativedutchmen.com>
Co-authored-by: Sean Sheng <s3sheng@gmail.com>
  • Loading branch information
7 people committed Sep 12, 2022
1 parent 2d03474 commit fcd518e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
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_formatting(
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
14 changes: 12 additions & 2 deletions bentoml/_internal/log.py
Expand Up @@ -50,6 +50,7 @@ def filter(self, record: logging.LogRecord) -> bool:
)
DATE_FORMAT = "%Y-%m-%dT%H:%M:%S%z"


SERVER_LOGGING_CONFIG: dict[str, t.Any] = {
"version": 1,
"disable_existing_loggers": True,
Expand Down Expand Up @@ -111,10 +112,19 @@ 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)
from .configuration.containers import BentoMLContainer

logging_formatting = BentoMLContainer.logging_formatting.get()
trace_id_format = logging_formatting["trace_id_format"]
span_id_format = logging_formatting["span_id_format"]

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)
record.request_id = trace_context.request_id # type: ignore (adding fields to record)

return record
Expand Down
23 changes: 19 additions & 4 deletions docs/source/guides/logging.rst
Expand Up @@ -89,15 +89,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

0 comments on commit fcd518e

Please sign in to comment.