-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add traces tool for Datadog APM trace retrieval (#6)
- Loading branch information
Showing
6 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { TRACES_TOOLS, TRACES_HANDLERS } from './tool' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { z } from 'zod' | ||
|
||
export const ListTracesZodSchema = z.object({ | ||
query: z.string().describe('Datadog APM trace query string'), | ||
from: z.number().describe('Start time in epoch seconds'), | ||
to: z.number().describe('End time in epoch seconds'), | ||
limit: z | ||
.number() | ||
.optional() | ||
.default(100) | ||
.describe('Maximum number of traces to return'), | ||
sort: z | ||
.enum(['timestamp', '-timestamp']) | ||
.optional() | ||
.default('-timestamp') | ||
.describe('Sort order for traces'), | ||
service: z.string().optional().describe('Filter by service name'), | ||
operation: z.string().optional().describe('Filter by operation name'), | ||
}) | ||
|
||
export type ListTracesArgs = z.infer<typeof ListTracesZodSchema> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { ExtendedTool, ToolHandlers } from '../../utils/types' | ||
import { v2 } from '@datadog/datadog-api-client' | ||
import { createToolSchema } from '../../utils/tool' | ||
import { datadogConfig as config } from '../../utils/datadog' | ||
import { ListTracesZodSchema } from './schema' | ||
|
||
type TracesToolName = 'list_traces' | ||
type TracesTool = ExtendedTool<TracesToolName> | ||
|
||
export const TRACES_TOOLS: TracesTool[] = [ | ||
createToolSchema( | ||
ListTracesZodSchema, | ||
'list_traces', | ||
'Get APM traces from Datadog', | ||
), | ||
] as const | ||
|
||
const API_INSTANCE = new v2.SpansApi(config) | ||
|
||
type TracesToolHandlers = ToolHandlers<TracesToolName> | ||
|
||
export const TRACES_HANDLERS: TracesToolHandlers = { | ||
list_traces: async (request) => { | ||
const { | ||
query, | ||
from, | ||
to, | ||
limit = 100, | ||
sort = '-timestamp', | ||
service, | ||
operation, | ||
} = request.params.arguments as { | ||
query: string | ||
from: number | ||
to: number | ||
limit?: number | ||
sort?: string | ||
service?: string | ||
operation?: string | ||
} | ||
|
||
const response = await API_INSTANCE.listSpans({ | ||
body: { | ||
data: { | ||
attributes: { | ||
filter: { | ||
query: [ | ||
query, | ||
...(service ? [`service:${service}`] : []), | ||
...(operation ? [`operation:${operation}`] : []), | ||
].join(' '), | ||
from: new Date(from * 1000).toISOString(), | ||
to: new Date(to * 1000).toISOString(), | ||
}, | ||
sort: sort as 'timestamp' | '-timestamp', | ||
page: { limit }, | ||
}, | ||
type: 'search_request', | ||
}, | ||
}, | ||
}) | ||
|
||
if (!response.data) { | ||
throw new Error('No traces data returned') | ||
} | ||
|
||
return { | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: `Traces: ${JSON.stringify({ | ||
traces: response.data, | ||
count: response.data.length, | ||
})}`, | ||
}, | ||
], | ||
} | ||
}, | ||
} as const |