Skip to content

Commit 9596a6f

Browse files
committed
feat(backend): fix
1 parent 2928546 commit 9596a6f

File tree

5 files changed

+47
-28
lines changed

5 files changed

+47
-28
lines changed

backend/modules/observability/infra/repo/ck/annotation.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ func (a *AnnotationCkDaoImpl) Get(ctx context.Context, params *GetAnnotationPara
8787
if err != nil {
8888
return nil, err
8989
}
90+
logs.CtxInfo(ctx, "Get Annotation SQL: %s", db.ToSQL(func(tx *gorm.DB) *gorm.DB {
91+
return tx.Find(nil)
92+
}))
9093
var annotations []*model.ObservabilityAnnotation
9194
if err := db.Find(&annotations).Error; err != nil {
9295
return nil, err
@@ -114,6 +117,9 @@ func (a *AnnotationCkDaoImpl) List(ctx context.Context, params *ListAnnotationsP
114117
if err != nil {
115118
return nil, err
116119
}
120+
logs.CtxInfo(ctx, "List Annotations SQL: %s", db.ToSQL(func(tx *gorm.DB) *gorm.DB {
121+
return tx.Find(nil)
122+
}))
117123
var annotations []*model.ObservabilityAnnotation
118124
if err := db.Find(&annotations).Error; err != nil {
119125
return nil, err
@@ -144,7 +150,11 @@ func (a *AnnotationCkDaoImpl) buildSql(ctx context.Context, param *annoSqlParam)
144150
if len(tableQueries) == 0 {
145151
return nil, fmt.Errorf("no table configured")
146152
} else if len(tableQueries) == 1 {
147-
return tableQueries[0], nil
153+
query := tableQueries[0].ToSQL(func(tx *gorm.DB) *gorm.DB {
154+
return tx.Find(nil)
155+
})
156+
query += " SETTINGS final = 1"
157+
return db.Raw(query), nil
148158
} else {
149159
queries := make([]string, 0)
150160
for i := 0; i < len(tableQueries); i++ {
@@ -159,7 +169,7 @@ func (a *AnnotationCkDaoImpl) buildSql(ctx context.Context, param *annoSqlParam)
159169
} else {
160170
sql += " ORDER BY created_at ASC"
161171
}
162-
sql += fmt.Sprintf(" LIMIT %d", param.Limit)
172+
sql += fmt.Sprintf(" LIMIT %d SETTINGS final = 1", param.Limit)
163173
return db.Raw(sql), nil
164174
}
165175
}

backend/modules/observability/infra/repo/ck/spans.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ func (s *SpansCkDaoImpl) Get(ctx context.Context, param *QueryParam) ([]*model.O
9999
if err := sql.Find(&spans).Error; err != nil {
100100
return nil, errorx.WrapByCode(err, obErrorx.CommercialCommonRPCErrorCodeCode)
101101
}
102+
for _, span := range spans {
103+
span.SystemTagsString[loop_span.SpanFieldTenant] = "cozeloop" // tenant
104+
}
102105
return spans, nil
103106
}
104107

@@ -167,28 +170,6 @@ func (s *SpansCkDaoImpl) buildSingleSql(ctx context.Context, param *buildSqlPara
167170
return sqlQuery, nil
168171
}
169172

170-
// 根据start_date查询start_time的子查询
171-
func (s *SpansCkDaoImpl) buildSubQuerySql(ctx context.Context, param *buildSqlParam) (*gorm.DB, error) {
172-
sqlQuery, err := s.buildSqlForFilterFields(ctx, param, param.queryParam.Filters)
173-
if err != nil {
174-
return nil, err
175-
}
176-
sqlQuery = param.db.
177-
Table(param.spanTable).
178-
Select("start_time").
179-
Where(sqlQuery).
180-
Where("start_time >= ?", param.queryParam.StartTime).
181-
Where("start_time <= ?", param.queryParam.EndTime)
182-
if param.queryParam.OrderByStartTime {
183-
sqlQuery = sqlQuery.Order(clause.OrderBy{Columns: []clause.OrderByColumn{
184-
{Column: clause.Column{Name: "start_time"}, Desc: true},
185-
{Column: clause.Column{Name: "span_id"}, Desc: true},
186-
}})
187-
}
188-
sqlQuery = sqlQuery.Limit(int(param.queryParam.Limit))
189-
return sqlQuery, nil
190-
}
191-
192173
// chain
193174
func (s *SpansCkDaoImpl) buildSqlForFilterFields(ctx context.Context, param *buildSqlParam, filter *loop_span.FilterFields) (*gorm.DB, error) {
194175
if filter == nil {
@@ -396,7 +377,10 @@ func (s *SpansCkDaoImpl) buildAnnotationSql(ctx context.Context, param *buildSql
396377
Where("deleted_at = 0").
397378
Where("start_time >= ?", param.queryParam.StartTime).
398379
Where("start_time <= ?", param.queryParam.EndTime)
399-
return param.db.Where("span_id in (?)", subsql), nil
380+
query := subsql.ToSQL(func(tx *gorm.DB) *gorm.DB {
381+
return tx.Find(nil)
382+
})
383+
return param.db.Where("span_id in (?)", param.db.Raw(query+" SETTINGS final = 1")), nil
400384
}
401385

402386
func (s *SpansCkDaoImpl) getSuperFieldsMap(ctx context.Context) map[string]bool {

backend/modules/observability/infra/repo/ck/spans_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,24 @@ func TestBuildSql(t *testing.T) {
197197
},
198198
expectedSql: "SELECT * FROM `observability_spans` WHERE `input` like '%123%' AND start_time >= 1 AND start_time <= 2 LIMIT 100",
199199
},
200+
{
201+
filter: &loop_span.FilterFields{
202+
FilterFields: []*loop_span.FilterField{
203+
{
204+
FieldName: "manual_feedback_abc",
205+
FieldType: loop_span.FieldTypeString,
206+
Values: []string{"123"},
207+
QueryType: ptr.Of(loop_span.QueryTypeEnumIn),
208+
},
209+
},
210+
},
211+
expectedSql: "SELECT * FROM `observability_spans` WHERE span_id in (SELECT span_id FROM `observability_annotations` WHERE (annotation_type = 'manual_feedback' AND key = 'abc' AND value_string IN ('123')) AND deleted_at = 0 AND start_time >= 1 AND start_time <= 2 FINAL) AND start_time >= 1 AND start_time <= 2 LIMIT 100",
212+
},
200213
}
201214
for _, tc := range testCases {
202215
qDb, err := new(SpansCkDaoImpl).buildSingleSql(context.Background(), &buildSqlParam{
203216
spanTable: "observability_spans",
217+
annoTable: "observability_annotations",
204218
queryParam: &QueryParam{
205219
StartTime: 1,
206220
EndTime: 2,

release/deployment/docker-compose/bootstrap/clickhouse-init/init-sql/observability_annotations.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ CREATE TABLE IF NOT EXISTS `observability_annotations` (
2121
`updated_by` String,
2222
`updated_at` UInt64,
2323
`deleted_at` UInt64,
24+
`start_date` Date,
25+
INDEX idx_id id TYPE bloom_filter() GRANULARITY 1,
2426
INDEX idx_span_id span_id TYPE bloom_filter() GRANULARITY 1,
2527
INDEX idx_trace_id trace_id TYPE bloom_filter() GRANULARITY 1,
2628
INDEX idx_space_id space_id TYPE bloom_filter() GRANULARITY 1,
2729
INDEX idx_annotation_type annotation_type TYPE bloom_filter() GRANULARITY 1
2830
) ENGINE = ReplacingMergeTree(updated_at) PARTITION BY toDate(start_time / 1000000)
29-
PRIMARY KEY (id)
30-
ORDER BY (id, start_time);
31+
PRIMARY KEY (start_time)
32+
ORDER BY (start_time, id);

release/deployment/docker-compose/conf/observability.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ trace_tenant_cfg:
6060
cozeloop:
6161
365d:
6262
span_table: "observability_spans"
63+
anno_table: "observability_annotations"
6364
default_ingest_tenant: "cozeloop"
6465
tenants_support_annotation:
65-
cozeloop: false
66+
cozeloop: true
6667

6768
trace_field_meta_info:
6869
available_fields:
@@ -176,6 +177,8 @@ trace_field_meta_info:
176177
- "exist"
177178
- "not_exist"
178179
support_custom: true
180+
feedback_manual:
181+
support_custom: true
179182
field_metas:
180183
cozeloop:
181184
root_span:
@@ -189,6 +192,7 @@ trace_field_meta_info:
189192
- "span_type"
190193
- "message_id"
191194
- "user_id"
195+
- "feedback_manual"
192196
all_span:
193197
- "input"
194198
- "output"
@@ -204,6 +208,7 @@ trace_field_meta_info:
204208
- "output_tokens"
205209
- "prompt_key"
206210
- "tokens"
211+
- "feedback_manual"
207212
llm_span:
208213
- "input"
209214
- "output"
@@ -217,6 +222,7 @@ trace_field_meta_info:
217222
- "input_tokens"
218223
- "output_tokens"
219224
- "tokens"
225+
- "feedback_manual"
220226
prompt:
221227
root_span:
222228
- "input"
@@ -229,6 +235,7 @@ trace_field_meta_info:
229235
- "span_type"
230236
- "prompt_key"
231237
- "user_id"
238+
- "feedback_manual"
232239
all_span:
233240
- "input"
234241
- "output"
@@ -243,6 +250,7 @@ trace_field_meta_info:
243250
- "output_tokens"
244251
- "prompt_key"
245252
- "tokens"
253+
- "feedback_manual"
246254
llm_span:
247255
- "input"
248256
- "output"
@@ -256,6 +264,7 @@ trace_field_meta_info:
256264
- "output_tokens"
257265
- "prompt_key"
258266
- "tokens"
267+
- "feedback_manual"
259268

260269
trace_collector_cfg:
261270
receivers:

0 commit comments

Comments
 (0)