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

Added example of TracesSampler callback function to MVC Sample #3593

Merged
merged 1 commit into from
Sep 4, 2024
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
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
Loading