@silenteer/natsu
Advanced tools
Comparing version 1.0.9 to 1.0.10
@@ -0,3 +1,4 @@ | ||
import * as Sentry from '@sentry/node'; | ||
import type { NatsService } from './type'; | ||
import type { NatsHandler } from './type'; | ||
import type { NatsRequest, NatsHandler } from './type'; | ||
declare const _default: { | ||
@@ -14,2 +15,6 @@ setup: <TInjection extends Record<string, unknown>>(params: { | ||
}; | ||
sentry?: { | ||
options: Pick<Sentry.NodeOptions, 'dsn' | 'tracesSampleRate' | 'environment' | 'release' | 'enabled'>; | ||
getUser: (data: NatsRequest<unknown>) => Sentry.User; | ||
}; | ||
}) => { | ||
@@ -16,0 +21,0 @@ start: () => Promise<void>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
const Sentry = (0, tslib_1.__importStar)(require("@sentry/node")); | ||
const tracing_1 = require("@sentry/tracing"); | ||
const nats_1 = require("nats"); | ||
@@ -14,3 +17,3 @@ const clients = {}; | ||
} | ||
const { urls, user, pass, verbose } = params; | ||
const { urls, user, pass, verbose, sentry } = params; | ||
const key = getClientKey(urls); | ||
@@ -21,2 +24,5 @@ if (!clients[key]) { | ||
if (!clients[key].natsService) { | ||
if (sentry) { | ||
Sentry.init(Object.assign({ integrations: [new Sentry.Integrations.Http({ tracing: true })], tracesSampleRate: 1.0 }, sentry.options)); | ||
} | ||
const client = yield (0, nats_1.connect)({ | ||
@@ -44,2 +50,3 @@ servers: urls, | ||
: undefined; | ||
let transaction; | ||
try { | ||
@@ -56,38 +63,94 @@ if (!data) { | ||
} | ||
Sentry.setUser(sentry.getUser(data)); | ||
transaction = Sentry.startTransaction({ | ||
name: subject, | ||
traceId: data.headers['trace-id'], | ||
}); | ||
Sentry.getCurrentHub().configureScope((scope) => scope.setSpan(transaction)); | ||
const injection = Object.assign(Object.assign({}, params.injections), { message, | ||
natsService }); | ||
if (handler.validate) { | ||
const validationResult = yield handler.validate(data, injection); | ||
if (validationResult.code !== 'OK') { | ||
respond({ | ||
message, | ||
data: responseCodec.encode(Object.assign(Object.assign({}, data), { code: validationResult.code, body: encodeBody(validationResult.errors) })), | ||
}); | ||
continue; | ||
const validateSpan = transaction.startChild({ | ||
description: `${subject} - validate`, | ||
}); | ||
try { | ||
const validationResult = yield handler.validate(data, injection); | ||
if (validationResult.code !== 'OK') { | ||
respond({ | ||
message, | ||
data: responseCodec.encode(Object.assign(Object.assign({}, data), { code: validationResult.code, body: encodeBody(validationResult.errors) })), | ||
}); | ||
validateSpan.setStatus((0, tracing_1.spanStatusfromHttpCode)(validationResult.code)); | ||
validateSpan.finish(); | ||
transaction.finish(); | ||
continue; | ||
} | ||
else { | ||
validateSpan.setStatus('ok'); | ||
validateSpan.finish(); | ||
} | ||
} | ||
catch (error) { | ||
validateSpan.setStatus('internal_error'); | ||
validateSpan.finish(); | ||
throw error; | ||
} | ||
} | ||
if (handler.authorize) { | ||
const authorizationResult = yield handler.authorize(data, injection); | ||
if (authorizationResult.code !== 'OK') { | ||
respond({ | ||
message, | ||
data: responseCodec.encode(Object.assign(Object.assign({}, data), { code: authorizationResult.code, body: encodeBody(authorizationResult.message) })), | ||
}); | ||
continue; | ||
const authorizeSpan = transaction.startChild({ | ||
description: `${subject} - authorize`, | ||
}); | ||
try { | ||
const authorizationResult = yield handler.authorize(data, injection); | ||
if (authorizationResult.code !== 'OK') { | ||
respond({ | ||
message, | ||
data: responseCodec.encode(Object.assign(Object.assign({}, data), { code: authorizationResult.code, body: encodeBody(authorizationResult.message) })), | ||
}); | ||
authorizeSpan.setStatus((0, tracing_1.spanStatusfromHttpCode)(authorizationResult.code)); | ||
authorizeSpan.finish(); | ||
transaction.finish(); | ||
continue; | ||
} | ||
else { | ||
authorizeSpan.setStatus('ok'); | ||
authorizeSpan.finish(); | ||
} | ||
} | ||
catch (error) { | ||
authorizeSpan.setStatus('internal_error'); | ||
authorizeSpan.finish(); | ||
throw error; | ||
} | ||
} | ||
if (handler.handle) { | ||
const handleResult = yield handler.handle(data, injection); | ||
if (handleResult.code !== 200) { | ||
const handleSpan = transaction.startChild({ | ||
description: `${subject} - handle`, | ||
}); | ||
try { | ||
const handleResult = yield handler.handle(data, injection); | ||
if (handleResult.code !== 200) { | ||
respond({ | ||
message, | ||
data: responseCodec.encode(Object.assign(Object.assign({}, data), { code: handleResult.code, body: encodeBody(handleResult.errors) })), | ||
}); | ||
Sentry.captureMessage(`${subject} [${handleResult.code}]`, { | ||
extra: Object.assign(Object.assign({}, data), { errors: handleResult.errors }), | ||
}); | ||
handleSpan.setStatus((0, tracing_1.spanStatusfromHttpCode)(handleResult.code)); | ||
} | ||
respond({ | ||
message, | ||
data: responseCodec.encode(Object.assign(Object.assign({}, data), { code: handleResult.code, body: encodeBody(handleResult.errors) })), | ||
data: responseCodec.encode(Object.assign(Object.assign({}, data), { headers: handleResult.headers | ||
? Object.assign(Object.assign({}, data.headers), handleResult.headers) : data.headers, code: handleResult.code, body: encodeBody(handleResult.body) })), | ||
}); | ||
continue; | ||
handleSpan.setStatus('ok'); | ||
} | ||
respond({ | ||
message, | ||
data: responseCodec.encode(Object.assign(Object.assign({}, data), { headers: handleResult.headers | ||
? Object.assign(Object.assign({}, data.headers), handleResult.headers) : data.headers, code: handleResult.code, body: encodeBody(handleResult.body) })), | ||
}); | ||
catch (error) { | ||
handleSpan.setStatus('internal_error'); | ||
handleSpan.finish(); | ||
throw error; | ||
} | ||
handleSpan.finish(); | ||
transaction.finish(); | ||
continue; | ||
@@ -99,2 +162,9 @@ } | ||
console.error(error); | ||
Sentry.captureException(error, { | ||
extra: { | ||
subject, | ||
data, | ||
code: 500, | ||
}, | ||
}); | ||
respond({ | ||
@@ -105,2 +175,5 @@ message, | ||
} | ||
finally { | ||
transaction === null || transaction === void 0 ? void 0 : transaction.finish(); | ||
} | ||
} | ||
@@ -212,5 +285,13 @@ } | ||
setup: (params) => { | ||
const { urls, injections, user, pass, verbose, namespace } = params; | ||
const { urls, injections, user, pass, verbose, namespace, sentry } = params; | ||
const client = { | ||
start: () => start({ urls, injections, user, pass, verbose, namespace }), | ||
start: () => start({ | ||
urls, | ||
injections, | ||
user, | ||
pass, | ||
verbose, | ||
namespace, | ||
sentry, | ||
}), | ||
stop: () => stop(urls), | ||
@@ -217,0 +298,0 @@ register: (handlers) => register({ urls, handlers }), |
{ | ||
"name": "@silenteer/natsu", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"license": "MIT", | ||
@@ -19,2 +19,3 @@ "private": false, | ||
"dependencies": { | ||
"@sentry/node": "6.17.5", | ||
"@silenteer/natsu-type": "0.0.10", | ||
@@ -21,0 +22,0 @@ "nats": "^2.2.0" |
Sorry, the diff of this file is not supported yet
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
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
36158
494
3
+ Added@sentry/node@6.17.5
+ Added@sentry/core@6.17.5(transitive)
+ Added@sentry/hub@6.17.5(transitive)
+ Added@sentry/minimal@6.17.5(transitive)
+ Added@sentry/node@6.17.5(transitive)
+ Added@sentry/tracing@6.17.5(transitive)
+ Added@sentry/types@6.17.5(transitive)
+ Added@sentry/utils@6.17.5(transitive)
+ Addedagent-base@6.0.2(transitive)
+ Addedcookie@0.4.2(transitive)
+ Addeddebug@4.4.0(transitive)
+ Addedhttps-proxy-agent@5.0.1(transitive)
+ Addedlru_map@0.3.3(transitive)
+ Addedms@2.1.3(transitive)
+ Addedtslib@1.14.1(transitive)