Documentation Index
Fetch the complete documentation index at: https://www.truefoundry.com/llms.txt
Use this file to discover all available pages before exploring further.
Filter Request Logs
While fetching all Gateway request logs is useful for general monitoring,
you’ll often want to filter logs based on specific criteria such as user identity, model names, etc.
You can achieve this using the filters parameter in the query_spans method.
The API supports the following common filter types:
- Span fields filtering: Filter logs by span fields such as
spanName, traceId, spanId, etc. See the Query Spans documentation to understand the supported options for spanFieldName and operator.
- Span attributes filtering: Filter logs by span attributes, e.g., using
tfy.model.name for model name. See Span attributes section to understand the supported options for spanAttributeKey
- Gateway request metadata filtering: Filter logs based on Custom Metadata keys and values that you passed to Gateway requests.
The following example demonstrates how to retrieve request logs for OpenAI models submitted by a specific user, where custom metadata matches certain values:
from truefoundry import client
from truefoundry_sdk import SpanFieldFilter, SpanAttributeFilter, GatewayRequestMetadataFilter
from truefoundry_sdk.types.span_field_filter_span_field_name import SpanFieldFilterSpanFieldName
from truefoundry_sdk.types.span_field_filter_operator import SpanFieldFilterOperator
from truefoundry_sdk.types.span_attribute_filter_operator import SpanAttributeFilterOperator
from truefoundry_sdk.types.gateway_request_metadata_filter_operator import GatewayRequestMetadataFilterOperator
# Fetch LLM Gateway request logs with filters
spans = client.traces.query_spans(
data_routing_destination="default",
start_time="2025-01-21T00:00:00.000Z",
filters=[
SpanFieldFilter(span_field_name=SpanFieldFilterSpanFieldName.CREATED_BY_SUBJECT_SLUG, operator=SpanFieldFilterOperator.EQUAL, value="user@example.com"),
SpanAttributeFilter(span_attribute_key="tfy.model.name", operator=SpanAttributeFilterOperator.STRING_CONTAINS, value="openai"),
GatewayRequestMetadataFilter(gateway_request_metadata_key="foo", operator=GatewayRequestMetadataFilterOperator.IN, value=["bar1", "bar2"]),
]
)
for span in spans:
print(span.span_name, span.span_attributes.get('tfy.span_type'))