Skip to content

Commit

Permalink
Add more parameters to command execution metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
calebkiage committed Feb 21, 2025
1 parent 0753202 commit bf14bdf
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 45 deletions.
12 changes: 12 additions & 0 deletions src/kiota/Extension/TagListExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Diagnostics;

namespace kiota.Extension;

internal static class TagListExtensions
{
public static TagList AddAll(this TagList tagList, IEnumerable<KeyValuePair<string, object?>> tags)
{
foreach (var tag in tags) tagList.Add(tag);
return tagList;
}
}
7 changes: 4 additions & 3 deletions src/kiota/Handlers/Client/AddHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public override async Task<int> InvokeAsync(InvocationContext context)
var meterRuntime = instrumentation?.CreateCommandDurationHistogram();
if (meterRuntime is null) stopwatch = null;
// Add this run to the command execution counter
instrumentation?.CreateCommandExecutionCounter().Add(1, _commonTags);
var tl = new TagList(_commonTags.AsSpan()).AddAll(tags.OrEmpty());
instrumentation?.CreateCommandExecutionCounter().Add(1, tl);

List<string> includePatterns = includePatterns0 ?? [];
List<string> excludePatterns = excludePatterns0 ?? [];
Expand Down Expand Up @@ -202,7 +203,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
}
finally
{
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, _commonTags);
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, tl);
}
}
}
Expand All @@ -213,7 +214,7 @@ private static void CreateTelemetryTags(ActivitySource? activitySource, Generati
out List<KeyValuePair<string, object?>>? tags)
{
// set up telemetry tags
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(16)
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(10)
{
new(TelemetryLabels.TagGeneratorLanguage, language.ToString("G")),
// new($"{TelemetryLabels.TagCommandParams}.type_access_modifier", typeAccessModifier.ToString("G")),
Expand Down
9 changes: 4 additions & 5 deletions src/kiota/Handlers/Client/EditHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ internal class EditHandler : BaseKiotaCommandHandler
new(TelemetryLabels.TagCommandName, "edit"),
new(TelemetryLabels.TagCommandRevision, 1)
];

public required Option<string> ClassOption
{
get; init;
Expand Down Expand Up @@ -111,15 +110,15 @@ public override async Task<int> InvokeAsync(InvocationContext context)

CreateTelemetryTags(activitySource, language, backingStore, excludeBackwardCompatible, skipGeneration, output0,
namespaceName0, includePatterns, excludePatterns, structuredMimeTypes, logLevel, out var tags);

// Start span
using var invokeActivity = activitySource?.StartActivity(TelemetryLabels.SpanEditClientCommand,
ActivityKind.Internal, startTime: startTime, parentContext: default,
tags: _commonTags.ConcatNullable(tags)?.Concat(Telemetry.Telemetry.GetThreadTags()));
var meterRuntime = instrumentation?.CreateCommandDurationHistogram();
if (meterRuntime is null) stopwatch = null;
// Add this run to the command execution counter
instrumentation?.CreateCommandExecutionCounter().Add(1, _commonTags);
var tl = new TagList(_commonTags.AsSpan()).AddAll(tags.OrEmpty());
instrumentation?.CreateCommandExecutionCounter().Add(1, tl);

Configuration.Generation.SkipGeneration = skipGeneration;
Configuration.Generation.Operation = ConsumerOperation.Edit;
Expand Down Expand Up @@ -223,7 +222,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
}
finally
{
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, _commonTags);
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, tl);
}
}
}
Expand All @@ -234,7 +233,7 @@ private static void CreateTelemetryTags(ActivitySource? activitySource, Generati
out List<KeyValuePair<string, object?>>? tags)
{
// set up telemetry tags
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(16) : null;
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(10) : null;
if (language is { } l) tags?.Add(new KeyValuePair<string, object?>(TelemetryLabels.TagGeneratorLanguage, l.ToString("G")));
// if (typeAccessModifier is { } tam) tags?.Add(new KeyValuePair<string, object?>($"{TelemetryLabels.TagCommandParams}.type_access_modifier", tam.ToString("G")));

Check warning on line 238 in src/kiota/Handlers/Client/EditHandler.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this commented out code. (https://rules.sonarsource.com/csharp/RSPEC-125)
if (backingStore is { } bs) tags?.Add(new KeyValuePair<string, object?>($"{TelemetryLabels.TagCommandParams}.backing_store", bs));
Expand Down
9 changes: 5 additions & 4 deletions src/kiota/Handlers/Client/GenerateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ public override async Task<int> InvokeAsync(InvocationContext context)
var activitySource = instrumentation?.ActivitySource;

CreateTelemetryTags(activitySource, refresh, className0, logLevel, out var tags);

// Start span
using var invokeActivity = activitySource?.StartActivity(TelemetryLabels.SpanGenerateClientCommand,
ActivityKind.Internal, startTime: startTime, parentContext: default,
tags: _commonTags.ConcatNullable(tags)?.Concat(Telemetry.Telemetry.GetThreadTags()));
var meterRuntime = instrumentation?.CreateCommandDurationHistogram();
if (meterRuntime is null) stopwatch = null;
// Add this run to the command execution counter
instrumentation?.CreateCommandExecutionCounter().Add(1, _commonTags);
var tl = new TagList(_commonTags.AsSpan()).AddAll(tags.OrEmpty());
instrumentation?.CreateCommandExecutionCounter().Add(1, tl);

var className = className0 ?? string.Empty;
var (loggerFactory, logger) = GetLoggerAndFactory<KiotaBuilder>(context, Configuration.Generation.OutputPath);
Expand Down Expand Up @@ -129,7 +130,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
}
finally
{
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, _commonTags);
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, tl);
}
}
}
Expand All @@ -138,7 +139,7 @@ private static void CreateTelemetryTags(ActivitySource? activitySource, bool ref
out List<KeyValuePair<string, object?>>? tags)
{
// set up telemetry tags
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(5)
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(3)
{
new($"{TelemetryLabels.TagCommandParams}.refresh", refresh),
} : null;
Expand Down
9 changes: 5 additions & 4 deletions src/kiota/Handlers/Client/RemoveHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ public override async Task<int> InvokeAsync(InvocationContext context)
var activitySource = instrumentation?.ActivitySource;

CreateTelemetryTags(activitySource, cleanOutput, className0, logLevel, out var tags);

// Start span
using var invokeActivity = activitySource?.StartActivity(TelemetryLabels.SpanRemoveClientCommand,
ActivityKind.Internal, startTime: startTime, parentContext: default,
tags: _commonTags.ConcatNullable(tags)?.Concat(Telemetry.Telemetry.GetThreadTags()));
var meterRuntime = instrumentation?.CreateCommandDurationHistogram();
if (meterRuntime is null) stopwatch = null;
// Add this run to the command execution counter
instrumentation?.CreateCommandExecutionCounter().Add(1, _commonTags);
var tl = new TagList(_commonTags.AsSpan()).AddAll(tags.OrEmpty());
instrumentation?.CreateCommandExecutionCounter().Add(1, tl);

string className = className0 ?? string.Empty;
using (loggerFactory)
Expand Down Expand Up @@ -79,7 +80,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
}
finally
{
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, _commonTags);
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, tl);
}
}
}
Expand All @@ -88,7 +89,7 @@ private static void CreateTelemetryTags(ActivitySource? activitySource, bool cle
out List<KeyValuePair<string, object?>>? tags)
{
// set up telemetry tags
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(5)
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(3)
{
new($"{TelemetryLabels.TagCommandParams}.clean_output", cleanOutput),
} : null;
Expand Down
7 changes: 4 additions & 3 deletions src/kiota/Handlers/KiotaDownloadCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public override async Task<int> InvokeAsync(InvocationContext context)
var meterRuntime = instrumentation?.CreateCommandDurationHistogram();
if (meterRuntime is null) stopwatch = null;
// Add this run to the command execution counter
instrumentation?.CreateCommandExecutionCounter().Add(1, _commonTags);
var tl = new TagList(_commonTags.AsSpan()).AddAll(tags.OrEmpty());
instrumentation?.CreateCommandExecutionCounter().Add(1, tl);

string outputPath = outputPath0.OrEmpty();
string version = version0.OrEmpty();
Expand Down Expand Up @@ -113,7 +114,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
}
finally
{
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, _commonTags);
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, tl);
}
}
}
Expand Down Expand Up @@ -200,7 +201,7 @@ private static void CreateTelemetryTags(ActivitySource? activitySource, string s
{
// set up telemetry tags
const string redacted = TelemetryLabels.RedactedValuePlaceholder;
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(16)
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(7)
{
// TODO: value is always the same. Is collection useful?

Check warning on line 206 in src/kiota/Handlers/KiotaDownloadCommandHandler.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
new($"{TelemetryLabels.TagCommandParams}.search_term", redacted),
Expand Down
7 changes: 4 additions & 3 deletions src/kiota/Handlers/KiotaGenerateCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public override async Task<int> InvokeAsync(InvocationContext context)
var meterRuntime = instrumentation?.CreateCommandDurationHistogram();
if (meterRuntime is null) stopwatch = null;
// Add this run to the command execution counter
instrumentation?.CreateCommandExecutionCounter().Add(1, _commonTags);
var tl = new TagList(_commonTags.AsSpan()).AddAll(tags.OrEmpty());
instrumentation?.CreateCommandExecutionCounter().Add(1, tl);

List<string> includePatterns = includePatterns0 ?? [];
List<string> excludePatterns = excludePatterns0 ?? [];
Expand Down Expand Up @@ -208,7 +209,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
}
finally
{
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, _commonTags);
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, tl);
}
}
}
Expand Down Expand Up @@ -244,7 +245,7 @@ private static void CreateTelemetryTags(ActivitySource? activitySource, Generati
List<string>? structuredMimeTypes, LogLevel? logLevel, out List<KeyValuePair<string, object?>>? tags)
{
// set up telemetry tags
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(16)
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(12)
{
new(TelemetryLabels.TagGeneratorLanguage, language.ToString("G")),
// new($"{TelemetryLabels.TagCommandParams}.type_access_modifier", typeAccessModifier.ToString("G")),
Expand Down
7 changes: 4 additions & 3 deletions src/kiota/Handlers/KiotaGitHubDeviceLoginCommandHanlder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public override async Task<int> InvokeAsync(InvocationContext context)
var meterRuntime = instrumentation?.CreateCommandDurationHistogram();
if (meterRuntime is null) stopwatch = null;
// Add this run to the command execution counter
instrumentation?.CreateCommandExecutionCounter().Add(1, _commonTags);
var tl = new TagList(_commonTags.AsSpan()).AddAll(tags.OrEmpty());
instrumentation?.CreateCommandExecutionCounter().Add(1, tl);

var (loggerFactory, logger) = GetLoggerAndFactory<DeviceCodeAuthenticationProvider>(context);
using (loggerFactory)
Expand All @@ -68,7 +69,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
}
finally
{
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, _commonTags);
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, tl);
}
}
}
Expand Down Expand Up @@ -122,7 +123,7 @@ private static void CreateTelemetryTags(ActivitySource? activitySource, LogLevel
out List<KeyValuePair<string, object?>>? tags)
{
// set up telemetry tags
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(16) : null;
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(1) : null;
if (logLevel is { } ll) tags?.Add(new KeyValuePair<string, object?>($"{TelemetryLabels.TagCommandParams}.log_level", ll.ToString("G")));
}
}
7 changes: 4 additions & 3 deletions src/kiota/Handlers/KiotaGitHubLogoutCommandhandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public override async Task<int> InvokeAsync(InvocationContext context)
var meterRuntime = instrumentation?.CreateCommandDurationHistogram();
if (meterRuntime is null) stopwatch = null;
// Add this run to the command execution counter
instrumentation?.CreateCommandExecutionCounter().Add(1, _commonTags);
var tl = new TagList(_commonTags.AsSpan()).AddAll(tags.OrEmpty());
instrumentation?.CreateCommandExecutionCounter().Add(1, tl);

