Skip to content

Commit 8a9a597

Browse files
snakorse郭刚平
and
郭刚平
authored
feature: Support collect headers & aggregate collected cookies into on span tag. (#381)
Co-authored-by: 郭刚平 <gangping.guo@xiaobao100.com>
1 parent 0f1b972 commit 8a9a597

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

sample/SkyApm.Sample.Frontend/skyapm.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
},
2929
"Component": {
3030
"AspNetCore": {
31-
"AutoTagCookies": ["c-b"]
31+
"CollectCookies": [ "c-b" ],
32+
"CollectHeaders": ["User-Agent"]
3233
},
3334
"HttpClient": {
3435
"StopHeaderPropagationPaths": [ "**/localhost:5002/api/values/stoppropagation" ]

src/SkyApm.Abstractions/Common/Tags.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public static class Tags
2727

2828
public static readonly string HTTP_METHOD = "http.method";
2929

30+
public static readonly string HTTP_COOKIES = "http.cookies";
31+
32+
public static readonly string HTTP_HEADERS = "http.headers";
33+
3034
public static readonly string STATUS_CODE = "status_code";
3135

3236
public static readonly string DB_TYPE = "db.type";

src/SkyApm.Diagnostics.AspNetCore/Config/HostingDiagnosticConfig.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ namespace SkyApm.Diagnostics.AspNetCore.Config
99
public class HostingDiagnosticConfig
1010
{
1111
/// <summary>
12-
/// Auto collect specific cookies as span tags.
12+
/// Auto collect specific cookies as span tag.
1313
/// </summary>
14-
public List<string> AutoTagCookies { get; set; }
14+
public List<string> CollectCookies { get; set; }
15+
16+
/// <summary>
17+
/// Auto collect specific headers as span tag
18+
/// </summary>
19+
public List<string> CollectHeaders { get; set; }
1520
}
1621
}

src/SkyApm.Diagnostics.AspNetCore/Handlers/DefaultHostingDiagnosticHandler.cs

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818

1919
using Microsoft.AspNetCore.Http;
2020
using Microsoft.AspNetCore.Http.Extensions;
21+
using Microsoft.Extensions.Primitives;
2122
using SkyApm.AspNetCore.Diagnostics;
2223
using SkyApm.Common;
2324
using SkyApm.Config;
2425
using SkyApm.Diagnostics.AspNetCore.Config;
2526
using SkyApm.Tracing;
2627
using SkyApm.Tracing.Segments;
28+
using System.Collections.Generic;
29+
using System.Text;
2730

2831
namespace SkyApm.Diagnostics.AspNetCore.Handlers
2932
{
@@ -52,14 +55,18 @@ public void BeginRequest(ITracingContext tracingContext, HttpContext httpContext
5255
context.Span.AddTag(Tags.PATH, httpContext.Request.Path);
5356
context.Span.AddTag(Tags.HTTP_METHOD, httpContext.Request.Method);
5457

55-
if(_config.AutoTagCookies?.Count > 0)
58+
if(_config.CollectCookies?.Count > 0)
5659
{
57-
foreach (var key in _config.AutoTagCookies)
58-
{
59-
if (!httpContext.Request.Cookies.TryGetValue(key, out string value))
60-
continue;
61-
context.Span.AddTag("cookie." + key, value);
62-
}
60+
var cookies = CollectCookies(httpContext, _config.CollectCookies);
61+
if (!string.IsNullOrEmpty(cookies))
62+
context.Span.AddTag(Tags.HTTP_COOKIES, cookies);
63+
}
64+
65+
if(_config.CollectHeaders?.Count > 0)
66+
{
67+
var headers = CollectHeaders(httpContext, _config.CollectHeaders);
68+
if (!string.IsNullOrEmpty(headers))
69+
context.Span.AddTag(Tags.HTTP_HEADERS, headers);
6370
}
6471
}
6572

@@ -73,5 +80,41 @@ public void EndRequest(SegmentContext segmentContext, HttpContext httpContext)
7380

7481
segmentContext.Span.AddTag(Tags.STATUS_CODE, statusCode);
7582
}
83+
84+
private string CollectCookies(HttpContext httpContext, IEnumerable<string> keys)
85+
{
86+
var sb = new StringBuilder();
87+
foreach (var key in keys)
88+
{
89+
if (!httpContext.Request.Cookies.TryGetValue(key, out string value))
90+
continue;
91+
92+
if(sb.Length > 0)
93+
sb.Append("; ");
94+
95+
sb.Append(key);
96+
sb.Append('=');
97+
sb.Append(value);
98+
}
99+
return sb.ToString();
100+
}
101+
102+
private string CollectHeaders(HttpContext httpContext, IEnumerable<string> keys)
103+
{
104+
var sb = new StringBuilder();
105+
foreach (var key in keys)
106+
{
107+
if (!httpContext.Request.Headers.TryGetValue(key, out StringValues value))
108+
continue;
109+
110+
if(sb.Length > 0)
111+
sb.Append('\n');
112+
113+
sb.Append(key);
114+
sb.Append(": ");
115+
sb.Append(value);
116+
}
117+
return sb.ToString();
118+
}
76119
}
77120
}

0 commit comments

Comments
 (0)