honeyhive.tracer.instrumentation
Instrumentation framework for HoneyHive tracing.
This module provides user-facing instrumentation capabilities including decorators, enrichment, and tracer initialization. All components use dynamic logic patterns for flexible, configuration-driven instrumentation.
atrace
atrace(
event_type: Optional[str] = None,
event_name: Optional[str] = None,
**kwargs: Any
) -> Union[
Callable[[Callable[..., Any]], Callable[..., Any]],
Callable[..., Any],
]
Legacy async-specific trace decorator (deprecated).
Note
This decorator is maintained for backwards compatibility.
Use the unified :func:trace decorator instead, which auto-detects
sync/async functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_type
|
Optional[str]
|
Type of event being traced (e.g., "model", "tool", "chain") |
None
|
event_name
|
Optional[str]
|
Name of the event (defaults to function name) |
None
|
**kwargs
|
Any
|
Additional tracing parameters (source, project, session_id, etc.) |
{}
|
Returns:
| Type | Description |
|---|---|
Union[Callable[[Callable[..., Any]], Callable[..., Any]], Callable[..., Any]]
|
Decorated async function with tracing capabilities |
See Also
:func:trace: Unified decorator that handles both sync and async functions
Source code in src/honeyhive/tracer/instrumentation/decorators.py
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 | |
trace
trace(
event_type: Optional[str] = None,
event_name: Optional[str] = None,
**kwargs: Any
) -> Union[
Callable[[Callable[..., T]], Callable[..., T]],
Callable[..., T],
]
Unified trace decorator that auto-detects sync/async functions.
Automatically detects whether the decorated function is synchronous or asynchronous and applies the appropriate wrapper. This decorator can be used on both sync and async functions without needing separate decorators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_type
|
Optional[str]
|
Type of event being traced (e.g., "model", "tool", "chain") |
None
|
event_name
|
Optional[str]
|
Name of the event (defaults to function name) |
None
|
**kwargs
|
Any
|
Additional tracing parameters (source, project, session_id, etc.) |
{}
|
Returns:
| Type | Description |
|---|---|
Union[Callable[[Callable[..., T]], Callable[..., T]], Callable[..., T]]
|
Decorated function with tracing capabilities |
Example
@trace(event_type="model", event_name="gpt_call") ... def sync_function(): ... return "result"
@trace(event_type="model", event_name="async_gpt_call") ... async def async_function(): ... return "async result"
Source code in src/honeyhive/tracer/instrumentation/decorators.py
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 | |
trace_class
Class decorator to automatically trace all public methods.
Uses dynamic reflection to discover and wrap all public methods of a class. Automatically detects sync/async methods and applies appropriate tracing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
The class to be decorated |
required |
Returns:
| Type | Description |
|---|---|
type
|
The decorated class with all public methods traced |
Example
@trace_class ... class MyService: ... def process_data(self, data): ... return data.upper() ... ... async def async_process(self, data): ... return await some_async_operation(data)
Source code in src/honeyhive/tracer/instrumentation/decorators.py
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 | |
enrich_span_core
enrich_span_core(
attributes: Optional[Dict[str, Any]] = None,
metadata: Optional[Dict[str, Any]] = None,
metrics: Optional[Dict[str, Any]] = None,
feedback: Optional[Dict[str, Any]] = None,
inputs: Optional[Dict[str, Any]] = None,
outputs: Optional[Dict[str, Any]] = None,
config: Optional[Dict[str, Any]] = None,
user_properties: Optional[Dict[str, Any]] = None,
error: Optional[str] = None,
event_id: Optional[str] = None,
update_event_id: Optional[str] = None,
tracer_instance: Optional[Any] = None,
verbose: bool = False,
**kwargs: Any
) -> Dict[str, Any]
Core enrichment logic with namespace support and backwards compatibility.
This function implements the unified enrichment architecture that supports multiple invocation patterns while maintaining backwards compatibility with the main branch interface. It routes parameters to proper attribute namespaces and handles arbitrary kwargs.
Backwards Compatibility: Supports the main branch reserved parameter interface (metadata, metrics, feedback, inputs, outputs, config, error, event_id).
New Features: - Simple dict via attributes parameter routes to metadata namespace - Arbitrary kwargs route to metadata namespace for convenience - user_properties routes to honeyhive_user_properties.* namespace
Parameter Precedence: When the same key appears in multiple places, merge/override with this order: 1. Reserved parameters (metadata, metrics, etc.) - Applied first 2. attributes dict - Applied second 3. **kwargs - Applied last (wins conflicts)
:param attributes: Simple dict that routes to metadata namespace :type attributes: Optional[Dict[str, Any]] :param metadata: Metadata namespace (honeyhive_metadata.) :type metadata: Optional[Dict[str, Any]] :param metrics: Metrics namespace (honeyhive_metrics.) :type metrics: Optional[Dict[str, Any]] :param feedback: Feedback namespace (honeyhive_feedback.) :type feedback: Optional[Dict[str, Any]] :param inputs: Inputs namespace (honeyhive_inputs.) :type inputs: Optional[Dict[str, Any]] :param outputs: Outputs namespace (honeyhive_outputs.) :type outputs: Optional[Dict[str, Any]] :param config: Config namespace (honeyhive_config.) :type config: Optional[Dict[str, Any]] :param user_properties: User properties namespace (honeyhive_user_properties.*) :type user_properties: Optional[Dict[str, Any]] :param error: Error string (honeyhive_error, non-namespaced) :type error: Optional[str] :param event_id: If provided, update an existing event with this ID via PUT /events API instead of enriching the current span :type event_id: Optional[str] :param update_event_id: Event ID to override the default event ID on the span (stored as honeyhive_event_id span attribute) :type update_event_id: Optional[str] :param tracer_instance: Optional tracer instance for logging :type tracer_instance: Optional[Any] :param verbose: Whether to log debug information :type verbose: bool :param kwargs: Arbitrary kwargs that route to metadata namespace :type kwargs: Any :return: Enrichment result with success status and span reference :rtype: Dict[str, Any]
Example:
.. code-block:: python
# Main branch backwards compatible usage
result = enrich_span_core(
metadata={"user_id": "123"},
metrics={"score": 0.95}
)
# New simplified usage
result = enrich_span_core(
user_id="123", # Routes to metadata
feature="chat" # Routes to metadata
)
# User properties usage
result = enrich_span_core(
user_properties={"user_id": "user-123", "plan": "premium"},
metrics={"score": 0.95}
)
Note:
This function is thread-safe and uses OpenTelemetry's context propagation to access the current span automatically.
Source code in src/honeyhive/tracer/instrumentation/enrichment.py
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 | |
enrich_span_unified
enrich_span_unified(
attributes: Optional[Dict[str, Any]] = None,
metadata: Optional[Dict[str, Any]] = None,
metrics: Optional[Dict[str, Any]] = None,
feedback: Optional[Dict[str, Any]] = None,
inputs: Optional[Dict[str, Any]] = None,
outputs: Optional[Dict[str, Any]] = None,
config: Optional[Dict[str, Any]] = None,
error: Optional[str] = None,
event_id: Optional[str] = None,
tracer_instance: Optional[Any] = None,
caller: str = "direct_call",
**kwargs: Any
) -> Union[bool, _GeneratorContextManager[Any, None, None]]
Unified enrich_span implementation with backwards compatibility.
This function implements the unified enrichment architecture with a simple caller parameter approach. Each caller explicitly identifies itself, making the behavior predictable and following dynamic logic standards.
Backwards Compatibility: Supports all main branch reserved parameters (metadata, metrics, etc.)
Tracer Discovery: If no tracer_instance is provided, automatically discovers tracer using: 1. Baggage-discovered tracer (context-aware) 2. Global default tracer (fallback)
:param attributes: Simple dict that routes to metadata namespace :type attributes: Optional[Dict[str, Any]] :param metadata: Metadata namespace :type metadata: Optional[Dict[str, Any]] :param metrics: Metrics namespace :type metrics: Optional[Dict[str, Any]] :param feedback: Feedback namespace :type feedback: Optional[Dict[str, Any]] :param inputs: Inputs namespace :type inputs: Optional[Dict[str, Any]] :param outputs: Outputs namespace :type outputs: Optional[Dict[str, Any]] :param config: Config namespace :type config: Optional[Dict[str, Any]] :param error: Error string :type error: Optional[str] :param event_id: Event ID :type event_id: Optional[str] :param tracer_instance: Optional tracer instance for context :type tracer_instance: Optional[Any] :param caller: Caller identification ('context_manager' or 'direct_call') :type caller: str :param kwargs: Arbitrary kwargs routing to metadata :type kwargs: Any :return: Context manager (Iterator) or boolean based on caller :rtype: Union[bool, Iterator[Any]]
Usage Patterns:
.. code-block:: python
# Context manager pattern - returns Iterator[Any]
enrich_span_unified(attrs, tracer, caller="context_manager")
# Direct call pattern - returns bool
enrich_span_unified(attrs, tracer, caller="direct_call")
Source code in src/honeyhive/tracer/instrumentation/enrichment.py
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 | |
initialize_tracer_instance
initialize_tracer_instance(
tracer_instance: HoneyHiveTracerBase,
) -> None
Initialize a HoneyHiveTracer instance with full setup.
This function handles the complete initialization process for a tracer instance, including OpenTelemetry setup, session creation, and provider configuration. It's called by the HoneyHiveTracer.init() class method.
:param tracer_instance: The tracer instance to initialize :type tracer_instance: HoneyHiveTracer :note: Uses graceful degradation - never crashes host application :note: Missing configuration triggers degraded mode with warnings :note: API failures result in no-op mode with local fallback
Example:
.. code-block:: python
tracer = HoneyHiveTracer(api_key="key", project="project")
initialize_tracer_instance(tracer)
# Tracer is now fully initialized and ready to use
Note:
This function modifies the tracer instance in-place and should only be called once per tracer instance during initialization.
Source code in src/honeyhive/tracer/instrumentation/initialization.py
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 | |