Skip to content
Merged
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
5 changes: 4 additions & 1 deletion sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ LANGFUSE_HOST="https://cloud.langfuse.com"

# Trace context file for distributed tracing (optional)
# Used for linking traces between fastomop and omcp across processes
# Default: Uses system temp directory (e.g., /tmp/ on Unix, %TEMP% on Windows)
# Default behavior (cross-platform):
# - Windows: Uses %TEMP%\.fastomop_langfuse_trace_context.json
# - macOS/Linux: Uses /tmp/.fastomop_langfuse_trace_context.json
# Only set this if you need a custom location or for cross-service coordination
# IMPORTANT: fastomop and omcp must use the same file path for trace propagation to work
#LANGFUSE_TRACE_CONTEXT_FILE=/tmp/.fastomop_langfuse_trace_context.json

# ============================================================================
Expand Down
6 changes: 6 additions & 0 deletions src/omcp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
from dotenv import load_dotenv
from langfuse import Langfuse
from langfuse import observe
from opentelemetry.propagate import set_global_textmap
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator

load_dotenv()

# Configure OpenTelemetry W3C Trace Context propagator
# This is required for cross-process trace propagation with Langfuse V3
set_global_textmap(TraceContextTextMapPropagator())

# Global configuration variables
ENABLE_LOGGING = os.environ.get("ENABLE_LOGGING", "true").lower() == "true"
ENABLE_LANGFUSE = os.environ.get("ENABLE_LANGFUSE", "true").lower() == "true"
Expand Down
24 changes: 19 additions & 5 deletions src/omcp/trace_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@
"""

import os
import tempfile
import json
import platform
import tempfile
from pathlib import Path
from typing import Optional, Dict


# Shared trace context file path (same as fastomop)
default_path = Path(tempfile.gettempdir()) / ".fastomop_langfuse_trace_context.json"
# Use platform-specific temp directory for cross-platform compatibility
if platform.system() == "Windows":
default_path = Path(tempfile.gettempdir()) / ".fastomop_langfuse_trace_context.json"
else:
# On Unix-like systems (macOS/Linux), use /tmp for consistency across processes
# tempfile.gettempdir() can return different paths in different contexts on macOS
default_path = Path("/tmp") / ".fastomop_langfuse_trace_context.json"

TRACE_CONTEXT_FILE = Path(
os.environ.get("LANGFUSE_TRACE_CONTEXT_FILE", str(default_path))
)
Expand Down Expand Up @@ -44,10 +52,16 @@ def read_trace_context() -> Dict[str, Optional[str]]:
# Backward compatibility: also read old trace_id format
"trace_id": context.get("trace_id"),
}
except Exception:
except Exception as e:
# Non-critical error, return empty context
# Don't log here to avoid noise, caller can decide how to handle None values
pass
# Only log if file exists but can't be read (actual error)
if TRACE_CONTEXT_FILE.exists():
import logging

logger = logging.getLogger("omcp")
logger.warning(
f"Failed to read trace context from {TRACE_CONTEXT_FILE}: {e}"
)

return {
"traceparent": None,
Expand Down