Skip to content

Commit

Permalink
fix: clickhouse query timezone bug
Browse files Browse the repository at this point in the history
      apm chart query bugs
  • Loading branch information
Qinyouzeng committed Mar 22, 2024
1 parent b34b3b8 commit d198d5e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Task<PaginatedListBase<EndpointListDto>> InstancePageAsync(BaseApmRequest
var selectField = $@"ResourceAttributesValues[indexOf(ResourceAttributesKeys,'service.instance.id')] instance`,
AVG(Duration/{MILLSECOND}) Latency,
count(1)*1.0/DATEDIFF(MINUTE ,toDateTime(@startTime),toDateTime (@endTime)) throughput
sum(has(['{string.Join(',', query.GetErrorStatusCodes())}'],`Attributes.http.status_code`))/count(1) failed";
sum(has(['{string.Join("','", query.GetErrorStatusCodes())}'],`Attributes.http.status_code`))/count(1) failed";
return GetEndpointAsync(query, groupBy, selectField, reader => new EndpointListDto()
{
Name = reader[0].ToString()!,
Expand All @@ -92,7 +92,7 @@ public Task<PaginatedListBase<EndpointListDto>> DependencyPageAsync(BaseApmReque
var selectField = $@"`Attributes.http.target`,ServiceName,SpanAttributesValues[indexOf(SpanAttributesKeys,'http.method')] `method`,
AVG(Duration{MILLSECOND}) Latency,
count(1)*1.0/DATEDIFF(MINUTE ,toDateTime(@startTime),toDateTime (@endTime)) throughput
sum(has(['{string.Join(',', query.GetErrorStatusCodes())}'],`Attributes.http.status_code`))/count(1) failed";
sum(has(['{string.Join("','", query.GetErrorStatusCodes())}'],`Attributes.http.status_code`))/count(1) failed";
return GetEndpointAsync(query, groupBy, selectField, ConvertEndpointDto);
}

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

Expand All @@ -136,9 +136,10 @@ public Task<IEnumerable<ChartLineDto>> ChartDataAsync(BaseApmRequestDto query)
query.IsServer = true;
var (where, parameters) = AppendWhere(query);
var result = new List<ChartLineDto>();
var groupby = "group by ServiceName ,`time` order by ServiceName ,`time`";
var field = query is ApmEndpointRequestDto apmEndpointDto && string.IsNullOrEmpty(apmEndpointDto.Endpoint) ? "Attributes.http.target" : "ServiceName";
var groupby = $"group by {field} ,`time` order by {field} ,`time`";
var sql = $@"select
ServiceName,
{field},
toStartOfInterval(`Timestamp` , INTERVAL {GetPeriod(query)} ) as `time`,
floor(avg(Duration/{MILLSECOND})) `latency`,
floor(quantile(0.95)(Duration/{MILLSECOND})) `p95`,
Expand Down Expand Up @@ -464,7 +465,7 @@ public Task<IEnumerable<ChartLineCountDto>> GetErrorChartAsync(ApmEndpointReques
var sql = $@"select
toStartOfInterval(`Timestamp` , INTERVAL {GetPeriod(query)} ) as `time`,
count(1) `total`
from {Constants.LogTableFull} where {where} and SeverityText='Error' {groupby}";
from {Constants.LogTableFull} where {where} and SeverityText='Error' and `Attributes.exception.message`!='' {groupby}";

return Task.FromResult(getChartCountData(sql, parameters, query.ComparisonType).AsEnumerable());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ internal sealed class MasaStackClickhouseConnection : ClickHouseConnection

public static TimeZoneInfo TimeZone { get; set; }

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

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

0 comments on commit d198d5e

Please sign in to comment.