Skip to content

Commit

Permalink
借助ISpan数值标签记录请求响应大小
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Apr 25, 2024
1 parent 29ddcf4 commit 00595a9
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions Stardust.Extensions/TracerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ public class TracerMiddleware
/// <summary>跟踪器</summary>
public static ITracer? Tracer { get; set; }

/// <summary>支持作为标签数据的内容类型</summary>
public static String[] TagTypes { get; set; } = new[] {
"text/plain", "text/xml", "application/json", "application/xml", "application/x-www-form-urlencoded"
};

/// <summary>实例化</summary>
/// <param name="next"></param>
public TracerMiddleware(RequestDelegate next) => _next = next ?? throw new ArgumentNullException(nameof(next));
Expand All @@ -47,6 +42,7 @@ public async Task Invoke(HttpContext ctx)
span = Tracer.NewSpan(action);
span.Tag = $"{ctx.GetUserHost()} {req.Method} {req.GetRawUrl()}";
span.Detach(req.Headers);
span.Value = req.ContentLength ?? 0;
if (span is DefaultSpan ds && ds.TraceFlag > 0)
{
var flag = false;
Expand Down Expand Up @@ -90,15 +86,16 @@ public async Task Invoke(HttpContext ctx)
// 根据状态码识别异常
if (span != null)
{
var code = ctx.Response.StatusCode;
var res = ctx.Response;
span.Value += res.ContentLength ?? 0;
var code = res.StatusCode;
if (code == 400 || code > 404)
span.SetError(new HttpRequestException($"Http Error {code} {(HttpStatusCode)code}"), null);
else if (code == 200)
{
if (span is DefaultSpan ds && ds.TraceFlag > 0 && (span.Tag == null || span.Tag.Length < 500))
{
var flag = false;
var res = ctx.Response;
if (res.ContentLength != null &&
res.ContentLength < 1024 * 8 &&
res.Body.CanSeek &&
Expand Down Expand Up @@ -150,17 +147,20 @@ public async Task Invoke(HttpContext ctx)
}
}

/// <summary>支持作为标签数据的内容类型</summary>
public static String[] TagTypes { get; set; } = [
"text/plain", "text/xml", "application/json", "application/xml", "application/x-www-form-urlencoded"
];

/// <summary>忽略的头部</summary>
public static String[] ExcludeHeaders { get; set; } = new[] {
"traceparent", "Authorization", "Cookie"
};
public static String[] ExcludeHeaders { get; set; } = ["traceparent", "Authorization", "Cookie"];

/// <summary>忽略的后缀</summary>
public static String[] ExcludeSuffixes { get; set; } = new[] {
public static String[] ExcludeSuffixes { get; set; } = [
".html", ".htm", ".js", ".css", ".map", ".png", ".jpg", ".gif", ".ico", // 脚本样式图片
".woff", ".woff2", ".svg", ".ttf", ".otf", ".eot" // 字体
};
private static readonly String[] CubeActions = new[] { "index", "detail", "add", "edit", "delete", "deleteSelect", "deleteAll", "ExportCsv", "Info", "SetEnable", "EnableSelect", "DisableSelect", "DeleteSelect" };
];
private static readonly String[] CubeActions = ["index", "detail", "add", "edit", "delete", "deleteSelect", "deleteAll", "ExportCsv", "Info", "SetEnable", "EnableSelect", "DisableSelect", "DeleteSelect"];

private static String? GetAction(HttpContext ctx)
{
Expand Down

0 comments on commit 00595a9

Please sign in to comment.