Operations Monitoring and Alerting
Online Monitoring and Alerting
A robust production system requires a comprehensive operations framework to ensure stable operation. This includes server monitoring, alerting, and network security prevention. The software lifecycle—development, iteration, and operations—places operations as the critical phase. Within operations, monitoring and alerting are paramount for maintaining secure and stable services. As an architect, recognizing this priority is essential, even if execution is delegated.
Key concepts: Operations, Monitoring, Alerting, Heartbeat Detection, Security.
Unified Exception Handling and Security
The goal is to enhance system security and stability through unified error handlign and security prevention measures.
Unified Exception Handling
Environment-specific error exposure is crucial: development and test environments should reveal detailed errors for debugging, while production environments should hide errors and present user-friendly prompts. Since production environments are complex, a unified approach using middleware is recommended.
// Example middleware for error handling in a Koa application
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.status = err.status || 500;
ctx.body = process.env.NODE_ENV === 'production'
? { message: 'An internal error occurred.' }
: { message: err.message, stack: err.stack };
ctx.app.emit('error', err, ctx);
}
});
Memory Leak Prevention
Configure process managers like PM2 to restart when memory usage exceeds a threshold (e.g., 300M). Frequent restarts may indicate memory leaks requiring investigation.
// PM2 configuration snippet
{
"name": "app",
"script": "./app.js",
"max_memory_restart": "300M"
}
Security Prevention
Common web attacks include SQL Injection, XSS, and CSRF.
- SQL Injection: Prevented by using ORM tools like Sequelize instead of raw SQL queries.
- XSS: Modern frameworks like Vue and React mitigate this. For raw HTML output, use
v-html(Vue) ordangerouslySetInnerHTML(React) with sanitization libraries likexss. - CSRF: Mitigated by using POST requests for data submission and JWT token validation.
HTTP Header Optimization
Use koa-helmet to set security headers:
const Koa = require('koa');
const helmet = require('koa-helmet');
const app = new Koa();
app.use(helmet());
Preventing Network Attacks
Network attacks like DDoS are handled via infrastructure services. Alibaba Cloud WAF (Web Application Firewall) is a cost-effective solution. Configuration involves pointing the domain CNAME to the WAF address, creating a traffic flow: Domain → WAF → Server.
Monitoring and Alerting Mechanisms
Proactive monitoring ensures issues are detected before users report them.
Heartbeat Detection
Implement scheduled "health checks" for APIs using cron patterns. Below is a Node.js example using the node-cron library.
const cron = require('node-cron');
const axios = require('axios');
// Check health every 10 minutes
cron.schedule('*/10 * * * *', async () => {
try {
const response = await axios.get('https://api.example.com/health');
console.log('Health check passed:', response.status);
} catch (error) {
console.error('Health check failed:', error.message);
}
});
Alerting
Email alerts are preferred over SMS for cost efficiency and flexibility. Use nodemailer to send notifications.
const nodemailer = require('nodemailer');
async function sendAlert(subject, text) {
const transporter = nodemailer.createTransport({
host: 'smtp.126.com',
port: 465,
secure: true,
auth: {
user: 'alert@example.com',
pass: 'smtp-auth-code'
}
});
await transporter.sendMail({
from: 'alert@example.com',
to: 'admin@example.com',
subject,
text
});
}
AliNode Server Monitoring
AliNode provides free server monitoring. After obtaining AppID and Secret from Alibaba Cloud:
// ali-node.config.json
{
"appid": "your-app-id",
"secret": "your-secret",
"logdir": "/tmp"
}
Start the agent with agenthub start ali-node.config.json and configure PM2 to enable logging.
CLI Publish Module Architecture
Architecture Design
The CLI publish module integrates GitFlow, cloud building, and cloud publishing. The workflow follows:
- Initialize Git repository via GitHub/Gitee API.
- Automate GitFlow processes (commit, tag).
- Trigger cloud build (packaging, asset upload).
- Execute cloud publish (deploy to CDN/OSS).
Vue Router Next Source Analysis
Key differences between Hash and History modes:
- Hash: Uses URL hash (
#), no server configuration needed. - History: Uses HTML5 History API, requires server fallback configurtaion.
Git Automation for CLI Publishing
GitHub and Gitee API Integration
Access repository APIs using tokens.
// GitHub API request example
const axios = require('axios');
class GitHubService {
constructor(token) {
this.token = token;
this.api = axios.create({
baseURL: 'https://api.github.com',
headers: { Authorization: `token ${this.token}` }
});
}
async createRepo(name) {
return this.api.post('/user/repos', { name, private: true });
}
}
// Gitee API request example
class GiteeService {
constructor(token) {
this.token = token;
this.api = axios.create({
baseURL: 'https://gitee.com/api/v5'
});
}
async createRepo(name) {
return this.api.post('/user/repos', {
name,
private: true,
access_token: this.token
});
}
}
Cloud Build System Development
Cloud Build Architecture
Cloud build centralizes packaging, dependency management, and security checks. It ensures environment consistency and performance optimization.
WebSocket and Redis Integration
WebSocket enables real-time communication betwean build servers and clients. Redis manages build queues and state.
// Egg.js WebSocket example
module.exports = (app) => {
return async (ctx, next) => {
const socket = ctx.socket;
socket.emit('connected', { pid: process.pid });
await next();
};
};
// Redis client setup in Egg.js
// config/plugin.js
exports.redis = {
enable: true,
package: 'egg-redis',
};
// config/config.default.js
config.redis = {
client: {
port: 6379,
host: '127.0.0.1',
password: '',
db: 0,
},
};
Cloud Publish Functionality
Cloud publishing involves uploading build artifacts to OSS (Object Storage Service) and supporting both Hash and History routing modes.
Component Publishing System
Frontend Material System
A material system includes reusable assets: components, blocks, page templates, and utility libraries. It evolves from basic component libraries to comprehensive code reuse frameworks.
Component Platform Architecture
The platform manages component lifecycle: development, publishing, preview, and documentation rendering.
Unit Testing for Projects
Mocha Framework Introduction
Mocha is a JavaScript test framework supporting BDD/TDD styles.
const assert = require('assert');
describe('Package Class', () => {
it('should throw error if options are empty', () => {
assert.throws(() => new Package(null), /Options required/);
});
});
Test Case Design
Focus on core flows (init, publish), utility classes (Package, Git, Command), and helper functions. Example test for a Package installer:
describe('Package Installation', () => {
it('should install dependencies correctly', async () => {
const pkg = new Package({ name: 'test-lib', version: '1.0.0' });
await pkg.install();
assert.strictEqual(await pkg.exists(), true);
});
});