Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
init() {
// using shimmer directly cause can only be bundled in node
shimmer.wrap(http, 'get', () => httpGetWrapper(http));
shimmer.wrap(http, 'request', httpWrapper);
shimmer.wrap(https, 'get', () => httpGetWrapper(https));
shimmer.wrap(https, 'request', httpWrapper);
moduleUtils.patchModule(
'wreck',
'request',
WreckWrapper
);
},
};
return function register(
// tslint:disable-next-line:no-any
this: grpcTypes.Server & { handlers: any },
name: string,
handler: grpcTypes.handleCall,
serialize: grpcTypes.serialize,
deserialize: grpcTypes.deserialize,
type: string
) {
const result = originalRegister.apply(this, arguments);
const handlerSet = this.handlers[name];
// Patch the methods that are invoked when a gRPC service call is
// made. The function 'func' is the user-implemented handling function.
shimmer.wrap(
handlerSet,
'func',
(originalFunc: grpcTypes.handleCall) => {
return function func(
this: typeof handlerSet,
call: ServerCallWithMeta,
callback: SendUnaryDataCallback
) {
const self = this;
const traceOptions: TraceOptions = {
name: `grpc.${name.replace('/', '')}`,
kind: SpanKind.SERVER,
};
const spanContext = GrpcPlugin.getSpanContext(call.metadata);
function instrumentPool(Pool) {
shimmer.wrap(Pool, 'query', shimQuery);
// There is also an 'execute' method on the pool object but it uses the connection internally, so we do not need to
// instrument it. This is handled by the instrumented methods on Connection. We do need to instrument 'pool.query',
// though.
shimmer.wrap(Pool, 'getConnection', shimGetConnection);
}
let filenames = []
files.forEach(filePattern => {
if (filePattern) {
filenames = filenames.concat(glob.sync(filePattern, opt))
}
})
exclude_files.forEach(filePattern => {
if (filePattern) {
_.pullAll(filenames, glob.sync(filePattern, opt))
}
})
filenames = _.uniq(filenames)
debug('matched files: %j', filenames)
// wrap Module.prototype._compile
shimmer.wrap(Module.prototype, '_compile', function (__compile) {
return function koaBreakpointCompile (content, filename) {
if (!_.includes(filenames, filename)) {
try {
return __compile.call(this, content, filename)
} catch (e) {
// `try { require('...') } catch (e) { ... }` will not print compile error message
debug('cannot compile file: %s', filename)
debug(e.stack)
throw e
}
}
let parsedCodes
try {
parsedCodes = esprima.parse(content, { loc: true })
} catch (e) {
onCompleted: sinon.stub(),
onError: sinon.stub(),
onNext: sinon.stub(),
getQueryString: sinon.stub().returns(this.query),
getVariables: sinon.stub().returns(this.variables)
}
this.observable = new StaticObservable()
shimmer.wrap(Observable.prototype, 'backoff', function (orig) {
return function () {
var ret = orig.apply(this, arguments)
self.backoffObservable = ret
sinon.spy(self.backoffObservable, 'subscribe')
return ret
}
})
shimmer.wrap(Function.prototype, 'bind', function (orig) {
return function () {
var bound = orig.apply(this, arguments)
bound.__bound = this
return bound
}
})
})
afterEach(function () {
function instrumentExecute(executeModule) {
shimmer.wrap(executeModule, 'execute', shimExecuteFunction.bind(null));
}
return function() {
var subscription = originalFunction.apply(this, arguments);
if (subscription) {
shimmer.wrap(subscription, 'emit', shimSubscriptionEmit.bind(null, natsUrl, arguments[0]));
}
return subscription;
};
}
let instrumentPg = function(pg, opts = {}) {
shimmer.wrap(pg.Client.prototype, "query", function(query) {
return function(...args) {
if (args.length < 1) {
return query.apply(this, args);
}
if (!api.traceActive()) {
return query.apply(this, args);
}
return api.startAsyncSpan(
{
[schema.EVENT_TYPE]: "pg",
[schema.PACKAGE_VERSION]: opts.packageVersion,
[schema.TRACE_SPAN_NAME]: "query",
"db.query": getQueryString(args),
"db.query_args": getQueryArgs(args),
"db.query_name": getQueryName(args),
function patchHttps(https: HttpsModule, api: Tracer) {
shimmer.wrap(https, 'request', request => {
return makeRequestTrace('https:', request, api);
});
shimmer.wrap(https, 'get', function getWrap(): typeof httpsModule.get {
return function getTrace(this: never) {
const req = https.request.apply(this, arguments);
req.end();
return req;
};
});
}
function instrumentRequest(Request) {
shimmer.wrap(Request.prototype, 'query', shimMethod.bind(null, instrumentedRequestMethod));
shimmer.wrap(Request.prototype, 'execute', shimMethod.bind(null, instrumentedRequestMethod));
shimmer.wrap(Request.prototype, 'batch', shimMethod.bind(null, instrumentedRequestMethod));
shimmer.wrap(Request.prototype, 'bulk', shimMethod.bind(null, instrumentedBulk));
}