Skip to content

Commit dcd4a3d

Browse files
authoredNov 8, 2022
Merge pull request newrelic#125 from jordigh/remove-dunder
NEWRELIC-4422: remove __NR prefix properties and usage
2 parents eaf9863 + 0e2af07 commit dcd4a3d

File tree

6 files changed

+82
-111
lines changed

6 files changed

+82
-111
lines changed
 

‎lib/instrumentation/koa/lib/instrumentation.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
'use strict'
7+
const symbols = require('./symbols')
78

89
module.exports = function initialize(shim, Koa) {
910
if (!shim || !Koa || Object.keys(Koa.prototype).length > 1) {
@@ -68,23 +69,23 @@ function wrapCreateContext(shim, fn, fnName, context) {
6869
// The `context.body` and `context.response.body` properties are how users set
6970
// the response contents. It is roughly equivalent to `res.send()` in Express.
7071
// Under the hood, these set the `_body` property on the `context.response`.
71-
context.__NR_body = context.response.body
72-
context.__NR_bodySet = false
72+
context[symbols.body] = context.response.body
73+
context[symbols.bodySet] = false
7374
Object.defineProperty(context.response, '_body', {
74-
get: () => context.__NR_body,
75+
get: () => context[symbols.body],
7576
set: function setBody(val) {
76-
if (!context.__NR_koaRouter) {
77+
if (!context[symbols.koaRouter]) {
7778
shim.savePossibleTransactionName(context.req)
7879
}
79-
context.__NR_body = val
80-
context.__NR_bodySet = true
80+
context[symbols.body] = val
81+
context[symbols.bodySet] = true
8182
}
8283
})
8384

84-
context.__NR_matchedRoute = null
85-
context.__NR_koaRouter = false
85+
context[symbols.matchedRoute] = null
86+
context[symbols.koaRouter] = false
8687
Object.defineProperty(context, '_matchedRoute', {
87-
get: () => context.__NR_matchedRoute,
88+
get: () => context[symbols.matchedRoute],
8889
set: (val) => {
8990
const match = getLayerForTransactionName(context)
9091

@@ -98,7 +99,7 @@ function wrapCreateContext(shim, fn, fnName, context) {
9899
if (currentSegment) {
99100
const transaction = currentSegment.transaction
100101

101-
if (context.__NR_matchedRoute) {
102+
if (context[symbols.matchedRoute]) {
102103
transaction.nameState.popPath()
103104
}
104105

@@ -107,10 +108,10 @@ function wrapCreateContext(shim, fn, fnName, context) {
107108
}
108109
}
109110

110-
context.__NR_matchedRoute = val
111+
context[symbols.matchedRoute] = val
111112
// still true if somehow match is undefined because we are
112113
// using koa-router naming and don't want to allow default naming
113-
context.__NR_koaRouter = true
114+
context[symbols.koaRouter] = true
114115
}
115116
})
116117

@@ -130,7 +131,7 @@ function wrapCreateContext(shim, fn, fnName, context) {
130131
Object.defineProperty(context.response, 'status', {
131132
get: () => statusDescriptor.get.call(context.response),
132133
set: function setStatus(val) {
133-
if (!context.__NR_bodySet && !context.__NR_koaRouter) {
134+
if (!context[symbols.bodySet] && !context[symbols.koaRouter]) {
134135
shim.savePossibleTransactionName(context.req)
135136
}
136137
return statusDescriptor.set.call(this, val)

‎lib/instrumentation/koa/lib/router-instrumentation.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
'use strict'
7+
const symbols = require('./symbols')
78

89
module.exports = function instrumentRouter(shim, Router) {
910
shim.setFramework(shim.KOA)
@@ -76,7 +77,7 @@ function wrapAllowedMethods(shim, fn, name, allowedMethodsMiddleware) {
7677
function wrapAllowedMethodsMiddleware(shim, original) {
7778
return function setRouteHandledOnContextWrapper() {
7879
const [ctx] = shim.argsToArray.apply(shim, arguments)
79-
ctx.__NR_koaRouter = true
80+
ctx[symbols.koaRouter] = true
8081

8182
return original.apply(this, arguments)
8283
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright 2022 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
module.exports = {
9+
body: Symbol('body'),
10+
bodySet: Symbol('bodySet'),
11+
koaRouter: Symbol('koaRouter'),
12+
matchedRoute: Symbol('matchedRoute')
13+
}

‎lib/instrumentation/koa/tests/unit/koa.tap.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,29 @@
55

66
'use strict'
77

8-
var tap = require('tap')
9-
var utils = require('@newrelic/test-utilities')
8+
const tap = require('tap')
9+
const utils = require('@newrelic/test-utilities')
1010

1111
utils.tap
1212

1313
tap.test('Koa instrumentation', function (t) {
14-
var helper = utils.TestAgent.makeInstrumented()
14+
const wrapped = ['createContext', 'use', 'emit']
15+
const notWrapped = ['handleRequest', 'listen', 'toJSON', 'inspect', 'callback', 'onerror']
16+
17+
const helper = utils.TestAgent.makeInstrumented()
1518
helper.registerInstrumentation({
1619
moduleName: 'koa',
1720
type: 'web-framework',
1821
onRequire: require('../../lib/instrumentation')
1922
})
20-
var Koa = require('koa')
21-
22-
var wrapped = ['createContext', 'use', 'emit']
23-
var notWrapped = ['handleRequest', 'listen', 'toJSON', 'inspect', 'callback', 'onerror']
23+
const Koa = require('koa')
24+
const shim = helper.getShim()
2425

2526
wrapped.forEach(function (method) {
26-
t.ok(Koa.prototype[method].__NR_original, method + ' is wrapped, as expected')
27+
t.ok(shim.isWrapped(Koa.prototype[method]), method + ' is wrapped, as expected')
2728
})
2829
notWrapped.forEach(function (method) {
29-
t.notOk(Koa.prototype[method].__NR_original, method + ' is not wrapped, as expected')
30+
t.not(shim.isWrapped(Koa.prototype[method]), method + ' is not wrapped, as expected')
3031
})
3132

3233
helper && helper.unload()

‎lib/instrumentation/koa/tests/unit/route.tap.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const utils = require('@newrelic/test-utilities')
1010
const { METHODS } = require('../../lib/http-methods')
1111

1212
tap.test('koa-route', function (t) {
13-
var helper = utils.TestAgent.makeInstrumented()
13+
const helper = utils.TestAgent.makeInstrumented()
1414

1515
t.teardown(function () {
1616
helper.unload()
@@ -22,10 +22,12 @@ tap.test('koa-route', function (t) {
2222
onRequire: require('../../lib/route-instrumentation.js')
2323
})
2424

25+
const shim = helper.getShim()
26+
2527
t.test('methods', function (t) {
26-
var route = require('koa-route')
28+
const route = require('koa-route')
2729
METHODS.forEach(function checkWrapped(method) {
28-
t.type(route[method].__NR_original, 'function', method + ' should be wrapped')
30+
t.ok(shim.isWrapped(route[method]), method + ' should be wrapped')
2931
})
3032
t.end()
3133
})

‎lib/instrumentation/koa/tests/unit/router.tap.js

+38-85
Original file line numberDiff line numberDiff line change
@@ -21,96 +21,49 @@ const UNWRAPPED_METHODS = METHODS.concat([
2121
])
2222
const UNWRAPPED_STATIC_METHODS = ['url']
2323

24-
tap.test('koa-router', function tests(t) {
25-
var helper = utils.TestAgent.makeInstrumented()
26-
t.teardown(function () {
27-
helper.unload()
28-
})
29-
helper.registerInstrumentation({
30-
type: 'web-framework',
31-
moduleName: 'koa-router',
32-
onRequire: instrumentation
33-
})
34-
35-
t.test('mounting paramware', function (t) {
36-
var Router = require('koa-router')
37-
var router = new Router()
38-
router.param('second', function () {})
39-
t.type(router.params.second.__NR_original, 'function', 'param function should be wrapped')
40-
t.end()
41-
})
24+
const koaRouterMods = ['koa-router', '@koa/router']
4225

43-
t.test('methods', function (t) {
44-
var Router = require('koa-router')
45-
WRAPPED_METHODS.forEach(function checkWrapped(method) {
46-
t.type(
47-
Router.prototype[method].__NR_original,
48-
'function',
49-
method + ' should be a wrapped method on the prototype'
50-
)
26+
koaRouterMods.forEach((koaRouterMod) => {
27+
tap.test(koaRouterMod, function tests(t) {
28+
const helper = utils.TestAgent.makeInstrumented()
29+
t.teardown(function () {
30+
helper.unload()
5131
})
52-
UNWRAPPED_METHODS.forEach(function checkUnwrapped(method) {
53-
t.type(
54-
Router.prototype[method].__NR_original,
55-
'undefined',
56-
method + ' should be a unwrapped method on the prototype'
57-
)
58-
})
59-
UNWRAPPED_STATIC_METHODS.forEach(function checkUnwrappedStatic(method) {
60-
t.type(
61-
Router[method].__NR_original,
62-
'undefined',
63-
method + ' should be an unwrapped static method'
64-
)
65-
})
66-
t.end()
67-
})
68-
t.autoend()
69-
})
32+
const shim = helper.getShim()
7033

71-
tap.test('@koa/router', function tests(t) {
72-
var helper = utils.TestAgent.makeInstrumented()
73-
t.teardown(function () {
74-
helper.unload()
75-
})
76-
helper.registerInstrumentation({
77-
type: 'web-framework',
78-
moduleName: '@koa/router',
79-
onRequire: instrumentation
80-
})
81-
82-
t.test('mounting paramware', function (t) {
83-
var Router = require('@koa/router')
84-
var router = new Router()
85-
router.param('second', function () {})
86-
t.type(router.params.second.__NR_original, 'function', 'param function should be wrapped')
87-
t.end()
88-
})
89-
90-
t.test('methods', function (t) {
91-
var Router = require('@koa/router')
92-
WRAPPED_METHODS.forEach(function checkWrapped(method) {
93-
t.type(
94-
Router.prototype[method].__NR_original,
95-
'function',
96-
method + ' should be a wrapped method on the prototype'
97-
)
34+
helper.registerInstrumentation({
35+
type: 'web-framework',
36+
moduleName: koaRouterMod,
37+
onRequire: instrumentation
9838
})
99-
UNWRAPPED_METHODS.forEach(function checkUnwrapped(method) {
100-
t.type(
101-
Router.prototype[method].__NR_original,
102-
'undefined',
103-
method + ' should be a unwrapped method on the prototype'
104-
)
39+
40+
t.test('mounting paramware', function (t) {
41+
var Router = require(koaRouterMod)
42+
var router = new Router()
43+
router.param('second', function () {})
44+
t.ok(shim.isWrapped(router.params.second), 'param function should be wrapped')
45+
t.end()
10546
})
106-
UNWRAPPED_STATIC_METHODS.forEach(function checkUnwrappedStatic(method) {
107-
t.type(
108-
Router[method].__NR_original,
109-
'undefined',
110-
method + ' should be an unwrapped static method'
111-
)
47+
48+
t.test('methods', function (t) {
49+
var Router = require(koaRouterMod)
50+
WRAPPED_METHODS.forEach(function checkWrapped(method) {
51+
t.ok(
52+
shim.isWrapped(Router.prototype[method]),
53+
method + ' should be a wrapped method on the prototype'
54+
)
55+
})
56+
UNWRAPPED_METHODS.forEach(function checkUnwrapped(method) {
57+
t.not(
58+
shim.isWrapped(Router.prototype[method]),
59+
method + ' should be a unwrapped method on the prototype'
60+
)
61+
})
62+
UNWRAPPED_STATIC_METHODS.forEach(function checkUnwrappedStatic(method) {
63+
t.not(shim.isWrapped(Router[method]), method + ' should be an unwrapped static method')
64+
})
65+
t.end()
11266
})
113-
t.end()
67+
t.autoend()
11468
})
115-
t.autoend()
11669
})

0 commit comments

Comments
 (0)
Please sign in to comment.