Skip to content

Commit 2197f22

Browse files
feat: add session ID support through HTTP headers
Co-Authored-By: [email protected] <[email protected]>
1 parent 8959561 commit 2197f22

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
LANGTRACE_REMOTE_URL = "https://app.langtrace.ai"
1+
LANGTRACE_REMOTE_URL = "https://app.langtrace.ai"
2+
LANGTRACE_SESSION_ID_HEADER = "x-langtrace-session-id"

src/langtrace_python_sdk/extensions/langtrace_exporter.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from langtrace_python_sdk.constants.exporter.langtrace_exporter import (
1111
LANGTRACE_REMOTE_URL,
12+
LANGTRACE_SESSION_ID_HEADER,
1213
)
1314
from colorama import Fore
1415
from requests.exceptions import RequestException
@@ -51,12 +52,14 @@ class LangTraceExporter(SpanExporter):
5152
api_key: str
5253
api_host: str
5354
disable_logging: bool
55+
session_id: str
5456

5557
def __init__(
5658
self,
5759
api_host,
5860
api_key: str = None,
5961
disable_logging: bool = False,
62+
session_id: str = None,
6063
) -> None:
6164
self.api_key = api_key or os.environ.get("LANGTRACE_API_KEY")
6265
self.api_host = (
@@ -65,6 +68,7 @@ def __init__(
6568
else api_host
6669
)
6770
self.disable_logging = disable_logging
71+
self.session_id = session_id or os.environ.get("LANGTRACE_SESSION_ID")
6872

6973
def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult:
7074
"""
@@ -82,6 +86,10 @@ def export(self, spans: typing.Sequence[ReadableSpan]) -> SpanExportResult:
8286
"User-Agent": "LangtraceExporter",
8387
}
8488

89+
# Add session ID if available
90+
if self.session_id:
91+
headers[LANGTRACE_SESSION_ID_HEADER] = self.session_id
92+
8593
# Check if the OTEL_EXPORTER_OTLP_HEADERS environment variable is set
8694
otel_headers = os.getenv("OTEL_EXPORTER_OTLP_HEADERS", None)
8795
if otel_headers:

src/langtrace_python_sdk/langtrace.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def __init__(self, **kwargs):
9898
or os.environ.get("LANGTRACE_HEADERS")
9999
or os.environ.get("OTEL_EXPORTER_OTLP_HEADERS")
100100
)
101+
self.session_id = kwargs.get("session_id") or os.environ.get("LANGTRACE_SESSION_ID")
101102

102103

103104
def get_host(config: LangtraceConfig) -> str:

src/langtrace_python_sdk/utils/with_root_span.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ def sync_wrapper(*args, **kwargs):
6161
span_id = str(span.get_span_context().span_id)
6262
trace_id = str(span.get_span_context().trace_id)
6363

64+
# Attach session ID if available
65+
session_id = os.environ.get("LANGTRACE_SESSION_ID")
66+
if session_id:
67+
span.set_attribute("session.id", session_id)
68+
6469
if (
6570
"span_id" in func.__code__.co_varnames
6671
and "trace_id" in func.__code__.co_varnames
@@ -82,6 +87,12 @@ async def async_wrapper(*args, **kwargs):
8287
) as span:
8388
span_id = span.get_span_context().span_id
8489
trace_id = span.get_span_context().trace_id
90+
91+
# Attach session ID if available
92+
session_id = os.environ.get("LANGTRACE_SESSION_ID")
93+
if session_id:
94+
span.set_attribute("session.id", session_id)
95+
8596
if (
8697
"span_id" in func.__code__.co_varnames
8798
and "trace_id" in func.__code__.co_varnames

0 commit comments

Comments
 (0)