-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
patrofimov
committed
Aug 19, 2024
1 parent
cf0ce5d
commit 841236c
Showing
12 changed files
with
300 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using Vostok.Clusterclient.Core.Model; | ||
|
||
namespace Vostok.Clusterclient.Core.Misc | ||
{ | ||
internal static class LoggingUtils | ||
{ | ||
public static void AppendQueryString(StringBuilder builder, Uri uri, RequestParametersLoggingSettings querySettings) | ||
{ | ||
if (querySettings.IsEnabledForAllKeys()) | ||
{ | ||
builder.Append(uri.Query); | ||
return; | ||
} | ||
|
||
var writtenFirst = false; | ||
var requestUrlParser = new RequestUrlParser(uri.ToString()); | ||
foreach (var pair in requestUrlParser.Where(kvp => querySettings.IsEnabledForKey(kvp.Key))) | ||
{ | ||
if (!writtenFirst) | ||
{ | ||
builder.Append('?'); | ||
writtenFirst = true; | ||
} | ||
|
||
builder.Append(pair.Key); | ||
builder.Append('='); | ||
builder.Append(pair.Value); | ||
} | ||
} | ||
|
||
public static void AppendHeaders(StringBuilder builder, Headers headers, RequestParametersLoggingSettings headersSettings, bool singleLineManner) | ||
{ | ||
var writtenFirst = false; | ||
foreach (var pair in headers) | ||
{ | ||
if (!headersSettings.IsEnabledForKey(pair.Name)) | ||
continue; | ||
|
||
if (!writtenFirst) | ||
{ | ||
if (singleLineManner) | ||
{ | ||
builder.Append(" "); | ||
} | ||
else | ||
{ | ||
builder.AppendLine(); | ||
} | ||
|
||
builder.Append("Headers:"); | ||
writtenFirst = true; | ||
} | ||
|
||
if (singleLineManner) | ||
{ | ||
builder.Append(" ("); | ||
builder.Append(pair.Name); | ||
builder.Append('='); | ||
builder.Append(pair.Value); | ||
builder.Append(')'); | ||
} | ||
else | ||
{ | ||
builder.AppendLine(); | ||
builder.Append('\t'); | ||
builder.Append(pair.Name); | ||
builder.Append('='); | ||
builder.Append(pair.Value); | ||
} | ||
} | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
Vostok.ClusterClient.Core/Misc/RequestParametersLoggingSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Collections.Generic; | ||
using JetBrains.Annotations; | ||
|
||
namespace Vostok.Clusterclient.Core.Misc | ||
{ | ||
[PublicAPI] | ||
public class RequestParametersLoggingSettings | ||
{ | ||
public RequestParametersLoggingSettings(bool enabled) | ||
{ | ||
Enabled = enabled; | ||
} | ||
|
||
/// <summary> | ||
/// Flag that decides whether to log request or response parameters. | ||
/// </summary> | ||
public bool Enabled { get; } | ||
|
||
/// <summary> | ||
/// <para>Case-insensitive whitelist of parameter keys to be logged.</para> | ||
/// <para><c>null</c> value allows all keys.</para> | ||
/// <para>Takes precedence over <see cref="Blacklist"/>.</para> | ||
/// </summary> | ||
[CanBeNull] | ||
public IReadOnlyCollection<string> Whitelist { get; set; } | ||
|
||
/// <summary> | ||
/// <para>Case-insensitive blacklist of parameter keys to be logged.</para> | ||
/// <para><c>null</c> value allows all keys.</para> | ||
/// </summary> | ||
[CanBeNull] | ||
public IReadOnlyCollection<string> Blacklist { get; set; } | ||
|
||
public static implicit operator RequestParametersLoggingSettings(bool enabled) => | ||
new RequestParametersLoggingSettings(enabled); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Vostok.ClusterClient.Core/Misc/RequestParametersLoggingSettingsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using JetBrains.Annotations; | ||
|
||
namespace Vostok.Clusterclient.Core.Misc | ||
{ | ||
internal static class RequestParametersLoggingSettingsExtensions | ||
{ | ||
public static bool IsEnabledForAllKeys([NotNull] this RequestParametersLoggingSettings settings) => | ||
IsEmpty(settings.Whitelist) && IsEmpty(settings.Blacklist); | ||
|
||
public static bool IsEnabledForKey([NotNull] this RequestParametersLoggingSettings settings, [NotNull] string key) | ||
{ | ||
if (IsEmpty(settings.Whitelist)) | ||
return IsEmpty(settings.Blacklist) || !settings.Blacklist!.Contains(key); | ||
|
||
return settings.Whitelist!.Contains(key); | ||
} | ||
|
||
public static RequestParametersLoggingSettings ToCaseInsensitive([CanBeNull] this RequestParametersLoggingSettings settings) | ||
=> settings == null | ||
? null | ||
: new RequestParametersLoggingSettings(settings.Enabled) | ||
{ | ||
Whitelist = IsEmpty(settings.Whitelist) ? null : new HashSet<string>(settings.Whitelist!, StringComparer.OrdinalIgnoreCase), | ||
Blacklist = IsEmpty(settings.Blacklist) ? null : new HashSet<string>(settings.Blacklist!, StringComparer.OrdinalIgnoreCase) | ||
}; | ||
|
||
private static bool IsEmpty(IReadOnlyCollection<string> collection) => collection == null || collection.Count == 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.