Skip to content
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
138 changes: 79 additions & 59 deletions docs/get-started/guides/logs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sentry_sdk.logger.<level>(message, attribute=value)

```csharp {tabTitle: .NET}
using Sentry;
SentrySdk.Logger.<Level>(message);
SentrySdk.Logger.Log<Level>(message);
```

```ruby {tabTitle: Ruby}
Expand All @@ -48,7 +48,7 @@ import io.sentry.Sentry
Sentry.logger().<level>(message)
```

**Levels:** `trace`, `debug`, `info`, `warn` (or `warning` in Python), `error`, `fatal`
**Levels:** `trace`, `debug`, `info`, `warn` (or `warning` in Python and .NET), `error`, `fatal`

**Attributes:** Key-value pairs you can search and filter on. Consistency matters, so use whatever naming convention fits your codebase.

Expand Down Expand Up @@ -82,11 +82,13 @@ sentry_sdk.logger.info("Order completed",
```csharp {tabTitle: .NET}
using Sentry;

SentrySdk.Logger.Info("Order completed", logger => logger
.SetAttribute("orderId", "order_123")
.SetAttribute("userId", user.Id)
.SetAttribute("amount", 149.99)
.SetAttribute("paymentMethod", "stripe"));
SentrySdk.Logger.LogInfo(log =>
{
log.SetAttribute("orderId", "order_123");
log.SetAttribute("userId", user.Id);
log.SetAttribute("amount", 149.99);
log.SetAttribute("paymentMethod", "stripe");
}, "Order completed");
```

```ruby {tabTitle: Ruby}
Expand Down Expand Up @@ -187,16 +189,20 @@ sentry_sdk.logger.warning("Login failed",

```csharp {tabTitle: .NET}
// After successful authentication
SentrySdk.Logger.Info("User logged in", logger => logger
.SetAttribute("userId", user.Id)
.SetAttribute("authMethod", "oauth")
.SetAttribute("provider", "google"));
SentrySdk.Logger.LogInfo(log =>
{
log.SetAttribute("userId", user.Id);
log.SetAttribute("authMethod", "oauth");
log.SetAttribute("provider", "google");
}, "User logged in");

// After authentication fails
SentrySdk.Logger.Warning("Login failed", logger => logger
.SetAttribute("email", maskedEmail)
.SetAttribute("reason", "invalid_password")
.SetAttribute("attemptCount", 3));
SentrySdk.Logger.LogWarning(log =>
{
log.SetAttribute("email", maskedEmail);
log.SetAttribute("reason", "invalid_password");
log.SetAttribute("attemptCount", 3);
}, "Login failed");
```

```ruby {tabTitle: Ruby}
Expand Down Expand Up @@ -306,12 +312,14 @@ sentry_sdk.logger.error("Payment failed",

```csharp {tabTitle: .NET}
// After payment gateway returns an error
SentrySdk.Logger.Error("Payment failed", logger => logger
.SetAttribute("orderId", "order_123")
.SetAttribute("amount", 99.99)
.SetAttribute("gateway", "stripe")
.SetAttribute("errorCode", "card_declined")
.SetAttribute("cartItems", 3));
SentrySdk.Logger.LogError(log =>
{
log.SetAttribute("orderId", "order_123");
log.SetAttribute("amount", 99.99);
log.SetAttribute("gateway", "stripe");
log.SetAttribute("errorCode", "card_declined");
log.SetAttribute("cartItems", 3);
}, "Payment failed");
```

```ruby {tabTitle: Ruby}
Expand Down Expand Up @@ -435,17 +443,21 @@ $response = $shippingApi->getRates($items);
var stopwatch = Stopwatch.StartNew();
var response = await shippingApi.GetRatesAsync(items);

SentrySdk.Logger.Info("Shipping rates fetched", logger => logger
.SetAttribute("service", "shipping-provider")
.SetAttribute("endpoint", "/rates")
.SetAttribute("durationMs", stopwatch.ElapsedMilliseconds)
.SetAttribute("rateCount", response.Rates.Count));
SentrySdk.Logger.LogInfo(log =>
{
log.SetAttribute("service", "shipping-provider");
log.SetAttribute("endpoint", "/rates");
log.SetAttribute("durationMs", stopwatch.ElapsedMilliseconds);
log.SetAttribute("rateCount", response.Rates.Count);
}, "Shipping rates fetched");

// Webhook received
SentrySdk.Logger.Info("Webhook received", logger => logger
.SetAttribute("source", "stripe")
.SetAttribute("eventType", "payment_intent.succeeded")
.SetAttribute("paymentId", eventData.Object.Id));
SentrySdk.Logger.LogInfo(log =>
{
log.SetAttribute("source", "stripe");
log.SetAttribute("eventType", "payment_intent.succeeded");
log.SetAttribute("paymentId", eventData.Object.Id);
}, "Webhook received");
```

```ruby {tabTitle: Ruby}
Expand Down Expand Up @@ -587,16 +599,20 @@ sentry_sdk.logger.error("Job failed",

```csharp {tabTitle: .NET}
// Inside background job handler
SentrySdk.Logger.Info("Job started", logger => logger
.SetAttribute("jobType", "email-digest")
.SetAttribute("jobId", "job_456")
.SetAttribute("queue", "notifications"));
SentrySdk.Logger.LogInfo(log =>
{
log.SetAttribute("jobType", "email-digest");
log.SetAttribute("jobId", "job_456");
log.SetAttribute("queue", "notifications");
}, "Job started");

SentrySdk.Logger.Error("Job failed", logger => logger
.SetAttribute("jobType", "email-digest")
.SetAttribute("jobId", "job_456")
.SetAttribute("retryCount", 3)
.SetAttribute("lastError", "SMTP timeout"));
SentrySdk.Logger.LogError(log =>
{
log.SetAttribute("jobType", "email-digest");
log.SetAttribute("jobId", "job_456");
log.SetAttribute("retryCount", 3);
log.SetAttribute("lastError", "SMTP timeout");
}, "Job failed");
```

```ruby {tabTitle: Ruby}
Expand Down Expand Up @@ -716,14 +732,18 @@ sentry_sdk.logger.warning("Config reloaded",

```csharp {tabTitle: .NET}
// When feature flag is checked or config changes
SentrySdk.Logger.Info("Feature flag evaluated", logger => logger
.SetAttribute("flag", "new-checkout-flow")
.SetAttribute("enabled", true)
.SetAttribute("userId", user.Id));
SentrySdk.Logger.LogInfo(log =>
{
log.SetAttribute("flag", "new-checkout-flow");
log.SetAttribute("enabled", true);
log.SetAttribute("userId", user.Id);
}, "Feature flag evaluated");

SentrySdk.Logger.Warning("Config reloaded", logger => logger
.SetAttribute("reason", "env-change")
.SetAttribute("changedKeys", new[] { "API_TIMEOUT", "MAX_CONNECTIONS" }));
SentrySdk.Logger.LogWarning(log =>
{
log.SetAttribute("reason", "env-change");
log.SetAttribute("changedKeys", "API_TIMEOUT,MAX_CONNECTIONS");
}, "Config reloaded");
```

```ruby {tabTitle: Ruby}
Expand Down Expand Up @@ -842,7 +862,6 @@ SentrySdk.Init(options =>
options.Dsn = "...";
options.Environment = "development";
options.TracesSampleRate = 1.0; // 100% of traces
options.EnableLogs = true;
});
```

Expand Down Expand Up @@ -918,9 +937,9 @@ sentry_sdk.logger.error("Payment failed", reason="Insufficient Funds")
```

```csharp {tabTitle: .NET}
SentrySdk.Logger.Info("Checkout started", l => l.SetAttribute("userId", "882"));
SentrySdk.Logger.Info("Discount applied", l => l.SetAttribute("code", "WINTER20"));
SentrySdk.Logger.Error("Payment failed", l => l.SetAttribute("reason", "Insufficient Funds"));
SentrySdk.Logger.LogInfo(log => log.SetAttribute("userId", "882"), "Checkout started");
SentrySdk.Logger.LogInfo(log => log.SetAttribute("code", "WINTER20"), "Discount applied");
SentrySdk.Logger.LogError(log => log.SetAttribute("reason", "Insufficient Funds"), "Payment failed");
```

```ruby {tabTitle: Ruby}
Expand Down Expand Up @@ -988,14 +1007,16 @@ sentry_sdk.logger.error("Checkout failed",
```

```csharp {tabTitle: .NET}
SentrySdk.Logger.Error("Checkout failed", logger => logger
.SetAttribute("userId", "882")
.SetAttribute("orderId", "order_pc_991")
.SetAttribute("cartTotal", 142.50)
.SetAttribute("discountCode", "WINTER20")
.SetAttribute("paymentMethod", "stripe")
.SetAttribute("errorReason", "Insufficient Funds")
.SetAttribute("itemCount", 4));
SentrySdk.Logger.LogError(log =>
{
log.SetAttribute("userId", "882");
log.SetAttribute("orderId", "order_pc_991");
log.SetAttribute("cartTotal", 142.50);
log.SetAttribute("discountCode", "WINTER20");
log.SetAttribute("paymentMethod", "stripe");
log.SetAttribute("errorReason", "Insufficient Funds");
log.SetAttribute("itemCount", 4);
}, "Checkout failed");
```

```ruby {tabTitle: Ruby}
Expand Down Expand Up @@ -1087,4 +1108,3 @@ To pipe Supabase infrastructure logs into Sentry and alert on them, see [Set up
## Next Steps

Explore the [Logs product walkthrough guides](/product/logs/) to learn more about the Sentry interface and discover additional tips.

105 changes: 58 additions & 47 deletions docs/get-started/guides/metrics.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ sentry_sdk.metrics.count("checkout.failed", 1, attributes={
```

```csharp {tabTitle: .NET}
SentrySdk.Metrics.Counter("checkout.failed")
.Tag("user_tier", "premium")
.Tag("failure_reason", "payment_declined")
.Increment();
SentrySdk.Metrics.EmitCounter("checkout.failed", 1, new Dictionary<string, object>
{
["user_tier"] = "premium",
["failure_reason"] = "payment_declined",
});
```

```ruby {tabTitle: Ruby}
Expand Down Expand Up @@ -121,15 +122,17 @@ sentry_sdk.metrics.count("checkout.failed", 1, attributes={
```

```csharp {tabTitle: .NET}
SentrySdk.Metrics.Counter("checkout.completed")
.Tag("user_tier", "premium")
.Tag("payment_method", "card")
.Increment();
SentrySdk.Metrics.EmitCounter("checkout.completed", 1, new Dictionary<string, object>
{
["user_tier"] = "premium",
["payment_method"] = "card",
});

SentrySdk.Metrics.Counter("checkout.failed")
.Tag("user_tier", "premium")
.Tag("failure_reason", "payment_declined")
.Increment();
SentrySdk.Metrics.EmitCounter("checkout.failed", 1, new Dictionary<string, object>
{
["user_tier"] = "premium",
["failure_reason"] = "payment_declined",
});
```

```ruby {tabTitle: Ruby}
Expand Down Expand Up @@ -229,20 +232,23 @@ sentry_sdk.metrics.count("job.processed", 1, attributes={
```

```csharp {tabTitle: .NET}
SentrySdk.Metrics.Counter("email.sent")
.Tag("email_type", "welcome")
.Tag("provider", "sendgrid")
.Increment();
SentrySdk.Metrics.EmitCounter("email.sent", 1, new Dictionary<string, object>
{
["email_type"] = "welcome",
["provider"] = "sendgrid",
});

SentrySdk.Metrics.Counter("email.failed")
.Tag("email_type", "welcome")
.Tag("error", "rate_limited")
.Increment();
SentrySdk.Metrics.EmitCounter("email.failed", 1, new Dictionary<string, object>
{
["email_type"] = "welcome",
["error"] = "rate_limited",
});

SentrySdk.Metrics.Counter("job.processed")
.Tag("job_type", "invoice-generation")
.Tag("queue", "billing")
.Increment();
SentrySdk.Metrics.EmitCounter("job.processed", 1, new Dictionary<string, object>
{
["job_type"] = "invoice-generation",
["queue"] = "billing",
});
```

```ruby {tabTitle: Ruby}
Expand Down Expand Up @@ -341,13 +347,15 @@ use \Sentry\Metrics\Unit;
```

```csharp {tabTitle: .NET}
SentrySdk.Metrics.Gauge("queue.depth")
.Tag("queue_name", "notifications")
.Set(queue.Size);
SentrySdk.Metrics.EmitGauge("queue.depth", queue.Size, MeasurementUnit.None, new Dictionary<string, object>
{
["queue_name"] = "notifications",
});

SentrySdk.Metrics.Gauge("pool.connections_active")
.Tag("pool_name", "postgres-primary")
.Set(pool.ActiveConnections);
SentrySdk.Metrics.EmitGauge("pool.connections_active", pool.ActiveConnections, MeasurementUnit.None, new Dictionary<string, object>
{
["pool_name"] = "postgres-primary",
});
```

```ruby {tabTitle: Ruby}
Expand Down Expand Up @@ -436,15 +444,17 @@ use \Sentry\Metrics\Unit;
```

```csharp {tabTitle: .NET}
SentrySdk.Metrics.Distribution("api.latency", responseTimeMs,
unit: MeasurementUnit.Duration.Millisecond)
.Tag("endpoint", "/api/orders")
.Tag("method", "POST");
SentrySdk.Metrics.EmitDistribution("api.latency", responseTimeMs, MeasurementUnit.Duration.Millisecond, new Dictionary<string, object>
{
["endpoint"] = "/api/orders",
["method"] = "POST",
});

SentrySdk.Metrics.Distribution("db.query_time", queryDurationMs,
unit: MeasurementUnit.Duration.Millisecond)
.Tag("table", "orders")
.Tag("operation", "select");
SentrySdk.Metrics.EmitDistribution("db.query_time", queryDurationMs, MeasurementUnit.Duration.Millisecond, new Dictionary<string, object>
{
["table"] = "orders",
["operation"] = "select",
});
```

```ruby {tabTitle: Ruby}
Expand Down Expand Up @@ -543,15 +553,17 @@ use \Sentry\Metrics\Unit;
```

```csharp {tabTitle: .NET}
SentrySdk.Metrics.Distribution("order.amount", order.TotalUsd,
unit: MeasurementUnit.Custom("usd"))
.Tag("user_tier", "premium")
.Tag("region", "us-west");
SentrySdk.Metrics.EmitDistribution("order.amount", (double)order.TotalUsd, MeasurementUnit.Custom("usd"), new Dictionary<string, object>
{
["user_tier"] = "premium",
["region"] = "us-west",
});

SentrySdk.Metrics.Distribution("upload.size", fileSizeBytes,
unit: MeasurementUnit.Information.Byte)
.Tag("file_type", "image")
.Tag("source", "profile-update");
SentrySdk.Metrics.EmitDistribution("upload.size", fileSizeBytes, MeasurementUnit.Information.Byte, new Dictionary<string, object>
{
["file_type"] = "image",
["source"] = "profile-update",
});
```

```ruby {tabTitle: Ruby}
Expand Down Expand Up @@ -637,4 +649,3 @@ You can also [create dashboards with an AI coding agent](https://sentry.io/cookb
All three are trace-connected. Start wherever makes sense and navigate to the others.

Explore the [Application Metrics product walkthrough](/product/metrics/) to learn more about the Sentry interface and discover additional tips.

Loading
Loading