Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instr-aws-sdk): @smithy/[email protected] change broke aws-sdk-v3 instrumentation #1913

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions plugins/node/opentelemetry-instrumentation-aws-sdk/.tav.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"aws-sdk":
# A small subset of releases in the range [2.308.0, 3) to reduce testing time.
versions: "2.308.0 || 2.548.0 || 2.785.0 || 2.1025.0 || 2.1265.0 || 2.1506.0 || >=2.1508.0"
versions: "2.308.0 || 2.556.0 || 2.801.0 || 2.1049.0 || 2.1297.0 || 2.1546.0 || >=2.1548.0"
commands:
- npm run test

"@aws-sdk/client-s3":
# A small subset of releases in the range [3.6.1, 4) to reduce testing time.
# - 3.377.0 was a bad release (see issue #1828).
versions: "3.6.1 || 3.53.0 || 3.163.0 || 3.266.0 || 3.354.0 || 3.458.0 || >=3.462.0"
versions: "3.6.1 || 3.55.0 || 3.180.0 || 3.289.0 || 3.385.0 || 3.498.0 || >=3.503.1"
commands:
- npm run test

"@aws-sdk/client-sqs":
# A small subset of releases in the range [3.24.0, 4) to reduce testing time.
versions: "3.24.0 || 3.85.0 || 3.194.0 || 3.278.0 || 3.357.0 || 3.461.0 || >=3.462.0"
versions: "3.24.0 || 3.94.0 || 3.202.0 || 3.296.0 || 3.388.0 || 3.496.0 || >=3.503.1"
commands:
- npm run test
26 changes: 16 additions & 10 deletions plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
normalizeV3Request,
removeSuffixFromStringIfExists,
} from './utils';
import { propwrap } from './propwrap';
import { RequestMetadata } from './services/ServiceExtension';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

Expand Down Expand Up @@ -112,19 +113,24 @@
v3MiddlewareStackFileNewVersions,
]);

