Skip to content

Commit 524f04d

Browse files
committed
Split InstrumentationOptions for Trace and Meter
1 parent e36e1c4 commit 524f04d

16 files changed

+317
-134
lines changed
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
#nullable enable
2-
OpenTelemetry.Instrumentation.Kusto.KustoInstrumentationOptions
3-
OpenTelemetry.Instrumentation.Kusto.KustoInstrumentationOptions.Enrich.get -> System.Action<System.Diagnostics.Activity!, Kusto.Cloud.Platform.Utils.TraceRecord!>?
4-
OpenTelemetry.Instrumentation.Kusto.KustoInstrumentationOptions.Enrich.set -> void
5-
OpenTelemetry.Instrumentation.Kusto.KustoInstrumentationOptions.KustoInstrumentationOptions() -> void
6-
OpenTelemetry.Instrumentation.Kusto.KustoInstrumentationOptions.RecordQuerySummary.get -> bool
7-
OpenTelemetry.Instrumentation.Kusto.KustoInstrumentationOptions.RecordQuerySummary.set -> void
8-
OpenTelemetry.Instrumentation.Kusto.KustoInstrumentationOptions.RecordQueryText.get -> bool
9-
OpenTelemetry.Instrumentation.Kusto.KustoInstrumentationOptions.RecordQueryText.set -> void
2+
OpenTelemetry.Metrics.KustoMeterInstrumentationOptions
3+
OpenTelemetry.Metrics.KustoMeterInstrumentationOptions.KustoMeterInstrumentationOptions() -> void
4+
OpenTelemetry.Metrics.KustoMeterInstrumentationOptions.RecordQuerySummary.get -> bool
5+
OpenTelemetry.Metrics.KustoMeterInstrumentationOptions.RecordQuerySummary.set -> void
6+
OpenTelemetry.Metrics.KustoMeterInstrumentationOptions.RecordQueryText.get -> bool
7+
OpenTelemetry.Metrics.KustoMeterInstrumentationOptions.RecordQueryText.set -> void
108
OpenTelemetry.Metrics.MeterProviderBuilderExtensions
9+
OpenTelemetry.Trace.KustoTraceInstrumentationOptions
10+
OpenTelemetry.Trace.KustoTraceInstrumentationOptions.Enrich.get -> System.Action<System.Diagnostics.Activity!, Kusto.Cloud.Platform.Utils.TraceRecord!>?
11+
OpenTelemetry.Trace.KustoTraceInstrumentationOptions.Enrich.set -> void
12+
OpenTelemetry.Trace.KustoTraceInstrumentationOptions.KustoTraceInstrumentationOptions() -> void
13+
OpenTelemetry.Trace.KustoTraceInstrumentationOptions.RecordQuerySummary.get -> bool
14+
OpenTelemetry.Trace.KustoTraceInstrumentationOptions.RecordQuerySummary.set -> void
15+
OpenTelemetry.Trace.KustoTraceInstrumentationOptions.RecordQueryText.get -> bool
16+
OpenTelemetry.Trace.KustoTraceInstrumentationOptions.RecordQueryText.set -> void
1117
OpenTelemetry.Trace.TracerProviderBuilderExtensions
1218
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddKustoInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder! builder) -> OpenTelemetry.Metrics.MeterProviderBuilder!
13-
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddKustoInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder! builder, System.Action<OpenTelemetry.Instrumentation.Kusto.KustoInstrumentationOptions!>! configureKustoInstrumentationOptions) -> OpenTelemetry.Metrics.MeterProviderBuilder!
19+
static OpenTelemetry.Metrics.MeterProviderBuilderExtensions.AddKustoInstrumentation(this OpenTelemetry.Metrics.MeterProviderBuilder! builder, System.Action<OpenTelemetry.Metrics.KustoMeterInstrumentationOptions!>? configureKustoMeterInstrumentationOptions) -> OpenTelemetry.Metrics.MeterProviderBuilder!
1420
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddKustoInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder) -> OpenTelemetry.Trace.TracerProviderBuilder!
15-
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddKustoInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder, System.Action<OpenTelemetry.Instrumentation.Kusto.KustoInstrumentationOptions!>! configureKustoInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder!
21+
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddKustoInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder! builder, System.Action<OpenTelemetry.Trace.KustoTraceInstrumentationOptions!>? configureKustoTraceInstrumentationOptions) -> OpenTelemetry.Trace.TracerProviderBuilder!

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

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

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ internal static class InstrumentationHandleManagerExtensions
1515
/// The <see cref="InstrumentationHandleManager"/> to check for active tracing handles.
1616
/// </param>
1717
/// <returns><see langword="true"/> if tracing is active; otherwise, <see langword="false"/>.</returns>
18-
public static bool IsTracingActive(this InstrumentationHandleManager handleManager)
19-
{
20-
return handleManager.TracingHandles > 0;
21-
}
18+
public static bool IsTracingActive(this InstrumentationHandleManager handleManager) => handleManager.TracingHandles > 0;
2219

