Skip to content

Commit da8a4f5

Browse files
authored
test(clickhouse): seed nested metadata (#13635)
1 parent f63b9d3 commit da8a4f5

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

packages/shared/scripts/seeder/utils/clickhouse-builder.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ export class ClickHouseQueryBuilder {
2424
return str.replace(/'/g, "''");
2525
}
2626

27+
private buildNestedMetadataMapSql(
28+
baseEntries: string[],
29+
rowExpression: string = "number",
30+
): string {
31+
return `map(
32+
${baseEntries.join(",\n ")},
33+
'customer.id', concat('customer_', toString(${rowExpression} % 100)),
34+
'customer.plan', arrayElement(['free', 'pro', 'enterprise'], 1 + (${rowExpression} % 3)),
35+
'customer.region.code', arrayElement(['eu-central-1', 'us-east-1', 'ap-south-1'], 1 + (${rowExpression} % 3)),
36+
'routing.queue', arrayElement(['support-chat', 'sales-chat', 'ops-chat'], 1 + (${rowExpression} % 3)),
37+
'routing.priority', arrayElement(['low', 'normal', 'high'], 1 + (${rowExpression} % 3)),
38+
'flags.beta', if(${rowExpression} % 2 = 0, 'true', 'false')
39+
)`;
40+
}
41+
2742
/**
2843
* Creates INSERT query for trace data using VALUES syntax.
2944
* Use for: Small datasets, detailed trace objects with all fields populated.
@@ -93,7 +108,7 @@ export class ClickHouseQueryBuilder {
93108
toDateTime(now() - randUniform(0, ${opts.numberOfDays} * 24 * 60 * 60)) AS timestamp,
94109
concat('trace-', toString(number % 10)) AS name,
95110
if(randUniform(0, 1) < 0.3, concat('user_', toString(rand() % 1000)), NULL) AS user_id,
96-
map('generated', 'bulk') AS metadata,
111+
${this.buildNestedMetadataMapSql(["'generated'", "'bulk'"])} AS metadata,
97112
NULL AS release,
98113
NULL AS version,
99114
'${projectId}' AS project_id,
@@ -162,7 +177,7 @@ export class ClickHouseQueryBuilder {
162177
when type = 'SPAN' then concat('span-', toString(number % 10))
163178
else concat('event-', toString(number % 10))
164179
end AS name,
165-
map('key', 'value') AS metadata,
180+
${this.buildNestedMetadataMapSql(["'key'", "'value'"])} AS metadata,
166181
if(randUniform(0, 1) < 0.85, 'DEFAULT', if(randUniform(0, 1) < 0.7, 'DEBUG', if(randUniform(0, 1) < 0.3, 'ERROR', 'WARNING'))) AS level,
167182
NULL AS status_message,
168183
NULL AS version,

packages/shared/scripts/seeder/utils/data-generators.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ export class DataGenerator {
7272
return Math.floor(Math.random() * (max - min + 1)) + min;
7373
}
7474

75+
private buildNestedSeedMetadata(
76+
source: string,
77+
index: number,
78+
overrides: Record<string, string> = {},
79+
): Record<string, string> {
80+
const plans = ["free", "pro", "enterprise"];
81+
const regions = ["eu-central-1", "us-east-1", "ap-south-1"];
82+
const queues = ["support-chat", "sales-chat", "ops-chat"];
83+
const priorities = ["low", "normal", "high"];
84+
85+
return {
86+
source,
87+
"customer.id": `customer_${index % 100}`,
88+
"customer.plan": plans[index % plans.length],
89+
"customer.region.code": regions[index % regions.length],
90+
"routing.queue": queues[index % queues.length],
91+
"routing.priority": priorities[index % priorities.length],
92+
"flags.beta": index % 2 === 0 ? "true" : "false",
93+
...overrides,
94+
};
95+
}
96+
7597
/**
7698
* Creates dataset run items for dataset runs.
7799
* Use for: Dataset experiment scenarios.
@@ -299,7 +321,9 @@ export class DataGenerator {
299321
? `session_${this.randomInt(1, 100)}`
300322
: undefined,
301323
environment: "default",
302-
metadata: { generated: "synthetic" },
324+
metadata: this.buildNestedSeedMetadata("synthetic", i, {
325+
generated: "synthetic",
326+
}),
303327
tags: this.randomBoolean(0.3) ? ["production", "ai-agent"] : [],
304328
public: this.randomBoolean(0.8),
305329
bookmarked: this.randomBoolean(0.1),
@@ -549,6 +573,14 @@ export class DataGenerator {
549573
? "WARNING"
550574
: "ERROR",
551575
environment: trace.environment,
576+
metadata: this.buildNestedSeedMetadata(
577+
"synthetic-observation",
578+
traceIndex * observationsPerTrace + i,
579+
{
580+
"observation.type": obsType,
581+
"workflow.step": String(i + 1),
582+
},
583+
),
552584
});
553585

554586
observations.push(observation);
@@ -1039,7 +1071,10 @@ export class DataGenerator {
10391071
timestamp: now + index * 1000,
10401072
name: "SupportChatSession",
10411073
user_id: null,
1042-
metadata: { scenario: "support-chat" },
1074+
metadata: this.buildNestedSeedMetadata("support-chat", index, {
1075+
scenario: "support-chat",
1076+
"routing.queue": "membership-support",
1077+
}),
10431078
release: null,
10441079
version: null,
10451080
project_id: projectId,

0 commit comments

Comments
 (0)