Skip to content

Commit 47478a4

Browse files
authored
fix(tsc-clickhouse): fix query bugs (#679)
* feat: add trace & log query with clickhouse * chore: update test project * chore: update * chore: fix path * chore: test update * chore: update test case * chore: update * chore: update * chore: update * chore: update * chore: update * fix: fix bugs * fix: fix query in and not in error; fix init table sql; fix query time using UTC time * chore: update
1 parent fb54095 commit 47478a4

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc.Clickhouse/Extensions/IDbConnectionExtensitions.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static PaginatedListBase<TraceResponseDto> QueryTrace(this IDbConnection
1515
var result = new PaginatedListBase<TraceResponseDto>() { Total = total, Result = new() };
1616
if (total > 0 && start - total < 0)
1717
{
18-
var querySql = CombineOrs($"select ServiceName,Timestamp,TraceId,SpanId,ParentSpanId,TraceState,SpanKind,Duration,SpanName,Spans,Resources from {MasaStackClickhouseConnection.TraceTable} where {where}", ors, orderBy);
18+
var querySql = CombineOrs($"select ServiceName,Timestamp,TraceId,SpanId,ParentSpanId,TraceState,SpanKind,Duration,SpanName,Spans,Resources from {MasaStackClickhouseConnection.TraceTable} where {where}", ors,orderBy);
1919
result.Result = Query(connection, $"select * from {querySql} as t limit {start},{query.PageSize}", parameters?.ToArray(), ConvertTraceDto);
2020
}
2121
return result;
@@ -76,7 +76,7 @@ public static List<TraceResponseDto> GetTraceByTraceId(this IDbConnection connec
7676

7777
public static string AppendOrderBy(BaseRequestDto query, bool isLog)
7878
{
79-
var str = query.Sort?.IsDesc ?? false ? " desc" : "";
79+
var str = query.Sort?.IsDesc ?? true ? " desc" : "";
8080
return $" order by Timestamp{str}";
8181
}
8282

@@ -242,7 +242,10 @@ private static void AppendField(FieldConditionDto item, List<IDataParameter> @pa
242242
private static void ParseWhere(StringBuilder sql, object value, List<IDataParameter> @paramerters, string fieldName, string paramName, string compare)
243243
{
244244
DbType dbType = value is DateTime ? DbType.DateTime2 : DbType.AnsiString;
245-
sql.Append($" and {fieldName} {compare} @{paramName}");
245+
if (value is IEnumerable)
246+
sql.Append($" and {fieldName} {compare} (@{paramName})");
247+
else
248+
sql.Append($" and {fieldName} {compare} @{paramName}");
246249
@paramerters.Add(new ClickHouseParameter { ParameterName = $"{paramName}", Value = value, DbType = dbType });
247250
}
248251

@@ -350,7 +353,7 @@ public static LogResponseDto ConvertLogDto(IDataReader reader)
350353
SeverityText = reader["SeverityText"].ToString()!,
351354
TraceFlags = Convert.ToInt32(reader["TraceFlags"]),
352355
SpanId = reader["SpanId"].ToString()!,
353-
Timestamp = Convert.ToDateTime(reader["Timestamp"]),
356+
Timestamp = Convert.ToDateTime(reader["Timestamp"]).ToLocalTime(),
354357
};
355358
if (!string.IsNullOrEmpty(resource))
356359
result.Resource = JsonSerializer.Deserialize<Dictionary<string, object>>(resource)!;

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc.Clickhouse/Extensions/ServiceExtensitions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private static void InitTable(MasaStackClickhouseConnection connection)
3232
var database = connection.ConnectionSettings?.Database;
3333
database ??= new ClickHouseConnectionSettings(connection.ConnectionString).Database;
3434

35-
if (Convert.ToInt32(connection.ExecuteScalar($"select * from system.tables where database ='{database}' and name in ['{MasaStackClickhouseConnection.TraceTable}','{MasaStackClickhouseConnection.LogTable}']")) > 0)
35+
if (Convert.ToInt32(connection.ExecuteScalar($"select count() from system.tables where database ='{database}' and name in ['{MasaStackClickhouseConnection.TraceTable.Split('.')[1]}','{MasaStackClickhouseConnection.LogTable.Split('.')[1]}']")) > 0)
3636
return;
3737

3838
var createTableSqls = new string[]{
@@ -59,7 +59,7 @@ private static void InitTable(MasaStackClickhouseConnection connection)
5959
`Resource.service.version` String CODEC(ZSTD(1)),
6060
`Resource.service.instance.id` String CODEC(ZSTD(1)),
6161
62-
`Attributes.taskId` String CODEC(ZSTD(1)),
62+
`Attributes.TaskId` String CODEC(ZSTD(1)),
6363
`Attributes.exception.message` String CODEC(ZSTD(1)),
6464
6565
ResourceAttributesKeys Array(String) CODEC(ZSTD(1)),
@@ -71,7 +71,7 @@ INDEX idx_log_id TraceId TYPE bloom_filter(0.001) GRANULARITY 1,
7171
INDEX idx_log_servicename ServiceName TYPE bloom_filter(0.001) GRANULARITY 1,
7272
INDEX idx_log_serviceinstanceid `Resource.service.instance.id` TYPE bloom_filter(0.001) GRANULARITY 1,
7373
INDEX idx_log_severitytext SeverityText TYPE bloom_filter(0.001) GRANULARITY 1,
74-
INDEX idx_log_taskid `Attributes.taskId` TYPE bloom_filter(0.001) GRANULARITY 1,
74+
INDEX idx_log_taskid `Attributes.TaskId` TYPE bloom_filter(0.001) GRANULARITY 1,
7575
7676
INDEX idx_string_body Body TYPE tokenbf_v1(30720, 2, 0) GRANULARITY 1,
7777
INDEX idx_string_exceptionmessage Attributes.exception.message TYPE tokenbf_v1(30720, 2, 0) GRANULARITY 1
@@ -155,7 +155,7 @@ TTL toDateTime(Timestamp) + toIntervalDay(30)
155155
ScopeSchemaUrl,ScopeName,ScopeVersion,toJSONString(ScopeAttributes) as Scopes,toJSONString(LogAttributes) as Logs,
156156
ResourceAttributes['service.namespace'] as `Resource.service.namespace`,ResourceAttributes['service.version'] as `Resource.service.version`,
157157
ResourceAttributes['service.instance.id'] as `Resource.service.instance.id`,
158-
LogAttributes['TaskId'] as `Attributes.taskId`,LogAttributes['exception.message'] as `Attributes.exception.message`,
158+
LogAttributes['TaskId'] as `Attributes.TaskId`,LogAttributes['exception.message'] as `Attributes.exception.message`,
159159
mapKeys(ResourceAttributes) as ResourceAttributesKeys,mapValues(ResourceAttributes) as ResourceAttributesValues,
160160
mapKeys(LogAttributes) as LogAttributesKeys,mapValues(LogAttributes) as LogAttributesValues
161161
FROM {MasaStackClickhouseConnection.LogSourceTable};

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc.Clickhouse/_Imports.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
global using Masa.Utils.Models;
1212
global using Microsoft.Extensions.DependencyInjection;
1313
global using Microsoft.Extensions.Logging;
14+
global using System.Collections;
1415
global using System.Data;
1516
global using System.Data.Common;
1617
global using System.Text;

0 commit comments

Comments
 (0)