@skbkontur/node-core
Advanced tools
Comparing version 1.3.0-rc.4 to 1.4.0-rc.1
@@ -1,2 +0,1 @@ | ||
# Конфигурация | ||
@@ -12,2 +11,4 @@ Для создания инстанса coreApp необходимо обязательно указать `config` cо всеми обязательными полями и объект `options` с полями которые в вашем случае отличаются от дефолтных. | ||
`config.loggerServerName` - Адрес ELK сервера для отправки логов | ||
`config.loggerApiKey` - Api ключ для ELK логера | ||
@@ -52,3 +53,7 @@ | ||
const coreApp = core.runApp(process, config, { // Значения по умолчанию | ||
const clusterConfigZone = '...' | ||
const clusterConfig = await core.initClusterConfig(clusterConfigZone) | ||
const coreApp = await core.runApp(config, clusterConfig, { // Значения по умолчанию | ||
monitoring: true, | ||
@@ -55,0 +60,0 @@ processMonitoring: true, |
@@ -13,25 +13,25 @@ // @flow | ||
useCore | ||
} = require('./middleware') | ||
} = require('./middleware'); | ||
const clusterClient = require('@skbkontur/cluster-client') | ||
const winstonKonturLogstash = require('@skbkontur/winston-kontur-logstash') | ||
const ClusterConfigClient = require('@skbkontur/cluster-config-client') | ||
const MonitoringClient = require('@skbkontur/monitoring-client') | ||
const SpanAggregatorClient = require('@skbkontur/span-aggregator-client') | ||
const clusterClient = require('@skbkontur/cluster-client'); | ||
const winstonKonturLogstash = require('@skbkontur/winston-kontur-logstash'); | ||
const ClusterConfigClient = require('@skbkontur/cluster-config-client'); | ||
const MonitoringClient = require('@skbkontur/monitoring-client'); | ||
const SpanAggregatorClient = require('@skbkontur/span-aggregator-client'); | ||
const types = require('./types') | ||
const types = require('./types'); | ||
const initComponents = require('./initComponents') | ||
const providers = require('./providers') | ||
const initComponents = require('./initComponents'); | ||
const providers = require('./providers'); | ||
function checkConfig(config) { | ||
for (let propertyName in config) { | ||
const value = config[propertyName] | ||
const value = config[propertyName]; | ||
if (typeof value === 'undefined' || value === null) { | ||
throw new Error(`Config property can not be null or undefined, but ${propertyName} = ${value}.`) | ||
throw new Error(`Config property can not be null or undefined, but ${propertyName} = ${value}.`); | ||
} | ||
if (typeof value === 'string' && value.startsWith('#{')) { | ||
throw new Error(`Config property ${propertyName} = ${value} looks like placeholder.`) | ||
throw new Error(`Config property ${propertyName} = ${value} looks like placeholder.`); | ||
} | ||
@@ -51,30 +51,34 @@ } | ||
prefixGraphiteTopology: 'topology/graphite-prod/' | ||
} | ||
}; | ||
async function runApp(config /* : Config */, clusterConfig /* : ClusterConfigClient */, options /* : UserOptions */) { | ||
const opts = Object.assign({}, defaultOptions, options) | ||
let logger = console | ||
const opts = Object.assign({}, defaultOptions, options); | ||
if (!options || !options.monitoring) { | ||
opts.monitoring = process.env.NODE_ENV === 'production'; | ||
} | ||
let logger = console; | ||
if (opts.errorHandling) { | ||
process.on('uncaughtException', err => { | ||
logger.error(`[uncaughtException] ${err.name} ${err.message} ${err.stack}`) | ||
}) | ||
logger.error(`[uncaughtException] ${err.name} ${err.message} ${err.stack}`); | ||
}); | ||
process.on('unhandledRejection', err => { | ||
logger.error(`[unhandledRejection] ${err.name} ${err.message} ${err.stack}`) | ||
}) | ||
logger.error(`[unhandledRejection] ${err.name} ${err.message} ${err.stack}`); | ||
}); | ||
} | ||
checkConfig(config) | ||
checkConfig(config); | ||
const coreComponents = await initComponents(config, clusterConfig, opts) | ||
logger = coreComponents.logger | ||
const coreComponents = await initComponents(config, clusterConfig, opts); | ||
logger = coreComponents.logger; | ||
return coreComponents | ||
return coreComponents; | ||
} | ||
async function initClusterConfig(zone /* : string */) /* : Promise<ClusterConfigClient> */ { | ||
const clusterConfig = new ClusterConfigClient({ zone }) | ||
await clusterConfig.waitForInitialization() | ||
const clusterConfig = new ClusterConfigClient({ zone }); | ||
await clusterConfig.waitForInitialization(); | ||
return clusterConfig | ||
return clusterConfig; | ||
} | ||
@@ -96,2 +100,2 @@ | ||
} | ||
} | ||
}; |
@@ -9,20 +9,20 @@ // @flow | ||
const loggerModule = require('./logger') | ||
const loggerModule = require('./logger'); | ||
const SpanAggregator = require('@skbkontur/span-aggregator-client') | ||
const SpanAggregator = require('@skbkontur/span-aggregator-client'); | ||
const MonitoringClient = require('@skbkontur/monitoring-client') | ||
const Monitor = require('@skbkontur/monitoring-client/lib/Monitor') | ||
const MonitoringClient = require('@skbkontur/monitoring-client'); | ||
const Monitor = require('@skbkontur/monitoring-client/lib/Monitor'); | ||
async function createSpanAggr(config /* : Config */, options /* : Options */, clusterConfig /* : ClusterConfigClient */) { | ||
let spanAggregator = null | ||
let spanAggregator = null; | ||
if (options.tracing) { | ||
const { spanAggrApiKey, hostName } = config | ||
const { spanAggrApiKey, hostName } = config; | ||
const spanAggrHostname = await clusterConfig.getSettingsByPrefix(options.prefixSpanAggrHostname) | ||
const spanAggrProtocol = await clusterConfig.getSettingsByPrefix(options.prefixSpanAggrProtocol) | ||
const spanAggrHostname = await clusterConfig.getSettingsByPrefix(options.prefixSpanAggrHostname); | ||
const spanAggrProtocol = await clusterConfig.getSettingsByPrefix(options.prefixSpanAggrProtocol); | ||
if (!spanAggrApiKey) { | ||
throw new TypeError('Config should contains spanAggrApiKey') | ||
throw new TypeError('Config should contains spanAggrApiKey'); | ||
} | ||
@@ -35,33 +35,33 @@ | ||
protocol: spanAggrProtocol | ||
}) | ||
}); | ||
} | ||
return spanAggregator | ||
return spanAggregator; | ||
} | ||
async function runMonitoring(config /* : Config */, options/* : Options */, clusterConfig /* : ClusterConfigClient */) { | ||
let monitoringClient = null | ||
let monitor = null | ||
let monitoringClient = null; | ||
let monitor = null; | ||
if (options.monitoring) { | ||
const { appName, id } = config | ||
const { appName, id } = config; | ||
const metricsPrefix = [ appName, id ] | ||
const graphiteUrl = await clusterConfig.getSettingsByPrefix(options.prefixGraphiteTopology) | ||
const metricsPrefix = [ appName, id ]; | ||
const graphiteUrl = await clusterConfig.getSettingsByPrefix(options.prefixGraphiteTopology); | ||
monitoringClient = new MonitoringClient(metricsPrefix, graphiteUrl) | ||
monitoringClient = new MonitoringClient(metricsPrefix, graphiteUrl); | ||
if (options.processMonitoring) { | ||
monitor = new Monitor(monitoringClient) | ||
monitor = new Monitor(monitoringClient); | ||
} | ||
} | ||
return { monitoringClient, monitor } | ||
return { monitoringClient, monitor }; | ||
} | ||
async function initComponents(config /* : Config */, clusterConfig /* : ClusterConfigClient */, options /* : Options */) { | ||
const logger = loggerModule.createLogger(config, options) | ||
const logger = loggerModule.createLogger(config, options); | ||
const { monitoringClient, monitor } = await runMonitoring(config, options, clusterConfig) | ||
const spanAggregator = await createSpanAggr(config, options, clusterConfig) | ||
const { monitoringClient, monitor } = await runMonitoring(config, options, clusterConfig); | ||
const spanAggregator = await createSpanAggr(config, options, clusterConfig); | ||
@@ -76,5 +76,5 @@ return { | ||
options | ||
} | ||
}; | ||
} | ||
module.exports = initComponents | ||
module.exports = initComponents; |
@@ -14,4 +14,4 @@ // @flow | ||
const winston = require('winston') | ||
const KonturLogstash = require('@skbkontur/winston-kontur-logstash') | ||
const winston = require('winston'); | ||
const KonturLogstash = require('@skbkontur/winston-kontur-logstash'); | ||
@@ -25,3 +25,3 @@ const KONTUR_LOGGER_LEVELS = { | ||
trace: 5 | ||
} | ||
}; | ||
@@ -35,5 +35,5 @@ const KONTUR_LOGGER_COLORS = { | ||
trace: 'blue' | ||
} | ||
}; | ||
let logger /* : ?ILogger */ = null | ||
let logger /* : ?ILogger */ = null; | ||
@@ -47,7 +47,7 @@ function createLogger(config /* : Config */, options /* : Options */) /* : ILogger */ { | ||
loggerApiKey | ||
} = config | ||
} = config; | ||
const transports = [ | ||
new winston.transports.Console({ colorize: true }) | ||
] | ||
]; | ||
@@ -60,7 +60,7 @@ if (options.defaultLoggerTransport) { | ||
apiKey: `ELK ${loggerApiKey}` | ||
} | ||
}; | ||
const konturLogstash = new KonturLogstash(ELKOptions) | ||
const konturLogstash = new KonturLogstash(ELKOptions); | ||
transports.push(konturLogstash) | ||
transports.push(konturLogstash); | ||
} | ||
@@ -71,10 +71,10 @@ | ||
transports | ||
}) | ||
winston.addColors(KONTUR_LOGGER_COLORS) | ||
}); | ||
winston.addColors(KONTUR_LOGGER_COLORS); | ||
return logger | ||
return logger; | ||
} | ||
function getLogger() { | ||
return logger || console | ||
return logger || console; | ||
} | ||
@@ -85,2 +85,2 @@ | ||
getLogger | ||
} | ||
}; |
@@ -8,29 +8,29 @@ // @flow | ||
const util = require('util') | ||
const util = require('util'); | ||
function appErrorHandlerModule(logger /* : ILogger */) { | ||
return async function appErrorHandler(err /* : Error */, ctx /* : Context */) { | ||
let availableLogger = logger | ||
let availableLogger = logger; | ||
if (ctx && ctx.state && ctx.state.container) { | ||
try { | ||
availableLogger = ctx.state.container.resolve('logger') | ||
availableLogger = ctx.state.container.resolve('logger'); | ||
} catch (ex) {} | ||
} | ||
let message = 'message is missing' | ||
let message = 'message is missing'; | ||
if (err.name && err.message && err.stack) { | ||
message = `[${err.name}] ${err.message} ${err.stack}` | ||
message = `[${err.name}] ${err.message} ${err.stack}`; | ||
} else { | ||
message = util.inspect(err) | ||
message = util.inspect(err); | ||
} | ||
if (typeof err.status === 'number' && err.status < 500) { | ||
availableLogger.error(message) | ||
availableLogger.error(message); | ||
} else { | ||
availableLogger.fatal(message) | ||
availableLogger.fatal(message); | ||
} | ||
} | ||
}; | ||
} | ||
module.exports = appErrorHandlerModule | ||
module.exports = appErrorHandlerModule; |
@@ -11,19 +11,19 @@ // @flow | ||
try { | ||
await next() | ||
await next(); | ||
} catch (error) { | ||
ctx.status = error.status || 500 | ||
ctx.status = error.status || 500; | ||
if (ctx.status >= 400) { | ||
let err = {} | ||
let err = {}; | ||
err.status = String(ctx.status) | ||
err.type = error.type | ||
err.title = error.message | ||
err.status = String(ctx.status); | ||
err.type = error.type; | ||
err.title = error.message; | ||
ctx.body = { | ||
errors: [err] | ||
} | ||
}; | ||
} | ||
ctx.app.emit('error', error, ctx) | ||
ctx.app.emit('error', error, ctx); | ||
} | ||
} | ||
}; |
@@ -7,8 +7,8 @@ // @flow | ||
const requestLogger = require('./requestLogger') | ||
const errorHandler = require('./errorHandler') | ||
const requestMonitoring = require('./requestMonitoring') | ||
const tracing = require('./tracing') | ||
const requestLogger = require('./requestLogger'); | ||
const errorHandler = require('./errorHandler'); | ||
const requestMonitoring = require('./requestMonitoring'); | ||
const tracing = require('./tracing'); | ||
const appErrorHandler = require('./appErrorHandler') | ||
const appErrorHandler = require('./appErrorHandler'); | ||
@@ -21,23 +21,23 @@ function useCore(app /* : Application */, coreApp /* : any */) { | ||
options | ||
} = coreApp | ||
} = coreApp; | ||
app.context.core = coreApp | ||
app.context.core = coreApp; | ||
if (options.errorHandling) { | ||
app.use(errorHandler) | ||
app.use(errorHandler); | ||
} | ||
if (options.requestLogger) { | ||
app.use(requestLogger(logger)) | ||
app.use(requestLogger(logger)); | ||
} | ||
if (options.processMonitoring) { | ||
app.use(requestMonitoring(monitor)) | ||
if (options.monitoring && options.processMonitoring) { | ||
app.use(requestMonitoring(monitor)); | ||
} | ||
if (options.tracing) { | ||
app.use(tracing(spanAggregator)) | ||
app.use(tracing(spanAggregator)); | ||
} | ||
app.on('error', appErrorHandler(logger)) | ||
app.on('error', appErrorHandler(logger)); | ||
} | ||
@@ -54,2 +54,2 @@ | ||
} | ||
} | ||
}; |
@@ -13,12 +13,12 @@ // @flow | ||
) => { | ||
await next() | ||
await next(); | ||
const method = ctx.request.method | ||
const url = ctx.request.url | ||
const status = ctx.response.status | ||
const method = ctx.request.method; | ||
const url = ctx.request.url; | ||
const status = ctx.response.status; | ||
logger.info(`${method} ${url}, response: ${status}`) | ||
} | ||
} | ||
logger.info(`${method} ${url}, response: ${status}`); | ||
}; | ||
}; | ||
module.exports = requestMonitoringModule | ||
module.exports = requestMonitoringModule; |
@@ -11,11 +11,11 @@ // @flow | ||
) { | ||
const start = Date.now() | ||
const start = Date.now(); | ||
await next() | ||
const delta = Math.ceil(Date.now() - start) | ||
await next(); | ||
const delta = Math.ceil(Date.now() - start); | ||
monitor.trackRequest(delta) | ||
} | ||
} | ||
monitor.trackRequest(delta); | ||
}; | ||
}; | ||
module.exports = requestMonitoringModule | ||
module.exports = requestMonitoringModule; |
@@ -6,5 +6,5 @@ // @flow | ||
*/ | ||
const Span = require('@skbkontur/span-aggregator-client/lib/Span') | ||
const Span = require('@skbkontur/span-aggregator-client/lib/Span'); | ||
const { HEADERS } = Span | ||
const { HEADERS } = Span; | ||
@@ -16,15 +16,15 @@ const tracingModule = (spanAggregator /* : SpanAggregatorClient */) => { | ||
) { | ||
const spanId = request.headers[HEADERS.spanId] | ||
const parentSpanId = request.headers[HEADERS.parentSpanId] | ||
const traceId = request.headers[HEADERS.traceId] | ||
const spanId = request.headers[HEADERS.spanId]; | ||
const parentSpanId = request.headers[HEADERS.parentSpanId]; | ||
const traceId = request.headers[HEADERS.traceId]; | ||
const span = new Span({ spanId, parentSpanId, traceId }) | ||
state.span = span | ||
const span = new Span({ spanId, parentSpanId, traceId }); | ||
state.span = span; | ||
await next() | ||
await next(); | ||
spanAggregator.send(span.close()) | ||
} | ||
} | ||
spanAggregator.send(span.close()); | ||
}; | ||
}; | ||
module.exports = tracingModule | ||
module.exports = tracingModule; |
@@ -8,5 +8,5 @@ // @flow | ||
senders | ||
} = require('@skbkontur/cluster-client') | ||
const { SequentialRequestStrategy } = strategies | ||
const { RequestSender } = senders | ||
} = require('@skbkontur/cluster-client'); | ||
const { SequentialRequestStrategy } = strategies; | ||
const { RequestSender } = senders; | ||
@@ -34,6 +34,6 @@ /* :: | ||
constructor({ clusterConfig } /* : Deps */) { | ||
this._clusterConfig = clusterConfig | ||
this._requestSender = new RequestSender() | ||
this._clusterConfig = clusterConfig; | ||
this._requestSender = new RequestSender(); | ||
this._strategy = new SequentialRequestStrategy(3) | ||
this._strategy = new SequentialRequestStrategy(3); | ||
} | ||
@@ -45,8 +45,8 @@ | ||
serviceDiscoveryZone /* : ?string */) /* : ClusterClient */ { | ||
const topology = new Topology(this._clusterConfig, topologyName, serviceDiscoveryName, serviceDiscoveryZone) | ||
const topology = new Topology(this._clusterConfig, topologyName, serviceDiscoveryName, serviceDiscoveryZone); | ||
return new ClusterClient(topology, this._strategy, this._requestSender) | ||
return new ClusterClient(topology, this._strategy, this._requestSender); | ||
} | ||
} | ||
module.exports = ClusterClientProvider | ||
module.exports = ClusterClientProvider; |
@@ -6,12 +6,12 @@ const { | ||
senders | ||
} = require('@skbkontur/cluster-client') | ||
const { ForkingRequestStrategy } = strategies | ||
const { RequestSender } = senders | ||
} = require('@skbkontur/cluster-client'); | ||
const { ForkingRequestStrategy } = strategies; | ||
const { RequestSender } = senders; | ||
class ForkingClusterClientProvider { | ||
constructor({ clusterConfig } /* : Deps */) { | ||
this._clusterConfig = clusterConfig | ||
this._requestSender = new RequestSender() | ||
this._clusterConfig = clusterConfig; | ||
this._requestSender = new RequestSender(); | ||
this._strategy = new ForkingRequestStrategy(300) | ||
this._strategy = new ForkingRequestStrategy(300); | ||
} | ||
@@ -23,8 +23,8 @@ | ||
serviceDiscoveryZone /* : ?string */) /* : ClusterClient */ { | ||
const topology = new Topology(this._clusterConfig, topologyName, serviceDiscoveryName, serviceDiscoveryZone) | ||
const topology = new Topology(this._clusterConfig, topologyName, serviceDiscoveryName, serviceDiscoveryZone); | ||
return new ClusterClient(topology, this._strategy, this._requestSender) | ||
return new ClusterClient(topology, this._strategy, this._requestSender); | ||
} | ||
} | ||
module.exports = ForkingClusterClientProvider | ||
module.exports = ForkingClusterClientProvider; |
// @flow | ||
const TraceClusterClientProvider = require('./TraceClusterClientProvider') | ||
const ClusterClientProvider = require('./ClusterClientProvider') | ||
const SingularClusterClientProvider = require('./SingularClusterClientProvider') | ||
const ForkingClusterClientProvider = require('./ForkingClusterClientProvider') | ||
const TraceClusterClientProvider = require('./TraceClusterClientProvider'); | ||
const ClusterClientProvider = require('./ClusterClientProvider'); | ||
const SingularClusterClientProvider = require('./SingularClusterClientProvider'); | ||
const ForkingClusterClientProvider = require('./ForkingClusterClientProvider'); | ||
@@ -13,2 +13,2 @@ module.exports = { | ||
ForkingClusterClientProvider | ||
} | ||
}; |
@@ -8,5 +8,5 @@ // @flow | ||
senders | ||
} = require('@skbkontur/cluster-client') | ||
const { SingularRequestStrategy } = strategies | ||
const { RequestSender } = senders | ||
} = require('@skbkontur/cluster-client'); | ||
const { SingularRequestStrategy } = strategies; | ||
const { RequestSender } = senders; | ||
@@ -34,6 +34,6 @@ /* :: | ||
constructor({ clusterConfig } /* : Deps */) { | ||
this._clusterConfig = clusterConfig | ||
this._requestSender = new RequestSender() | ||
this._clusterConfig = clusterConfig; | ||
this._requestSender = new RequestSender(); | ||
this._strategy = new SingularRequestStrategy(clusterConfig, 3) | ||
this._strategy = new SingularRequestStrategy(clusterConfig, 3); | ||
} | ||
@@ -46,11 +46,11 @@ | ||
if (!serviceDiscoveryName) { | ||
throw new TypeError('serviceDiscoveryName is required') | ||
throw new TypeError('serviceDiscoveryName is required'); | ||
} | ||
const topology = new Topology(this._clusterConfig, topologyName, serviceDiscoveryName, serviceDiscoveryZone) | ||
const topology = new Topology(this._clusterConfig, topologyName, serviceDiscoveryName, serviceDiscoveryZone); | ||
return new ClusterClient(topology, this._strategy, this._requestSender) | ||
return new ClusterClient(topology, this._strategy, this._requestSender); | ||
} | ||
} | ||
module.exports = SingularClusterClientProvider | ||
module.exports = SingularClusterClientProvider; |
@@ -25,5 +25,5 @@ // @flow | ||
senders | ||
} = require('@skbkontur/cluster-client') | ||
const { SequentialRequestStrategy } = strategies | ||
const { TraceRequestSender } = senders | ||
} = require('@skbkontur/cluster-client'); | ||
const { SequentialRequestStrategy } = strategies; | ||
const { TraceRequestSender } = senders; | ||
@@ -36,6 +36,6 @@ class ClusterClientProvider /* :: implements IClusterClientProvider */ { | ||
constructor({ clusterConfig, span, spanAggregator } /* : Deps */) { | ||
this._clusterConfig = clusterConfig | ||
this._strategy = new SequentialRequestStrategy(3) | ||
this._clusterConfig = clusterConfig; | ||
this._strategy = new SequentialRequestStrategy(3); | ||
this._requestSender = new TraceRequestSender(span, spanAggregator) | ||
this._requestSender = new TraceRequestSender(span, spanAggregator); | ||
} | ||
@@ -47,8 +47,8 @@ | ||
serviceDiscoveryZone /* : ?string */) /* : ClusterClient */ { | ||
const topology = new Topology(this._clusterConfig, topologyName) | ||
const topology = new Topology(this._clusterConfig, topologyName); | ||
return new ClusterClient(topology, this._strategy, this._requestSender) | ||
return new ClusterClient(topology, this._strategy, this._requestSender); | ||
} | ||
} | ||
module.exports = ClusterClientProvider | ||
module.exports = ClusterClientProvider; |
{ | ||
"name": "@skbkontur/node-core", | ||
"version": "1.3.0-rc.4", | ||
"version": "1.4.0-rc.1", | ||
"description": "Основные компоненты для разработки на Node.js", | ||
@@ -17,3 +17,3 @@ "main": "./lib/index.js", | ||
"@skbkontur/cluster-client": "^1.1.0-rc.3", | ||
"@skbkontur/cluster-config-client": "^0.0.5", | ||
"@skbkontur/cluster-config-client": "^0.1.0-rc.1", | ||
"@skbkontur/monitoring-client": "^1.0.1", | ||
@@ -40,2 +40,6 @@ "@skbkontur/span-aggregator-client": "^0.2.1", | ||
"never" | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
] | ||
@@ -42,0 +46,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43910
839
1
+ Added@skbkontur/cluster-config-client@0.1.0-rc.1(transitive)