Skip to content

honeyhive.tracer.integration.http

Dynamic HTTP instrumentation for HoneyHive tracing.

This module provides flexible HTTP instrumentation using dynamic library detection, configuration-driven enablement, and extensible tracing patterns. All instrumentation logic is designed to be non-intrusive and gracefully degrade when libraries are not available.

HTTPInstrumentation

Dynamic HTTP instrumentation for automatic request tracing.

Source code in src/honeyhive/tracer/integration/http.py
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
class HTTPInstrumentation:
    """Dynamic HTTP instrumentation for automatic request tracing."""

    def __init__(self, tracer_instance: Any = None) -> None:
        """Initialize HTTP instrumentation with dynamic library detection.

        Args:
            tracer_instance: Optional tracer instance for logging context
        """
        self.tracer_instance = tracer_instance
        self._library_availability = self._detect_libraries_dynamically()
        self._original_methods: Dict[str, Any] = {}
        self._is_instrumented = False
        self._instrumentation_config = self._build_instrumentation_config_dynamically()

    def _detect_libraries_dynamically(self) -> Dict[str, bool]:
        """Dynamically detect available HTTP libraries.

        Returns:
            Dictionary mapping library names to availability status
        """
        libraries = {}

        # Dynamic library detection patterns
        library_detection_map = {
            "httpx": "httpx",
            "requests": "requests",
            "aiohttp": "aiohttp",
            "urllib3": "urllib3",
        }

        for lib_name, import_name in library_detection_map.items():
            try:
                __import__(import_name)
                libraries[lib_name] = True
                safe_log(
                    self.tracer_instance,
                    "debug",
                    f"HTTP library detected: {lib_name}",
                    honeyhive_data={"library": lib_name},
                )
            except ImportError:
                libraries[lib_name] = False

        return libraries

    def _build_instrumentation_config_dynamically(self) -> Dict[str, Any]:
        """Dynamically build instrumentation configuration.

        Returns:
            Configuration dictionary for instrumentation
        """
        return {
            "enabled": not self._is_http_tracing_disabled_dynamically(),
            "libraries": {
                "httpx": {
                    "enabled": self._library_availability.get("httpx", False),
                    "methods": ["request"],
                    "classes": ["Client", "AsyncClient"],
                },
                "requests": {
                    "enabled": self._library_availability.get("requests", False),
                    "methods": ["request"],
                    "classes": ["Session"],
                },
            },
            "span_attributes": {
                "http.method": True,
                "http.url": True,
                "http.status_code": True,
                "http.user_agent": False,  # Privacy consideration
            },
            "error_handling": {
                "graceful_degradation": True,
                "log_failures": True,
                "fallback_to_original": True,
            },
        }

    def _is_http_tracing_disabled_dynamically(self) -> bool:
        """Dynamically check if HTTP tracing is disabled.

        Returns:
            True if HTTP tracing is disabled
        """
        # Dynamic environment variable patterns
        disable_patterns = [
            "HH_DISABLE_HTTP_TRACING",
            "HONEYHIVE_DISABLE_HTTP_TRACING",
            "DISABLE_HTTP_TRACING",
        ]

        for pattern in disable_patterns:
            value = os.getenv(pattern, "false").lower()
            if value in {"true", "1", "yes", "on"}:
                return True

        return False

    def instrument(self) -> None:
        """Dynamically instrument HTTP libraries for automatic tracing."""
        if self._is_instrumented:
            safe_log(
                self.tracer_instance, "debug", "HTTP instrumentation already active"
            )
            return

        if not self._instrumentation_config["enabled"]:
            safe_log(
                self.tracer_instance, "info", "HTTP tracing disabled by configuration"
            )
            return

        # Dynamic instrumentation execution
        instrumentation_results = self._execute_instrumentation_dynamically()

        self._is_instrumented = any(instrumentation_results.values())

        safe_log(
            self.tracer_instance,
            "info",
            "HTTP instrumentation completed",
            honeyhive_data={
                "instrumented_libraries": [
                    lib for lib, success in instrumentation_results.items() if success
                ],
                "total_instrumented": sum(instrumentation_results.values()),
            },
        )

    def _execute_instrumentation_dynamically(self) -> Dict[str, bool]:
        """Dynamically execute instrumentation for available libraries.

        Returns:
            Dictionary mapping library names to instrumentation success status
        """
        results = {}

        # Dynamic instrumentation strategies
        instrumentation_strategies = {
            "httpx": self._instrument_httpx_dynamically,
            "requests": self._instrument_requests_dynamically,
        }

        for library_name, strategy in instrumentation_strategies.items():
            if self._should_instrument_library_dynamically(library_name):
                try:
                    success = strategy()
                    results[library_name] = success

                    if success:
                        safe_log(
                            self.tracer_instance,
                            "debug",
                            f"Successfully instrumented {library_name}",
                            honeyhive_data={"library": library_name},
                        )
                    else:
                        safe_log(
                            self.tracer_instance,
                            "warning",
                            f"Failed to instrument {library_name}",
                            honeyhive_data={"library": library_name},
                        )

                except Exception as e:
                    results[library_name] = False
                    safe_log(
                        self.tracer_instance,
                        "error",
                        f"Error instrumenting {library_name}",
                        honeyhive_data={
                            "library": library_name,
                            "error": str(e),
                            "error_type": type(e).__name__,
                        },
                    )
            else:
                results[library_name] = False

        return results

    def _should_instrument_library_dynamically(self, library_name: str) -> bool:
        """Dynamically determine if library should be instrumented.

        Args:
            library_name: Name of the library to check

        Returns:
            True if library should be instrumented
        """
        library_config = self._instrumentation_config["libraries"].get(library_name, {})

        return library_config.get("enabled", False) and self._library_availability.get(
            library_name, False
        )

    def _instrument_httpx_dynamically(self) -> bool:
        """Dynamically instrument httpx library.

        Returns:
            True if instrumentation successful
        """
        try:
            import httpx  # pylint: disable=import-outside-toplevel

            # Store original methods dynamically
            original_methods = self._store_original_methods_dynamically(
                httpx, ["Client", "AsyncClient"], ["request"]
            )

            if not original_methods:
                return False

            self._original_methods["httpx"] = original_methods

            # Create instrumented methods dynamically
            instrumented_methods = self._create_instrumented_methods_dynamically(
                "httpx", original_methods
            )

            # Apply instrumentation dynamically
            return self._apply_instrumentation_dynamically(
                httpx, instrumented_methods, ["Client", "AsyncClient"]
            )

        except ImportError:
            safe_log(
                self.tracer_instance, "debug", "httpx not available for instrumentation"
            )
            return False
        except Exception as e:
            safe_log(
                self.tracer_instance,
                "error",
                "Failed to instrument httpx",
                honeyhive_data={"error": str(e)},
            )
            return False

    def _instrument_httpx(self) -> None:
        """Instrument httpx for automatic tracing (compatibility method)."""
        # This is a compatibility method for tests that expect the original naming
        self._instrument_httpx_dynamically()

    def _instrument_requests(self) -> None:
        """Instrument requests for automatic tracing (compatibility method)."""
        # This is a compatibility method for tests that expect the original naming
        self._instrument_requests_dynamically()

    def _instrument_requests_dynamically(self) -> bool:
        """Dynamically instrument requests library.

        Returns:
            True if instrumentation successful
        """
        try:
            import requests  # pylint: disable=import-outside-toplevel

            # Store original methods dynamically
            original_methods = self._store_original_methods_dynamically(
                requests, ["Session"], ["request"]
            )

            if not original_methods:
                return False

            self._original_methods["requests"] = original_methods

            # Create instrumented methods dynamically
            instrumented_methods = self._create_instrumented_methods_dynamically(
                "requests", original_methods
            )

            # Apply instrumentation dynamically
            return self._apply_instrumentation_dynamically(
                requests, instrumented_methods, ["Session"]
            )

        except ImportError:
            safe_log(
                self.tracer_instance,
                "debug",
                "requests not available for instrumentation",
            )
            return False
        except Exception as e:
            safe_log(
                self.tracer_instance,
                "error",
                "Failed to instrument requests",
                honeyhive_data={"error": str(e)},
            )
            return False

    def _store_original_methods_dynamically(
        self, module: Any, class_names: List[str], method_names: List[str]
    ) -> Dict[str, Any]:
        """Dynamically store original methods before instrumentation.

        Args:
            module: Module containing classes to instrument
            class_names: Names of classes to instrument
            method_names: Names of methods to instrument

        Returns:
            Dictionary of original methods
        """
        original_methods = {}

        for class_name in class_names:
            if not hasattr(module, class_name):
                continue

            class_obj = getattr(module, class_name)

            for method_name in method_names:
                if hasattr(class_obj, method_name):
                    method_key = f"{class_name}.{method_name}"
                    original_methods[method_key] = getattr(class_obj, method_name)

        return original_methods

    def _create_instrumented_methods_dynamically(
        self, library_name: str, original_methods: Dict[str, Any]
    ) -> Dict[str, Any]:
        """Dynamically create instrumented methods.

        Args:
            library_name: Name of the library being instrumented
            original_methods: Dictionary of original methods

        Returns:
            Dictionary of instrumented methods
        """
        instrumented_methods = {}

        for method_key, original_method in original_methods.items():
            # Create instrumented wrapper dynamically
            instrumented_method = self._create_method_wrapper_dynamically(
                library_name, method_key, original_method
            )
            instrumented_methods[method_key] = instrumented_method

        return instrumented_methods

    def _create_method_wrapper_dynamically(
        self, library_name: str, method_key: str, original_method: Any
    ) -> Any:
        """Dynamically create method wrapper with tracing.

        Args:
            library_name: Name of the library
            method_key: Key identifying the method
            original_method: Original method to wrap

        Returns:
            Wrapped method with tracing
        """

        def instrumented_wrapper(self_obj: Any, *args: Any, **kwargs: Any) -> Any:
            """Dynamically instrumented method wrapper."""
            try:
                # Dynamic span creation and tracing
                return self._execute_with_tracing_dynamically(
                    library_name,
                    method_key,
                    original_method=original_method,
                    self_obj=self_obj,
                    args=args,
                    kwargs=kwargs,
                )
            except Exception as e:
                # Graceful degradation on instrumentation failure
                safe_log(
                    self.tracer_instance,
                    "debug",
                    "Instrumentation wrapper failed, falling back to original",
                    honeyhive_data={
                        "library": library_name,
                        "method": method_key,
                        "error": str(e),
                    },
                )
                return original_method(self_obj, *args, **kwargs)

        return instrumented_wrapper

    # pylint: disable=too-many-arguments
    # Justification: HTTP tracing execution requires multiple parameters for
    # library identification, method handling, and argument processing.
    def _execute_with_tracing_dynamically(
        self,
        library_name: str,
        _method_key: str,
        *,
        original_method: Any,
        self_obj: Any,
        args: tuple,
        kwargs: dict,
    ) -> Any:
        """Dynamically execute method with tracing.

        Args:
            library_name: Name of the library
            method_key: Method identifier
            original_method: Original method to call
            self_obj: Instance object
            args: Method arguments
            kwargs: Method keyword arguments

        Returns:
            Result of original method call
        """
        # For now, just call the original method
        # Future enhancement: Add actual tracing logic here

        # Dynamic attribute extraction for tracing
        trace_attributes = self._extract_trace_attributes_dynamically(
            library_name, args, kwargs
        )

        safe_log(
            self.tracer_instance,
            "debug",
            "HTTP request traced",
            honeyhive_data={
                "library": library_name,
                "method": _method_key,
                "attributes": trace_attributes,
            },
        )

        return original_method(self_obj, *args, **kwargs)

    def _extract_trace_attributes_dynamically(
        self, library_name: str, args: tuple, _kwargs: dict
    ) -> Dict[str, Any]:
        """Dynamically extract trace attributes from method arguments.

        Args:
            library_name: Name of the library
            args: Method arguments
            kwargs: Method keyword arguments

        Returns:
            Dictionary of trace attributes
        """
        attributes = {}

        # Dynamic attribute extraction patterns
        if library_name in {"httpx", "requests"}:
            # Extract HTTP method and URL
            if len(args) >= 1:
                attributes["http.method"] = str(args[0]).upper()
            if len(args) >= 2:
                attributes["http.url"] = str(args[1])

        return attributes

    def _apply_instrumentation_dynamically(
        self, module: Any, instrumented_methods: Dict[str, Any], class_names: List[str]
    ) -> bool:
        """Dynamically apply instrumentation to module classes.

        Args:
            module: Module to instrument
            instrumented_methods: Dictionary of instrumented methods
            class_names: Names of classes to instrument

        Returns:
            True if instrumentation applied successfully
        """
        try:
            for method_key, instrumented_method in instrumented_methods.items():
                class_name, method_name = method_key.split(".", 1)

                if class_name in class_names and hasattr(module, class_name):
                    class_obj = getattr(module, class_name)
                    setattr(class_obj, method_name, instrumented_method)

            return True

        except Exception as e:
            safe_log(
                self.tracer_instance,
                "error",
                "Failed to apply instrumentation",
                honeyhive_data={
                    "module": module.__name__,
                    "error": str(e),
                },
            )
            return False

    def uninstrument(self) -> None:
        """Dynamically remove HTTP instrumentation."""
        if not self._is_instrumented:
            safe_log(self.tracer_instance, "debug", "HTTP instrumentation not active")
            return

        # Dynamic uninstrumentation
        uninstrumentation_results = self._execute_uninstrumentation_dynamically()

        self._is_instrumented = False

        safe_log(
            self.tracer_instance,
            "info",
            "HTTP uninstrumentation completed",
            honeyhive_data={
                "uninstrumented_libraries": [
                    lib for lib, success in uninstrumentation_results.items() if success
                ],
                "total_uninstrumented": sum(uninstrumentation_results.values()),
            },
        )

    def _execute_uninstrumentation_dynamically(self) -> Dict[str, bool]:
        """Dynamically execute uninstrumentation for all libraries.

        Returns:
            Dictionary mapping library names to uninstrumentation success status
        """
        results = {}

        for library_name, original_methods in self._original_methods.items():
            try:
                success = self._restore_original_methods_dynamically(
                    library_name, original_methods
                )
                results[library_name] = success
            except Exception as e:
                results[library_name] = False
                safe_log(
                    self.tracer_instance,
                    "error",
                    f"Error uninstrumenting {library_name}",
                    honeyhive_data={
                        "library": library_name,
                        "error": str(e),
                    },
                )

        return results

    def _restore_original_methods_dynamically(
        self, library_name: str, original_methods: Dict[str, Any]
    ) -> bool:
        """Dynamically restore original methods.

        Args:
            library_name: Name of the library
            original_methods: Dictionary of original methods

        Returns:
            True if restoration successful
        """
        try:
            # Dynamic module import
            module = __import__(library_name)

            # Restore methods dynamically
            for method_key, original_method in original_methods.items():
                class_name, method_name = method_key.split(".", 1)

                if hasattr(module, class_name):
                    class_obj = getattr(module, class_name)
                    setattr(class_obj, method_name, original_method)

            return True

        except Exception as e:
            safe_log(
                self.tracer_instance,
                "error",
                f"Failed to restore original methods for {library_name}",
                honeyhive_data={"error": str(e)},
            )
            return False

    def get_instrumentation_status(self) -> Dict[str, Any]:
        """Get dynamic instrumentation status.

        Returns:
            Dictionary with instrumentation status information
        """
        return {
            "is_instrumented": self._is_instrumented,
            "enabled": self._instrumentation_config["enabled"],
            "library_availability": self._library_availability,
            "instrumented_libraries": list(self._original_methods.keys()),
            "configuration": self._instrumentation_config,
        }

