Skip to content

Commit

Permalink
Log forwarding support (#413)
Browse files Browse the repository at this point in the history
Fixes #311
  • Loading branch information
cretz authored Nov 6, 2023
1 parent 6dbe2f4 commit 4802d2f
Show file tree
Hide file tree
Showing 8 changed files with 461 additions and 71 deletions.
49 changes: 26 additions & 23 deletions temporalio/bridge/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions temporalio/bridge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ name = "temporal_sdk_bridge"
crate-type = ["cdylib"]

[dependencies]
futures = "0.3"
log = "0.4"
once_cell = "1.16.0"
parking_lot = "0.12"
prost = "0.11"
prost-types = "0.11"
pyo3 = { version = "0.18", features = ["extension-module", "abi3-py37"] }
pyo3-asyncio = { version = "0.18", features = ["tokio-runtime"] }
pyo3 = { version = "0.19", features = ["extension-module", "abi3-py37"] }
pyo3-asyncio = { version = "0.19", features = ["tokio-runtime"] }
pythonize = "0.19"
temporal-client = { version = "0.1.0", path = "./sdk-core/client" }
temporal-sdk-core = { version = "0.1.0", path = "./sdk-core/core", features = ["ephemeral-server"] }
temporal-sdk-core-api = { version = "0.1.0", path = "./sdk-core/core-api" }
Expand Down
53 changes: 51 additions & 2 deletions temporalio/bridge/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Mapping, Optional, Sequence, Type
from typing import Any, Callable, Dict, Mapping, Optional, Sequence, Type

from typing_extensions import Protocol

import temporalio.bridge.temporal_sdk_bridge

Expand All @@ -29,13 +31,21 @@ def retrieve_buffered_metrics(self) -> Sequence[Any]:
"""Get buffered metrics."""
return self._ref.retrieve_buffered_metrics()

def write_test_info_log(self, message: str, extra_data: str) -> None:
"""Write a test core log at INFO level."""
self._ref.write_test_info_log(message, extra_data)

def write_test_debug_log(self, message: str, extra_data: str) -> None:
"""Write a test core log at DEBUG level."""
self._ref.write_test_debug_log(message, extra_data)


@dataclass(frozen=True)
class LoggingConfig:
"""Python representation of the Rust struct for logging config."""

filter: str
forward: bool
forward_to: Optional[Callable[[Sequence[BufferedLogEntry]], None]]


@dataclass(frozen=True)
Expand Down Expand Up @@ -75,3 +85,42 @@ class TelemetryConfig:

logging: Optional[LoggingConfig]
metrics: Optional[MetricsConfig]


# WARNING: This must match Rust runtime::BufferedLogEntry
class BufferedLogEntry(Protocol):
"""A buffered log entry."""

@property
def target(self) -> str:
"""Target category for the log entry."""
...

@property
def message(self) -> str:
"""Log message."""
...

@property
def time(self) -> float:
"""Time as from ``time.time`` since Unix epoch."""
...

@property
def level(self) -> int:
"""Python log level, with trace as 9."""
...

@property
def fields(self) -> Dict[str, Any]:
"""Additional log entry fields.
Requesting this property performs a conversion from the internal
representation to the Python representation on every request. Therefore
callers should store the result instead of repeatedly calling.
Raises:
Exception: If the internal representation cannot be converted. This
should not happen and if it does it is considered a bug in the
SDK and should be reported.
"""
...
2 changes: 1 addition & 1 deletion temporalio/bridge/sdk-core
1 change: 1 addition & 0 deletions temporalio/bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn temporal_sdk_bridge(py: Python, m: &PyModule) -> PyResult<()> {

// Runtime stuff
m.add_class::<runtime::RuntimeRef>()?;
m.add_class::<runtime::BufferedLogEntry>()?;
m.add_function(wrap_pyfunction!(init_runtime, m)?)?;
m.add_function(wrap_pyfunction!(raise_in_thread, m)?)?;

Expand Down
Loading

0 comments on commit 4802d2f

Please sign in to comment.