Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report ThreadPool stats #1399

Merged
merged 5 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Events no longer get dropped because of non-serializable contexts or attachments ([#1401](https://github.com/getsentry/sentry-dotnet/pull/1401))
- Add MemoryInfo to sentry event ([#1337](https://github.com/getsentry/sentry-dotnet/pull/1337))
- Report ThreadPool stats ([#1399](https://github.com/getsentry/sentry-dotnet/pull/1399))

## 3.12.2

Expand Down
17 changes: 16 additions & 1 deletion src/Sentry/Internal/MainSentryEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class MainSentryEventProcessor : ISentryEventProcessor
internal const string CultureInfoKey = "Current Culture";
internal const string CurrentUiCultureKey = "Current UI Culture";
internal const string MemoryInfoKey = "Memory Info";
internal const string ThreadPoolInfoKey = "ThreadPool Info";

private readonly Enricher _enricher;

Expand Down Expand Up @@ -56,6 +57,7 @@ public SentryEvent Process(SentryEvent @event)
}

AddMemoryInfo(@event.Contexts);
AddThreadPoolInfo(@event.Contexts);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bruno-garcia we only want that information for Events?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgive my ignorance, where else would we have it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe Lucas is referring to Transactions as the other possible place to add it.
We only run SentryEvent instances here so transactions won't be getting this information.

A few things go through when deciding:

  • Does this information help understand performance problems with the app?
  • Is it cheap to retrieve this information (as in resource intensiveness)?

If the answer is 'yes' to both, it's a good candidate. Ideally we add information that's relative to the transaction itself (like what happened during the duration of that transaction). Like: How many GC gen0/1/2 ran during the transaction, for example. As 'point in time' information alone within a transaction might not be as valuable.

Ultimately, this might be more useful only as Metrics. Which is something we'll look into adding in the future

if (@event.ServerName == null)
{
// Value set on the options take precedence over device name.
Expand Down Expand Up @@ -170,6 +172,20 @@ private void AddMemoryInfo(Contexts contexts)
#endif
}

private void AddThreadPoolInfo(Contexts contexts)
{
ThreadPool.GetMinThreads(out var minWorkerThreads, out var minCompletionPortThreads);
ThreadPool.GetMaxThreads(out var maxWorkerThreads, out var maxCompletionPortThreads);
ThreadPool.GetAvailableThreads(out var availableWorkerThreads, out var availableCompletionPortThreads);
contexts[ThreadPoolInfoKey] = new ThreadPoolInfo(
minWorkerThreads,
minCompletionPortThreads,
maxWorkerThreads,
maxCompletionPortThreads,
availableWorkerThreads,
availableCompletionPortThreads);
}

private static IDictionary<string, string>? CultureInfoToDictionary(CultureInfo cultureInfo)
{
var dic = new Dictionary<string, string>();
Expand All @@ -190,5 +206,4 @@ private void AddMemoryInfo(Contexts contexts)
return dic.Count > 0 ? dic : null;
}
}

}
45 changes: 45 additions & 0 deletions src/Sentry/Internal/ThreadPoolInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Text.Json;
using Sentry.Extensibility;

namespace Sentry
{
internal sealed class ThreadPoolInfo : IJsonSerializable
{
public ThreadPoolInfo(
int minWorkerThreads,
int minCompletionPortThreads,
int maxWorkerThreads,
int maxCompletionPortThreads,
int availableWorkerThreads,
int availableCompletionPortThreads)
{
MinWorkerThreads = minWorkerThreads;
MinCompletionPortThreads = minCompletionPortThreads;
MaxWorkerThreads = maxWorkerThreads;
MaxCompletionPortThreads = maxCompletionPortThreads;
AvailableWorkerThreads = availableWorkerThreads;
AvailableCompletionPortThreads = availableCompletionPortThreads;
}

public int MinWorkerThreads { get; }
public int MinCompletionPortThreads { get; }
public int MaxWorkerThreads { get; }
public int MaxCompletionPortThreads { get; }
public int AvailableWorkerThreads { get; }
public int AvailableCompletionPortThreads { get; }

public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger)
{
writer.WriteStartObject();

writer.WriteNumber("minWorkerThreads", MinWorkerThreads);
writer.WriteNumber("minCompletionPortThreads", MinCompletionPortThreads);
writer.WriteNumber("maxWorkerThreads", MaxWorkerThreads);
writer.WriteNumber("maxCompletionPortThreads", MaxCompletionPortThreads);
writer.WriteNumber("availableWorkerThreads", AvailableWorkerThreads);
writer.WriteNumber("availableCompletionPortThreads", AvailableCompletionPortThreads);

writer.WriteEndObject();
}
}
}
16 changes: 16 additions & 0 deletions test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ public void Process_SendDefaultPiiTrueIdEnvironmentDefault_UserNameSet()
Assert.Equal(Environment.UserName, evt.User.Username);
}

[Fact]
public void EnsureThreadPoolInfoExists()
{
var evt = new SentryEvent();

_fixture.SentryOptions.SendDefaultPii = true;
var sut = _fixture.GetSut();

_ = sut.Process(evt);
var info = (ThreadPoolInfo)evt.Contexts[MainSentryEventProcessor.ThreadPoolInfoKey];
Assert.NotEqual(0, info.MinWorkerThreads);
Assert.NotEqual(0, info.MinCompletionPortThreads);
Assert.NotEqual(0, info.MaxWorkerThreads);
Assert.NotEqual(0, info.MaxCompletionPortThreads);
}

#if NETCOREAPP3_1_OR_GREATER
[Fact]
public void EnsureMemoryInfoExists()
Expand Down