Skip to content

Commit

Permalink
Extend step activity with information about exception
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Nov 6, 2024
1 parent 6a3f07d commit face624
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/NScenario/StepExecutors/InstrumentationScenarioStepExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,44 @@ public InstrumentationScenarioStepExecutor(IScenarioStepExecutor scenarioStepExe
public async Task Step(string scenarioName, string stepDescription, Func<Task> action, StepContext stepContext)
{
using var activity = NScenarioInstrumentation.DiagnosticSource.StartActivity(name: stepDescription);
await _scenarioStepExecutorImplementation.Step(scenarioName, stepDescription, action, stepContext);
try
{
await _scenarioStepExecutorImplementation.Step(scenarioName, stepDescription, action, stepContext);
}
catch (Exception e)
{
TryToMarkActivityAsFailed(activity, e);
throw;
}
}

private static void TryToMarkActivityAsFailed(Activity? activity, Exception e)
{
if (activity != null)
{
activity.AddEvent(new ActivityEvent("exception",tags: new ActivityTagsCollection
{
["operation.status"] = "failed",
["exception.escaped"] = true,
["exception.type"] = e.GetType().ToString(),
["exception.message"] = e.Message,
["exception.stacktrace"] = e.StackTrace
}));
activity.SetStatus(ActivityStatusCode.Error, e.Message);
}
}

public async Task Step(string scenarioName, string stepDescription, Action action, StepContext stepContext)
{
using var activity = NScenarioInstrumentation.DiagnosticSource.StartActivity(name: stepDescription);
await _scenarioStepExecutorImplementation.Step(scenarioName, stepDescription, action, stepContext);
try
{
await _scenarioStepExecutorImplementation.Step(scenarioName, stepDescription, action, stepContext);
}
catch (Exception e)
{
TryToMarkActivityAsFailed(activity, e);
throw;
}
}
}

0 comments on commit face624

Please sign in to comment.