Skip to content

Commit

Permalink
Added example of TracesSampler callback function to MVC Sample (#3593)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescrosswell authored Sep 4, 2024
1 parent 49620c6 commit d955f6f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ public IActionResult Index()
return View();
}

[HttpGet("[controller]/sampler")]
public Task<string> Sampler()
{
// The sampling for this route is determined by the TraceSampler function configured in Program.cs when
// initializing the Sentry SDK... here we just display the result of that sampling decision.
var transaction = SentrySdk.GetSpan()?.GetTransaction();
return Task.FromResult($"Sampled: {transaction?.IsSampled}");
}

// GET /home/block/true or /home/block/false to observe events
[HttpGet("[controller]/block/{block?}")]
public async Task<string> Block([FromRoute] bool block)
Expand Down
31 changes: 28 additions & 3 deletions samples/Sentry.Samples.AspNetCore.Mvc/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net;
using Samples.AspNetCore.Mvc;
using Sentry.AspNetCore;
using Sentry.Extensibility;

var builder = WebApplication.CreateBuilder(args);
Expand All @@ -17,8 +18,6 @@
options.MaxBreadcrumbs = 200;
options.TracesSampleRate = 1.0;
// Set a proxy for outgoing HTTP connections
options.HttpProxy = null; // new WebProxy("https://localhost:3128");
Expand All @@ -31,7 +30,33 @@
options.MaxQueueItems = 100;
options.ShutdownTimeout = TimeSpan.FromSeconds(5);
options.TracesSampleRate = 1.0; // For production you may want to lower this to stay inside your quota
options.TracesSampleRate = 1.0; // For production, you may want to lower this to stay inside your quota
// In addition to (or instead of) setting the TracesSampleRate to a fixed value, you can also define a function
// that calculates the sample rate dynamically based on the current context.
options.TracesSampler = ctx =>
{
// In distributed tracing scenarios, we may want to respect upstream sampling decisions... for example, if
// the javascript frontend contains tracing instrumentation, a decision not to sample this request may
// already have been made. In that case, we don't want to sample this request since it would result in an
// incomplete trace.
if (ctx.TransactionContext.IsParentSampled == false)
{
// If the parent transaction is not sampled, don't sample this one either
return 0.0;
}
// Only sample 30% of the requests to this /home/sampler route
var httpPath = ctx.TryGetHttpPath();
if (string.Equals(httpPath, "/home/sampler", StringComparison.OrdinalIgnoreCase))
{
return 0.3;
}
// For all other routes, use the statically configured sample rate. Returning null here would achieve the
// same thing (if the TraceSampler function returns null, the SDK falls back to the TracesSampleRate)
return options.TracesSampleRate;
};
// Configures the root scope
options.ConfigureScope(s => s.SetTag("Always sent", "this tag"));
Expand Down

0 comments on commit d955f6f

Please sign in to comment.