-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(connect): fix wrong rpcMetada.route value not handle nested route (…
…#1555) * fix(connect): fix wrong rpcMetada.route value not handle nested route * fix: update base on PR feedback * fix: lint issue * Fix PR's feedback * Fix lint issue * Fix typo --------- Co-authored-by: Marc Pichler <[email protected]> Co-authored-by: Gerhard Stöbich <[email protected]> Co-authored-by: Haddas Bronfman <[email protected]>
- Loading branch information
1 parent
8802eae
commit 704f76f
Showing
5 changed files
with
285 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
plugins/node/opentelemetry-instrumentation-connect/src/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { diag } from '@opentelemetry/api'; | ||
import { _LAYERS_STORE_PROPERTY, PatchedRequest } from './internal-types'; | ||
|
||
export const addNewStackLayer = (request: PatchedRequest) => { | ||
if (Array.isArray(request[_LAYERS_STORE_PROPERTY]) === false) { | ||
Object.defineProperty(request, _LAYERS_STORE_PROPERTY, { | ||
enumerable: false, | ||
value: [], | ||
}); | ||
} | ||
request[_LAYERS_STORE_PROPERTY].push('/'); | ||
|
||
const stackLength = request[_LAYERS_STORE_PROPERTY].length; | ||
|
||
return () => { | ||
if (stackLength === request[_LAYERS_STORE_PROPERTY].length) { | ||
request[_LAYERS_STORE_PROPERTY].pop(); | ||
} else { | ||
diag.warn('Connect: Trying to pop the stack multiple time'); | ||
} | ||
}; | ||
}; | ||
|
||
export const replaceCurrentStackRoute = ( | ||
request: PatchedRequest, | ||
newRoute?: string | ||
) => { | ||
if (newRoute) { | ||
request[_LAYERS_STORE_PROPERTY].splice(-1, 1, newRoute); | ||
} | ||
}; | ||
|
||
// generage route from existing stack on request object. | ||
// splash between stack layer will be dedup | ||
// ["/first/", "/second", "/third/"] => /first/second/thrid/ | ||
export const generateRoute = (request: PatchedRequest) => { | ||
return request[_LAYERS_STORE_PROPERTY].reduce( | ||
(acc, sub) => acc.replace(/\/+$/, '') + sub | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
plugins/node/opentelemetry-instrumentation-connect/test/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import * as assert from 'assert'; | ||
|
||
import { PatchedRequest, _LAYERS_STORE_PROPERTY } from '../src/internal-types'; | ||
import { | ||
addNewStackLayer, | ||
generateRoute, | ||
replaceCurrentStackRoute, | ||
} from '../src/utils'; | ||
|
||
describe('utils', () => { | ||
describe('addNewStackLayer', () => { | ||
it('should inject new array to symbol property if not exist', () => { | ||
const fakeRequest = {} as PatchedRequest; | ||
|
||
addNewStackLayer(fakeRequest); | ||
|
||
assert.strictEqual(fakeRequest[_LAYERS_STORE_PROPERTY].length, 1); | ||
}); | ||
|
||
it('should append new stack item if private symbol already exists', () => { | ||
const stack = ['/first']; | ||
const fakeRequest = { | ||
[_LAYERS_STORE_PROPERTY]: stack, | ||
} as PatchedRequest; | ||
|
||
addNewStackLayer(fakeRequest); | ||
|
||
assert.equal(fakeRequest[_LAYERS_STORE_PROPERTY], stack); | ||
assert.strictEqual(fakeRequest[_LAYERS_STORE_PROPERTY].length, 2); | ||
}); | ||
|
||
it('should return pop method to remove newly add stack', () => { | ||
const fakeRequest = {} as PatchedRequest; | ||
|
||
const pop = addNewStackLayer(fakeRequest); | ||
|
||
assert.notStrictEqual(pop, undefined); | ||
|
||
pop(); | ||
|
||
assert.strictEqual(fakeRequest[_LAYERS_STORE_PROPERTY].length, 0); | ||
}); | ||
|
||
it('should prevent pop the same stack item multiple time', () => { | ||
const fakeRequest = {} as PatchedRequest; | ||
|
||
addNewStackLayer(fakeRequest); // add first stack item | ||
const pop = addNewStackLayer(fakeRequest); // add second stack item | ||
|
||
pop(); | ||
pop(); | ||
|
||
assert.strictEqual(fakeRequest[_LAYERS_STORE_PROPERTY].length, 1); | ||
}); | ||
}); | ||
|
||
describe('replaceCurrentStackRoute', () => { | ||
it('should replace the last stack item with new value', () => { | ||
const fakeRequest = { | ||
[_LAYERS_STORE_PROPERTY]: ['/first', '/second'], | ||
} as PatchedRequest; | ||
|
||
replaceCurrentStackRoute(fakeRequest, '/new_route'); | ||
|
||
assert.strictEqual(fakeRequest[_LAYERS_STORE_PROPERTY].length, 2); | ||
assert.strictEqual(fakeRequest[_LAYERS_STORE_PROPERTY][1], '/new_route'); | ||
}); | ||
}); | ||
|
||
describe('generateRoute', () => { | ||
it('should combine the stack and striped any slash between layer', () => { | ||
const fakeRequest = { | ||
[_LAYERS_STORE_PROPERTY]: ['/first/', '/second', '/third/'], | ||
} as PatchedRequest; | ||
|
||
const route = generateRoute(fakeRequest); | ||
|
||
assert.strictEqual(route, '/first/second/third/'); | ||
}); | ||
}); | ||
}); |