Skip to content

Commit

Permalink
feature: Support collect headers & aggregate collected cookies into o…
Browse files Browse the repository at this point in the history
…n span tag. (#381)

Co-authored-by: 郭刚平 <[email protected]>
  • Loading branch information
snakorse and 郭刚平 authored Jan 28, 2021
1 parent 0f1b972 commit 8a9a597
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
3 changes: 2 additions & 1 deletion sample/SkyApm.Sample.Frontend/skyapm.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
},
"Component": {
"AspNetCore": {
"AutoTagCookies": ["c-b"]
"CollectCookies": [ "c-b" ],
"CollectHeaders": ["User-Agent"]
},
"HttpClient": {
"StopHeaderPropagationPaths": [ "**/localhost:5002/api/values/stoppropagation" ]
Expand Down
4 changes: 4 additions & 0 deletions src/SkyApm.Abstractions/Common/Tags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public static class Tags

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

public static readonly string HTTP_COOKIES = "http.cookies";

public static readonly string HTTP_HEADERS = "http.headers";

public static readonly string STATUS_CODE = "status_code";

public static readonly string DB_TYPE = "db.type";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ namespace SkyApm.Diagnostics.AspNetCore.Config
public class HostingDiagnosticConfig
{
/// <summary>
/// Auto collect specific cookies as span tags.
/// Auto collect specific cookies as span tag.
/// </summary>
public List<string> AutoTagCookies { get; set; }
public List<string> CollectCookies { get; set; }

/// <summary>
/// Auto collect specific headers as span tag
/// </summary>
public List<string> CollectHeaders { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.Primitives;
using SkyApm.AspNetCore.Diagnostics;
using SkyApm.Common;
using SkyApm.Config;
using SkyApm.Diagnostics.AspNetCore.Config;
using SkyApm.Tracing;
using SkyApm.Tracing.Segments;
using System.Collections.Generic;
using System.Text;

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

if(_config.AutoTagCookies?.Count > 0)
if(_config.CollectCookies?.Count > 0)
{
foreach (var key in _config.AutoTagCookies)
{
if (!httpContext.Request.Cookies.TryGetValue(key, out string value))
continue;
context.Span.AddTag("cookie." + key, value);
}
var cookies = CollectCookies(httpContext, _config.CollectCookies);
if (!string.IsNullOrEmpty(cookies))
context.Span.AddTag(Tags.HTTP_COOKIES, cookies);
}

if(_config.CollectHeaders?.Count > 0)
{
var headers = CollectHeaders(httpContext, _config.CollectHeaders);
if (!string.IsNullOrEmpty(headers))
context.Span.AddTag(Tags.HTTP_HEADERS, headers);
}
}

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

segmentContext.Span.AddTag(Tags.STATUS_CODE, statusCode);
}

private string CollectCookies(HttpContext httpContext, IEnumerable<string> keys)
{
var sb = new StringBuilder();
foreach (var key in keys)
{
if (!httpContext.Request.Cookies.TryGetValue(key, out string value))
continue;

if(sb.Length > 0)
sb.Append("; ");

sb.Append(key);
sb.Append('=');
sb.Append(value);
}
return sb.ToString();
}

private string CollectHeaders(HttpContext httpContext, IEnumerable<string> keys)
{
var sb = new StringBuilder();
foreach (var key in keys)
{
if (!httpContext.Request.Headers.TryGetValue(key, out StringValues value))
continue;

if(sb.Length > 0)
sb.Append('\n');

sb.Append(key);
sb.Append(": ");
sb.Append(value);
}
return sb.ToString();
}
}
}

0 comments on commit 8a9a597

Please sign in to comment.