Skip to content

Commit

Permalink
fix: trace 数据搜索bug修复;
Browse files Browse the repository at this point in the history
     添加log和error的trace直接跳转
     添加 getip接口
  • Loading branch information
Qinyouzeng committed Aug 8, 2024
1 parent c047512 commit 215b919
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 52 deletions.
2 changes: 1 addition & 1 deletion power.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Write-Host "Hello $t"
docker login --username=$u registry.cn-hangzhou.aliyuncs.com --password=$p

$ServiceDockerfilePath="./src/Services/Masa.Tsc.Service.Admin/Dockerfile"
$ServiceServerName="masa-tsc-service-admin"
$ServiceServerName="masa-tsc-service"
$WebDockerfilePath="./src/Web/Masa.Tsc.Web.Admin.Server/Dockerfile"
$WebServerName="masa-tsc-web-admin"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,28 +315,32 @@ public async Task<PaginatedListBase<TraceResponseDto>> TraceLatencyDetailAsync(A

public async Task<PaginatedListBase<SimpleTraceListDto>> GetSimpleTraceListAsync(ApmEndpointRequestDto query)
{
//query.IsServer = default;
//query.IsTrace = true;
var (where, ors, parameters) = AppendWhere(query);
var orderBy = GetOrderBy(query, new() { { StorageConst.Current.Timestimap, StorageConst.Current.Timestimap } });

var (where, ors, parameters) = AppendWhere(query);
PaginatedListBase<SimpleTraceListDto> result = new() { };
if (query.HasPage)
{
//var sql1 = CombineOrs($@"select TraceId from {Constants.DurationTable} where {where}", ors);
//var countSql = $"select count(1) from {sql1}";
var sql1 = CombineOrs($@"select countMerge(Total) as Total from {Constants.DurationCountTable} where {where}", ors);
string sql1;
if (query.IsInstrument)
{
sql1 = $"select count(1) as Total from {MasaStackClickhouseConnection.TraceHttpServerTable} where {where}";
}
else
{
sql1 = $@"select countMerge(Total) as Total from {Constants.DurationCountTable} where {where}";
}

sql1 = CombineOrs(sql1, ors);
var countSql = $"select sum(Total) from({sql1})";
result.Total = Convert.ToInt64(await Scalar(countSql, parameters));
}
//Constants.DurationTable

var sql = CombineOrs($@"select TraceId,Duration,Timestamp from {Constants.DurationTable} where {where}", ors);

var sql = CombineOrs($@"select TraceId,Duration,Timestamp from {(query.IsInstrument ? MasaStackClickhouseConnection.TraceHttpServerTable : Constants.DurationTable)} where {where}", ors);
sql = $"select TraceId,Duration,Timestamp from {sql} {orderBy} @limit";

await SetData(sql, parameters, result, query, ToSampleTraceListDto);
return result;
}
}

