Skip to content

Commit d955f6f

Browse files
Added example of TracesSampler callback function to MVC Sample (#3593)
1 parent 49620c6 commit d955f6f

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

samples/Sentry.Samples.AspNetCore.Mvc/Controllers/HomeController.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ public IActionResult Index()
1313
return View();
1414
}
1515

16+
[HttpGet("[controller]/sampler")]
17+
public Task<string> Sampler()
18+
{
19+
// The sampling for this route is determined by the TraceSampler function configured in Program.cs when
20+
// initializing the Sentry SDK... here we just display the result of that sampling decision.
21+
var transaction = SentrySdk.GetSpan()?.GetTransaction();
22+
return Task.FromResult($"Sampled: {transaction?.IsSampled}");
23+
}
24+
1625
// GET /home/block/true or /home/block/false to observe events
1726
[HttpGet("[controller]/block/{block?}")]
1827
public async Task<string> Block([FromRoute] bool block)

samples/Sentry.Samples.AspNetCore.Mvc/Program.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Net;
22
using Samples.AspNetCore.Mvc;
3+
using Sentry.AspNetCore;
34
using Sentry.Extensibility;
45

56
var builder = WebApplication.CreateBuilder(args);
@@ -17,8 +18,6 @@
1718

1819
options.MaxBreadcrumbs = 200;
1920

20-
options.TracesSampleRate = 1.0;
21-
2221
// Set a proxy for outgoing HTTP connections
2322
options.HttpProxy = null; // new WebProxy("https://localhost:3128");
2423

@@ -31,7 +30,33 @@
3130
options.MaxQueueItems = 100;
3231
options.ShutdownTimeout = TimeSpan.FromSeconds(5);
3332

34-
options.TracesSampleRate = 1.0; // For production you may want to lower this to stay inside your quota
33+
options.TracesSampleRate = 1.0; // For production, you may want to lower this to stay inside your quota
34+
35+
// In addition to (or instead of) setting the TracesSampleRate to a fixed value, you can also define a function
36+
// that calculates the sample rate dynamically based on the current context.
37+
options.TracesSampler = ctx =>
38+
{
39+
// In distributed tracing scenarios, we may want to respect upstream sampling decisions... for example, if
40+
// the javascript frontend contains tracing instrumentation, a decision not to sample this request may
41+
// already have been made. In that case, we don't want to sample this request since it would result in an
42+
// incomplete trace.
43+
if (ctx.TransactionContext.IsParentSampled == false)
44+
{
45+
// If the parent transaction is not sampled, don't sample this one either
46+
return 0.0;
47+
}
48+
49+
// Only sample 30% of the requests to this /home/sampler route
50+
var httpPath = ctx.TryGetHttpPath();
51+
if (string.Equals(httpPath, "/home/sampler", StringComparison.OrdinalIgnoreCase))
52+
{
53+
return 0.3;
54+
}
55+
56+
// For all other routes, use the statically configured sample rate. Returning null here would achieve the
57+
// same thing (if the TraceSampler function returns null, the SDK falls back to the TracesSampleRate)
58+
return options.TracesSampleRate;
59+
};
3560

3661
// Configures the root scope
3762
options.ConfigureScope(s => s.SetTag("Always sent", "this tag"));

0 commit comments

Comments
 (0)