Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tsc-clickhouse):fix query bugs, timezone with utc, and correct init create table sql #680

Merged
merged 15 commits into from
Nov 29, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static PaginatedListBase<TraceResponseDto> QueryTrace(this IDbConnection
var result = new PaginatedListBase<TraceResponseDto>() { Total = total, Result = new() };
if (total > 0 && start - total < 0)
{
var querySql = CombineOrs($"select ServiceName,Timestamp,TraceId,SpanId,ParentSpanId,TraceState,SpanKind,Duration,SpanName,Spans,Resources from {MasaStackClickhouseConnection.TraceTable} where {where}", ors,orderBy);
var querySql = CombineOrs($"select ServiceName,Timestamp,TraceId,SpanId,ParentSpanId,TraceState,SpanKind,Duration,SpanName,Spans,Resources from {MasaStackClickhouseConnection.TraceTable} where {where}", ors, orderBy);
result.Result = Query(connection, $"select * from {querySql} as t limit {start},{query.PageSize}", parameters?.ToArray(), ConvertTraceDto);
}
return result;
Expand Down Expand Up @@ -90,8 +90,8 @@ public static (string where, List<IDataParameter> @parameters, List<string> ors)
&& query.End > query.Start)
{
sql.Append($" and Timestamp BETWEEN @Start and @End");
@paramerters.Add(new ClickHouseParameter() { ParameterName = "Start", Value = query.Start.ToLocalTime(), DbType = DbType.DateTime2 });
@paramerters.Add(new ClickHouseParameter() { ParameterName = "End", Value = query.End.ToLocalTime(), DbType = DbType.DateTime2 });
@paramerters.Add(new ClickHouseParameter() { ParameterName = "Start", Value = MasaStackClickhouseConnection.ToTimeZone(query.Start), DbType = DbType.DateTime2 });
@paramerters.Add(new ClickHouseParameter() { ParameterName = "End", Value = MasaStackClickhouseConnection.ToTimeZone(query.End), DbType = DbType.DateTime2 });
}
if (!string.IsNullOrEmpty(query.Service))
{
Expand Down Expand Up @@ -163,7 +163,7 @@ private static void AppendConditions(IEnumerable<FieldConditionDto>? conditions,

if (item.Value is DateTime time)
{
item.Value = time.ToLocalTime();
item.Value = MasaStackClickhouseConnection.ToTimeZone(time);
}
if (item.Name.StartsWith("resource.", StringComparison.CurrentCultureIgnoreCase))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ private static void Init(IServiceCollection services, bool createTable = true)
if (createTable)
InitTable(connection);
InitMappingTable(connection);

var timezoneStr = GetTimezone(connection);
MasaStackClickhouseConnection.TimeZone = TZConvert.GetTimeZoneInfo(timezoneStr);
}

private static void InitTable(MasaStackClickhouseConnection connection)
Expand Down Expand Up @@ -119,7 +122,7 @@ TTL toDateTime(Timestamp) + toIntervalDay(30)
`Attributes.http.status_code` String CODEC(ZSTD(1)),
`Attributes.http.response_content_body` String CODEC(ZSTD(1)),
`Attributes.http.request_content_body` String CODEC(ZSTD(1)),
`Attributes.http.target` String CODEC(ZSTD(1)),
`Attributes.http.Target` String CODEC(ZSTD(1)),
`Attributes.exception.message` String CODEC(ZSTD(1)),

`ResourceAttributesKeys` Array(String) CODEC(ZSTD(1)),
Expand Down Expand Up @@ -158,7 +161,7 @@ TTL toDateTime(Timestamp) + toIntervalDay(30)
LogAttributes['TaskId'] as `Attributes.TaskId`,LogAttributes['exception.message'] as `Attributes.exception.message`,
mapKeys(ResourceAttributes) as ResourceAttributesKeys,mapValues(ResourceAttributes) as ResourceAttributesValues,
mapKeys(LogAttributes) as LogAttributesKeys,mapValues(LogAttributes) as LogAttributesValues
FROM {MasaStackClickhouseConnection.LogSourceTable};
FROM {MasaStackClickhouseConnection.LogSourceTable}
",
$@"CREATE MATERIALIZED VIEW {MasaStackClickhouseConnection.TraceTable}_v TO {MasaStackClickhouseConnection.TraceTable}
AS
Expand All @@ -181,7 +184,7 @@ TTL toDateTime(Timestamp) + toIntervalDay(30)
mapValues(ResourceAttributes) AS ResourceAttributesValues,
mapKeys(SpanAttributes) AS SpanAttributesKeys,
mapValues(SpanAttributes) AS SpanAttributesValues
FROM {MasaStackClickhouseConnection.TraceSourceTable};
FROM {MasaStackClickhouseConnection.TraceSourceTable}
" };

foreach (var sql in createTableSqls)
Expand Down Expand Up @@ -213,19 +216,19 @@ ORDER BY Name
@$"CREATE MATERIALIZED VIEW {database}.v_otel_traces_attribute_mapping to {MasaStackClickhouseConnection.MappingTable}
as
select DISTINCT arraySort(mapKeys(SpanAttributes)) as Name, 'trace_attributes' as Type
from {MasaStackClickhouseConnection.TraceTable}",
from {MasaStackClickhouseConnection.TraceSourceTable}",
$@"CREATE MATERIALIZED VIEW {database}.v_otel_traces_resource_mapping to {MasaStackClickhouseConnection.MappingTable}
as
select DISTINCT arraySort(mapKeys(ResourceAttributes)) as Name, 'trace_resource' as Type
from {MasaStackClickhouseConnection.TraceTable}",
from {MasaStackClickhouseConnection.TraceSourceTable}",
$@"CREATE MATERIALIZED VIEW {database}.v_otel_logs_attribute_mapping to {MasaStackClickhouseConnection.MappingTable}
as
select DISTINCT arraySort(mapKeys(LogAttributes)) as Name, 'log_attributes' as Type
from {MasaStackClickhouseConnection.LogTable}",
from {MasaStackClickhouseConnection.LogSourceTable}",
$@"CREATE MATERIALIZED VIEW {database}.v_otel_logs_resource_mapping to {MasaStackClickhouseConnection.MappingTable}
as
select DISTINCT arraySort(mapKeys(ResourceAttributes)) as Name, 'log_resource' as Type
from {MasaStackClickhouseConnection.LogTable}",
from {MasaStackClickhouseConnection.LogSourceTable}",
$@"insert into {MasaStackClickhouseConnection.MappingTable}
values (['Timestamp','TraceId','SpanId','TraceFlag','SeverityText','SeverityNumber','Body'],'log_basic'),
(['Timestamp','TraceId','SpanId','ParentSpanId','TraceState','SpanKind','Duration'],'trace_basic');
Expand All @@ -249,4 +252,22 @@ private static void ExecuteSql(MasaStackClickhouseConnection connection, string
Logger?.LogError(ex, "ExecuteSql {rawSql} error", sql);
}
}

private static string GetTimezone(MasaStackClickhouseConnection connection)
{
using var cmd = connection.CreateCommand();
if (connection.State != ConnectionState.Open)
connection.Open();
var sql = "select timezone()";
cmd.CommandText = sql;
try
{
return cmd.ExecuteScalar()?.ToString()!;
}
catch (Exception ex)
{
Logger?.LogError(ex, "ExecuteSql {rawSql} error", sql);
throw;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="ClickHouse.Ado" Version="1.5.5" />
<PackageReference Include="TimeZoneConverter" Version="6.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ internal class MasaStackClickhouseConnection : ClickHouseConnection

public static string MappingTable { get; private set; }

public static TimeZoneInfo TimeZone { get; set; }

public static DateTime ToTimeZone(DateTime utcTime)
{
return utcTime + TimeZone.BaseUtcOffset;
}

public MasaStackClickhouseConnection(string connection, string logTable, string traceTable, string? logSourceTable = null, string? traceSourceTable = null)
{
ArgumentNullException.ThrowIfNull(connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
global using System.Text;
global using System.Text.Json;
global using System.Text.RegularExpressions;
global using TimeZoneConverter;
Loading