Skip to content

Commit 73c3083

Browse files
authored
fix(tsc-clickhouse): fix clickhouse timezone and apm chart query bugs (#704)
* fix: clickhouse query timezone bug apm chart query bugs * chore: update
1 parent b34b3b8 commit 73c3083

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc.Apm.Clickhouse/Cliclhouse/ClickhouseApmService.cs

+11-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal class ClickhouseApmService : IApmService
1010
private readonly ITraceService _traceService;
1111
private readonly static object lockObj = new();
1212
private static Dictionary<string, string> serviceOrders = new() {
13-
{nameof(ServiceListDto.Name),"ServiceName"},
13+
{nameof(ServiceListDto.Name),SERVICE_NAME},
1414
{nameof(ServiceListDto.Envs),"env"},
1515
{nameof(ServiceListDto.Latency),"latency"},
1616
{nameof(ServiceListDto.Throughput),"throughput"},
@@ -19,7 +19,7 @@ internal class ClickhouseApmService : IApmService
1919

2020
private static Dictionary<string, string> endpointOrders = new() {
2121
{nameof(EndpointListDto.Name),"`Attributes.http.target`"},
22-
{nameof(EndpointListDto.Service),"ServiceName"},
22+
{nameof(EndpointListDto.Service),SERVICE_NAME},
2323
{nameof(EndpointListDto.Method),"`method`"},
2424
{nameof(EndpointListDto.Latency),"latency"},
2525
{nameof(EndpointListDto.Throughput),"throughput"},
@@ -33,6 +33,7 @@ internal class ClickhouseApmService : IApmService
3333
{nameof(ErrorMessageDto.Total),"`total`"}
3434
};
3535
const double MILLSECOND = 1e6;
36+
const string SERVICE_NAME = "ServiceName";
3637

3738
public ClickhouseApmService(MasaStackClickhouseConnection dbConnection, ITraceService traceService)
3839
{
@@ -50,7 +51,7 @@ public Task<PaginatedListBase<ServiceListDto>> ServicePageAsync(BaseApmRequestDt
5051
var groupby = "group by ServiceName";
5152
var countSql = $"select count(1) from(select count(1) from {Constants.TraceTableFull} where {where} {groupby})";
5253
PaginatedListBase<ServiceListDto> result = new() { Total = Convert.ToInt64(Scalar(countSql, parameters)) };
53-
var orderBy = GetOrderBy(query, serviceOrders, defaultSort: "ServiceName");
54+
var orderBy = GetOrderBy(query, serviceOrders, defaultSort: SERVICE_NAME);
5455
var sql = $@"select * from(
5556
select
5657
ServiceName,
@@ -76,7 +77,7 @@ public Task<PaginatedListBase<EndpointListDto>> InstancePageAsync(BaseApmRequest
7677
var selectField = $@"ResourceAttributesValues[indexOf(ResourceAttributesKeys,'service.instance.id')] instance`,
7778
AVG(Duration/{MILLSECOND}) Latency,
7879
count(1)*1.0/DATEDIFF(MINUTE ,toDateTime(@startTime),toDateTime (@endTime)) throughput
79-
sum(has(['{string.Join(',', query.GetErrorStatusCodes())}'],`Attributes.http.status_code`))/count(1) failed";
80+
sum(has(['{string.Join("','", query.GetErrorStatusCodes())}'],`Attributes.http.status_code`))/count(1) failed";
8081
return GetEndpointAsync(query, groupBy, selectField, reader => new EndpointListDto()
8182
{
8283
Name = reader[0].ToString()!,
@@ -92,7 +93,7 @@ public Task<PaginatedListBase<EndpointListDto>> DependencyPageAsync(BaseApmReque
9293
var selectField = $@"`Attributes.http.target`,ServiceName,SpanAttributesValues[indexOf(SpanAttributesKeys,'http.method')] `method`,
9394
AVG(Duration{MILLSECOND}) Latency,
9495
count(1)*1.0/DATEDIFF(MINUTE ,toDateTime(@startTime),toDateTime (@endTime)) throughput
95-
sum(has(['{string.Join(',', query.GetErrorStatusCodes())}'],`Attributes.http.status_code`))/count(1) failed";
96+
sum(has(['{string.Join("','", query.GetErrorStatusCodes())}'],`Attributes.http.status_code`))/count(1) failed";
9697
return GetEndpointAsync(query, groupBy, selectField, ConvertEndpointDto);
9798
}
9899

@@ -114,7 +115,7 @@ public Task<PaginatedListBase<EndpointListDto>> EndpointPageAsync(BaseApmRequest
114115
var selectField = $@"`Attributes.http.target`,ServiceName,SpanAttributesValues[indexOf(SpanAttributesKeys,'http.method')] `method`,
115116
floor(AVG(Duration/{MILLSECOND})) latency,
116117
round(count(1)*1.0/DATEDIFF(MINUTE ,toDateTime(@startTime),toDateTime (@endTime)),2) throughput,
117-
round(sum(has(['{string.Join(',', query.GetErrorStatusCodes())}'],`Attributes.http.status_code`))*100.0/count(1),2) failed";
118+
round(sum(has(['{string.Join("','", query.GetErrorStatusCodes())}'],`Attributes.http.status_code`))*100.0/count(1),2) failed";
118119
return GetEndpointAsync(query, groupBy, selectField, ConvertEndpointDto);
119120
}
120121

@@ -136,9 +137,10 @@ public Task<IEnumerable<ChartLineDto>> ChartDataAsync(BaseApmRequestDto query)
136137
query.IsServer = true;
137138
var (where, parameters) = AppendWhere(query);
138139
var result = new List<ChartLineDto>();
139-
var groupby = "group by ServiceName ,`time` order by ServiceName ,`time`";
140+
var field = query is ApmEndpointRequestDto apmEndpointDto && string.IsNullOrEmpty(apmEndpointDto.Endpoint) ? "Attributes.http.target" : SERVICE_NAME;
141+
var groupby = $"group by {field} ,`time` order by {field} ,`time`";
140142
var sql = $@"select
141-
ServiceName,
143+
{field},
142144
toStartOfInterval(`Timestamp` , INTERVAL {GetPeriod(query)} ) as `time`,
143145
floor(avg(Duration/{MILLSECOND})) `latency`,
144146
floor(quantile(0.95)(Duration/{MILLSECOND})) `p95`,
@@ -464,7 +466,7 @@ public Task<IEnumerable<ChartLineCountDto>> GetErrorChartAsync(ApmEndpointReques
464466
var sql = $@"select
465467
toStartOfInterval(`Timestamp` , INTERVAL {GetPeriod(query)} ) as `time`,
466468
count(1) `total`
467-
from {Constants.LogTableFull} where {where} and SeverityText='Error' {groupby}";
469+
from {Constants.LogTableFull} where {where} and SeverityText='Error' and `Attributes.exception.message`!='' {groupby}";
468470

469471
return Task.FromResult(getChartCountData(sql, parameters, query.ComparisonType).AsEnumerable());
470472
}

src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc.Clickhouse/Model/MASAStackClickhouseConnection.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ internal sealed class MasaStackClickhouseConnection : ClickHouseConnection
1919

2020
public static TimeZoneInfo TimeZone { get; set; }
2121

22-
public static DateTime ToTimeZone(DateTime utcTime)
22+
public static DateTime ToTimeZone(DateTime time)
2323
{
24-
return utcTime + TimeZone.BaseUtcOffset;
24+
var newTime = time.Kind == DateTimeKind.Unspecified ? time : DateTime.SpecifyKind(time, DateTimeKind.Unspecified);
25+
return new DateTimeOffset(newTime + TimeZone.BaseUtcOffset, TimeZone.BaseUtcOffset).DateTime;
2526
}
2627

2728
public object LockObj { get; init; } = new();

0 commit comments

Comments
 (0)