Skip to content

Commit 48c989c

Browse files
authored
[WIP] Ingest spans and process segments (#1436)
* Ingest spans and process segments * wip
1 parent c33c461 commit 48c989c

File tree

94 files changed

+18491
-512
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+18491
-512
lines changed

apps/gateway/src/api.routes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,8 @@ export const API_ROUTES = {
4949
annotate:
5050
'/api/v3/conversations/:conversationUuid/evaluations/:evaluationUuid/annotate',
5151
},
52+
traces: {
53+
ingest: '/api/v3/traces',
54+
},
5255
},
5356
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { default as v1Routes } from '$/routes/api/v1'
2+
import { default as v2Routes } from '$/routes/api/v2'
13
import { conversationsRouter } from '$/routes/api/v3/conversations'
24
import { projectsRouter } from '$/routes/api/v3/projects'
5+
import { tracesRouter } from '$/routes/api/v3/traces'
36
import { OpenAPIHono } from '@hono/zod-openapi'
4-
import { default as v1Routes } from '$/routes/api/v1'
5-
import { default as v2Routes } from '$/routes/api/v2'
67

78
export function configureApiRoutes(app: OpenAPIHono) {
89
// Deprecated
@@ -12,4 +13,5 @@ export function configureApiRoutes(app: OpenAPIHono) {
1213
// V3
1314
app.route('/', conversationsRouter)
1415
app.route('/', projectsRouter)
16+
app.route('/', tracesRouter)
1517
}

apps/gateway/src/routes/api/v1/run/run.handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const runHandler: AppRouteHandler<RunRoute> = async (c) => {
4242
}
4343

4444
const { stream: newStream } = await runDocumentAtCommit({
45-
context: BACKGROUND(),
45+
context: BACKGROUND({ workspaceId: workspace.id }),
4646
workspace,
4747
document,
4848
commit,

apps/gateway/src/routes/api/v2/documents/run/run.handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const runHandler: AppRouteHandler<RunRoute> = async (c) => {
5151
error,
5252
trace,
5353
} = await runDocumentAtCommit({
54-
context: BACKGROUND(),
54+
context: BACKGROUND({ workspaceId: workspace.id }),
5555
workspace,
5656
document,
5757
commit,

apps/gateway/src/routes/api/v3/conversations/chat/chat.handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const chatHandler: AppRouteHandler<ChatRoute> = async (c) => {
1515
const { messages, stream: useSSE, trace, __internal } = c.req.valid('json')
1616
const workspace = c.get('workspace')
1717

18-
let context = BACKGROUND()
18+
let context = BACKGROUND({ workspaceId: workspace.id })
1919
if (trace) context = telemetry.resume(trace)
2020

2121
const result = (

apps/gateway/src/routes/api/v3/projects/versions/documents/run/run.handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const runHandler: AppRouteHandler<RunRoute> = async (c) => {
4444
}
4545

4646
const result = await runDocumentAtCommit({
47-
context: BACKGROUND(),
47+
context: BACKGROUND({ workspaceId: workspace.id }),
4848
workspace,
4949
document,
5050
commit,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createRouter } from '$/openApi/createApp'
2+
import { ingestHandler } from './ingest/ingest.handler'
3+
import { ingestRoute } from './ingest/ingest.route'
4+
5+
export const tracesRouter = createRouter().openapi(ingestRoute, ingestHandler)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { AppRouteHandler } from '$/openApi/types'
2+
import { TRACING_JOBS_MAX_ATTEMPTS } from '@latitude-data/core/browser'
3+
import { tracingQueue } from '@latitude-data/core/queues'
4+
import { IngestRoute } from './ingest.route'
5+
6+
export const ingestHandler: AppRouteHandler<IngestRoute> = async (ctx) => {
7+
const workspace = ctx.get('workspace')
8+
const apiKey = ctx.get('apiKey')
9+
const request = ctx.req.valid('json')
10+
11+
await tracingQueue.add(
12+
'ingestSpansJob',
13+
{
14+
spans: request.resourceSpans,
15+
apiKeyId: apiKey.id,
16+
workspaceId: workspace.id,
17+
},
18+
{ attempts: TRACING_JOBS_MAX_ATTEMPTS },
19+
)
20+
21+
return ctx.body(null, 200)
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import http from '$/common/http'
2+
import { GENERIC_ERROR_RESPONSES } from '$/openApi/responses/errorResponses'
3+
import { ROUTES } from '$/routes'
4+
import { createRoute } from '@hono/zod-openapi'
5+
import { Otlp } from '@latitude-data/core/browser'
6+
7+
export const ingestRoute = createRoute({
8+
method: http.Methods.POST,
9+
path: ROUTES.api.v3.traces.ingest,
10+
tags: ['Traces'],
11+
description: 'Ingest OTLP spans',
12+
request: {
13+
body: {
14+
content: {
15+
[http.MediaTypes.JSON]: { schema: Otlp.serviceRequestSchema },
16+
},
17+
},
18+
},
19+
responses: {
20+
...GENERIC_ERROR_RESPONSES,
21+
[http.Status.OK]: {
22+
description: 'Spans ingested successfully',
23+
},
24+
},
25+
})
26+
27+
export type IngestRoute = typeof ingestRoute

apps/web/src/app/api/documents/logs/[documentLogUuid]/chat/route.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {
33
LogSources,
44
toolCallSchema,
55
traceContextSchema,
6+
User,
7+
Workspace,
68
} from '@latitude-data/core/browser'
79
import {
810
BACKGROUND,
@@ -16,7 +18,6 @@ import { z } from 'zod'
1618
import { createSdk } from '$/app/(private)/_lib/createSdk'
1719
import { authHandler } from '$/middlewares/authHandler'
1820
import { errorHandler } from '$/middlewares/errorHandler'
19-
import { getCurrentUserOrError } from '$/services/auth/getCurrentUser'
2021
import { publisher } from '@latitude-data/core/events/publisher'
2122

2223
const inputSchema = z.object({
@@ -36,12 +37,14 @@ export const POST = errorHandler(
3637
req: NextRequest,
3738
{
3839
params,
40+
workspace,
41+
user,
3942
}: {
4043
params: {
4144
documentLogUuid: string
4245
}
43-
workspace: any
44-
user: any
46+
workspace: Workspace
47+
user: User
4548
},
4649
) => {
4750
const body = await req.json()
@@ -50,7 +53,9 @@ export const POST = errorHandler(
5053
const { messages: response, toolCalls, trace } = inputSchema.parse(body)
5154
const documentLogUuid = params.documentLogUuid
5255
const messages = response as Message[]
53-
const context = trace ? telemetry.resume(trace) : BACKGROUND()
56+
const context = trace
57+
? telemetry.resume(trace)
58+
: BACKGROUND({ workspaceId: workspace.id })
5459

5560
// Note: faking playground tool spans so they show in the trace
5661
await fakeToolSpans(context, messages, toolCalls)
@@ -61,14 +66,14 @@ export const POST = errorHandler(
6166
data: {
6267
documentLogUuid,
6368
messages,
64-
workspaceId: (await getCurrentUserOrError()).workspace.id,
65-
userEmail: (await getCurrentUserOrError()).user.email,
69+
workspaceId: workspace.id,
70+
userEmail: user.email,
6671
},
6772
})
6873

6974
// Create SDK
7075
const sdkResult = await createSdk({
71-
workspace: (await getCurrentUserOrError()).workspace,
76+
workspace: workspace,
7277
__internal: { source: LogSources.Playground },
7378
})
7479

0 commit comments

Comments
 (0)