|
1 | 1 | using System.Net; |
2 | 2 | using Samples.AspNetCore.Mvc; |
| 3 | +using Sentry.AspNetCore; |
3 | 4 | using Sentry.Extensibility; |
4 | 5 |
|
5 | 6 | var builder = WebApplication.CreateBuilder(args); |
|
17 | 18 |
|
18 | 19 | options.MaxBreadcrumbs = 200; |
19 | 20 |
|
20 | | - options.TracesSampleRate = 1.0; |
21 | | - |
22 | 21 | // Set a proxy for outgoing HTTP connections |
23 | 22 | options.HttpProxy = null; // new WebProxy("https://localhost:3128"); |
24 | 23 |
|
|
31 | 30 | options.MaxQueueItems = 100; |
32 | 31 | options.ShutdownTimeout = TimeSpan.FromSeconds(5); |
33 | 32 |
|
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 | + }; |
35 | 60 |
|
36 | 61 | // Configures the root scope |
37 | 62 | options.ConfigureScope(s => s.SetTag("Always sent", "this tag")); |
|
0 commit comments