|
24 | 24 | # Module-level storage for originals |
25 | 25 | _original_handle_chat_attributes: Callable[..., Any] | None = None |
26 | 26 | _original_handle_response: Callable[..., Any] | None = None |
27 | | -_switch = False |
| 27 | +_agentops_service_enabled = False |
28 | 28 |
|
29 | 29 |
|
30 | | -def set_switch(value: bool): |
| 30 | +def enable_agentops_service(enabled: bool = True) -> None: |
31 | 31 | """ |
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. |
33 | 38 | """ |
34 | | - global _switch |
35 | | - _switch = value |
| 39 | + global _agentops_service_enabled |
| 40 | + _agentops_service_enabled = enabled |
36 | 41 | logger.info(f"Switch set to {value} for exporters and clients.") |
37 | 42 |
|
38 | 43 |
|
@@ -211,50 +216,70 @@ def uninstrument_agentops(): |
211 | 216 |
|
212 | 217 |
|
213 | 218 | class SwitchableAuthenticatedOTLPExporter(AuthenticatedOTLPExporter): |
| 219 | + """ |
| 220 | + AuthenticatedOTLPExporter with switchable service control. |
| 221 | + When `_agentops_service_enabled` is False, skip export and return success. |
| 222 | + """ |
214 | 223 |
|
215 | 224 | def export(self, *args: Any, **kwargs: Any) -> SpanExportResult: |
216 | | - if _switch: |
| 225 | + if _agentops_service_enabled: |
217 | 226 | return super().export(*args, **kwargs) |
218 | 227 | else: |
219 | 228 | logger.debug("SwitchableAuthenticatedOTLPExporter is switched off, skipping export.") |
220 | 229 | return SpanExportResult.SUCCESS |
221 | 230 |
|
222 | 231 |
|
223 | 232 | class SwitchableOTLPMetricExporter(OTLPMetricExporter): |
| 233 | + """ |
| 234 | + OTLPMetricExporter with switchable service control. |
| 235 | + When `_agentops_service_enabled` is False, skip export and return success. |
| 236 | + """ |
224 | 237 |
|
225 | 238 | def export(self, *args: Any, **kwargs: Any) -> MetricExportResult: |
226 | | - if _switch: |
| 239 | + if _agentops_service_enabled: |
227 | 240 | return super().export(*args, **kwargs) # type: ignore[reportUnknownMemberType] |
228 | 241 | else: |
229 | 242 | logger.debug("SwitchableOTLPMetricExporter is switched off, skipping export.") |
230 | 243 | return MetricExportResult.SUCCESS |
231 | 244 |
|
232 | 245 |
|
233 | 246 | class SwitchableOTLPSpanExporter(OTLPSpanExporter): |
| 247 | + """ |
| 248 | + OTLPSpanExporter with switchable service control. |
| 249 | + When `_agentops_service_enabled` is False, skip export and return success. |
| 250 | + """ |
234 | 251 |
|
235 | 252 | def export(self, *args: Any, **kwargs: Any) -> SpanExportResult: |
236 | | - if _switch: |
| 253 | + if _agentops_service_enabled: |
237 | 254 | return super().export(*args, **kwargs) |
238 | 255 | else: |
239 | 256 | logger.debug("SwitchableOTLPSpanExporter is switched off, skipping export.") |
240 | 257 | return SpanExportResult.SUCCESS |
241 | 258 |
|
242 | 259 |
|
243 | 260 | class SwitchableV3Client(V3Client): |
| 261 | + """ |
| 262 | + V3Client with toggleable authentication calls. |
| 263 | + Returns dummy auth response when `_agentops_service_enabled` is False. |
| 264 | + """ |
244 | 265 |
|
245 | 266 | def fetch_auth_token(self, *args: Any, **kwargs: Any) -> AuthTokenResponse: |
246 | | - if _switch: |
| 267 | + if _agentops_service_enabled: |
247 | 268 | resp = super().post(*args, **kwargs) |
248 | 269 | return cast(AuthTokenResponse, resp) |
249 | 270 | 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.") |
251 | 272 | return AuthTokenResponse(token="dummy", project_id="dummy") |
252 | 273 |
|
253 | 274 |
|
254 | 275 | class SwitchableV4Client(V4Client): |
| 276 | + """ |
| 277 | + V4Client with toggleable post requests. |
| 278 | + Returns dummy response when `_agentops_service_enabled` is False. |
| 279 | + """ |
255 | 280 |
|
256 | 281 | def post(self, *args: Any, **kwargs: Any) -> requests.Response: |
257 | | - if _switch: |
| 282 | + if _agentops_service_enabled: |
258 | 283 | return super().post(*args, **kwargs) |
259 | 284 | else: |
260 | 285 | logger.debug("SwitchableV4Client is switched off, skipping post request.") |
|
0 commit comments