You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
swarms/docs/swarms/utils/phoenix_tracer.md

4.0 KiB

Phoenix Trace Decorator Documentation

Introduction

Welcome to the documentation for the phoenix_trace_decorator module. This module provides a convenient decorator for tracing Python functions and capturing exceptions using Phoenix, a versatile tracing and monitoring tool. Phoenix allows you to gain insights into the execution of your code, capture errors, and monitor performance.

Table of Contents

  1. Installation
  2. Getting Started
  3. Decorator Usage
  4. Examples
  5. Best Practices
  6. References

1. Installation

Before using the phoenix_trace_decorator, you need to install the Swarms library. You can install Phoenix using pip:

pip install swarms

2. Getting Started

Phoenix is a powerful tracing and monitoring tool, and the phoenix_trace_decorator simplifies the process of tracing functions and capturing exceptions within your Python code. To begin, ensure that Phoenix is installed, and then import the phoenix_trace_decorator module into your Python script.

from swarms import phoenix_trace_decorator

3. Decorator Usage

The phoenix_trace_decorator module provides a decorator, phoenix_trace_decorator, which can be applied to functions you want to trace. The decorator takes a single argument, a docstring that describes the purpose of the function being traced.

Here is the basic structure of using the decorator:

@phoenix_trace_decorator("Description of the function")
def my_function(param1, param2):
    # Function implementation
    pass

4. Examples

Let's explore some practical examples of using the phoenix_trace_decorator in your code.

Example 1: Basic Tracing

In this example, we'll trace a simple function and print a message.

@phoenix_trace_decorator("Tracing a basic function")
def hello_world():
    print("Hello, World!")

# Call the decorated function
hello_world()

Example 2: Tracing a Function with Parameters

You can use the decorator with functions that have parameters.

@phoenix_trace_decorator("Tracing a function with parameters")
def add_numbers(a, b):
    result = a + b
    print(f"Result: {result}")

# Call the decorated function with parameters
add_numbers(2, 3)

Example 3: Tracing Nested Calls

The decorator can also trace nested function calls.

@phoenix_trace_decorator("Outer function")
def outer_function():
    print("Outer function")

    @phoenix_trace_decorator("Inner function")
    def inner_function():
        print("Inner function")

    inner_function()

# Call the decorated functions
outer_function()

Example 4: Exception Handling

Phoenix can capture exceptions and provide detailed information about them.

@phoenix_trace_decorator("Function with exception handling")
def divide(a, b):
    try:
        result = a / b
    except ZeroDivisionError as e:
        raise ValueError("Division by zero") from e

# Call the decorated function with an exception
try:
    divide(5, 0)
except ValueError as e:
    print(f"Error: {e}")

5. Best Practices

When using the phoenix_trace_decorator, consider the following best practices:

  • Use meaningful docstrings to describe the purpose of the traced functions.
  • Keep your tracing focused on critical parts of your code.
  • Make sure Phoenix is properly configured and running before using the decorator.

6. References

For more information on Phoenix and advanced usage, please refer to the Phoenix Documentation.


By following this documentation, you can effectively use the phoenix_trace_decorator to trace your Python functions, capture exceptions, and gain insights into the execution of your code. This tool is valuable for debugging, performance optimization, and monitoring the health of your applications.