Skip to content

Commit ddd711c

Browse files
authored
Merge pull request #227 from autonomys/fix-request-tracing
fix: request tracing
2 parents 4e1c8d1 + 1c3b0be commit ddd711c

File tree

6 files changed

+31
-51
lines changed

6 files changed

+31
-51
lines changed

backend/src/api.ts

-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { handleAuth } from './services/auth/express.js'
88
import { uploadController } from './http/controllers/upload.js'
99
import { config } from './config.js'
1010
import { logger } from './drivers/logger.js'
11-
import { requestTrace } from './http/middlewares/requestTrace.js'
1211

1312
const createServer = async () => {
1413
const app = express()
@@ -35,9 +34,6 @@ const createServer = async () => {
3534
app.use('/objects', objectController)
3635
app.use('/subscriptions', subscriptionController)
3736
app.use('/uploads', uploadController)
38-
if (config.monitoring.active) {
39-
app.use(requestTrace)
40-
}
4137

4238
app.get('/health', (_req, res) => {
4339
res.sendStatus(204)

backend/src/config.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ export const config = {
4747
url: env('RABBITMQ_URL'),
4848
},
4949
monitoring: {
50-
active: env('MONITORING_ENABLED', 'false') === 'true',
50+
active: env('VICTORIA_ACTIVE', 'false') === 'true',
5151
victoriaEndpoint: process.env.VICTORIA_ENDPOINT,
5252
auth: {
53-
username: process.env.VICTORIA_AUTH_USERNAME,
54-
password: process.env.VICTORIA_AUTH_PASSWORD,
53+
username: process.env.VICTORIA_USERNAME,
54+
password: process.env.VICTORIA_PASSWORD,
5555
},
56-
metricEnvironmentTag: env('ENVIRONMENT_TAG', 'chain=unknown'),
56+
metricEnvironmentTag: env('METRIC_ENVIRONMENT_TAG', 'chain=unknown'),
5757
},
5858
params: {
5959
maxConcurrentUploads: Number(env('MAX_CONCURRENT_UPLOADS', '40')),

backend/src/drivers/vmetrics.ts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const sendMetricToVictoria = async (metric: Metric): Promise<void> => {
1818
).toString('base64')
1919

2020
if (!config.monitoring.victoriaEndpoint) {
21+
console.log(JSON.stringify(config.monitoring, null, 2))
2122
console.error('Victoria endpoint is not set')
2223
return
2324
}
+17-30
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,26 @@
11
import { RequestHandler } from 'express'
22
import { Metric, sendMetricToVictoria } from '../../drivers/vmetrics.js'
33
import { config } from '../../config.js'
4-
import { logger } from '../../drivers/logger.js'
54

6-
export const requestTrace: RequestHandler = (req, res, next) => {
5+
export const requestTrace: RequestHandler = (req, res) => {
76
const path = req.route?.path
87

9-
if (!path) {
10-
logger.debug('No path found for request', {
11-
method: req.method,
12-
url: req.url,
13-
})
14-
return next()
15-
}
16-
17-
res.once('finish', () => {
18-
const method = req.method
19-
const provider =
20-
typeof req.headers['x-auth-provider'] === 'string'
21-
? req.headers['x-auth-provider']
22-
: 'unknown'
8+
const method = req.method
9+
const provider =
10+
typeof req.headers['x-auth-provider'] === 'string'
11+
? req.headers['x-auth-provider']
12+
: 'unknown'
2313

24-
const metric: Metric = {
25-
measurement: 'auto_drive_api_request',
26-
tag: config.monitoring.metricEnvironmentTag,
27-
fields: {
28-
status: res.statusCode,
29-
method,
30-
path,
31-
provider,
32-
},
33-
}
34-
35-
sendMetricToVictoria(metric).catch(console.error)
36-
})
14+
const metric: Metric = {
15+
measurement: 'auto_drive_api_request',
16+
tag: config.monitoring.metricEnvironmentTag,
17+
fields: {
18+
status: res.statusCode,
19+
method,
20+
path,
21+
provider,
22+
},
23+
}
3724

38-
next()
25+
sendMetricToVictoria(metric).catch(console.error)
3926
}

backend/src/utils/express.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { NextFunction, Request, Response } from 'express'
2+
import { requestTrace } from '../http/middlewares/requestTrace.js'
3+
import { config } from '../config.js'
24

35
export const asyncSafeHandler = (
46
fn: (req: Request, res: Response) => unknown,
57
) => {
68
return async (req: Request, res: Response, next: NextFunction) => {
79
try {
810
await fn(req, res)
9-
next()
11+
if (config.monitoring.active) {
12+
requestTrace(req, res, next)
13+
} else {
14+
console.log('Request trace disabled')
15+
}
1016
} catch (err) {
1117
next(err)
1218
}

docker-compose.devProdAuth.yml

+2-12
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,11 @@ services:
3535
- rabbitmq
3636
- postgres
3737
restart: unless-stopped
38+
env_file:
39+
- .env
3840
environment:
3941
DATABASE_URL: postgres://postgres:postgres@postgres:5432/postgres
40-
RPC_ENDPOINT: ${RPC_ENDPOINT}
41-
PRIVATE_KEYS_PATH: ${PRIVATE_KEYS_PATH}
42-
CORS_ALLOWED_ORIGINS: ${CORS_ALLOWED_ORIGINS}
43-
OBJECT_MAPPING_ARCHIVER_URL: ${OBJECT_MAPPING_ARCHIVER_URL}
44-
MAX_CACHE_SIZE: ${MAX_CACHE_SIZE}
45-
JWT_SECRET: ${JWT_SECRET}
46-
FILES_GATEWAY_URL: ${FILES_GATEWAY_URL}
47-
FILES_GATEWAY_TOKEN: ${FILES_GATEWAY_TOKEN}
48-
AUTH_SERVICE_URL: ${AUTH_SERVICE_URL}
49-
AUTH_SERVICE_API_KEY: ${AUTH_SERVICE_API_KEY}
5042
RABBITMQ_URL: amqp://guest:guest@rabbitmq:5672
51-
OPTIONAL_AUTH: ${OPTIONAL_AUTH}
52-
MAX_CONCURRENT_UPLOADS: ${MAX_CONCURRENT_UPLOADS}
5343
healthcheck:
5444
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
5545
interval: 15s

0 commit comments

Comments
 (0)