-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Telemetry improvements for Plan and SKFunction (#2180)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Based on PR: #2152. Should fix: #2151 This PR contains changes to move telemetry logic from `Plan` class to separate class-decorator and add telemetry for `SKFunction` using decorator pattern. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> 1. Added interface `IPlan` which inherits from `ISKFunction` to decouple `Plan` and `SKFunction` instrumentation and future decorations. 2. Implemented `InstrumentedPlan` with telemetry for `InvokeAsync` methods. 3. Implemented `InstrumentedSKFunction` with telemetry for `InvokeAsync` methods. 4. Added extension methods to create instrumented instances of plans and functions. 5. Added additional counters for Plan/Function to measure number of total, successful and failed Plan/Function executions. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
- Loading branch information
1 parent
32447c0
commit 33f88a1
Showing
7 changed files
with
403 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.SemanticKernel.SkillDefinition; | ||
|
||
namespace Microsoft.SemanticKernel.Planning; | ||
|
||
/// <summary> | ||
/// Interface for standard Semantic Kernel callable plan. | ||
/// </summary> | ||
public interface IPlan : ISKFunction | ||
{ | ||
} |
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,169 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Diagnostics.Metrics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.SemanticKernel.AI.TextCompletion; | ||
using Microsoft.SemanticKernel.Orchestration; | ||
using Microsoft.SemanticKernel.SkillDefinition; | ||
|
||
namespace Microsoft.SemanticKernel.Planning; | ||
|
||
/// <summary> | ||
/// Standard Semantic Kernel callable plan with instrumentation. | ||
/// </summary> | ||
public sealed class InstrumentedPlan : IPlan | ||
{ | ||
/// <inheritdoc/> | ||
public string Name => this._plan.Name; | ||
|
||
/// <inheritdoc/> | ||
public string SkillName => this._plan.SkillName; | ||
|
||
/// <inheritdoc/> | ||
public string Description => this._plan.Description; | ||
|
||
/// <inheritdoc/> | ||
public bool IsSemantic => this._plan.IsSemantic; | ||
|
||
/// <inheritdoc/> | ||
public CompleteRequestSettings RequestSettings => this._plan.RequestSettings; | ||
|
||
/// <summary> | ||
/// Initialize a new instance of the <see cref="InstrumentedPlan"/> class. | ||
/// </summary> | ||
/// <param name="plan">Instance of <see cref="IPlan"/> to decorate.</param> | ||
/// <param name="logger">Optional logger.</param> | ||
public InstrumentedPlan( | ||
IPlan plan, | ||
ILogger? logger = null) | ||
{ | ||
this._plan = plan; | ||
this._logger = logger ?? NullLogger.Instance; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public FunctionView Describe() | ||
{ | ||
return this._plan.Describe(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<SKContext> InvokeAsync( | ||
SKContext context, | ||
CompleteRequestSettings? settings = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return await this.InvokeWithInstrumentationAsync(() => | ||
this._plan.InvokeAsync(context, settings, cancellationToken)).ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<SKContext> InvokeAsync( | ||
string? input = null, | ||
CompleteRequestSettings? settings = null, | ||
ILogger? logger = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return await this.InvokeWithInstrumentationAsync(() => | ||
this._plan.InvokeAsync(input, settings, logger ?? this._logger, cancellationToken)).ConfigureAwait(false); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public ISKFunction SetAIConfiguration(CompleteRequestSettings settings) => | ||
this._plan.SetAIConfiguration(settings); | ||
|
||
/// <inheritdoc/> | ||
public ISKFunction SetAIService(Func<ITextCompletion> serviceFactory) => | ||
this._plan.SetAIService(serviceFactory); | ||
|
||
/// <inheritdoc/> | ||
public ISKFunction SetDefaultSkillCollection(IReadOnlySkillCollection skills) => | ||
this._plan.SetDefaultSkillCollection(skills); | ||
|
||
#region private ================================================================================ | ||
|
||
private readonly IPlan _plan; | ||
private readonly ILogger _logger; | ||
|
||
/// <summary> | ||
/// Instance of <see cref="Meter"/> for plan-related metrics. | ||
/// </summary> | ||
private static Meter s_meter = new(typeof(Plan).FullName); | ||
|
||
/// <summary> | ||
/// Instance of <see cref="Histogram{T}"/> to measure and track the time of plan execution. | ||
/// </summary> | ||
private static Histogram<double> s_executionTimeHistogram = | ||
s_meter.CreateHistogram<double>( | ||
name: "SK.Plan.Execution.ExecutionTime", | ||
unit: "ms", | ||
description: "Duration of plan execution"); | ||
|
||
/// <summary> | ||
/// Instance of <see cref="Counter{T}"/> to keep track of the total number of plan executions. | ||
/// </summary> | ||
private static Counter<int> s_executionTotalCounter = | ||
s_meter.CreateCounter<int>( | ||
name: "SK.Plan.Execution.ExecutionTotal", | ||
description: "Total number of plan executions"); | ||
|
||
/// <summary> | ||
/// Instance of <see cref="Counter{T}"/> to keep track of the number of successful plan executions. | ||
/// </summary> | ||
private static Counter<int> s_executionSuccessCounter = | ||
s_meter.CreateCounter<int>( | ||
name: "SK.Plan.Execution.ExecutionSuccess", | ||
description: "Number of successful plan executions"); | ||
|
||
/// <summary> | ||
/// Instance of <see cref="Counter{T}"/> to keep track of the number of failed plan executions. | ||
/// </summary> | ||
private static Counter<int> s_executionFailureCounter = | ||
s_meter.CreateCounter<int>( | ||
name: "SK.Plan.Execution.ExecutionFailure", | ||
description: "Number of failed plan executions"); | ||
|
||
/// <summary> | ||
/// Wrapper for instrumentation to be used in multiple invocation places. | ||
/// </summary> | ||
/// <param name="func">Delegate to instrument.</param> | ||
private async Task<SKContext> InvokeWithInstrumentationAsync(Func<Task<SKContext>> func) | ||
{ | ||
this._logger.LogInformation("Plan execution started."); | ||
|
||
var stopwatch = new Stopwatch(); | ||
|
||
stopwatch.Start(); | ||
|
||
var result = await func().ConfigureAwait(false); | ||
|
||
stopwatch.Stop(); | ||
|
||
if (result.ErrorOccurred) | ||
{ | ||
this._logger.LogWarning("Plan execution status: {Status}", "Failed"); | ||
this._logger.LogError(result.LastException, "Plan execution exception details: {Message}", result.LastErrorDescription); | ||
|
||
s_executionFailureCounter.Add(1); | ||
} | ||
else | ||
{ | ||
this._logger.LogInformation("Plan execution status: {Status}", "Success"); | ||
this._logger.LogInformation("Plan execution finished in {ExecutionTime}ms", stopwatch.ElapsedMilliseconds); | ||
|
||
s_executionSuccessCounter.Add(1); | ||
} | ||
|
||
s_executionTotalCounter.Add(1); | ||
s_executionTimeHistogram.Record(stopwatch.ElapsedMilliseconds); | ||
|
||
return result; | ||
} | ||
|
||
#endregion | ||
} |
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.