Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update tracing package for Railway deployment #3471

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"npmClient": "yarn",
"useWorkspaces": true,
"packages": ["packages/*", "packages/rfq-indexer/*", "docs/*"],
"packages": ["packages/*", "packages/rfq-indexer/*", "docs/*", "packages/synapse-constants"],
"version": "independent"
}
1 change: 1 addition & 0 deletions packages/rest-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@ethersproject/units": "5.7.0",
"@synapsecns/sdk-router": "^0.11.9",
"@synapsecns/synapse-constants": "^1.8.6",
"@synapsecns/tracing": "^0.1.0",
"bignumber": "^1.1.0",
"cross-fetch": "^4.0.0",
"dotenv": "^16.4.5",
Expand Down
32 changes: 32 additions & 0 deletions packages/rest-api/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import express from 'express'
import swaggerUi from 'swagger-ui-express'
import {
startPyroscope,
initializeTracing,
tracingMiddleware,
} from '@synapsecns/tracing'

import { specs } from './swagger'
import routes from './routes'
Expand All @@ -11,9 +16,27 @@
rfqIndexerProxy,
} from './utils/isGatewayRoute'

// Initialize tracing
const sdk = initializeTracing({
serviceName: 'rest-api',
version: process.env.VERSION || '0.0.0'

Check warning on line 22 in packages/rest-api/src/app.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`

Check warning on line 22 in packages/rest-api/src/app.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
})

// Start pyroscope
startPyroscope({
applicationName: 'rest-api',
serverAddress: process.env.PYROSCOPE_ENDPOINT,
tags: {
version: process.env.VERSION

Check warning on line 30 in packages/rest-api/src/app.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`

Check warning on line 30 in packages/rest-api/src/app.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
}

Check warning on line 31 in packages/rest-api/src/app.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`

Check warning on line 31 in packages/rest-api/src/app.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
})

const app = express()
const port = process.env.PORT || 3000

// Add middleware
app.use(tracingMiddleware())

app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS')
Expand Down Expand Up @@ -111,3 +134,12 @@
process.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Rejection at:', promise, 'reason:', reason)
})

process.on('SIGTERM', () => {
sdk.shutdown()

Check warning on line 139 in packages/rest-api/src/app.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎····`

Check warning on line 139 in packages/rest-api/src/app.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎····`
.then(() => process.exit(0))
.catch((error) => {
console.log('Error shutting down SDK', error)
process.exit(1)
})
})
1 change: 1 addition & 0 deletions packages/synapse-constants/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"lint:fix": "npm run lint -- --fix",
"lint:check": "eslint . --max-warnings=0 --config .eslintrc.cjs",
"prepare": "rollup -c --bundleConfigAsCjs",
"prepack": "yarn build",
"build": "rollup -c --bundleConfigAsCjs",
"preinstall": "command -v rollup >/dev/null 2>&1 && rollup -c --buildConfigAsCjs || echo 'rollup not found'",
"prepublish": "yarn build",
Expand Down
18 changes: 18 additions & 0 deletions packages/tracing/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended'
],
env: {
node: true,
jest: true
},
rules: {
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/explicit-module-boundary-types': 'error',
'@typescript-eslint/no-unused-vars': ['error', { 'argsIgnorePattern': '^_' }]
}
};
46 changes: 46 additions & 0 deletions packages/tracing/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@synapsecns/tracing",
"version": "0.1.0",
"description": "Tracing and profiling utilities for Synapse services",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
"files": ["dist"],
"publishConfig": {
"access": "public"
},
"scripts": {
"prepare": "rollup -c --bundleConfigAsCjs",
"build": "rollup -c --bundleConfigAsCjs",
"prepublish": "yarn build",
"lint:check": "eslint . --max-warnings=0",
"ci:lint": "yarn lint:check",
"test:coverage": "echo 'No tests defined.'"
},
"dependencies": {
"@opentelemetry/api": "1.8.0",
"@opentelemetry/sdk-node": "^0.49.1",
"@opentelemetry/instrumentation-express": "^0.47.0",
"@pyroscope/nodejs": "^0.4.3",
"@opentelemetry/sdk-metrics": "^1.22.0"
},
"peerDependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^28.0.0",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.3.0",
"@rollup/plugin-terser": "^0.4.4",
"rollup": "^4.22.4",
"rollup-plugin-typescript2": "^0.36.0",
"typescript": "^5.3.3",
"@types/node": "^20.0.0",
"@types/jest": "^29.0.0",
"jest": "^29.0.0",
"ts-jest": "^29.0.0",
"eslint": "^8.0.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0"
}
}
38 changes: 38 additions & 0 deletions packages/tracing/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import typescript from 'rollup-plugin-typescript2'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import terser from '@rollup/plugin-terser'

import packageJson from './package.json'

export default [
{
input: 'src/index.ts',
output: [
{
file: packageJson.main,
format: 'cjs',
},
{
file: packageJson.module,
format: 'esm',
},
],
plugins: [
nodeResolve({
preferBuiltins: true,
}),
commonjs(),
json(),
typescript({
tsconfig: './tsconfig.json',
declaration: true,
declarationDir: './dist/types',
useTsconfigDeclarationDir: true,
}),
terser(),
],
external: ['express', '@opentelemetry/api', '@opentelemetry/sdk-node', '@pyroscope/nodejs'],
},
]
3 changes: 3 additions & 0 deletions packages/tracing/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { startPyroscope, type PyroscopeConfig } from './pyroscope';
export { initializeTracing, type TracingConfig } from './tracing';
export { tracingMiddleware, type TracingMiddlewareConfig } from './middleware/express';
42 changes: 42 additions & 0 deletions packages/tracing/src/middleware/express.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { trace, context, SpanStatusCode } from '@opentelemetry/api';
import { Request, Response, NextFunction } from 'express';

export interface TracingMiddlewareConfig {
serviceName?: string;
}

export function tracingMiddleware(config: TracingMiddlewareConfig = {}): (req: Request, res: Response, next: NextFunction) => void {
const tracer = trace.getTracer(config.serviceName || 'rest-api');

return (req: Request, res: Response, next: NextFunction) => {
const span = tracer.startSpan(`${req.method} ${req.path}`, {
attributes: {
'http.method': req.method,
'http.url': req.url,
'http.route': req.path,
},
});

// Set the current span in context
const ctx = trace.setSpan(context.active(), span);
return context.with(ctx, () => {
// Handle response finish
res.on('finish', () => {
span.setAttributes({
'http.status_code': res.statusCode,
});

if (res.statusCode >= 400) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: `HTTP ${res.statusCode}`,
});
}

span.end();
});

next();
});
};
}
43 changes: 43 additions & 0 deletions packages/tracing/src/middleware/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { metrics } from '@opentelemetry/api';
import { Request, Response, NextFunction } from 'express';

const HTTP_REQUEST_DURATION = 'http.server.duration';
const HTTP_REQUEST_ACTIVE = 'http.server.active_requests';

export interface MetricsMiddlewareConfig {
serviceName: string;
}

export function metricsMiddleware(config: MetricsMiddlewareConfig): (req: Request, res: Response, next: NextFunction) => void {
const meter = metrics.getMeter(config.serviceName);

const requestDuration = meter.createHistogram(HTTP_REQUEST_DURATION, {
description: 'Duration of HTTP requests',
unit: 'ms',
});

const activeRequests = meter.createUpDownCounter(HTTP_REQUEST_ACTIVE, {
description: 'Number of concurrent HTTP requests',
});

return (req: Request, res: Response, next: NextFunction) => {
const startTime = Date.now();
const attributes = {
method: req.method,
route: req.path,
};

activeRequests.add(1, attributes);

res.on('finish', () => {
const duration = Date.now() - startTime;
requestDuration.record(duration, {
...attributes,
status_code: res.statusCode.toString(),
});
activeRequests.add(-1, attributes);
});

next();
};
}
27 changes: 27 additions & 0 deletions packages/tracing/src/pyroscope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pyroscope from '@pyroscope/nodejs';

export interface PyroscopeConfig {
applicationName: string;
serverAddress?: string;
tags?: Record<string, string>;
}

export function startPyroscope(config: PyroscopeConfig): void {
if (!process.env.PYROSCOPE_ENDPOINT) {
return;
}

pyroscope.init({
serverAddress: process.env.PYROSCOPE_ENDPOINT,
appName: config.applicationName,
tags: {
...config.tags,
hostname: process.env.HOSTNAME || 'unknown',
},
// @ts-expect-error: pyroscope types are incomplete
profilers: {
cpu: true,
heap: true,
},
});
}
26 changes: 26 additions & 0 deletions packages/tracing/src/tracing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NodeSDK } from '@opentelemetry/sdk-node';
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';

export interface TracingConfig {
serviceName: string;
version: string;
environment?: string;
}

export function initializeTracing(config: TracingConfig): NodeSDK {
const sdk = new NodeSDK({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: config.serviceName,
[SemanticResourceAttributes.SERVICE_VERSION]: config.version,
environment: config.environment || 'development',
}),
instrumentations: [
new ExpressInstrumentation(),
],
});

sdk.start();
return sdk;
}
16 changes: 16 additions & 0 deletions packages/tracing/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "es2018",
"module": "esnext",
"lib": ["esnext", "dom"],
"declaration": true,
"declarationDir": "dist/types",
"outDir": "dist",
"strict": true,
"esModuleInterop": true,
"moduleResolution": "node",
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
5 changes: 4 additions & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
"experimentalDecorators": true,
"typeRoots": [
"node_modules/@types"
]
],
"paths": {
"@synapsecns/*": ["./packages/*/src"]
}
},
"exclude": [
"node_modules",
Expand Down
Loading
Loading