docent-python 0.1.3a0__py3-none-any.whl → 0.1.4a0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of docent-python might be problematic. Click here for more details.

docent/trace_alt.py CHANGED
@@ -1,10 +1,11 @@
1
1
  import asyncio
2
2
  import atexit
3
3
  import functools
4
+ import io
4
5
  import logging
5
6
  import os
6
7
  import uuid
7
- from contextlib import asynccontextmanager, contextmanager
8
+ from contextlib import asynccontextmanager, contextmanager, redirect_stdout
8
9
  from contextvars import ContextVar, Token
9
10
  from typing import Any, AsyncIterator, Callable, Dict, Iterator, Optional, Set
10
11
 
@@ -14,6 +15,10 @@ from opentelemetry.sdk.trace import ReadableSpan, SpanProcessor
14
15
  from opentelemetry.trace import Span
15
16
  from traceloop.sdk import Traceloop
16
17
 
18
+ # Configure logging
19
+ logger = logging.getLogger(__name__)
20
+ logger.disabled = True
21
+
17
22
  DEFAULT_ENDPOINT = "https://api.docent.transluce.org/rest/telemetry"
18
23
 
19
24
  # Context variables for tracking current agent run and collection
@@ -26,6 +31,7 @@ _current_collection_id: ContextVar[Optional[str]] = ContextVar(
26
31
  _tracing_initialized = False
27
32
  _collection_name: Optional[str] = None
28
33
  _collection_id: Optional[str] = None
34
+ _default_agent_run_id: Optional[str] = None
29
35
  _endpoint: Optional[str] = None
30
36
  _api_key: Optional[str] = None
31
37
  _enable_console_export = False
@@ -55,11 +61,11 @@ class DocentSpanProcessor(SpanProcessor):
55
61
  if agent_run_id:
56
62
  span.set_attribute("agent_run_id", agent_run_id)
57
63
  else:
58
- # If no agent_run_id in context, mark this as a default span
64
+ span.set_attribute("agent_run_id", _get_default_agent_run_id())
59
65
  span.set_attribute("agent_run_id_default", True)
60
66
 
61
67
  # Add service name for better integration with existing OTEL setups
62
- span.set_attribute("service.name", _collection_name or "docent-service")
68
+ span.set_attribute("service.name", _collection_name or "docent-trace")
63
69
 
64
70
  if self.enable_console_export:
65
71
  logging.debug(
@@ -102,7 +108,7 @@ def initialize_tracing(
102
108
  instruments: Set of instruments to enable (None = all instruments)
103
109
  block_instruments: Set of instruments to explicitly disable
104
110
  """
105
- global _tracing_initialized, _collection_name, _collection_id, _endpoint, _api_key
111
+ global _tracing_initialized, _collection_name, _collection_id, _default_agent_run_id, _endpoint, _api_key
106
112
  global _enable_console_export, _disable_batch, _instruments, _block_instruments
107
113
 
108
114
  if _tracing_initialized:
@@ -111,6 +117,7 @@ def initialize_tracing(
111
117
 
112
118
  _collection_name = collection_name
113
119
  _collection_id = collection_id or _generate_id()
120
+ _default_agent_run_id = _get_default_agent_run_id() # Generate default ID if not set
114
121
  _endpoint = endpoint or DEFAULT_ENDPOINT
115
122
  _api_key = api_key or os.getenv("DOCENT_API_KEY")
116
123
  _enable_console_export = enable_console_export
@@ -134,29 +141,30 @@ def initialize_tracing(
134
141
  docent_processor = DocentSpanProcessor(_collection_id, enable_console_export)
135
142
 
136
143
  # Get Traceloop's default span processor for export
137
- traceloop_processor = get_default_span_processor(
144
+ export_processor = get_default_span_processor(
138
145
  disable_batch=_disable_batch,
139
146
  api_endpoint=_endpoint,
140
147
  headers={"Authorization": f"Bearer {_api_key}"},
141
148
  )
142
149
 
143
150
  # Combine both processors
144
- processors = [docent_processor, traceloop_processor]
151
+ processors = [docent_processor, export_processor]
145
152
 
146
153
  os.environ["TRACELOOP_METRICS_ENABLED"] = "false"
147
154
  os.environ["TRACELOOP_TRACE_ENABLED"] = "true"
148
- # Temporarily redirect stdout to suppress Traceloop's print statements
149
- # with redirect_stdout(io.StringIO()):
150
- Traceloop.init( # type: ignore
151
- app_name=collection_name,
152
- api_endpoint=_endpoint,
153
- api_key=_api_key,
154
- telemetry_enabled=False, # don't send telemetry to traceloop's backend
155
- disable_batch=_disable_batch,
156
- instruments=_instruments,
157
- block_instruments=_block_instruments,
158
- processor=processors, # Add both our context processor and Traceloop's export processor
159
- )
155
+
156
+ # Temporarily redirect stdout to suppress print statements
157
+ with redirect_stdout(io.StringIO()):
158
+ Traceloop.init( # type: ignore
159
+ app_name=collection_name,
160
+ api_endpoint=_endpoint,
161
+ api_key=_api_key,
162
+ telemetry_enabled=False, # don't send analytics to traceloop's backend
163
+ disable_batch=_disable_batch,
164
+ instruments=_instruments,
165
+ block_instruments=_block_instruments,
166
+ processor=processors, # Add both our context processor and export processor
167
+ )
160
168
 
161
169
  _tracing_initialized = True
162
170
  logging.info(
@@ -203,6 +211,14 @@ def _get_current_collection_id() -> Optional[str]:
203
211
  return _current_collection_id.get()
204
212
 
205
213
 
214
+ def _get_default_agent_run_id() -> str:
215
+ """Get the default agent run ID, generating it if not set."""
216
+ global _default_agent_run_id
217
+ if _default_agent_run_id is None:
218
+ _default_agent_run_id = _generate_id()
219
+ return _default_agent_run_id
220
+
221
+
206
222
  def _set_current_agent_run_id(agent_run_id: Optional[str]) -> Token[Optional[str]]:
207
223
  """Set the current agent run ID in context."""
208
224
  return _current_agent_run_id.set(agent_run_id)