tracer_instance instance-attribute

tracer_instance = tracer_instance

instrument

instrument() -> None

Dynamically instrument HTTP libraries for automatic tracing.

Source code in src/honeyhive/tracer/integration/http.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
def instrument(self) -> None:
    """Dynamically instrument HTTP libraries for automatic tracing."""
    if self._is_instrumented:
        safe_log(
            self.tracer_instance, "debug", "HTTP instrumentation already active"
        )
        return

    if not self._instrumentation_config["enabled"]:
        safe_log(
            self.tracer_instance, "info", "HTTP tracing disabled by configuration"
        )
        return

    # Dynamic instrumentation execution
    instrumentation_results = self._execute_instrumentation_dynamically()

    self._is_instrumented = any(instrumentation_results.values())

    safe_log(
        self.tracer_instance,
        "info",
        "HTTP instrumentation completed",
        honeyhive_data={
            "instrumented_libraries": [
                lib for lib, success in instrumentation_results.items() if success
            ],
            "total_instrumented": sum(instrumentation_results.values()),
        },
    )

uninstrument

uninstrument() -> None

Dynamically remove HTTP instrumentation.

Source code in src/honeyhive/tracer/integration/http.py
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
def uninstrument(self) -> None:
    """Dynamically remove HTTP instrumentation."""
    if not self._is_instrumented:
        safe_log(self.tracer_instance, "debug", "HTTP instrumentation not active")
        return

    # Dynamic uninstrumentation
    uninstrumentation_results = self._execute_uninstrumentation_dynamically()

    self._is_instrumented = False

    safe_log(
        self.tracer_instance,
        "info",
        "HTTP uninstrumentation completed",
        honeyhive_data={
            "uninstrumented_libraries": [
                lib for lib, success in uninstrumentation_results.items() if success
            ],
            "total_uninstrumented": sum(uninstrumentation_results.values()),
        },
    )