2320
/// <summary>
2421
/// Returns <see langword="true"/> if metrics is active (i.e., there is at least one metrics handle); otherwise, <see langword="false"/>.
@@ -27,8 +24,5 @@ public static bool IsTracingActive(this InstrumentationHandleManager handleManag
2724
/// The <see cref="InstrumentationHandleManager"/> to check for active metrics handles.
2825
/// </param>
2926
/// <returns><see langword="true"/> if metrics is active; otherwise, <see langword="false"/>.</returns>
30-
public static bool IsMetricsActive(this InstrumentationHandleManager handleManager)
31-
{
32-
return handleManager.MetricHandles > 0;
33-
}
27+
public static bool IsMetricsActive(this InstrumentationHandleManager handleManager) => handleManager.MetricHandles > 0;
3428
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using Kusto.Cloud.Platform.Utils;
5+
using OpenTelemetry.Metrics;
6+
using OpenTelemetry.Trace;
57

68
namespace OpenTelemetry.Instrumentation.Kusto.Implementation;
79

@@ -21,14 +23,22 @@ internal static class KustoInstrumentation
2123
});
2224

2325
/// <summary>
24-
/// Gets the post-configured options for Kusto instrumentation.
26+
/// Gets or sets the post-configured trace options for Kusto instrumentation.
2527
/// </summary>
26-
public static KustoInstrumentationOptions Options { get; } = new KustoInstrumentationOptions();
28+
public static KustoTraceInstrumentationOptions TraceOptions { get; set; } = new KustoTraceInstrumentationOptions();
29+
30+
/// <summary>
31+
/// Gets or sets the post-configured meter options for Kusto instrumentation.
32+
/// </summary>
33+
public static KustoMeterInstrumentationOptions MeterOptions { get; set; } = new KustoMeterInstrumentationOptions();
2734

2835
/// <summary>
2936
/// Gets the <see cref="InstrumentationHandleManager"/> that tracks if there are any active listeners for <see cref="KustoTraceRecordListener"/>.
3037
/// </summary>
3138
public static InstrumentationHandleManager HandleManager { get; } = new InstrumentationHandleManager();
3239

40+
/// <summary>
41+
/// Initializes the Kusto instrumentation by ensuring the listener is created and registered with the client library.
42+
/// </summary>
3343
public static void Initialize() => _ = Listener.Value;
3444
}

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,13 @@ public void EnrichmentException(Exception ex)
2424
}
2525

2626
[Event(1, Message = "Enrichment exception: {0}", Level = EventLevel.Error)]
27-
public void EnrichmentException(string exception)
28-
{
29-
this.WriteEvent(1, exception);
30-
}
27+
public void EnrichmentException(string exception) => this.WriteEvent(1, exception);
3128

3229
[Event(2, Message = "Trace record payload is NULL or has NULL message, record will not be processed.", Level = EventLevel.Warning)]
33-
public void NullPayload()
34-
{
35-
this.WriteEvent(2);
36-
}
30+
public void NullPayload() => this.WriteEvent(2);
3731

3832
[Event(3, Message = "Failed to find context for activity ID '{0}', operation data will not be recorded.", Level = EventLevel.Warning)]
39-
public void ContextNotFound(string activityId)
40-
{
41-
this.WriteEvent(3, activityId);
42-
}
33+
public void ContextNotFound(string activityId) => this.WriteEvent(3, activityId);
4334

4435
[NonEvent]
4536
public void UnknownErrorProcessingTraceRecord(Exception ex)
@@ -51,8 +42,5 @@ public void UnknownErrorProcessingTraceRecord(Exception ex)
5142
}
5243

