| | | 1 | | using Kestrun.Languages; |
| | | 2 | | using Kestrun.SharedState; |
| | | 3 | | using Microsoft.CodeAnalysis; |
| | | 4 | | using Microsoft.CodeAnalysis.Scripting; |
| | | 5 | | using RoslynCompilationErrorException = Microsoft.CodeAnalysis.Scripting.CompilationErrorException; |
| | | 6 | | using SerilogLogger = Serilog.ILogger; |
| | | 7 | | using Serilog.Events; |
| | | 8 | | |
| | | 9 | | namespace Kestrun.Health; |
| | | 10 | | /// <summary> |
| | | 11 | | /// A health probe implemented via a C# script. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="name">The name of the probe.</param> |
| | | 14 | | /// <param name="tags">The tags associated with the probe.</param> |
| | | 15 | | /// <param name="runner">The script runner to execute the probe.</param> |
| | | 16 | | /// <param name="locals">The local variables for the script.</param> |
| | | 17 | | /// <param name="logger">The logger to use for logging.</param> |
| | | 18 | | internal sealed class CSharpScriptProbe( |
| | | 19 | | string name, |
| | | 20 | | IEnumerable<string>? tags, |
| | | 21 | | ScriptRunner<ProbeResult> runner, |
| | | 22 | | IReadOnlyDictionary<string, object?>? locals, |
| | 0 | 23 | | SerilogLogger logger) : Probe(name, tags, logger), IProbe |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// The script runner to execute the probe. |
| | | 27 | | /// </summary> |
| | 0 | 28 | | private readonly ScriptRunner<ProbeResult> _runner = runner ?? throw new ArgumentNullException(nameof(runner)); |
| | | 29 | | /// <summary> |
| | | 30 | | /// The local variables for the script. |
| | | 31 | | /// </summary> |
| | 0 | 32 | | private readonly IReadOnlyDictionary<string, object?>? _locals = locals; |
| | | 33 | | |
| | | 34 | | /// <inheritdoc/> |
| | | 35 | | public override async Task<ProbeResult> CheckAsync(CancellationToken ct = default) |
| | | 36 | | { |
| | 0 | 37 | | var globals = _locals is { Count: > 0 } |
| | 0 | 38 | | ? new CsGlobals(SharedStateStore.Snapshot(), _locals) |
| | 0 | 39 | | : new CsGlobals(SharedStateStore.Snapshot()); |
| | | 40 | | try |
| | | 41 | | { |
| | 0 | 42 | | if (Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 43 | | { |
| | 0 | 44 | | Logger.Debug("CSharpScriptProbe {Probe} executing", Name); |
| | | 45 | | } |
| | 0 | 46 | | var result = await _runner(globals, ct).ConfigureAwait(false) |
| | 0 | 47 | | ?? new ProbeResult(ProbeStatus.Unhealthy, "Script returned null result"); |
| | 0 | 48 | | if (Logger.IsEnabled(LogEventLevel.Debug)) |
| | | 49 | | { |
| | 0 | 50 | | Logger.Debug("CSharpScriptProbe {Probe} completed status={Status}", Name, result.Status); |
| | | 51 | | } |
| | 0 | 52 | | return result; |
| | | 53 | | } |
| | 0 | 54 | | catch (OperationCanceledException) when (ct.IsCancellationRequested) |
| | | 55 | | { |
| | 0 | 56 | | throw; |
| | | 57 | | } |
| | 0 | 58 | | catch (RoslynCompilationErrorException ex) |
| | | 59 | | { |
| | 0 | 60 | | Logger.Error(ex, "C# health probe {Probe} failed to execute.", Name); |
| | 0 | 61 | | return new ProbeResult(ProbeStatus.Unhealthy, string.Join("; ", ex.Diagnostics.Select(static d => d.GetMessa |
| | | 62 | | } |
| | 0 | 63 | | catch (Exception ex) |
| | | 64 | | { |
| | 0 | 65 | | Logger.Error(ex, "C# health probe {Probe} threw an exception.", Name); |
| | 0 | 66 | | return new ProbeResult(ProbeStatus.Unhealthy, $"Exception: {ex.Message}"); |
| | | 67 | | } |
| | 0 | 68 | | } |
| | | 69 | | } |