Skip to content
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies = [
"requests",
"pydantic>=2,<3",
"workflows>=3.0",
"opentelemetry-api==1.20"
]

[dependency-groups]
Expand Down
26 changes: 26 additions & 0 deletions src/zocalo/service/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,23 @@
from importlib.metadata import entry_points

import workflows.recipe
from opentelemetry import trace
from workflows.services.common_service import CommonService


# Helper method to get dcid. Used for injecting it into current span
def _extract_dcid(params: dict) -> int | None:
if not isinstance(params, dict):
return None

if dcid := params.get("ispyb_dcid"):
return dcid
if dcid := params.get("dcid"):
return dcid

return None


class Dispatcher(CommonService):
"""
Single point of contact service that takes in job meta-information
Expand Down Expand Up @@ -205,6 +219,18 @@ def process(self, rw, header, message):
recipe_id = parameters.get("guid") or str(uuid.uuid4())
parameters["guid"] = recipe_id

# Extract DCID and set on trace span if OpenTelemetry is available
if trace is not None:
try:
span = trace.get_current_span()
if span and span.is_recording():
dcid = _extract_dcid(parameters)
if dcid:
span.set_attribute("dcid", dcid)
self.log.debug(f"Set DCID {dcid} on trace span")
except Exception as e:
self.log.warning(f"Failed to set DCID on trace span: {e}")

if rw:
# If we received a recipe wrapper then we already have a recipe_ID
# attached to logs. Make a note of the downstream recipe ID so that
Expand Down