aula-15: implementação completa APM (Tempo + OTel + demo app)
Componentes: - tempo-values.yaml: Grafana Tempo monolithic, 256Mi, 10Gi PVC - otel-collector-values.yaml: recebe OTLP, exporta traces→Tempo, gera span metrics (RED)→Victoria Metrics via spanmetrics connector - demo-app/: Node.js com rotas /fast (1 query), /slow (N+1, 51 queries), /fixed (JOIN), auto-instrumentado com OpenTelemetry - alerts/latency-alerts.yaml: VMRule com Doherty threshold (p95>400ms) - setup.sh: instala Tempo, OTel Collector, configura Grafana datasource, deploy demo app via ConfigMap (sem Docker build necessário) - cleanup.sh: remove apenas recursos da aula-15, preserva aula-12 Zero hardcoded hostnames. Tudo via .env e placeholders.
This commit is contained in:
12
aula-15/demo-app/Dockerfile
Normal file
12
aula-15/demo-app/Dockerfile
Normal file
@@ -0,0 +1,12 @@
|
||||
FROM node:20-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json package-lock.json* ./
|
||||
RUN npm install --production
|
||||
|
||||
COPY . .
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
CMD ["node", "--require", "./tracing.js", "app.js"]
|
||||
230
aula-15/demo-app/app.js
Normal file
230
aula-15/demo-app/app.js
Normal file
@@ -0,0 +1,230 @@
|
||||
'use strict';
|
||||
|
||||
const express = require('express');
|
||||
const { Pool } = require('pg');
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
const pool = new Pool({
|
||||
host: process.env.PG_HOST || 'localhost',
|
||||
port: parseInt(process.env.PG_PORT || '5432', 10),
|
||||
user: process.env.PG_USER || 'demo',
|
||||
password: process.env.PG_PASSWORD || 'demo',
|
||||
database: process.env.PG_DATABASE || 'demo',
|
||||
});
|
||||
|
||||
// --- Database seeding ---
|
||||
|
||||
async function seedDatabase() {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
const tableCheck = await client.query(`
|
||||
SELECT EXISTS (
|
||||
SELECT FROM information_schema.tables WHERE table_name = 'users'
|
||||
)
|
||||
`);
|
||||
|
||||
if (tableCheck.rows[0].exists) {
|
||||
const countResult = await client.query('SELECT COUNT(*) FROM users');
|
||||
if (parseInt(countResult.rows[0].count, 10) > 0) {
|
||||
console.log('Database already seeded, skipping.');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Seeding database...');
|
||||
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
email VARCHAR(150) NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT NOW()
|
||||
)
|
||||
`);
|
||||
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS posts (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INTEGER REFERENCES users(id),
|
||||
title VARCHAR(200) NOT NULL,
|
||||
body TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT NOW()
|
||||
)
|
||||
`);
|
||||
|
||||
// Insert 50 users
|
||||
for (let i = 1; i <= 50; i++) {
|
||||
await client.query(
|
||||
'INSERT INTO users (name, email) VALUES ($1, $2)',
|
||||
[`User ${i}`, `user${i}@example.com`]
|
||||
);
|
||||
}
|
||||
|
||||
// Insert 10 posts per user (500 posts total)
|
||||
for (let userId = 1; userId <= 50; userId++) {
|
||||
for (let p = 1; p <= 10; p++) {
|
||||
await client.query(
|
||||
'INSERT INTO posts (user_id, title, body) VALUES ($1, $2, $3)',
|
||||
[userId, `Post ${p} by User ${userId}`, `Content of post ${p} by user ${userId}. Lorem ipsum dolor sit amet.`]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Database seeded: 50 users, 500 posts.');
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
// --- Routes ---
|
||||
|
||||
app.get('/health', (_req, res) => {
|
||||
res.json({ status: 'ok' });
|
||||
});
|
||||
|
||||
// Fast route: single query, returns 10 users
|
||||
app.get('/fast', async (_req, res) => {
|
||||
const start = Date.now();
|
||||
try {
|
||||
const result = await pool.query('SELECT * FROM users LIMIT 10');
|
||||
const duration = Date.now() - start;
|
||||
res.json({
|
||||
route: '/fast',
|
||||
description: 'Single query - SELECT users LIMIT 10',
|
||||
query_count: 1,
|
||||
duration_ms: duration,
|
||||
data: result.rows,
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Slow route: N+1 query pattern
|
||||
app.get('/slow', async (_req, res) => {
|
||||
const start = Date.now();
|
||||
try {
|
||||
const usersResult = await pool.query('SELECT * FROM users');
|
||||
const users = usersResult.rows;
|
||||
let queryCount = 1;
|
||||
|
||||
const usersWithPosts = [];
|
||||
for (const user of users) {
|
||||
const postsResult = await pool.query(
|
||||
'SELECT * FROM posts WHERE user_id = $1',
|
||||
[user.id]
|
||||
);
|
||||
queryCount++;
|
||||
usersWithPosts.push({
|
||||
...user,
|
||||
posts: postsResult.rows,
|
||||
});
|
||||
}
|
||||
|
||||
const duration = Date.now() - start;
|
||||
res.json({
|
||||
route: '/slow',
|
||||
description: 'N+1 pattern - 1 query for users + 1 query per user for posts',
|
||||
query_count: queryCount,
|
||||
user_count: users.length,
|
||||
total_posts: usersWithPosts.reduce((sum, u) => sum + u.posts.length, 0),
|
||||
duration_ms: duration,
|
||||
data: usersWithPosts,
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Fixed route: single JOIN query
|
||||
app.get('/fixed', async (_req, res) => {
|
||||
const start = Date.now();
|
||||
try {
|
||||
const result = await pool.query(`
|
||||
SELECT
|
||||
u.id AS user_id,
|
||||
u.name,
|
||||
u.email,
|
||||
u.created_at AS user_created_at,
|
||||
p.id AS post_id,
|
||||
p.title,
|
||||
p.body,
|
||||
p.created_at AS post_created_at
|
||||
FROM users u
|
||||
LEFT JOIN posts p ON p.user_id = u.id
|
||||
ORDER BY u.id, p.id
|
||||
`);
|
||||
|
||||
// Group results by user
|
||||
const usersMap = new Map();
|
||||
for (const row of result.rows) {
|
||||
if (!usersMap.has(row.user_id)) {
|
||||
usersMap.set(row.user_id, {
|
||||
id: row.user_id,
|
||||
name: row.name,
|
||||
email: row.email,
|
||||
created_at: row.user_created_at,
|
||||
posts: [],
|
||||
});
|
||||
}
|
||||
if (row.post_id) {
|
||||
usersMap.get(row.user_id).posts.push({
|
||||
id: row.post_id,
|
||||
title: row.title,
|
||||
body: row.body,
|
||||
created_at: row.post_created_at,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const usersWithPosts = Array.from(usersMap.values());
|
||||
const duration = Date.now() - start;
|
||||
|
||||
res.json({
|
||||
route: '/fixed',
|
||||
description: 'Single JOIN query - the correct way',
|
||||
query_count: 1,
|
||||
user_count: usersWithPosts.length,
|
||||
total_posts: usersWithPosts.reduce((sum, u) => sum + u.posts.length, 0),
|
||||
duration_ms: duration,
|
||||
data: usersWithPosts,
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// --- Startup ---
|
||||
|
||||
async function main() {
|
||||
// Wait for PostgreSQL to be ready (with retries)
|
||||
let retries = 10;
|
||||
while (retries > 0) {
|
||||
try {
|
||||
await pool.query('SELECT 1');
|
||||
console.log('Connected to PostgreSQL.');
|
||||
break;
|
||||
} catch (err) {
|
||||
retries--;
|
||||
if (retries === 0) {
|
||||
console.error('Failed to connect to PostgreSQL after retries:', err.message);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`Waiting for PostgreSQL... (${retries} retries left)`);
|
||||
await new Promise((resolve) => setTimeout(resolve, 3000));
|
||||
}
|
||||
}
|
||||
|
||||
await seedDatabase();
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Demo app listening on port ${PORT}`);
|
||||
});
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error('Fatal error:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
64
aula-15/demo-app/k8s/deployment.yaml
Normal file
64
aula-15/demo-app/k8s/deployment.yaml
Normal file
@@ -0,0 +1,64 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: demo-app
|
||||
namespace: demo
|
||||
labels:
|
||||
app: demo-app
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: demo-app
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: demo-app
|
||||
spec:
|
||||
containers:
|
||||
- name: demo-app
|
||||
image: REGISTRY_PLACEHOLDER/IMAGE_NAME_PLACEHOLDER:latest
|
||||
ports:
|
||||
- containerPort: 3000
|
||||
env:
|
||||
- name: PG_HOST
|
||||
value: demo-postgresql
|
||||
- name: PG_PORT
|
||||
value: "5432"
|
||||
- name: PG_USER
|
||||
value: demo
|
||||
- name: PG_PASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: demo-postgresql
|
||||
key: POSTGRES_PASSWORD
|
||||
- name: PG_DATABASE
|
||||
value: demo
|
||||
- name: OTEL_EXPORTER_OTLP_ENDPOINT
|
||||
value: http://otel-collector-opentelemetry-collector.monitoring:4317
|
||||
- name: OTEL_SERVICE_NAME
|
||||
value: demo-app
|
||||
- name: NODE_OPTIONS
|
||||
value: "--require ./tracing.js"
|
||||
resources:
|
||||
requests:
|
||||
memory: "64Mi"
|
||||
cpu: "50m"
|
||||
limits:
|
||||
memory: "128Mi"
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
path: /health
|
||||
port: 3000
|
||||
initialDelaySeconds: 10
|
||||
periodSeconds: 10
|
||||
timeoutSeconds: 3
|
||||
failureThreshold: 3
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
path: /health
|
||||
port: 3000
|
||||
initialDelaySeconds: 5
|
||||
periodSeconds: 5
|
||||
timeoutSeconds: 3
|
||||
failureThreshold: 3
|
||||
24
aula-15/demo-app/k8s/ingress.yaml
Normal file
24
aula-15/demo-app/k8s/ingress.yaml
Normal file
@@ -0,0 +1,24 @@
|
||||
apiVersion: networking.k8s.io/v1
|
||||
kind: Ingress
|
||||
metadata:
|
||||
name: demo-app
|
||||
namespace: demo
|
||||
annotations:
|
||||
cert-manager.io/cluster-issuer: CLUSTER_ISSUER_PLACEHOLDER
|
||||
spec:
|
||||
ingressClassName: nginx
|
||||
tls:
|
||||
- hosts:
|
||||
- DEMO_HOST_PLACEHOLDER
|
||||
secretName: demo-app-tls
|
||||
rules:
|
||||
- host: DEMO_HOST_PLACEHOLDER
|
||||
http:
|
||||
paths:
|
||||
- path: /
|
||||
pathType: Prefix
|
||||
backend:
|
||||
service:
|
||||
name: demo-app
|
||||
port:
|
||||
number: 3000
|
||||
4
aula-15/demo-app/k8s/namespace.yaml
Normal file
4
aula-15/demo-app/k8s/namespace.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: demo
|
||||
84
aula-15/demo-app/k8s/postgresql.yaml
Normal file
84
aula-15/demo-app/k8s/postgresql.yaml
Normal file
@@ -0,0 +1,84 @@
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Secret
|
||||
metadata:
|
||||
name: demo-postgresql
|
||||
namespace: demo
|
||||
type: Opaque
|
||||
stringData:
|
||||
POSTGRES_PASSWORD: demo-secret-pw
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: PersistentVolumeClaim
|
||||
metadata:
|
||||
name: demo-postgresql-data
|
||||
namespace: demo
|
||||
spec:
|
||||
accessModes:
|
||||
- ReadWriteOnce
|
||||
resources:
|
||||
requests:
|
||||
storage: 1Gi
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: demo-postgresql
|
||||
namespace: demo
|
||||
labels:
|
||||
app: demo-postgresql
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: demo-postgresql
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: demo-postgresql
|
||||
spec:
|
||||
containers:
|
||||
- name: postgresql
|
||||
image: postgres:17-alpine
|
||||
ports:
|
||||
- containerPort: 5432
|
||||
env:
|
||||
- name: POSTGRES_DB
|
||||
value: demo
|
||||
- name: POSTGRES_USER
|
||||
value: demo
|
||||
- name: POSTGRES_PASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: demo-postgresql
|
||||
key: POSTGRES_PASSWORD
|
||||
resources:
|
||||
requests:
|
||||
memory: "64Mi"
|
||||
cpu: "50m"
|
||||
limits:
|
||||
memory: "128Mi"
|
||||
volumeMounts:
|
||||
- name: data
|
||||
mountPath: /var/lib/postgresql/data
|
||||
subPath: pgdata
|
||||
volumes:
|
||||
- name: data
|
||||
persistentVolumeClaim:
|
||||
claimName: demo-postgresql-data
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: demo-postgresql
|
||||
namespace: demo
|
||||
labels:
|
||||
app: demo-postgresql
|
||||
spec:
|
||||
type: ClusterIP
|
||||
ports:
|
||||
- port: 5432
|
||||
targetPort: 5432
|
||||
protocol: TCP
|
||||
selector:
|
||||
app: demo-postgresql
|
||||
15
aula-15/demo-app/k8s/service.yaml
Normal file
15
aula-15/demo-app/k8s/service.yaml
Normal file
@@ -0,0 +1,15 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: demo-app
|
||||
namespace: demo
|
||||
labels:
|
||||
app: demo-app
|
||||
spec:
|
||||
type: ClusterIP
|
||||
ports:
|
||||
- port: 3000
|
||||
targetPort: 3000
|
||||
protocol: TCP
|
||||
selector:
|
||||
app: demo-app
|
||||
17
aula-15/demo-app/package.json
Normal file
17
aula-15/demo-app/package.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "demo-app",
|
||||
"version": "1.0.0",
|
||||
"description": "Demo app for APM with OpenTelemetry - Workshop Aula 15",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"start": "node --require ./tracing.js app.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.21.2",
|
||||
"pg": "^8.13.1",
|
||||
"@opentelemetry/sdk-node": "^0.57.2",
|
||||
"@opentelemetry/auto-instrumentations-node": "^0.56.1",
|
||||
"@opentelemetry/exporter-trace-otlp-grpc": "^0.57.2",
|
||||
"@opentelemetry/exporter-metrics-otlp-grpc": "^0.57.2"
|
||||
}
|
||||
}
|
||||
45
aula-15/demo-app/tracing.js
Normal file
45
aula-15/demo-app/tracing.js
Normal file
@@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
const { NodeSDK } = require('@opentelemetry/sdk-node');
|
||||
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
|
||||
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
|
||||
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc');
|
||||
const { PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
|
||||
|
||||
const otlpEndpoint = process.env.OTEL_EXPORTER_OTLP_ENDPOINT || 'http://otel-collector.monitoring:4317';
|
||||
const serviceName = process.env.OTEL_SERVICE_NAME || 'demo-app';
|
||||
|
||||
const traceExporter = new OTLPTraceExporter({
|
||||
url: otlpEndpoint,
|
||||
});
|
||||
|
||||
const metricExporter = new OTLPMetricExporter({
|
||||
url: otlpEndpoint,
|
||||
});
|
||||
|
||||
const metricReader = new PeriodicExportingMetricReader({
|
||||
exporter: metricExporter,
|
||||
exportIntervalMillis: 15000,
|
||||
});
|
||||
|
||||
const sdk = new NodeSDK({
|
||||
serviceName,
|
||||
traceExporter,
|
||||
metricReader,
|
||||
instrumentations: [
|
||||
getNodeAutoInstrumentations({
|
||||
'@opentelemetry/instrumentation-fs': { enabled: false },
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
sdk.start();
|
||||
|
||||
process.on('SIGTERM', () => {
|
||||
sdk.shutdown()
|
||||
.then(() => console.log('OpenTelemetry SDK shut down'))
|
||||
.catch((err) => console.error('Error shutting down OpenTelemetry SDK', err))
|
||||
.finally(() => process.exit(0));
|
||||
});
|
||||
|
||||
console.log(`OpenTelemetry initialized for service "${serviceName}" -> ${otlpEndpoint}`);
|
||||
Reference in New Issue
Block a user