var (loggerFactory, logger) = GetLoggerAndFactory<TempFolderCachingAccessTokenProvider>(context);
using (loggerFactory)
Expand Down Expand Up @@ -70,7 +71,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
}
finally
{
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, _commonTags);
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, tl);
}
}
}
Expand All @@ -79,7 +80,7 @@ private static void CreateTelemetryTags(ActivitySource? activitySource, LogLevel
out List<KeyValuePair<string, object?>>? tags)
{
// set up telemetry tags
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(16) : null;
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(1) : null;
if (logLevel is { } ll) tags?.Add(new KeyValuePair<string, object?>($"{TelemetryLabels.TagCommandParams}.log_level", ll.ToString("G")));
}
}
7 changes: 4 additions & 3 deletions src/kiota/Handlers/KiotaGitHubPatLoginCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public override async Task<int> InvokeAsync(InvocationContext context)
var meterRuntime = instrumentation?.CreateCommandDurationHistogram();
if (meterRuntime is null) stopwatch = null;
// Add this run to the command execution counter
instrumentation?.CreateCommandExecutionCounter().Add(1, _commonTags);
var tl = new TagList(_commonTags.AsSpan()).AddAll(tags.OrEmpty());
instrumentation?.CreateCommandExecutionCounter().Add(1, tl);

