Skip to content

Commit efa14dc

Browse files
committed
Add comments and remove dead code
1 parent 2e3a3cf commit efa14dc

File tree

9 files changed

+108
-47
lines changed

9 files changed

+108
-47
lines changed

src/OpenTelemetry.Instrumentation.Kusto/Implementation/ActivityExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,26 @@
55

66
namespace OpenTelemetry.Instrumentation.Kusto.Implementation;
77

8+
/// <summary>
9+
/// Extensions on for <see cref="Activity"/>.
10+
/// </summary>
811
internal static class ActivityExtensions
912
{
13+
/// <summary>
14+
/// Adds the specified tags to the activity if they do not already exist.
15+
/// </summary>
16+
/// <remarks>
17+
/// This method does not overwrite existing tags on the activity. Only tags with keys not already
18+
/// present are added.
19+
/// </remarks>
20+
/// <param name="activity">The activity to which tags will be added.</param>
21+
/// <param name="tags">
22+
/// The collection of tags to add to the activity. Each tag is added only if its key does not already exist on the
23+
/// activity.
24+
/// </param>
25+
/// <returns>
26+
/// The activity instance with the new tags added, or unchanged if all tag keys already exist.
27+
/// </returns>
1028
public static Activity AddTags(this Activity activity, TagList tags)
1129
{
1230
foreach (var tag in tags)

src/OpenTelemetry.Instrumentation.Kusto/Implementation/InstrumentationHandleManagerExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,30 @@
33

44
namespace OpenTelemetry.Instrumentation.Kusto.Implementation;
55

6+
/// <summary>
7+
/// Provides extension methods for <see cref="InstrumentationHandleManager" />.
8+
/// </summary>
69
internal static class InstrumentationHandleManagerExtensions
710
{
11+
/// <summary>
12+
/// Returns <see langword="true"/> if tracing is active (i.e., there is at least one tracing handle); otherwise, <see langword="false"/>.
13+
/// </summary>
14+
/// <param name="handleManager">
15+
/// The <see cref="InstrumentationHandleManager"/> to check for active tracing handles.
16+
/// </param>
17+
/// <returns><see langword="true"/> if tracing is active; otherwise, <see langword="false"/>.</returns>
818
public static bool IsTracingActive(this InstrumentationHandleManager handleManager)
919
{
1020
return handleManager.TracingHandles > 0;
1121
}
1222

23+
/// <summary>
24+
/// Returns <see langword="true"/> if metrics is active (i.e., there is at least one metrics handle); otherwise, <see langword="false"/>.
25+
/// </summary>
26+
/// <param name="handleManager">
27+
/// The <see cref="InstrumentationHandleManager"/> to check for active metrics handles.
28+
/// </param>
29+
/// <returns><see langword="true"/> if metrics is active; otherwise, <see langword="false"/>.</returns>
1330
public static bool IsMetricsActive(this InstrumentationHandleManager handleManager)
1431
{
1532
return handleManager.MetricHandles > 0;

src/OpenTelemetry.Instrumentation.Kusto/Implementation/KustoInstrumentation.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace OpenTelemetry.Instrumentation.Kusto.Implementation;
77

8+
/// <summary>
9+
/// Class to hold the singleton instances used for Kusto instrumentation.
10+
/// </summary>
811
internal static class KustoInstrumentation
912
{
1013
private static readonly Lazy<ITraceListener> Listener = new(() =>
@@ -17,8 +20,14 @@ internal static class KustoInstrumentation
1720
return listener;
1821
});
1922

23+
/// <summary>
24+
/// Gets the post-configured options for Kusto instrumentation.
25+
/// </summary>
2026
public static KustoInstrumentationOptions Options { get; } = new KustoInstrumentationOptions();
2127

28+
/// <summary>
29+
/// Gets the <see cref="InstrumentationHandleManager"/> that tracks if there are any active listeners for <see cref="KustoTraceRecordListener"/>.
30+
/// </summary>
2231
public static InstrumentationHandleManager HandleManager { get; } = new InstrumentationHandleManager();
2332

2433
public static void Initialize() => _ = Listener.Value;

src/OpenTelemetry.Instrumentation.Kusto/Implementation/KustoProcessor.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88

99
namespace OpenTelemetry.Instrumentation.Kusto.Implementation;
1010

11+
/// <summary>
12+
/// Use the Kusto query language services to process Kusto queries for summarization and sanitization.
13+
/// </summary>
1114
internal static class KustoProcessor
1215
{
16+
// Because we're not doing full semantic analysis for queries, we can reuse the default global state (which includes all built-in functions and types)
1317
private static readonly GlobalState KustoParserGlobalState = GlobalState.Default.WithCache();
1418

1519
private enum ReplacementKind
@@ -18,14 +22,39 @@ private enum ReplacementKind
1822
Remove,
1923
}
2024

25+
/// <summary>
26+
/// Processes the specified Kusto query and optionally generates a summary and/or a sanitized version based on the
27+
/// provided options.
28+
/// </summary>
29+
/// <remarks>
30+
/// If both summarization and sanitization are requested, the query is parsed only once for
31+
/// efficiency. The returned <see cref="KustoStatementInfo"/> will have null values for summary or sanitized output
32+
/// if the corresponding option is not enabled.
33+
/// </remarks>
34+
/// <param name="shouldSummarize">
35+
/// Indicates whether to generate a summary of the query. If <see langword="true"/>, the returned object will
36+
/// include a summarized representation.
37+
/// </param>
38+
/// <param name="shouldSanitize">
39+
/// Indicates whether to generate a sanitized version of the query. If <see langword="true"/>, the returned object
40+
/// will include a sanitized representation.
41+
/// </param>
42+
/// <param name="query">
43+
/// The Kusto query to process.
44+
/// </param>
45+
/// <returns>
46+
/// A <see cref="KustoStatementInfo"/> containing the summary and/or sanitized version of the query, depending on
47+
/// the options specified.
48+
/// </returns>
2149
public static KustoStatementInfo Process(bool shouldSummarize, bool shouldSanitize, string query)
2250
{
2351
string? summarized = null;
2452
string? sanitized = null;
2553

2654
KustoCode? code = null;
2755

28-
// Note that order matters here as summarization requires semantic analysis, but we want to avoid parsing twice if both are requested.
56+
// Note that order matters here as summarization requires semantic analysis to find potential table references,
57+
// but we want to avoid parsing twice if both are requested.
2958
if (shouldSummarize)
3059
{
3160
code ??= KustoCode.ParseAndAnalyze(query, KustoParserGlobalState);
@@ -75,6 +104,9 @@ private static string Summarize(KustoCode code)
75104

76105
private static TextEdit CreateRemoval(SyntaxElement node) => TextEdit.Deletion(node.TextStart, node.Width);
77106

107+
/// <summary>
108+
/// Visitor that traverses the KQL looking for literal values to replace with the PLACEHOLDER value.
109+
/// </summary>
78110
private sealed class SanitizerVisitor : DefaultSyntaxVisitor
79111
{
80112
private readonly List<TextEdit> edits = [];
@@ -119,6 +151,9 @@ private void VisitChildren(SyntaxNode node)
119151
}
120152
}
121153

154+
/// <summary>
155+
/// Visitor that traverses the KQL to produce a summarized representation of the query.
156+
/// </summary>
122157
private sealed class SummarizerVisitor : DefaultSyntaxVisitor, IDisposable
123158
{
124159
private readonly TruncatingStringBuilder builder = new();

src/OpenTelemetry.Instrumentation.Kusto/Implementation/KustoTraceRecordListener.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
namespace OpenTelemetry.Instrumentation.Kusto.Implementation;
1010

11+
/// <summary>
12+
/// Class that is registered with the Kusto client library to receive trace records.
13+
/// </summary>
14+
/// <remarks>
15+
/// The Kusto client library uses its own tracing infrastructure. Many types share names with common diagnostic types
16+
/// (e.g. Activity, ITraceListener, etc.) but in the Kusto.Cloud.Platform.Utils namespace.
17+
/// </remarks>
1118
internal sealed class KustoTraceRecordListener : KustoUtils.ITraceListener
1219
{
1320
// The client's async machinery may not call us back using the same AsyncLocal context, so we must manually track

src/OpenTelemetry.Instrumentation.Kusto/Implementation/SpanExtensions.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33

44
namespace OpenTelemetry.Instrumentation.Kusto.Implementation;
55

6+
/// <summary>
7+
/// Extension methods for <see cref="ReadOnlySpan{T}"/>.
8+
/// </summary>
69
internal static class SpanExtensions
710
{
11+
/// <summary>
12+
/// Slices the span after the first occurrence of the <paramref name="needle"/>.
13+
/// </summary>
14+
/// <param name="span">
15+
/// The span to slice.
16+
/// </param>
17+
/// <param name="needle">
18+
/// The value to search for.
19+
/// </param>
20+
/// <returns>
21+
/// A <see cref="ReadOnlySpan{T}"/> that is a slice of the original span after the first occurrence of the <paramref name="needle"/>.
22+
/// </returns>
823
public static ReadOnlySpan<char> SliceAfter(this ReadOnlySpan<char> span, ReadOnlySpan<char> needle)
924
{
1025
var idx = span.IndexOf(needle);
1126
return idx >= 0 ? span.Slice(idx + needle.Length) : [];
1227
}
13-
14-
public static ReadOnlySpan<char> SliceBefore(this ReadOnlySpan<char> span, ReadOnlySpan<char> needle)
15-
{
16-
var idx = span.IndexOf(needle);
17-
return idx >= 0 ? span.Slice(0, idx) : [];
18-
}
1928
}

src/OpenTelemetry.Instrumentation.Kusto/Implementation/StringBuilderExtensions.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/OpenTelemetry.Instrumentation.Kusto/Implementation/TraceRecordParser.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace OpenTelemetry.Instrumentation.Kusto.Implementation;
99

10+
/// <summary>
11+
/// Class that parses the delimited messages in <see cref="global::Kusto.Cloud.Platform.Utils.TraceRecord"/> instances.
12+
/// </summary>
1013
internal class TraceRecordParser
1114
{
1215
#if NET9_0_OR_GREATER
@@ -25,7 +28,7 @@ public static ParsedRequestStart ParseRequestStart(ReadOnlySpan<char> message)
2528
// so we can just take everything after "text="
2629
var queryText = message.SliceAfter("text=");
2730

28-
return new ParsedRequestStart(uri, parsed?.Host, parsed?.Port, database, queryText);
31+
return new ParsedRequestStart(parsed?.Host, parsed?.Port, database, queryText);
2932
}
3033

3134
public static ParsedActivityComplete ParseActivityComplete(ReadOnlySpan<char> message)
@@ -59,15 +62,13 @@ private static ReadOnlySpan<char> ExtractValueBetween(ReadOnlySpan<char> haystac
5962

6063
internal readonly ref struct ParsedRequestStart
6164
{
62-
public readonly string Uri;
6365
public readonly string? ServerAddress;
6466
public readonly int? ServerPort;
6567
public readonly ReadOnlySpan<char> Database;
6668
public readonly ReadOnlySpan<char> QueryText;
6769

68-
public ParsedRequestStart(string uri, string? serverAddress, int? serverPort, ReadOnlySpan<char> database, ReadOnlySpan<char> queryText)
70+
public ParsedRequestStart(string? serverAddress, int? serverPort, ReadOnlySpan<char> database, ReadOnlySpan<char> queryText)
6971
{
70-
this.Uri = uri;
7172
this.ServerAddress = serverAddress;
7273
this.ServerPort = serverPort;
7374
this.Database = database;

test/OpenTelemetry.Instrumentation.Kusto.Tests/TraceRecordParserTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public void ParseRequestStartSuccess()
1414
const string message = "$$HTTPREQUEST[RestClient2]: Verb=POST, Uri=http://127.0.0.1:49902/v1/rest/message, DatabaseName=NetDefaultDB, App=testhost, User=REDMOND\\mattkot, ClientVersion=Kusto.Dotnet.Client:{14.0.2+b2d66614da1a4ff4561c5037c48e5be7002d66d4}|Runtime:{.NET_10.0.0/CLRv10.0.0/10.0.0-rtm.25523.111}, ClientRequestId=SW52YWxpZFRhYmxlIHwgdGFrZSAxMCB8IHdoZXJlIENvbDEgPSA3, text=InvalidTable | take 10 | where Col1=7 | summarize by Date, Time";
1515
var result = TraceRecordParser.ParseRequestStart(message);
1616

17-
Assert.Equal("http://127.0.0.1:49902/v1/rest/message", result.Uri);
1817
Assert.Equal("127.0.0.1", result.ServerAddress);
1918
Assert.Equal("49902", result.ServerPort.ToString());
2019
Assert.Equal("NetDefaultDB", result.Database.ToString());
@@ -27,14 +26,14 @@ public void ParseRequestStartFailure()
2726
const string message = "$$HTTPREQUEST[RestClient2]: Verb=POST, Uri=http://";
2827
var result = TraceRecordParser.ParseRequestStart(message);
2928

30-
Assert.Equal("http://", result.Uri);
3129
Assert.Null(result.ServerAddress);
3230
Assert.Equal(string.Empty, result.ServerPort.ToString());
3331
Assert.Equal(string.Empty, result.Database.ToString());
3432
Assert.Equal(string.Empty, result.QueryText.ToString());
3533
}
3634

3735
[Theory]
36+
[InlineData("$$HTTPREQUEST[RestClient2]: Verb=POST, Uri=https://clustername.kusto.windows.net/v1/rest/query, DatabaseName=TestDB, text=print 1", "clustername.kusto.windows.net", 443)]
3837
[InlineData("$$HTTPREQUEST[RestClient2]: Verb=POST, Uri=http://localhost/v1/rest/query, DatabaseName=TestDB, text=print 1", "localhost", 80)]
3938
[InlineData("$$HTTPREQUEST[RestClient2]: Verb=POST, Uri=http://[2001:db8::1]:8080/v1/rest/query, DatabaseName=TestDB, text=print 1", "[2001:db8::1]", 8080)]
4039
[InlineData("$$HTTPREQUEST[RestClient2]: Verb=POST, Uri=https://[2001:db8::1]/v1/rest/query, DatabaseName=TestDB, text=print 1", "[2001:db8::1]", 443)]

0 commit comments

Comments
 (0)