chore(server): quick js to ts #5 - remaining stragglers
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
/* istanbul ignore file */
|
||||
'use strict'
|
||||
|
||||
const env = process.env.NODE_ENV || 'development'
|
||||
const configs = require('@/knexfile.js')
|
||||
const { dbStartupLogger } = require('@/logging/logging')
|
||||
import configs from '@/knexfile.js'
|
||||
import { dbStartupLogger } from '@/logging/logging'
|
||||
import knex from 'knex'
|
||||
|
||||
const config = configs[env]
|
||||
|
||||
config.log = {
|
||||
@@ -18,12 +18,7 @@ config.log = {
|
||||
|
||||
dbStartupLogger.info(`Loaded knex conf for ${env}`)
|
||||
|
||||
/**
|
||||
* Need to override type because type def file incorrectly uses ES6
|
||||
* @type {import('knex').default}
|
||||
*/
|
||||
const knex = require('knex')
|
||||
const knexInstance = knex(config)
|
||||
|
||||
module.exports = knexInstance
|
||||
module.exports.db = knexInstance
|
||||
export default knexInstance
|
||||
export { knexInstance as db }
|
||||
@@ -1,19 +1,19 @@
|
||||
/* eslint-disable no-restricted-imports */
|
||||
/* eslint-disable camelcase */
|
||||
/* istanbul ignore file */
|
||||
'use strict'
|
||||
|
||||
const { packageRoot } = require('./bootstrap')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const {
|
||||
import { packageRoot } from './bootstrap'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import {
|
||||
isTestEnv,
|
||||
ignoreMissingMigrations,
|
||||
postgresMaxConnections
|
||||
} = require('@/modules/shared/helpers/envHelper')
|
||||
const { dbLogger: logger } = require('./logging/logging')
|
||||
} from '@/modules/shared/helpers/envHelper'
|
||||
import { dbLogger as logger } from './logging/logging'
|
||||
import { Knex } from 'knex'
|
||||
|
||||
function walk(dir) {
|
||||
let results = []
|
||||
function walk(dir: string) {
|
||||
let results: string[] = []
|
||||
const list = fs.readdirSync(dir)
|
||||
list.forEach(function (file) {
|
||||
const fullFile = path.join(dir, file)
|
||||
@@ -50,7 +50,7 @@ if (env.POSTGRES_USER && env.POSTGRES_PASSWORD) {
|
||||
env.POSTGRES_USER
|
||||
)}:${encodeURIComponent(env.POSTGRES_PASSWORD)}@${
|
||||
env.POSTGRES_URL
|
||||
}/${encodeURIComponent(env.POSTGRES_DB)}`
|
||||
}/${encodeURIComponent(env.POSTGRES_DB as string)}`
|
||||
} else {
|
||||
connectionUri = env.POSTGRES_URL
|
||||
}
|
||||
@@ -77,16 +77,16 @@ const commonConfig = {
|
||||
directory: migrationDirs
|
||||
},
|
||||
log: {
|
||||
warn(message) {
|
||||
warn(message: unknown) {
|
||||
logger.warn(message)
|
||||
},
|
||||
error(message) {
|
||||
error(message: unknown) {
|
||||
logger.error(message)
|
||||
},
|
||||
deprecate(message) {
|
||||
deprecate(message: unknown) {
|
||||
logger.info(message)
|
||||
},
|
||||
debug(message) {
|
||||
debug(message: unknown) {
|
||||
logger.debug(message)
|
||||
}
|
||||
},
|
||||
@@ -100,8 +100,7 @@ const commonConfig = {
|
||||
}
|
||||
}
|
||||
|
||||
/** @type {Object<string, import('knex').Knex.Config>} */
|
||||
const config = {
|
||||
const config: Record<string, Knex.Config> = {
|
||||
test: {
|
||||
...commonConfig,
|
||||
connection: {
|
||||
@@ -129,4 +128,4 @@ const config = {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = config
|
||||
export default config
|
||||
@@ -1,23 +0,0 @@
|
||||
/* istanbul ignore file */
|
||||
const { logger } = require('@/logging/logging')
|
||||
const prometheusClient = require('prom-client')
|
||||
|
||||
let metricErrorCount = null
|
||||
|
||||
module.exports = {
|
||||
errorLoggingMiddleware(err, req, res, next) {
|
||||
if (metricErrorCount === null) {
|
||||
metricErrorCount = new prometheusClient.Counter({
|
||||
name: 'speckle_server_request_errors',
|
||||
help: 'Number of requests that threw exceptions',
|
||||
labelNames: ['route']
|
||||
})
|
||||
}
|
||||
|
||||
logger.error(err, `Error when handling ${req.originalUrl} from ${req.ip}`)
|
||||
let route = 'unknown'
|
||||
if (req.route && req.route.path) route = req.route.path
|
||||
metricErrorCount.labels(route).inc()
|
||||
next(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/* istanbul ignore file */
|
||||
import { logger } from '@/logging/logging'
|
||||
import type { Nullable } from '@speckle/shared'
|
||||
import prometheusClient from 'prom-client'
|
||||
import type express from 'express'
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let metricErrorCount: Nullable<prometheusClient.Counter<any>> = null
|
||||
|
||||
export const errorLoggingMiddleware: express.ErrorRequestHandler = (
|
||||
err,
|
||||
req,
|
||||
res,
|
||||
next
|
||||
) => {
|
||||
if (metricErrorCount === null) {
|
||||
metricErrorCount = new prometheusClient.Counter({
|
||||
name: 'speckle_server_request_errors',
|
||||
help: 'Number of requests that threw exceptions',
|
||||
labelNames: ['route']
|
||||
})
|
||||
}
|
||||
|
||||
logger.error(err, `Error when handling ${req.originalUrl} from ${req.ip}`)
|
||||
let route = 'unknown'
|
||||
if (req.route && req.route.path) route = req.route.path
|
||||
metricErrorCount.labels(route).inc()
|
||||
next(err)
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
/* istanbul ignore file */
|
||||
/* eslint-disable no-unused-vars */
|
||||
'use strict'
|
||||
|
||||
const prometheusClient = require('prom-client')
|
||||
|
||||
let metricActiveConnections = null
|
||||
|
||||
module.exports = {
|
||||
monitorActiveConnections(httpServer) {
|
||||
if (metricActiveConnections !== null) {
|
||||
prometheusClient.register.removeSingleMetric('speckle_server_active_connections')
|
||||
}
|
||||
|
||||
metricActiveConnections = new prometheusClient.Gauge({
|
||||
name: 'speckle_server_active_connections',
|
||||
help: 'Number of active http connections',
|
||||
async collect() {
|
||||
let connectionCount = await new Promise((resolve, reject) => {
|
||||
httpServer.getConnections(function (error, count) {
|
||||
if (error) resolve(-1)
|
||||
else resolve(count)
|
||||
})
|
||||
})
|
||||
if (isNaN(connectionCount)) connectionCount = -1
|
||||
this.set(connectionCount)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/* istanbul ignore file */
|
||||
import type { Nullable } from '@speckle/shared'
|
||||
import prometheusClient from 'prom-client'
|
||||
import type http from 'http'
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let metricActiveConnections: Nullable<prometheusClient.Gauge<any>> = null
|
||||
|
||||
export const monitorActiveConnections = (httpServer: http.Server) => {
|
||||
if (metricActiveConnections !== null) {
|
||||
prometheusClient.register.removeSingleMetric('speckle_server_active_connections')
|
||||
}
|
||||
|
||||
metricActiveConnections = new prometheusClient.Gauge({
|
||||
name: 'speckle_server_active_connections',
|
||||
help: 'Number of active http connections',
|
||||
async collect() {
|
||||
let connectionCount = await new Promise<number>((resolve) => {
|
||||
httpServer.getConnections(function (error, count) {
|
||||
if (error) resolve(-1)
|
||||
else resolve(count)
|
||||
})
|
||||
})
|
||||
if (isNaN(connectionCount)) connectionCount = -1
|
||||
this.set(connectionCount)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,20 +1,17 @@
|
||||
/* istanbul ignore file */
|
||||
const prometheusClient = require('prom-client')
|
||||
const promBundle = require('express-prom-bundle')
|
||||
import prometheusClient from 'prom-client'
|
||||
import promBundle from 'express-prom-bundle'
|
||||
|
||||
const { initKnexPrometheusMetrics } = require('@/logging/knexMonitoring')
|
||||
const {
|
||||
initHighFrequencyMonitoring
|
||||
} = require('@/logging/highFrequencyMetrics/highfrequencyMonitoring')
|
||||
const knex = require('@/db/knex')
|
||||
const {
|
||||
highFrequencyMetricsCollectionPeriodMs
|
||||
} = require('@/modules/shared/helpers/envHelper')
|
||||
const { startupLogger: logger } = require('@/logging/logging')
|
||||
import { initKnexPrometheusMetrics } from '@/logging/knexMonitoring'
|
||||
import { initHighFrequencyMonitoring } from '@/logging/highFrequencyMetrics/highfrequencyMonitoring'
|
||||
import knex from '@/db/knex'
|
||||
import { highFrequencyMetricsCollectionPeriodMs } from '@/modules/shared/helpers/envHelper'
|
||||
import { startupLogger as logger } from '@/logging/logging'
|
||||
import type express from 'express'
|
||||
|
||||
let prometheusInitialized = false
|
||||
|
||||
module.exports = function (app) {
|
||||
export default function (app: express.Express) {
|
||||
if (!prometheusInitialized) {
|
||||
prometheusInitialized = true
|
||||
prometheusClient.register.clear()
|
||||
@@ -114,8 +114,10 @@
|
||||
"test/**/*",
|
||||
"app.ts",
|
||||
"otel.ts",
|
||||
"bootstrap.ts",
|
||||
"knexfile.ts",
|
||||
"bootstrap.js",
|
||||
"knexfile.js"
|
||||
"knexfile.ts"
|
||||
],
|
||||
"exclude": ["node_modules", "coverage", "reports"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user