Skip to content

Commit 29cd8c6

Browse files
authored
don't write tag classes, create new ones on read (#905)
1 parent 4a392d3 commit 29cd8c6

File tree

14 files changed

+136
-138
lines changed

14 files changed

+136
-138
lines changed

app-server/src/api/v1/tag.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::sync::Arc;
22

33
use crate::{
4-
ch::spans::append_tags_to_span,
5-
db::{DB, project_api_keys::ProjectApiKey, tags::TagSource},
4+
ch::{spans::append_tags_to_span, tags::insert_tag},
5+
db::{project_api_keys::ProjectApiKey, tags::TagSource},
66
query_engine::QueryEngine,
77
routes::types::ResponseResult,
88
sql::{self, ClickhouseReadonlyClient},
9-
tags::create_tag,
109
};
1110
use actix_web::{
1211
HttpResponse, post,
@@ -41,7 +40,6 @@ pub enum TagRequest {
4140
#[post("tag")]
4241
pub async fn tag_trace(
4342
req: Json<TagRequest>,
44-
db: web::Data<DB>,
4543
clickhouse: web::Data<clickhouse::Client>,
4644
clickhouse_ro: web::Data<Option<Arc<ClickhouseReadonlyClient>>>,
4745
query_engine: web::Data<Arc<QueryEngine>>,
@@ -90,13 +88,12 @@ pub async fn tag_trace(
9088
let futures = names
9189
.iter()
9290
.map(|name| {
93-
create_tag(
94-
&db.pool,
91+
insert_tag(
9592
clickhouse.clone(),
9693
project_api_key.project_id,
97-
span_id,
9894
name.clone(),
9995
TagSource::CODE,
96+
span_id,
10097
)
10198
})
10299
.collect::<Vec<_>>();

app-server/src/ch/tags.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ impl CHTag {
5353
pub async fn insert_tag(
5454
client: clickhouse::Client,
5555
project_id: Uuid,
56-
id: Uuid,
5756
name: String,
5857
source: TagSource,
5958
span_id: Uuid,
60-
) -> Result<()> {
59+
) -> Result<Uuid> {
60+
let id = Uuid::new_v4();
6161
let tag = CHTag::new(project_id, id, name, source, span_id);
6262
let ch_insert = client.insert("tags");
6363
match ch_insert {
6464
Ok(mut ch_insert) => {
6565
ch_insert.write(&tag).await?;
6666
let ch_insert_end_res = ch_insert.end().await;
6767
match ch_insert_end_res {
68-
Ok(_) => Ok(()),
68+
Ok(_) => Ok(id),
6969
Err(e) => {
7070
return Err(anyhow::anyhow!("Clickhouse tag insertion failed: {:?}", e));
7171
}

app-server/src/db/tags.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use anyhow::Result;
21
use chrono::{DateTime, Utc};
32
use serde::{Deserialize, Serialize};
4-
use sqlx::{FromRow, PgPool};
3+
use sqlx::FromRow;
54
use uuid::Uuid;
65

76
#[derive(sqlx::Type, Serialize, Deserialize, Clone, PartialEq)]
@@ -50,18 +49,3 @@ pub struct SpanTag {
5049
pub user_id: Option<Uuid>,
5150
pub user_email: Option<String>,
5251
}
53-
54-
pub async fn insert_tag_class(pool: &PgPool, project_id: Uuid, tag_name: &String) -> Result<()> {
55-
sqlx::query(
56-
"
57-
INSERT INTO tag_classes (project_id, name)
58-
VALUES ($1, $2)
59-
ON CONFLICT (project_id, name) DO NOTHING
60-
",
61-
)
62-
.bind(project_id)
63-
.bind(tag_name)
64-
.execute(pool)
65-
.await?;
66-
Ok(())
67-
}

app-server/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ mod db;
6060
mod evaluations;
6161
mod evaluators;
6262
mod features;
63-
mod tags;
6463
mod language_model;
6564
mod mq;
6665
mod names;

app-server/src/tags/mod.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

app-server/src/traces/consumer.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::{
2828
events::record_events,
2929
limits::update_workspace_limit_exceeded_by_project_id,
3030
provider::convert_span_to_provider_format,
31-
utils::{get_llm_usage_for_span, prepare_span_for_recording, record_tags_to_db_and_ch},
31+
utils::{get_llm_usage_for_span, prepare_span_for_recording, record_tags},
3232
},
3333
};
3434

@@ -346,8 +346,7 @@ async fn process_batch(
346346
}
347347

348348
for span in &stripped_spans {
349-
if let Err(e) = record_tags_to_db_and_ch(
350-
db.clone(),
349+
if let Err(e) = record_tags(
351350
clickhouse.clone(),
352351
&span.tags,
353352
&span.span_id,

app-server/src/traces/utils.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ pub async fn get_llm_usage_for_span(
9595
}
9696
}
9797

98-
pub async fn record_tags_to_db_and_ch(
99-
db: Arc<DB>,
98+
pub async fn record_tags(
10099
clickhouse: clickhouse::Client,
101100
tags: &[String],
102101
span_id: &Uuid,
@@ -107,13 +106,12 @@ pub async fn record_tags_to_db_and_ch(
107106
}
108107

109108
for tag_name in tags {
110-
crate::tags::create_tag(
111-
&db.pool,
109+
crate::ch::tags::insert_tag(
112110
clickhouse.clone(),
113111
*project_id,
114-
*span_id,
115112
tag_name.clone(),
116113
TagSource::CODE,
114+
*span_id,
117115
)
118116
.await?;
119117
}

frontend/app/api/projects/[projectId]/spans/[spanId]/tags/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function POST(
2424
const projectId = params.projectId;
2525
const spanId = params.spanId;
2626

27-
const body = (await req.json()) as { classId: string; name: string };
27+
const body = (await req.json()) as { name: string };
2828

2929
const res = await addSpanTag({
3030
spanId,

frontend/app/api/projects/[projectId]/tag-classes/[tagName]/route.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { and, eq } from 'drizzle-orm';
2+
import { prettifyError, ZodError } from 'zod';
23

4+
import { createOrUpdateTagClass } from '@/lib/actions/tags';
35
import { db } from '@/lib/db/drizzle';
46
import { tagClasses } from '@/lib/db/migrations/schema';
57

@@ -11,21 +13,22 @@ export async function POST(
1113
const params = await props.params;
1214
const projectId = params.projectId;
1315
const tagName = params.tagName;
14-
1516
const body = await req.json();
1617

17-
const result = await db.update(tagClasses).set({
18-
name: body.name,
19-
color: body.color,
20-
}).where(
21-
and(eq(tagClasses.name, tagName), eq(tagClasses.projectId, projectId))
22-
).returning();
23-
24-
if (result.length === 0) {
25-
return new Response('Tag class not found', { status: 404 });
18+
try {
19+
const result = await createOrUpdateTagClass({
20+
projectId,
21+
name: tagName,
22+
color: body.color,
23+
});
24+
return Response.json(result, { status: 200 });
25+
} catch (error) {
26+
if (error instanceof ZodError) {
27+
return Response.json({ error: prettifyError(error) }, { status: 400 });
28+
}
29+
30+
return Response.json({ error: "Internal server error" }, { status: 500 });
2631
}
27-
28-
return Response.json(result[0], { status: 200 });
2932
}
3033

3134
export async function DELETE(

frontend/app/api/projects/[projectId]/tag-classes/route.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,3 @@ export async function GET(req: Request, props: { params: Promise<{ projectId: st
1515

1616
return new Response(JSON.stringify(res), { status: 200 });
1717
}
18-
19-
export async function POST(req: Request, props: { params: Promise<{ projectId: string }> }): Promise<Response> {
20-
const params = await props.params;
21-
const projectId = params.projectId;
22-
23-
const body = await req.json();
24-
25-
const res = await db
26-
.insert(tagClasses)
27-
.values({
28-
projectId,
29-
name: body.name,
30-
color: body.color,
31-
})
32-
.returning();
33-
34-
if (res.length === 0) {
35-
return new Response(JSON.stringify({ error: "Failed to create tag class" }), { status: 500 });
36-
}
37-
38-
return new Response(JSON.stringify(res[0]), { status: 200 });
39-
}

0 commit comments

Comments
 (0)