DEV Community

XIMENA ANDREA ORTIZ FERNANDEZ
XIMENA ANDREA ORTIZ FERNANDEZ

Posted on

Observability Practices with Sentry: Tracking

Ximena Andrea Ortiz Fernandez

Abstract

Observability is essential in modern software systems, enabling teams to detect and resolve failures rapidly.

This article demonstrates how to apply observability best practices using Sentry in a Node.js payment processing service.

We illustrate how to track unhandled exceptions, monitor performance, and troubleshoot issues using real-time insights from Sentry.

Readers will learn how integrating Sentry leads to faster debugging, improved stability, and better user experience.


1. What is Observability?

Observability refers to the ability to infer the internal state of a system based on its external outputs. It involves collecting and analyzing:

  • Logs – Event records of what happened in the system
  • Metrics – Numerical data over time
  • Traces – Execution paths of requests across services

Without observability, failures in production systems may go undetected, or worse — remain unresolved due to lack of actionable insights.


2. Why Use Sentry?

Sentry is an observability tool focused on real-time error tracking and performance monitoring. It integrates easily with various programming languages and frameworks. Its benefits include:

  • Captures uncaught exceptions and rejected promises
  • Visualizes stack traces with source code context
  • Tracks user sessions, releases, and error frequency
  • Provides performance metrics like response times and bottlenecks

3. Real-World Scenario: Payment Service Failure

Let's build a real-world payment microservice with Sentry observability using Express.js and a modular file structure.


📦 Packages

We will use the following Node.js packages:

  • express – For building the REST API.
  • @sentry/node – Sentry SDK for Node.js error tracking.

Install all required packages:

npm install express @sentry/node
Enter fullscreen mode Exit fullscreen mode

📁 Project Structure

payment-service/
├── CreditCard.js
├── PaymentService.js
├── Sentry.js
└── server.js
Enter fullscreen mode Exit fullscreen mode

Step 1: Domain Model – CreditCard.js

class CreditCard {
  constructor(number, expiry, cvv) {
    this.number = number;
    this.expiry = expiry;
    this.cvv = cvv;
  }

  isValid() {
    return this.number && this.expiry && this.cvv;
  }

  isTestFailure() {
    return this.number === '1234-ERROR';
  }
}

module.exports = CreditCard;
Enter fullscreen mode Exit fullscreen mode

Step 2: Business Logic – PaymentService.js

class PaymentService {
  process(card, amount) {
    if (!card.isValid()) {
      throw new Error("Invalid credit card information.");
    }

    if (card.isTestFailure()) {
      throw new Error("Payment gateway timeout (simulated).");
    }

    return {
      status: "success",
      amountProcessed: amount
    };
  }
}

module.exports = PaymentService;
Enter fullscreen mode Exit fullscreen mode

Step 3: Sentry Integration – Sentry.js

const Sentry = require('@sentry/node');

Sentry.init({
  dsn: 'YOUR_SENTRY_DSN_HERE',
  tracesSampleRate: 1.0,
  sendDefaultPii: true
});

module.exports = Sentry;
Enter fullscreen mode Exit fullscreen mode

Step 4: API Entry Point – server.js

require('./Sentry.js');

const express = require('express');
const Sentry = require('@sentry/node');
const CreditCard = require('./CreditCard');
const PaymentService = require('./PaymentService');

const app = express();
app.use(express.json());

const service = new PaymentService();

app.post('/pay', (req, res, next) => {
  const { number, expiry, cvv, amount } = req.body;
  const card = new CreditCard(number, expiry, cvv);

  try {
    const result = service.process(card, amount);
    res.json(result);
  } catch (err) {
    Sentry.captureException(err);
    res.status(500).json({ error: err.message });
  }
});

app.listen(3000, () => {
  console.log('✅ Payment service running on http://localhost:3000');
});
Enter fullscreen mode Exit fullscreen mode

Step 5: Triggering an Error

To simulate a failed transaction and confirm that Sentry logs the error, use:

curl -X POST http://localhost:3000/pay \
  -H "Content-Type: application/json" \
  -d '{"number":"1234-ERROR","expiry":"12/26","cvv":"123","amount":100}'
Enter fullscreen mode Exit fullscreen mode

Step 6: View in Sentry Dashboard

Log into your Sentry project to view:

  • Real-time error alerts
  • Stack traces with line numbers
  • HTTP request payloads
  • User impact statistics

4. How Sentry Helps Debugging

When a user attempts to pay with the test card 1234-ERROR, the app throws a custom error. Sentry immediately captures:

  • Error message: Payment gateway timeout
  • Stack trace: Shows exact line of failure
  • Request data: Includes card number and amount
  • User context: If set, correlates issues to users

Sentry also tracks how often this error occurs and whether it was introduced by a specific release.


5. Conclusion

📂 GitHub Repository: ximena-ortiz/ObservabilityWithSentry

Observability is a fundamental practice for reliable software systems. By integrating Sentry into your application, you can:

  • Catch errors before users report them
  • Understand the context of each failure
  • Monitor performance trends and anomalies
  • Improve developer velocity and user trust

In production systems where uptime and user satisfaction matter, observability is not optional — it's essential.

Top comments (3)

Collapse
 
angel_gadielhernandezcr profile image
ANGEL GADIEL HERNANDEZ CRUZ

This article provides a clear and practical introduction to observability using Sentry in a Node.js environment. It effectively explains key concepts like logs, metrics, and traces, and demonstrates how Sentry helps track errors and monitor performance in real time. The real-world payment service example makes it easy to follow. Including code snippets and alert setup would further enhance the guide. Overall, it's a useful read for developers aiming to improve system reliability and debugging efficiency.

Collapse
 
camila_fernandacabrerac profile image
CAMILA FERNANDA CABRERA CATARI

This article provides a clear and practical introduction to observability principles applied in a real-world Node.js environment. The choice of Sentry as the observability tool effectively showcases its value in capturing errors, visualizing stack traces, and improving the performance of critical services like a payment system. Additionally, the step-by-step approach—from foundational concepts to a real-world scenario—makes it accessible for both beginner developers and professionals aiming to enhance system reliability.

Collapse
 
alvaro_javiercontrerasl profile image
ALVARO JAVIER CONTRERAS LIPA

Great article, Ximena. The clear, step-by-step explanation of how to integrate Sentry into a Node.js payment microservice makes it very approachable for both new and experienced developers. I especially appreciated the realistic failure scenario and how Sentry helps capture valuable context for debugging. The project structure breakdown and the curl test example are also very practical. It would be interesting to see a follow-up covering distributed tracing across multiple services.