From 0dfad0f3fcf91eeb2f9ec9c02d73e1d766ea4a6e Mon Sep 17 00:00:00 2001 From: qinyouzeng Date: Mon, 13 Nov 2023 15:06:38 +0800 Subject: [PATCH] chore: update --- .../Extensions/IDbConnectionExtensitions.cs | 85 +++++++++---------- 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc.Clickhouse/Extensions/IDbConnectionExtensitions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc.Clickhouse/Extensions/IDbConnectionExtensitions.cs index 666a705a1..695ddfad8 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc.Clickhouse/Extensions/IDbConnectionExtensitions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Tsc.Clickhouse/Extensions/IDbConnectionExtensitions.cs @@ -15,7 +15,7 @@ public static PaginatedListBase QueryTrace(this IDbConnection var result = new PaginatedListBase() { 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; @@ -33,7 +33,7 @@ public static PaginatedListBase QueryLog(this IDbConnection conn if (total > 0 && start - total < 0) { - var querySql = CombineOrs($"select Timestamp,TraceId,SpanId,TraceFlags,SeverityText,SeverityNumber,ServiceName,Body,Resources,Logs from {MasaStackClickhouseConnection.LogTable} where {where}", ors,orderBy); + var querySql = CombineOrs($"select Timestamp,TraceId,SpanId,TraceFlags,SeverityText,SeverityNumber,ServiceName,Body,Resources,Logs from {MasaStackClickhouseConnection.LogTable} where {where}", ors, orderBy); result.Result = Query(connection, $"select * from {querySql} as t limit {start},{query.PageSize}", parameters?.ToArray(), ConvertLogDto); } return result; @@ -49,7 +49,7 @@ private static string CombineOrs(string sql, IEnumerable ors, string? or { text.AppendLine($" union all {sql}{or} {orderBy}"); } - text.Remove(0, 11).Insert(0, "(").Append(")"); + text.Remove(0, 11).Insert(0, "(").Append(')'); return text.ToString(); } @@ -365,19 +365,7 @@ public static object AggregationQuery(this IDbConnection dbConnection, SimpleAgg var append = new StringBuilder(); var appendWhere = new StringBuilder(); var name = GetName(requestDto.Name, isLog); - //if (name.StartsWith("ResourceAttributes[", StringComparison.CurrentCultureIgnoreCase)) - //{ - // var filed = requestDto.Name["resource.".Length..]; - // //appendWhere.Append($" mapContains(ResourceAttributes,'{filed}') and "); - //} - //else if (requestDto.Name.StartsWith("attributes.", StringComparison.CurrentCultureIgnoreCase)) - //{ - // var filed = requestDto.Name["attributes.".Length..]; - // //appendWhere.Append($" mapContains({(isLog ? "Log" : "Span")}Attributes,'{filed}') and "); - //} - AppendAggtype(requestDto, sql, append, name, out var isScalar); - sql.AppendFormat(" from {0} ", isLog ? MasaStackClickhouseConnection.LogTable : MasaStackClickhouseConnection.TraceTable); var (where, @paremeters, _) = AppendWhere(requestDto, !isLog); sql.Append($" where {appendWhere} {where}"); @@ -456,39 +444,50 @@ private static void AppendAggtype(SimpleAggregateRequestDto requestDto, StringBu private static string GetName(string name, bool isLog) { - if (name.Equals("resource.service.name", StringComparison.CurrentCultureIgnoreCase)) - { - return "ServiceName"; - } - else if (name.Equals("@timestamp", StringComparison.CurrentCultureIgnoreCase)) - { + if (name.Equals("@timestamp", StringComparison.CurrentCultureIgnoreCase)) return "Timestamp"; - } - else if (name.StartsWith("resource.", StringComparison.CurrentCultureIgnoreCase)) - { - var field = name[("resource.".Length)..]; - if (field == "service.namespace" || field == "service.instance.id") - return $"Resource.{field}"; - else - return $"ResourceAttributesValues[indexOf(ResourceAttributesKeys,'{field}')]"; - } - else if (name.StartsWith("attributes.", StringComparison.CurrentCultureIgnoreCase)) - { - var pre = isLog ? "Log" : "Span"; - var field = name[("attributes.".Length)..]; - if (isLog && (field == "exception.message")) - return $"Attributes.{field}"; - else if (!isLog && (field == "http.status_code" || field == "http.request_content_body" || field == "http.response_content_body" || field == "exception.message")) - return $"Attributes.{field}"; - return $"{pre}AttributesValues[indexOf({pre}AttributesKeys,'{field}')]"; - } - else if (!isLog && name.Equals("kind", StringComparison.InvariantCultureIgnoreCase)) - { + + if (!isLog && name.Equals("kind", StringComparison.InvariantCultureIgnoreCase)) return "SpanKind"; - } + + if (!name.StartsWith("resource.", StringComparison.CurrentCultureIgnoreCase)) + return GetResourceName(name); + + if (!name.StartsWith("attributes.", StringComparison.CurrentCultureIgnoreCase)) + return GetAttributeName(name, isLog); + return name; } + private static string GetResourceName(string name) + { + var field = name[("resource.".Length)..]; + if (field.Equals("service.name", StringComparison.CurrentCultureIgnoreCase)) + return "ServiceName"; + + if (field.Equals("service.namespace", StringComparison.CurrentCultureIgnoreCase) || field.Equals("service.instance.id", StringComparison.CurrentCultureIgnoreCase)) + return $"Resource.{field}"; + + return $"ResourceAttributesValues[indexOf(ResourceAttributesKeys,'{field}')]"; + } + + private static string GetAttributeName(string name, bool isLog) + { + var pre = isLog ? "Log" : "Span"; + var field = name[("attributes.".Length)..]; + if (isLog && (field.Equals("exception.message", StringComparison.CurrentCultureIgnoreCase))) + return $"Attributes.{field}"; + + if (!isLog && (field.Equals("http.status_code", StringComparison.CurrentCultureIgnoreCase) + || field.Equals("http.request_content_body", StringComparison.CurrentCultureIgnoreCase) + || field.Equals("http.response_content_body", StringComparison.CurrentCultureIgnoreCase) + || field.Equals("exception.message", StringComparison.CurrentCultureIgnoreCase)) + ) + return $"Attributes.{field}"; + + return $"{pre}AttributesValues[indexOf({pre}AttributesKeys,'{field}')]"; + } + public static int ConvertInterval(string s) { var unit = Regex.Replace(s, @"\d+", "", RegexOptions.IgnoreCase, TimeSpan.FromSeconds(5));