var (loggerFactory, logger) = GetLoggerAndFactory<PatAuthenticationProvider>(context);
using (loggerFactory)
Expand All @@ -69,7 +70,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
}
finally
{
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, _commonTags);
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, tl);
}
}
}
Expand All @@ -91,7 +92,7 @@ private static void CreateTelemetryTags(ActivitySource? activitySource, LogLevel
out List<KeyValuePair<string, object?>>? tags)
{
// set up telemetry tags
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(16) : null;
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(1) : null;
if (logLevel is { } ll) tags?.Add(new KeyValuePair<string, object?>($"{TelemetryLabels.TagCommandParams}.log_level", ll.ToString("G")));
}
}
7 changes: 4 additions & 3 deletions src/kiota/Handlers/Plugin/AddHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public override async Task<int> InvokeAsync(InvocationContext context)
var meterRuntime = instrumentation?.CreateCommandDurationHistogram();
if (meterRuntime is null) stopwatch = null;
// Add this run to the command execution counter
instrumentation?.CreateCommandExecutionCounter().Add(1, _commonTags);
var tl = new TagList(_commonTags.AsSpan()).AddAll(tags.OrEmpty());
instrumentation?.CreateCommandExecutionCounter().Add(1, tl);

AssignIfNotNullOrEmpty(output, (c, s) => c.OutputPath = s);
AssignIfNotNullOrEmpty(openapi, (c, s) => c.OpenAPIFilePath = s);
Expand Down Expand Up @@ -157,7 +158,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
}
finally
{
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, _commonTags);
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, tl);
}
}
}
Expand All @@ -168,7 +169,7 @@ private static void CreateTelemetryTags(ActivitySource? activitySource, List<Plu
out List<KeyValuePair<string, object?>>? tags)
{
// set up telemetry tags
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(16)
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(8)
{
new($"{TelemetryLabels.TagCommandParams}.skip_generation", skipGeneration),
} : null;
Expand Down
7 changes: 4 additions & 3 deletions src/kiota/Handlers/Plugin/EditHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public override async Task<int> InvokeAsync(InvocationContext context)
var meterRuntime = instrumentation?.CreateCommandDurationHistogram();
if (meterRuntime is null) stopwatch = null;
// Add this run to the command execution counter
instrumentation?.CreateCommandExecutionCounter().Add(1, _commonTags);
var tl = new TagList(_commonTags.AsSpan()).AddAll(tags.OrEmpty());
instrumentation?.CreateCommandExecutionCounter().Add(1, tl);

var className = className0.OrEmpty();
var pluginAuthRefId = pluginAuthRefId0.OrEmpty();
Expand Down Expand Up @@ -184,7 +185,7 @@ public override async Task<int> InvokeAsync(InvocationContext context)
}
finally
{
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, _commonTags);
if (stopwatch is not null) meterRuntime?.Record(stopwatch.Elapsed.TotalSeconds, tl);
}
}
}
Expand All @@ -195,7 +196,7 @@ private static void CreateTelemetryTags(ActivitySource? activitySource, List<Plu
out List<KeyValuePair<string, object?>>? tags)
{
// set up telemetry tags
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(16)
tags = activitySource?.HasListeners() == true ? new List<KeyValuePair<string, object?>>(8)
{
new($"{TelemetryLabels.TagCommandParams}.skip_generation", skipGeneration),
} : null;
Expand Down
Loading

0 comments on commit bf14bdf

Please sign in to comment.