// patch for @smithy/middleware-stack for aws-sdk packages v3.363.0+
const v3SmithyMiddlewareStackFile = new InstrumentationNodeModuleFile(
'@smithy/middleware-stack/dist-cjs/MiddlewareStack.js',
['>=1.0.1'],
this.patchV3ConstructStack.bind(this),
this.unpatchV3ConstructStack.bind(this)
);
// Patch for @smithy/middleware-stack for @aws-sdk/* packages v3.363.0+.
// As of @smithy/[email protected] `constructStack` is only available
// as a getter, so we cannot use `this._wrap()`.
const self = this;
const v3SmithyMiddlewareStack = new InstrumentationNodeModuleDefinition(
'@smithy/middleware-stack',
['>=2.0.0'],
undefined,
undefined,
[v3SmithyMiddlewareStackFile]
(moduleExports, moduleVersion) => {
const newExports = propwrap(

Check warning on line 124 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts#L123-L124

Added lines #L123 - L124 were not covered by tests
moduleExports,
'constructStack',
(orig: any) => {
self._diag.debug('propwrapping aws-sdk v3 constructStack');
return self._getV3ConstructStackPatch(moduleVersion, orig);

Check warning on line 129 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts#L127-L129

Added lines #L127 - L129 were not covered by tests
}
);
return newExports;

Check warning on line 132 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/aws-sdk.ts#L132

Added line #L132 was not covered by tests
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for reviewers: There is now no unwrapping. I'm not sure if that has possible adverse effects.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to admit i'm not as knowledgeable on this as I'd like. IIUC we use the unwrapping mostly for testing and ensuring we don't double wrap a library (using auto-instrumentations and aws-sdk instrumentation directly and registering the same one twice, or registering it twice, but with different instrumentation versions). It could also be that users want to disable the instrumentation on runtime for some reason (though, I've never seen such a thing done IRL).

Hoping that maybe @blumamir or @dyladan have more insights.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was discussed in the OTel JS SIG today (https://docs.google.com/document/d/1tCyoQK49WVcE-x8oryZOTTToFm7sIeUhxFPm9g-qL1k/edit#heading=h.7y4kcwttgzlh). Dan suggested not having an unwrap would be fine. In his opinion (I hope I'm not misrepresenting), instrumentation unwrap functionality in general is really there for testing and probably isn't something to rely heavily on for production usage.

);

const v3SmithyClient = new InstrumentationNodeModuleDefinition<typeof AWS>(
Expand Down
155 changes: 155 additions & 0 deletions plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for reviewers: I'm open to suggestions if we want this in a more shareable place. Or perhaps we consider that if/when there is some other instrumentation that requires a similar facility for wrapping.

Copy link
Member

@pichlermarc pichlermarc Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely useful to have it in a more shareable place. I think if we run into the same problem somewhere else, we can consider moving that to the @opentelemetry/instrumentation package.

Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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.
*/

/*
* This block is derived from esbuild's bundling support.
* https://github.com/evanw/esbuild/blob/v0.14.42/internal/runtime/runtime.go#L22
*
* License:
* MIT License
*
* Copyright (c) 2020 Evan Wallace
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
const __defProp = Object.defineProperty;
const __getOwnPropDesc = Object.getOwnPropertyDescriptor;
const __hasOwnProp = Object.prototype.hasOwnProperty;
const __getOwnPropNames = Object.getOwnPropertyNames;
const __copyProps = (
to: any,
from: any,
except: string,
desc?: PropertyDescriptor | undefined
) => {
if ((from && typeof from === 'object') || typeof from === 'function') {
for (const key of __getOwnPropNames(from)) {
if (!__hasOwnProp.call(to, key) && key !== except) {
__defProp(to, key, {
get: () => from[key] as any,
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,

Check warning on line 59 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts#L53-L59

Added lines #L53 - L59 were not covered by tests
});
}
}
}
return to;

Check warning on line 64 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts#L64

Added line #L64 was not covered by tests
};

/**
* Return a new object that is a copy of `obj`, with its `subpath` property
* replaced with the return value of `wrapper(original)`.
*
* This is similar to shimmer (i.e. `InstrumentationBase.prototype._wrap`).
* However, it uses a different technique to support wrapping properties that
* are only available via a getter (i.e. their property descriptor is `.writable
* === false`).
*
* For example:
* var os = propwrap(require('os'), 'platform', (orig) => {
* return function wrappedPlatform () {
* return orig().toUpperCase()
* }
* })
* console.log(os.platform()) // => DARWIN
*
* The subpath can indicate a nested property. Each property in that subpath,
* except the last, must identify an *Object*.
*
* Limitations:
* - This doesn't handle possible Symbol properties on the copied object(s).
* - This cannot wrap a property of a function, because we cannot create a
* copy of the function.
*
* @param {object} obj
* @param {string} subpath - The property subpath on `obj` to wrap. This may
* point to a nested property by using a '.' to separate levels. For example:
* var fs = wrap(fs, 'promises.sync', (orig) => { ... })
* @param {Function} wrapper - A function of the form `function (orig)`, where
* `orig` is the original property value. This must synchronously return the
* new property value.
* @returns {object} A new object with the wrapped property.
* @throws {TypeError} if the subpath points to a non-existant property, or if
* any but the last subpath part points to a non-Object.
*/
export const propwrap = (obj: any, subpath: string, wrapper: Function): any => {
const parts = subpath.split('.');
const namespaces = [obj];
let namespace = obj;

Check warning on line 106 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts#L104-L106

Added lines #L104 - L106 were not covered by tests
let key;
let val;

// 1. Traverse the subpath parts to sanity check and get references to the
// Objects that we will be copying.
for (let i = 0; i < parts.length; i++) {
key = parts[i];
val = namespace[key];
if (!val) {
throw new TypeError(

Check warning on line 116 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts#L112-L116

Added lines #L112 - L116 were not covered by tests
`cannot wrap "${subpath}": "<obj>.${parts
.slice(0, i)
.join('.')}" is ${typeof val}`
);
} else if (i < parts.length - 1) {
if (typeof val !== 'object') {
throw new TypeError(

Check warning on line 123 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts#L121-L123

Added lines #L121 - L123 were not covered by tests
`cannot wrap "${subpath}": "<obj>.${parts
.slice(0, i)
.join('.')}" is not an Object`
);
}
namespace = val;
namespaces.push(namespace);

Check warning on line 130 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts#L129-L130

Added lines #L129 - L130 were not covered by tests
}
}

// 2. Now work backwards, wrapping each namespace with a new object that has a
// copy of all the properties, except the one that we've wrapped.
for (let i = parts.length - 1; i >= 0; i--) {
key = parts[i];
namespace = namespaces[i];
if (i === parts.length - 1) {
const orig = namespace[key];
val = wrapper(orig);

Check warning on line 141 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts#L136-L141

Added lines #L136 - L141 were not covered by tests
} else {
val = namespaces[i + 1];

Check warning on line 143 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts#L143

Added line #L143 was not covered by tests
}
const desc = __getOwnPropDesc(namespace, key);
const wrappedNamespace = __defProp({}, key, {

Check warning on line 146 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts#L145-L146

Added lines #L145 - L146 were not covered by tests
value: val,
enumerable: !desc || desc.enumerable,

Check warning on line 148 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts#L148

Added line #L148 was not covered by tests
});
__copyProps(wrappedNamespace, namespace, key);
namespaces[i] = wrappedNamespace;

Check warning on line 151 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts#L150-L151

Added lines #L150 - L151 were not covered by tests
}

return namespaces[0];

Check warning on line 154 in plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts

View check run for this annotation

Codecov / codecov/patch

plugins/node/opentelemetry-instrumentation-aws-sdk/src/propwrap.ts#L154

Added line #L154 was not covered by tests
};
Loading