@arizeai/phoenix-client
Version:
A client for the Phoenix API
96 lines • 2.88 kB
JavaScript
import { createClient } from "../client.js";
import { LIST_PROJECT_TRACES } from "../constants/serverRequirements.js";
import { resolveProjectIdentifier } from "../types/projects.js";
import { ensureServerCapability } from "../utils/serverVersionUtils.js";
/**
* Get traces from a project with filtering and sorting options.
*
* This method fetches traces from a project with support for time range filtering,
* sorting, session filtering, and cursor-based pagination.
*
* @requires Phoenix server >= 13.15.0
*
* @param params - The parameters to get traces
* @returns A paginated response containing traces and optional next cursor
*
* @example
* ```ts
* // Get recent traces from a project
* const result = await getTraces({
* client,
* project: { projectName: "my-project" },
* limit: 50,
* });
*
* // Get traces in a time range with spans included
* const result = await getTraces({
* client,
* project: { projectName: "my-project" },
* startTime: new Date("2024-01-01"),
* endTime: new Date("2024-01-02"),
* includeSpans: true,
* });
*
* // Paginate through results
* let cursor: string | undefined;
* do {
* const result = await getTraces({
* client,
* project: { projectName: "my-project" },
* cursor,
* limit: 100,
* });
* result.traces.forEach(trace => {
* console.log(`Trace: ${trace.trace_id}`);
* });
* cursor = result.nextCursor || undefined;
* } while (cursor);
* ```
*/
export async function getTraces({ client: _client, project, cursor, limit = 100, startTime, endTime, sort, order, includeSpans, sessionId, }) {
const client = _client ?? createClient();
await ensureServerCapability({ client, requirement: LIST_PROJECT_TRACES });
const projectIdentifier = resolveProjectIdentifier(project);
const params = {
limit,
};
if (cursor) {
params.cursor = cursor;
}
if (startTime) {
params.start_time =
startTime instanceof Date ? startTime.toISOString() : startTime;
}
if (endTime) {
params.end_time = endTime instanceof Date ? endTime.toISOString() : endTime;
}
if (sort) {
params.sort = sort;
}
if (order) {
params.order = order;
}
if (includeSpans) {
params.include_spans = true;
}
if (sessionId) {
params.session_identifier = Array.isArray(sessionId)
? sessionId
: [sessionId];
}
const { data, error } = await client.GET("/v1/projects/{project_identifier}/traces", {
params: {
path: {
project_identifier: projectIdentifier,
},
query: params,
},
});
if (error)
throw error;
return {
traces: data?.data ?? [],
nextCursor: data?.next_cursor ?? null,
};
}
//# sourceMappingURL=getTraces.js.map