Automatic Instrumentation
Capturing transactions requires that you first set up performance monitoring if you haven't already. You'll also need to enable the base Sentry integration for ASP.NET Core.
Sentry's ASP.NET Core integration includes middleware to automatically capture each incoming HTTP request and turn it into a transaction.
Transaction names follow the pattern <HTTP method> <Route>
; for example, a request to the following action will create a transaction named GET /person/{id}
:
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
public class HomeController : Controller
{
[HttpGet("/person/{id}")]
public IActionResult Person(string id) { /* ... */ }
}
Transactions created by this integration are automatically added to the current scope. If you want to start your own spans from the automatically created transaction, you can use the following approach:
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Sentry;
public class HomeController : Controller
{
private readonly IHub _sentryHub;
public HomeController(IHub sentryHub) => _sentryHub = sentryHub;
[HttpGet("/person/{id}")]
public IActionResult Person(string id)
{
var childSpan = _sentryHub.GetSpan()?.StartChild("additional-work");
try
{
// Do the work that gets measured.
childSpan?.Finish(SpanStatus.Ok);
}
catch (Exception e)
{
childSpan?.Finish(SpanStatus.InternalError);
throw;
}
}
}
Sentry also provides a custom filter for HTTP client factory commonly used in ASP.NET Core applications. This filter adds an additional message handler that automatically injects Sentry's trace header and tracks outgoing HTTP requests in separate spans attached to the current transaction.
To enable this integration, you need to have HTTP client factory enabled within your application:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
/* ... */
services.AddRouting();
// This is required for HTTP client integration
services.AddHttpClient();
}
}
If you already have AddHttpClient()
in your service collection (which is likely the case), then no action is required.
Starting with version 3.9.0, the SDK automatically integrates with Entity Framework Core and SQLClient whenever available. Those integrations are automatically activated if your project matches one of the following conditions:
- Includes
Sentry.AspNet
3.9.0 or higher - Includes
Sentry.AspNetCore
3.9.0 or higher - Includes
Sentry
3.9.0 and targets .NET Core 3.0 or higher (for example, .NET 5)
If you don't want to have this integration, you can disable it on SentryOptions
by calling DisableDiagnosticSourceIntegration();
option.DisableDiagnosticSourceIntegration();
If your project doesn't match any of the conditions above, (for example, it targets .NET Framework 4.6.1 and uses only the Sentry
package), you can still manually activate those instrumentations by including the package Sentry.DiagnosticSource
and enabling it during on the SDK's initialization.
// Requires NuGet package: Sentry.DiagnosticSource
option.AddDiagnosticSourceIntegration();
This integration is part of the DiagnosticSource
integration and will automatically create spans for EF Core queries for the following operations:
Query compiling
Occurs when EF Core optimizes a query. It then caches it so that future queries with the same input get reused. The parameters for this span are:
Operation:
db.query_compiler
Description: The query to be compiled
Database connection
Represents the lifecycle of a database connection. One connection may contain one or more query execution spans, and, in some circumstances, may not be registered, due to the nature of the EF Core event model. The parameters for this span are:
- Operation:
db.connection
Query Execution
Happens during the execution of a query. It represents how long a query took to be executed The parameters for this span are:
Operation:
db.query
Description: The query to be executed.
This integration is part of the DiagnosticSource integration and will automatically create spans for SQLClient operations, the integrated operations are:
Database connection
Represents the lifecycle of a database connection. One connection may contain one or more query execution spans. The parameters for this span are:
Operation:
db.connection
db.connection_id
: The Connection ID from the connection.db.operation_id
: The Operation ID from the connection.rows_sent
: The number of rows sent during the connection.bytes_received
: The amount of data (in bytes) received during the connection.bytes_sent
: The amount of data (in bytes) sent during the connection.
Query Execution
Happens during the execution of a query. It represents how long a query took to be executed. The parameters for this span are:
Operation:
db.query
Description: The query to be executed.
db.operation_id
: The Operation ID from the connection.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").
- Package:
- nuget:Sentry.AspNetCore
- Version:
- 4.4.0
- Repository:
- https://github.com/getsentry/sentry-dotnet