private static SimpleTraceListDto ToSampleTraceListDto(IDataReader reader)
{
Expand Down Expand Up @@ -777,7 +781,7 @@ private static void AppendEndpoint(ApmEndpointRequestDto? traceQuery, StringBuil
if (!string.IsNullOrEmpty(traceQuery.Method))
{
sql.AppendLine($" and {StorageConst.Current.Trace.HttpMethod}=@method");
parameters.Add(new ClickHouseParameter { ParameterName = "method", Value = traceQuery.Endpoint });
parameters.Add(new ClickHouseParameter { ParameterName = "method", Value = traceQuery.Method });
}

if (!isMetric)
Expand Down
15 changes: 8 additions & 7 deletions src/Services/Masa.Tsc.Service.Admin/Services/ApmService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ public async Task<PaginatedListBase<EndpointListDto>> GetEndpoints([FromServices
Page = page,
PageSize = pageSize,
Endpoint = endpoint!,
TextField = textField,
TextValue = textValue,
StatusCode = statusCode,
ExType = exType,
TraceId = traceId,
TextField = textField!,
TextValue = textValue!,
StatusCode = statusCode!,
ExType = exType!,
TraceId = traceId!,
Service = service,
StatusCodes = string.Join(',', ConfigConst.TraceErrorStatus)
});

public async Task<IEnumerable<ChartLineDto>> GetCharts([FromServices] IApmService apmService, string start, string end, string? env, string? service, string? endpoint, ComparisonTypes? comparisonType, string? queries)
public async Task<IEnumerable<ChartLineDto>> GetCharts([FromServices] IApmService apmService, string start, string end, string? env, string? service, string? endpoint, string? method, ComparisonTypes? comparisonType, string? queries)
{
BaseApmRequestDto queryDto;

Expand All @@ -59,7 +59,8 @@ public async Task<IEnumerable<ChartLineDto>> GetCharts([FromServices] IApmServic
{
queryDto = new ApmEndpointRequestDto()
{
Endpoint = endpoint.Equals("@all", StringComparison.InvariantCultureIgnoreCase) ? "" : endpoint!
Endpoint = endpoint.Equals("@all", StringComparison.InvariantCultureIgnoreCase) ? "" : endpoint!,
Method = method!
};
}
queryDto.Start = start.ParseUTCTime();
Expand Down
31 changes: 31 additions & 0 deletions src/Services/Masa.Tsc.Service.Admin/Services/ClientService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.

namespace Masa.Tsc.Service.Admin.Services;

public class ClientService : ServiceBase
{
public ClientService() : base("/api/client")
{ }

public string GetIp([FromServices] IHttpContextAccessor httpContext)
{
return GetIp(httpContext.HttpContext!.Request.Headers, httpContext.HttpContext.Connection.RemoteIpAddress);
}

private static string GetIp(IHeaderDictionary headers, IPAddress? deafultIp)
{
if (headers.TryGetValue("X-Forwarded-For", out StringValues value))
{
var ip = value.ToString().Split(',')[0].Trim();
if (ip.Length > 0) return ip;
}
if (headers.TryGetValue("X-Real-IP", out value))
{
var ip = value.ToString();
if (ip.Length > 0) return ip;
}

return deafultIp?.ToString() ?? string.Empty;
}
}
2 changes: 2 additions & 0 deletions src/Services/Masa.Tsc.Service.Admin/_Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@
global using Microsoft.EntityFrameworkCore.Design;
global using Microsoft.EntityFrameworkCore.Metadata.Builders;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.Primitives;
global using Microsoft.OpenApi.Models;
global using System.Linq.Expressions;
global using System.Net;
global using System.Reflection;
global using System.Text;
global using System.Text.Json;
Expand Down
19 changes: 18 additions & 1 deletion src/Web/Masa.Tsc.Web.Admin.Rcl/Components/Apm/ApmTraceView.razor
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,24 @@ else

<tr>
<td>@currentName</td>
<td class="text-wrap">@value</td>
<td class="text-wrap">
@{
if (IsRedirectTrace && string.Equals(currentName, StorageConst.Current.TraceId, StringComparison.CurrentCultureIgnoreCase))
{
var resources = (Dictionary<string, object>)dic["Resource"];
var attributes = (Dictionary<string, object>)dic["Attributes"];
attributes.TryGetValue("RequestPath", out var path);
var time1 = (DateTime)dic["Timestamp"];
DateTime start = time1.AddHours(-6), end = time1.AddHours(6);
var url = path?.ToString().Split('?')[0];
<a style="text-decoration:none" href="/apm/endpoints/@(HttpUtility.UrlEncode(url)+GetUrlParam(service: resources["service.name"].ToString(), env: resources["service.namespace"].ToString(),endpoint:url, start: start, end: end,traceId:dic["TraceId"].ToString()))" target="_blank">@value</a>
}
else
{
@value
}
}
</td>
</tr>
}
else if (value is IDictionary<string, object> dicValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public partial class ApmTraceView
[Parameter]
public StringNumber? Height { get; set; }

[Parameter]
public bool IsRedirectTrace { get; set; } = true;

private async Task CloseAsync()
{
Show = false;
Expand Down
7 changes: 5 additions & 2 deletions src/Web/Masa.Tsc.Web.Admin.Rcl/Data/Apm/ListChartData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ internal class ListChartData

public string Endpoint { get; set; }

public string Method { get; set; }

public string Envs { get; set; }

public long Latency { get; set; }
Expand All @@ -31,10 +33,11 @@ internal class ListChartData

public ChartData P95ChartData { get; set; }

public ChartData P99ChartData { get; set; }
public ChartData P99ChartData { get; set; }
}

public class ChartData {
public class ChartData
{

public bool HasChart { get; set; } = true;

Expand Down
4 changes: 2 additions & 2 deletions src/Web/Masa.Tsc.Web.Admin.Rcl/Pages/Apm/Endpoint.razor
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<MTooltip Top Context="tooltipContent">
<ActivatorContent>
<div @attributes="@tooltipContent.Attrs" style="width:400px;overflow:hidden" class="text-truncate">
<a href="/apm/endpoints/@(HttpUtility.UrlEncode(context.Item.Name)+GetUrlParam(service: context.Item.Service, env: GetSearchEnv(Search.Environment, context.Item.Envs?.Split(',')),endpoint:context.Item.Endpoint, comparisonType: Search.ComparisonType, start: Search.Start, end: Search.End))"
<a href="/apm/endpoints/@(HttpUtility.UrlEncode(context.Item.Name)+GetUrlParam(service: context.Item.Service, env: GetSearchEnv(Search.Environment, context.Item.Envs?.Split(',')),endpoint:context.Item.Endpoint, comparisonType: Search.ComparisonType, start: Search.Start, end: Search.End,method:context.Item.Method))"
title="@context.Item.Name">@context.Item.Name</a>
</div>
</ActivatorContent>
Expand Down Expand Up @@ -95,7 +95,7 @@
</SDataTable>
<style>
.m-data-table .m-data-table__wrapper {
height: calc(100vh - 290px)
height: calc(100vh - 300px)
}
</style>
<MCard Rounded=true Class="px-6 py-2" Style="border-top-left-radius: 0 !important; border-top-right-radius: 0 !important;">
Expand Down
1 change: 1 addition & 0 deletions src/Web/Masa.Tsc.Web.Admin.Rcl/Pages/Apm/Endpoint.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private async Task LoadPageDataAsync()
data.AddRange(result.Result.Select(item => new ListChartData
{
Name = $"{item.Method} {item.Endpoint}",
Method = item.Method,
Endpoint = item.Endpoint,
Service = item.Service,
Failed = item.Failed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@page "/apm/endpoints/{name}"
<PageTitle>@I18n.Apm("PageTitles.EndpointDetail")</PageTitle>

<ApmSearchComponent @bind-Value="Search" IsEndpoint
<ApmSearchComponent Value="Search" ValueChanged="OnSearchValueChanged" IsEndpoint
ShowComparison />

<MTabs @bind-Value="index" Class="rounded-t-xl">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public partial class OverView
private ChartData failed = new();
private readonly ChartData timeTypeCount = new();
private string? lastKey = null;
private readonly ApmTraceLatencyRequestDto query = new();
private List<TraceResponseDto>? traceDetails = null;
private List<ChartPointDto>? errors = null;
int page = 1, total = 1;
Expand All @@ -36,17 +35,6 @@ protected override async Task OnParametersSetAsync()
}
}

//protected override void OnInitialized()
//{
// var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);
// var queries = HttpUtility.ParseQueryString(uri.Query);
// var endpoint = queries.Get("endpoint");
// SearchData.Endpoint = endpoint;
// query.Endpoint = endpoint!;
// //traceId = queries.Get("traceId");
// base.OnInitialized();
//}

private async Task LoadTraceDetailAsync(int page = 1)
{
this.page = page;
Expand All @@ -66,7 +54,10 @@ private async Task LoadTraceDetailAsync(int page = 1)
Env = SearchData.Environment!,
Page = 1,
PageSize = 100,
Method=query.Method,
Method = SearchData.Method,
TextField = SearchData.TextField,
TextValue = SearchData.TextValue,
StatusCode = SearchData.Status,
//Queries = SearchData.Text,
OrderField = "Timestamp",
IsDesc = true
Expand All @@ -85,7 +76,7 @@ private async Task LoadTraceDetailAsync(int page = 1)
Start = SearchData.Start,
End = SearchData.End,
Endpoint = SearchData.Endpoint!,
Method = query.Method,
Method = SearchData.Method,
Service = SearchData.Service!,
Env = SearchData.Environment!,
Page = 1,
Expand Down Expand Up @@ -152,6 +143,7 @@ private void CaculatePercentil()
percentile = lessTotal * 1.0 / sum;
}

//需要优化,不是自己关注的条件不刷新数据
private async Task LoadDataAsync()
{
var query = new ApmEndpointRequestDto
Expand All @@ -161,6 +153,7 @@ private async Task LoadDataAsync()
Service = SearchData.Service,
Env = SearchData.Environment,
Endpoint = SearchData.Endpoint!,
Method = SearchData.Method,
//Queries = SearchData.Text,
ComparisonType = SearchData.ComparisonType.ToComparisonType()
};
Expand Down Expand Up @@ -241,7 +234,7 @@ private async Task LoadDistributionDataAsync()
Service = SearchData.Service,
Env = SearchData.Environment,
Endpoint = SearchData.Endpoint!,
Method = SearchData.Method,
Method = SearchData.Method,
//Queries = SearchData.Text,
ComparisonType = SearchData.ComparisonType.ToComparisonType()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</MCardText>
</MCard>

<ApmTraceView Value="currentTimeLine?.Trace" @bind-Show="showTraceDetail" LinkUrl="@spanLinkUrl" />
<ApmTraceView Value="currentTimeLine?.Trace" @bind-Show="showTraceDetail" LinkUrl="@spanLinkUrl" IsRedirectTrace=false />

@code {

Expand All @@ -75,7 +75,7 @@
{
style = $"padding-left:{timeLine.Left}%";
}
else if (35 - timeLine.Process - timeLine.Right >= 0)
else if (40 - timeLine.Process - timeLine.Right >= 0)
{
className = "d-flex justify-end";
style = $"padding-right:{timeLine.Right}%";
Expand Down
25 changes: 22 additions & 3 deletions src/Web/Masa.Tsc.Web.Admin.Rcl/Pages/Apm/ErrorDetail.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@namespace Masa.Tsc.Web.Admin.Rcl.Pages.Apm
@inherits ApmComponentBase

<div class="pa-4 container overflow-y-auto" style="height:840px">
<div class="pa-4 container overflow-y-auto" style="height:100%">
<MCard Class="rounded-lg mt-4" Height="256" Style="border:solid #ccc 1px">
<MCardSubtitle Class="d-flex">
<div class="font-weight-black h6 ml-4 mt-2">@I18n.Apm("Chart.ErrorOccurrences")</div>
Expand Down Expand Up @@ -95,7 +95,26 @@

<tr>
<td>@currentName</td>
<td><div class="right-error-text">@value</div></td>
<td>
<div class="right-error-text">
@{
if (string.Equals(currentName, StorageConst.Current.TraceId, StringComparison.CurrentCultureIgnoreCase))
{
var resources = (Dictionary<string, object>)dic["Resource"];
var attributes = (Dictionary<string, object>)dic["Attributes"];
attributes.TryGetValue("RequestPath", out var path);
var time1 = (DateTime)dic["Timestamp"];
DateTime start = time1.AddHours(-6), end = time1.AddHours(6);
var url = path?.ToString().Split('?')[0];
<a style="text-decoration:none" href="/apm/endpoints/@(HttpUtility.UrlEncode(url)+GetUrlParam(service: resources["service.name"].ToString(), env: resources["service.namespace"].ToString(),endpoint:url, start: start, end: end,traceId:dic["TraceId"].ToString()))" target="_blank">@value</a>
}
else
{
@value
}
}
</div>
</td>
</tr>
}
else if (value is IDictionary<string, object> dicValue)
Expand All @@ -109,4 +128,4 @@
{
return type.IsPrimitive || type.IsEnum || type.Equals(typeof(string)) || type.Equals(typeof(DateTime));
}
}
}
2 changes: 1 addition & 1 deletion src/Web/Masa.Tsc.Web.Admin.Rcl/Pages/Apm/Errors.razor
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</SDataTable>
<style>
.m-data-table .m-data-table__wrapper {
height: calc(100vh - 290px)
height: calc(100vh - 300px)
}
</style>
<MCard Rounded=true Class="px-6 py-2" Style="border-top-left-radius: 0 !important; border-top-right-radius: 0 !important;">
Expand Down
Loading

0 comments on commit 215b919

Please sign in to comment.