Skip to main content

Python class

Tracer

Tracer

class max.profiler.Tracer(message=None, color='modular_purple')

source

Bases: object

A stack-based profiling manager for creating nested profiling spans.

Manages a stack of profiling spans that allows for nested tracing without requiring deeply nested with Trace(name): statements. This is especially useful when you need to dynamically create and manage profiling spans based on runtime conditions or when profiling spans don’t align with your code’s block structure.

The Tracer can be used both as a context manager and as a manual stack manager. As a context manager, it ensures all pushed spans are properly closed when the context exits.

from max.profiler import Tracer

tracer = Tracer("parent_operation", color="modular_purple")
tracer.push("child_operation")
# ... perform work ...
tracer.pop()

# Context manager with manual stack
with Tracer("parent_operation", color="modular_purple") as tracer:
    # The parent span is named "parent_operation"
    tracer.push("child_operation")
    # ... perform work ...
    tracer.pop()
    # All spans are automatically closed on context exit

Initializes the tracer stack.

Creates an empty trace stack and optionally pushes an initial profiling span if a message is provided.

Parameters:

  • message (str | None) – The name of the initial profiling span. If None, no initial span is created.
  • color (str) – The color of the profiling span for visualization tools.

cleanup()

cleanup()

source

Closes all remaining profiling spans.

Pops and closes all profiling spans that were pushed onto the stack. This method is automatically called when the tracer is used as a context manager or when the object is deleted.

Return type:

None

mark()

mark()

source

Marks the current profiling span with a timestamp.

Records a timestamp event within the current profiling span. This is useful for marking significant events or milestones within a longer operation.

Raises:

AssertionError – If the stack is empty when mark is called.

Return type:

None

next()

next(message, color='modular_purple')

source

Transitions to the next profiling span.

Pops the current profiling span and immediately pushes a new one with the specified message. This is a convenience method for sequential operations at the same nesting level.

Parameters:

  • message (str) – The name of the new profiling span.
  • color (str) – The color of the profiling span for visualization tools.

Return type:

None

pop()

pop(exc_type=None, exc_value=None, traceback=None)

source

Pops a profiling span off the stack and closes it.

Removes the most recently pushed profiling span from the stack and closes it, recording its execution time. Exception information can be passed through for proper error handling in context managers.

Parameters:

  • exc_type (type[BaseException] | None) – The exception type if an exception occurred, or None.
  • exc_value (BaseException | None) – The exception instance if an exception occurred, or None.
  • traceback (TracebackType | None) – The traceback object if an exception occurred, or None.

Return type:

None

push()

push(message=None, color='modular_purple')

source

Pushes a new profiling span onto the stack.

Creates and activates a new profiling span. If profiling is disabled or no message is provided, pushes a None placeholder to maintain stack consistency.

Parameters:

  • message (str | None) – The name of the profiling span. If None, no span is created.
  • color (str) – The color of the profiling span for visualization tools.

Return type:

None