Skip to content

Commit 10b9fef

Browse files
author
Hao Ni (CSI Interfusion Co Ltd)
committed
add docstring and log
1 parent d5ec72f commit 10b9fef

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

agentlightning/instrumentation/agentops.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,20 @@
2424
# Module-level storage for originals
2525
_original_handle_chat_attributes: Callable[..., Any] | None = None
2626
_original_handle_response: Callable[..., Any] | None = None
27-
_switch = False
27+
_agentops_service_enabled = False
2828

2929

30-
def set_switch(value: bool):
30+
def enable_agentops_service(enabled: bool = True) -> None:
3131
"""
32-
Set the switch for exporters and clients to control their behavior.
32+
Enable or disable communication with the AgentOps service.
33+
34+
False (default): AgentOps exporters and clients will run in local mode
35+
and will not attempt to communicate with the remote AgentOps service.
36+
True: all exporters and clients will operate in normal mode and send data
37+
to the AgentOps service as expected.
3338
"""
34-
global _switch
35-
_switch = value
39+
global _agentops_service_enabled
40+
_agentops_service_enabled = enabled
3641
logger.info(f"Switch set to {value} for exporters and clients.")
3742

3843

@@ -211,50 +216,70 @@ def uninstrument_agentops():
211216

212217

213218
class SwitchableAuthenticatedOTLPExporter(AuthenticatedOTLPExporter):
219+
"""
220+
AuthenticatedOTLPExporter with switchable service control.
221+
When `_agentops_service_enabled` is False, skip export and return success.
222+
"""
214223

215224
def export(self, *args: Any, **kwargs: Any) -> SpanExportResult:
216-
if _switch:
225+
if _agentops_service_enabled:
217226
return super().export(*args, **kwargs)
218227
else:
219228
logger.debug("SwitchableAuthenticatedOTLPExporter is switched off, skipping export.")
220229
return SpanExportResult.SUCCESS
221230

222231

223232
class SwitchableOTLPMetricExporter(OTLPMetricExporter):
233+
"""
234+
OTLPMetricExporter with switchable service control.
235+
When `_agentops_service_enabled` is False, skip export and return success.
236+
"""
224237

225238
def export(self, *args: Any, **kwargs: Any) -> MetricExportResult:
226-
if _switch:
239+
if _agentops_service_enabled:
227240
return super().export(*args, **kwargs) # type: ignore[reportUnknownMemberType]
228241
else:
229242
logger.debug("SwitchableOTLPMetricExporter is switched off, skipping export.")
230243
return MetricExportResult.SUCCESS
231244

232245

233246
class SwitchableOTLPSpanExporter(OTLPSpanExporter):
247+
"""
248+
OTLPSpanExporter with switchable service control.
249+
When `_agentops_service_enabled` is False, skip export and return success.
250+
"""
234251

235252
def export(self, *args: Any, **kwargs: Any) -> SpanExportResult:
236-
if _switch:
253+
if _agentops_service_enabled:
237254
return super().export(*args, **kwargs)
238255
else:
239256
logger.debug("SwitchableOTLPSpanExporter is switched off, skipping export.")
240257
return SpanExportResult.SUCCESS
241258

242259

243260
class SwitchableV3Client(V3Client):
261+
"""
262+
V3Client with toggleable authentication calls.
263+
Returns dummy auth response when `_agentops_service_enabled` is False.
264+
"""
244265

245266
def fetch_auth_token(self, *args: Any, **kwargs: Any) -> AuthTokenResponse:
246-
if _switch:
267+
if _agentops_service_enabled:
247268
resp = super().post(*args, **kwargs)
248269
return cast(AuthTokenResponse, resp)
249270
else:
250-
logger.debug("SwitchableV4Client is switched off, skipping fetch_auth_token request.")
271+
logger.debug("SwitchableV3Client is switched off, skipping fetch_auth_token request.")
251272
return AuthTokenResponse(token="dummy", project_id="dummy")
252273

253274

254275
class SwitchableV4Client(V4Client):
276+
"""
277+
V4Client with toggleable post requests.
278+
Returns dummy response when `_agentops_service_enabled` is False.
279+
"""
255280

256281
def post(self, *args: Any, **kwargs: Any) -> requests.Response:
257-
if _switch:
282+
if _agentops_service_enabled:
258283
return super().post(*args, **kwargs)
259284
else:
260285
logger.debug("SwitchableV4Client is switched off, skipping post request.")

agentlightning/tracer/agentops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ def _trace_context_sync(
170170
status = StatusCode.SUCCESS # type: ignore
171171
try:
172172
yield processor
173-
except:
174-
# Need logging or raise here?
173+
except Exception as e:
175174
status = StatusCode.ERROR # type: ignore
175+
logger.debug(f"Trace failed for rollout_id={rollout_id}, attempt_id={attempt_id}, error={e}")
176176
finally:
177177
agentops.end_trace(trace, end_state=status) # type: ignore
178178
elif store is None and rollout_id is None and attempt_id is None:

0 commit comments

Comments
 (0)