Skip to content

SP-DIT/grafana-loki-tester-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Express.js Logging with Grafana Loki

This project sets up a simple Express.js backend with Winston logging. It supports structured logging to both stdout and optionally Grafana Loki, allowing log monitoring in real-time.

Additionally, an Artillery load test script is provided to simulate different traffic patterns for performance testing.

📌 Prerequisites

Ensure you have the following installed:

  • Node.js (>=14.x)
  • npm (Node Package Manager)
  • Grafana & Loki (Optional, for log aggregation)

🛠️ Setup

1️⃣ Install Dependencies

Run the following command to install the required packages:

npm install

2️⃣ Configure Environment Variables

Create a .env file in the project root and define the following variables:

PORT=3000
LOKI_URL=http://localhost:3100

PORT – The port number where the Express server runs (default: 3000). LOKI_URL – If provided, logs will be sent to Loki; otherwise, only stdout logging is enabled.

🚀 Running the Server

Start the express server with:

npm start

The server will run on http://localhost:3000

📊 Running the Load Test

The Artillery load test simulates real-world traffic with:

  1. A warm-up phase
  2. A gradual increase in load
  3. A sustained high traffic period
  4. A sudden traffic spike
  5. A cool-down phase

To start the load test, run:

npm run load-test

This will execute requests against the /items endpoint, logging results in both stdout and Loki (if configured).

📜 Logs

  • All logs are printed to the terminal (stdout).
  • If LOKI_URL is set, logs are also sent to Grafana Loki for centralized monitoring.
  • Errors (e.g., duplicate items) are logged as error messages in Winston.

🛠️ Troubleshooting

  • Cannot connect to Loki? Ensure Loki is running and LOKI_URL is correctly set.
  • Load test fails? Check if the server is running before executing npm run load-test.
  • Duplicate item error? The backend prevents duplicate names, and logs an error if one exists.

Useful resources

  1. Background
  2. What is Observability? | Grafana for Beginners Ep. 1 ⁠ 2. What is DevOps? | Grafana for Beginners Ep.2
  3. Setting up:
  4. ⁠Set up a new Grafana Cloud Account | Grafana for Beginners Ep. 5 - YouTube
  5. Loki - To store logs
    1. How to Get Started with Loki | Zero to Hero: Loki | Grafana - YouTube
    2. ⁠Intro to Logging | Zero to Hero: Loki | Grafana
  6. Sending logs to Loki
    1. ⁠Winston - Logging in JavaScript & Node.js applications
    2. Winston-Loki transport:
    3. winston - npm
    4. winston-loki - npm
  7. Connecting Grafana to Loki
    1. Adding data sources to Grafana (Loki, Tempo, & Mimir) | Grafana for Beginners Ep. 6
  8. Creating dashboard:
  9. ⁠Exploring logs, metrics, and traces with Grafana | Grafana for Beginners Ep. 7
  10. Most commonly used visualizations in Grafana | Grafana for Beginners Ep. 8
  11. Creating visualizations with Grafana | Grafana for Beginners Ep. 9

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published