5344
[Event(4, Message = "Unknown error processing trace record, Exception: {0}", Level = EventLevel.Error)]
54-
public void UnknownErrorProcessingTraceRecord(string exception)
55-
{
56-
this.WriteEvent(4, exception);
57-
}
45+
public void UnknownErrorProcessingTraceRecord(string exception) => this.WriteEvent(4, exception);
5846
}

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

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private void CallEnrichment(KustoUtils.TraceRecord record)
8888
var activity = this.GetContext(record)?.Activity;
8989
if (activity is not null && activity.IsAllDataRequested)
9090
{
91-
KustoInstrumentation.Options.Enrich?.Invoke(activity, record);
91+
KustoInstrumentation.TraceOptions.Enrich?.Invoke(activity, record);
9292
}
9393
}
9494
catch (Exception ex)
@@ -110,7 +110,7 @@ private void HandleException(KustoUtils.TraceRecord record)
110110
if (!result.ErrorType.IsEmpty)
111111
{
112112
activity?.SetTag(SemanticConventions.AttributeErrorType, result.ErrorType.ToString());
113-
context.Value.Tags.Add(SemanticConventions.AttributeErrorType, result.ErrorType.ToString());
113+
context.Value.MeterTags.Add(SemanticConventions.AttributeErrorType, result.ErrorType.ToString());
114114
}
115115

116116
var description = result.ErrorMessage.IsEmpty ? null : result.ErrorMessage.ToString();
@@ -125,52 +125,74 @@ private void HandleHttpRequestStart(KustoUtils.TraceRecord record)
125125
var operationName = record.Activity.ActivityType;
126126

127127
var activity = KustoActivitySourceHelper.ActivitySource.StartActivity(operationName, ActivityKind.Client);
128-
var tagList = default(TagList);
128+
var meterTags = default(TagList);
129129

130130
if (ShouldComputeTags(activity))
131131
{
132132
activity?.DisplayName = operationName;
133133
activity?.AddTag(KustoActivitySourceHelper.ClientRequestIdTagKey, record.Activity.ClientRequestId.ToString());
134134

135-
tagList.Add(SemanticConventions.AttributeDbSystemName, KustoActivitySourceHelper.DbSystem);
136-
tagList.Add(SemanticConventions.AttributeDbOperationName, operationName);
135+
activity?.AddTag(SemanticConventions.AttributeDbSystemName, KustoActivitySourceHelper.DbSystem);
136+
activity?.AddTag(SemanticConventions.AttributeDbOperationName, operationName);
137+
meterTags.Add(SemanticConventions.AttributeDbSystemName, KustoActivitySourceHelper.DbSystem);
138+
meterTags.Add(SemanticConventions.AttributeDbOperationName, operationName);
137139

138140
var result = TraceRecordParser.ParseRequestStart(record.Message.AsSpan());
139141

140142
if (!string.IsNullOrEmpty(result.ServerAddress))
141143
{
142-
tagList.Add(SemanticConventions.AttributeServerAddress, result.ServerAddress);
144+
activity?.AddTag(SemanticConventions.AttributeServerAddress, result.ServerAddress);
145+
meterTags.Add(SemanticConventions.AttributeServerAddress, result.ServerAddress);
143146
}
144147

145148
if (result.ServerPort is not null)
146149
{
147-
tagList.Add(SemanticConventions.AttributeServerPort, result.ServerPort.Value);
150+
activity?.AddTag(SemanticConventions.AttributeServerPort, result.ServerPort.Value);
151+
meterTags.Add(SemanticConventions.AttributeServerPort, result.ServerPort.Value);
148152
}
149153

150154
if (!result.Database.IsEmpty)
151155
{
152-
tagList.Add(SemanticConventions.AttributeDbNamespace, result.Database.ToString());
156+
activity?.AddTag(SemanticConventions.AttributeDbNamespace, result.Database.ToString());
157+
meterTags.Add(SemanticConventions.AttributeDbNamespace, result.Database.ToString());
153158
}
154159

155160
if (!result.QueryText.IsEmpty)
156161
{
157-
var info = KustoProcessor.Process(shouldSummarize: KustoInstrumentation.Options.RecordQuerySummary, shouldSanitize: KustoInstrumentation.Options.RecordQueryText, result.QueryText.ToString());
162+
var shouldSummarize = KustoInstrumentation.TraceOptions.RecordQuerySummary || KustoInstrumentation.MeterOptions.RecordQuerySummary;
163+
var shouldSanitize = KustoInstrumentation.TraceOptions.RecordQueryText || KustoInstrumentation.MeterOptions.RecordQueryText;
164+
var info = KustoProcessor.Process(shouldSummarize, shouldSanitize, result.QueryText.ToString());
158165

159-
if (KustoInstrumentation.Options.RecordQueryText)
166+
if (!string.IsNullOrEmpty(info.Sanitized))
160167
{
161-
tagList.Add(SemanticConventions.AttributeDbQueryText, info.Sanitized);
168+
if (KustoInstrumentation.TraceOptions.RecordQueryText)
169+
{
170+
activity?.AddTag(SemanticConventions.AttributeDbQueryText, info.Sanitized);
171+
}
172+
173+
if (KustoInstrumentation.MeterOptions.RecordQueryText)
174+
{
175+
meterTags.Add(SemanticConventions.AttributeDbQueryText, info.Sanitized);
176+
}
162177
}
163178

164-
// Set query summary and use it as display name per spec
165179
if (!string.IsNullOrEmpty(info.Summarized))
166180
{
167-
tagList.Add(SemanticConventions.AttributeDbQuerySummary, info.Summarized);
168-
activity?.DisplayName = info.Summarized!;
181+
if (KustoInstrumentation.TraceOptions.RecordQuerySummary)
182+
{
183+
activity?.AddTag(SemanticConventions.AttributeDbQuerySummary, info.Summarized);
184+
activity?.DisplayName = info.Summarized!;
185+
}
186+
187+
if (KustoInstrumentation.MeterOptions.RecordQuerySummary)
188+
{
189+
meterTags.Add(SemanticConventions.AttributeDbQuerySummary, info.Summarized);
190+
}
169191
}
170192
}
171193
}
172194

