Skip to content

Commit bdd88f5

Browse files
authored
feat: adding function for checking wrapped into instrumentation (#1572)
1 parent 83e6af0 commit bdd88f5

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

packages/opentelemetry-instrumentation/src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,13 @@ export interface InstrumentationConfig {
6767
*/
6868
path?: string;
6969
}
70+
71+
/**
72+
* This interface defines the params that are be added to the wrapped function
73+
* using the "shimmer.wrap"
74+
*/
75+
export interface ShimWrapped {
76+
__wrapped: boolean;
77+
__unwrap: Function;
78+
__original: Function;
79+
}

packages/opentelemetry-instrumentation/src/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { ShimWrapped } from './types';
18+
1719
/**
1820
* function to execute patched function and being able to catch errors
1921
* @param execute - function to be executed
@@ -39,3 +41,16 @@ export function safeExecuteInTheMiddle<T>(
3941
return result as T;
4042
}
4143
}
44+
45+
/**
46+
* Checks if certain function has been already wrapped
47+
* @param func
48+
*/
49+
export function isWrapped(func: any) {
50+
return (
51+
typeof func === 'function' &&
52+
typeof (func as ShimWrapped).__original === 'function' &&
53+
typeof (func as ShimWrapped).__unwrap === 'function' &&
54+
(func as ShimWrapped).__wrapped === true
55+
);
56+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as assert from 'assert';
18+
import { isWrapped } from '../../src';
19+
20+
describe('isWrapped', () => {
21+
describe('when function is wrapped', () => {
22+
it('should return true', () => {
23+
const obj: any = {
24+
wrapMe: function () {},
25+
};
26+
obj.wrapMe.__original = function () {};
27+
obj.wrapMe.__unwrap = function () {};
28+
obj.wrapMe.__wrapped = true;
29+
30+
assert.deepStrictEqual(isWrapped(obj.wrapMe), true);
31+
});
32+
});
33+
describe('when function is NOT wrapped', () => {
34+
it('should return false', () => {
35+
const obj: any = {
36+
wrapMe: function () {},
37+
};
38+
obj.wrapMe.__unwrap = function () {};
39+
obj.wrapMe.__wrapped = true;
40+
41+
assert.deepStrictEqual(isWrapped(obj.wrapMe), false);
42+
});
43+
});
44+
});

0 commit comments

Comments
 (0)