Skip to content

Commit

Permalink
Exporter: add TrimPathPrefixes option
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrogers committed Jun 28, 2023
1 parent 41998d3 commit ca404ce
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<Target Name="AddNuGetPackageVersionToThisAssembly" DependsOnTargets="GetBuildVersion;InitializeSourceControlInformationFromSourceControlManager" BeforeTargets="GenerateAssemblyNBGVVersionInfo">
<ItemGroup>
<AdditionalThisAssemblyFields Include="NuGetPackageVersion" String="$(NuGetPackageVersion)" />
<AdditionalThisAssemblyFields Include="SourceRoot" String="@(SourceRoot->Replace('\', '\\'))" Condition=" '%(SourceRoot.SourceControl)' == 'git' " />
</ItemGroup>
</Target>

</Project>
13 changes: 13 additions & 0 deletions XO.Diagnostics.Bugsnag.OpenTelemetry/BugsnagExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,19 @@ private void AddNotifyEventExceptionStacktrace(NotifyEvent notifyEvent, Activity
if (IsInProject(activityEvent, file.Value, method.Value))
stacktraceLine.InProject = true;

// trim file paths for in-project lines
if (stacktraceLine.InProject == true && file.Success)
{
foreach (var prefix in _options.TrimPathPrefixes)
{
if (stacktraceLine.File.StartsWith(prefix))
{
stacktraceLine.File = file.Value.Substring(prefix.Length);
break;
}
}
}

notifyEventException.Stacktrace.Add(stacktraceLine);
}
else if (!frames)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public sealed class BugsnagExporterOptions
= static (_, _, _, inProjectNamespace) => inProjectNamespace;

public string[]? ProjectNamespaces { get; set; }

public string[] TrimPathPrefixes { get; set; } = Array.Empty<string>();
}
24 changes: 24 additions & 0 deletions XO.Diagnostics.Tests/Bugsnag/BugsnagExporterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,30 @@ public async Task Export_ReportsSessionPerRootActivity_WithNestedActivity()
() => Assert.Empty(_notifyEvents));
}

[Fact]
public async Task Export_TrimsPaths()
{
ExporterOptions.Value.ProjectNamespaces = new[] {
this.GetType().Namespace!,
};
ExporterOptions.Value.TrimPathPrefixes = new[] {
"/_/",
ThisAssembly.SourceRoot,
};

using var host = await StartHostAsync();
using var tracerProvider = CreateTracerProvider(host);

_ = CreateActivity("foo", _ => throw new InvalidOperationException());

var notifyEvent = Assert.Single(_notifyEvents);
var notifyEventException = Assert.Single(notifyEvent.Exceptions);

Assert.Contains(
notifyEventException.Stacktrace,
stacktraceLine => stacktraceLine.InProject == true && stacktraceLine.File.StartsWith("XO.Diagnostics.Tests"));
}

private static Activity CreateActivity(string name, Action<Activity>? configure = null)
{
var activity = Source.StartActivity(name);
Expand Down
1 change: 1 addition & 0 deletions XO.Diagnostics.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.gitignore = .gitignore
codecov.yml = codecov.yml
Directory.Build.props = Directory.Build.props
Directory.Build.targets = Directory.Build.targets
global.json = global.json
LICENSE.txt = LICENSE.txt
nuget.config = nuget.config
Expand Down

0 comments on commit ca404ce

Please sign in to comment.