get_instrumentation_status

get_instrumentation_status() -> Dict[str, Any]

Get dynamic instrumentation status.

Returns:

Type Description
Dict[str, Any]

Dictionary with instrumentation status information

Source code in src/honeyhive/tracer/integration/http.py
604
605
606
607
608
609
610
611
612
613
614
615
616
def get_instrumentation_status(self) -> Dict[str, Any]:
    """Get dynamic instrumentation status.

    Returns:
        Dictionary with instrumentation status information
    """
    return {
        "is_instrumented": self._is_instrumented,
        "enabled": self._instrumentation_config["enabled"],
        "library_availability": self._library_availability,
        "instrumented_libraries": list(self._original_methods.keys()),
        "configuration": self._instrumentation_config,
    }

DummyInstrumentation

Dummy HTTP instrumentation that does nothing when HTTP tracing is disabled.

Source code in src/honeyhive/tracer/integration/http.py
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
class DummyInstrumentation:
    """Dummy HTTP instrumentation that does nothing when HTTP tracing is disabled."""

    def __init__(self, tracer_instance: Any = None) -> None:
        """Initialize dummy instrumentation.

        Args:
            tracer_instance: Optional tracer instance for logging context
        """
        self.tracer_instance = tracer_instance

    def instrument(self) -> None:
        """No-op instrument method."""
        safe_log(
            self.tracer_instance,
            "debug",
            "HTTP instrumentation disabled - using dummy implementation",
        )

    def uninstrument(self) -> None:
        """No-op uninstrument method."""

    def get_instrumentation_status(self) -> Dict[str, Any]:
        """Get dummy instrumentation status."""
        return {
            "is_instrumented": False,
            "enabled": False,
            "library_availability": {},
            "instrumented_libraries": [],
            "configuration": {"enabled": False},
        }