173-
this.contexts[record.Activity.ActivityId] = new ContextData(beginTimestamp, tagList, activity!);
195+
this.contexts[record.Activity.ActivityId] = new ContextData(beginTimestamp, meterTags, activity!);
174196

175197
this.CallEnrichment(record);
176198
}
@@ -191,12 +213,11 @@ private void HandleActivityComplete(KustoUtils.TraceRecord record)
191213
activity?.SetStatus(ActivityStatusCode.Ok);
192214
}
193215

194-
activity?.AddTags(context.Value.Tags);
195216
this.CallEnrichment(record);
196217
activity?.Stop();
197218

198219
var duration = activity?.Duration.TotalSeconds ?? GetElapsedTime(context.Value.BeginTimestamp);
199-
KustoActivitySourceHelper.OperationDurationHistogram.Record(duration, context.Value.Tags);
220+
KustoActivitySourceHelper.OperationDurationHistogram.Record(duration, context.Value.MeterTags);
200221

201222
this.contexts.TryRemove(record.Activity.ActivityId, out _);
202223
}
@@ -217,10 +238,10 @@ private void HandleActivityComplete(KustoUtils.TraceRecord record)
217238
/// </summary>
218239
private readonly struct ContextData
219240
{
220-
public ContextData(long beginTimestamp, TagList tags, Activity activity)
241+
public ContextData(long beginTimestamp, TagList meterTags, Activity activity)
221242
{
222243
this.BeginTimestamp = beginTimestamp;
223-
this.Tags = tags;
244+
this.MeterTags = meterTags;
224245
this.Activity = activity;
225246
}
226247

@@ -231,9 +252,9 @@ public ContextData(long beginTimestamp, TagList tags, Activity activity)
231252
public long BeginTimestamp { get; }
232253

233254
/// <summary>
234-
/// Gets the collection of tags associated with the operation that should be shared between the span and metrics.
255+
/// Gets the collection of tags associated with the operation that should be applies to metrics.
235256
/// </summary>
236-
public TagList Tags { get; }
257+
public TagList MeterTags { get; }
237258

238259
/// <summary>
239260
/// Gets the current activity associated with the instance, if any.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
namespace OpenTelemetry.Metrics;
5+
6+
/// <summary>
7+
/// Options for Kusto meter instrumentation.
8+
/// </summary>
9+
public sealed class KustoMeterInstrumentationOptions
10+
{
11+
/// <summary>
12+
/// Gets or sets a value indicating whether the query text should be recorded as an attribute on the activity.
13+
/// Default is <see langword="false"/>.
14+
/// </summary>
15+
public bool RecordQueryText { get; set; }
16+
17+
/// <summary>
18+
/// Gets or sets a value indicating whether a summary of the query should be recorded as an attribute on the activity.
19+
/// Default is <see langword="true"/>.
20+
/// </summary>
21+
public bool RecordQuerySummary { get; set; } = true;
22+
}

src/OpenTelemetry.Instrumentation.Kusto/KustoInstrumentationOptions.cs renamed to src/OpenTelemetry.Instrumentation.Kusto/KustoTraceInstrumentationOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
using System.Diagnostics;
55
using KustoUtils = Kusto.Cloud.Platform.Utils;
66

7-
namespace OpenTelemetry.Instrumentation.Kusto;
7+
namespace OpenTelemetry.Trace;
88

99
/// <summary>
10-
/// Options for Kusto instrumentation.
10+
/// Options for Kusto trace instrumentation.
1111
/// </summary>
12-
public class KustoInstrumentationOptions
12+
public sealed class KustoTraceInstrumentationOptions
1313
{
1414
/// <summary>
1515
/// Gets or sets a value indicating whether the query text should be recorded as an attribute on the activity.

0 commit comments

Comments
 (0)