Using Association Properties

Last updated: December 13, 2024

Traceloop allows you to set association properties for your traces. These properties can be used to attach unique identifiers to each trace, making it easier to search and identify specific API calls.

Generating unique identifiers

To associate a unique identifier with each trace, follow these steps:

  1. Generate a unique identifier for each API request. You can use libraries like uuid to create unique IDs.

  2. Use the Traceloop.set_association_properties() method to attach the identifier to the trace.

Example implementation

Here's an example of how to implement this in a FastAPI application:

from fastapi import APIRouter
from uuid import uuid4
from pydantic import BaseModel
from traceloop import Traceloop, aworkflow

router = APIRouter()

class Response(BaseModel):
    session_id: str

@router.get("/hello", response_model=Response)
async def hello():
    """
    Generates a unique UUID v4 for each request
    """
    session_id = str(uuid4())
    some_workflow(session_id)
    return Response(session_id=session_id)

@aworkflow("some_workflow")
async def some_workflow(session_id: str):
    Traceloop.set_association_properties({ "session_id": session_id })

In this example:

  1. We generate a unique session_id using uuid4() for each API request.

  2. The session_id is passed to the some_workflow function.

  3. Inside the workflow, we use Traceloop.set_association_properties() to associate the session_id with the trace.

Searching for traces

Once you've implemented this approach, you can use the Traceloop search functionality to find traces associated with specific session_id values. This allows you to easily identify which trace corresponds to a particular API call made through Postman or any other testing tool.

By using dynamic, unique identifiers for each request, you can ensure that your traces are easily searchable and identifiable, improving your debugging and monitoring capabilities.