tracer_instance instance-attribute

tracer_instance = tracer_instance

instrument

instrument() -> None

No-op instrument method.

Source code in src/honeyhive/tracer/integration/http.py
630
631
632
633
634
635
636
def instrument(self) -> None:
    """No-op instrument method."""
    safe_log(
        self.tracer_instance,
        "debug",
        "HTTP instrumentation disabled - using dummy implementation",
    )

uninstrument

uninstrument() -> None

No-op uninstrument method.

Source code in src/honeyhive/tracer/integration/http.py
638
639
def uninstrument(self) -> None:
    """No-op uninstrument method."""

get_instrumentation_status

get_instrumentation_status() -> Dict[str, Any]

Get dummy instrumentation status.

Source code in src/honeyhive/tracer/integration/http.py
641
642
643
644
645
646
647
648
649
def get_instrumentation_status(self) -> Dict[str, Any]:
    """Get dummy instrumentation status."""
    return {
        "is_instrumented": False,
        "enabled": False,
        "library_availability": {},
        "instrumented_libraries": [],
        "configuration": {"enabled": False},
    }

instrument_http

instrument_http() -> None

Dynamically instrument HTTP libraries for automatic tracing.

Source code in src/honeyhive/tracer/integration/http.py
677
678
679
def instrument_http() -> None:
    """Dynamically instrument HTTP libraries for automatic tracing."""
    _instrumentation.instrument()

uninstrument_http

uninstrument_http() -> None

Dynamically remove HTTP instrumentation.

Source code in src/honeyhive/tracer/integration/http.py
682
683
684
def uninstrument_http() -> None:
    """Dynamically remove HTTP instrumentation."""
    _instrumentation.uninstrument()

get_http_instrumentation_status

get_http_instrumentation_status() -> Dict[str, Any]

Get current HTTP instrumentation status.

Returns:

Type Description
Dict[str, Any]

Dictionary with instrumentation status information

Source code in src/honeyhive/tracer/integration/http.py
687
688
689
690
691
692
693
694
def get_http_instrumentation_status() -> Dict[str, Any]:
    """Get current HTTP instrumentation status.

    Returns:
        Dictionary with instrumentation status information
    """
    result = _instrumentation.get_instrumentation_status()
    return dict(result) if result else {}