Skip to content

Commit

Permalink
Merge branch 'main' into aws-dynamo-capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
blumamir authored Aug 12, 2023
2 parents 958925b + 704f76f commit e89d7ad
Show file tree
Hide file tree
Showing 55 changed files with 441 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"devDependencies": {
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/contrib-test-utils": "^0.34.0",
"@opentelemetry/sdk-node": "^0.41.0",
"@opentelemetry/sdk-node": "^0.41.2",
"@types/mocha": "8.2.3",
"@types/node": "18.16.19",
"@types/semver": "7.5.0",
Expand Down
8 changes: 4 additions & 4 deletions metapackages/auto-instrumentations-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"typescript": "4.4.4"
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.41.0",
"@opentelemetry/instrumentation": "^0.41.2",
"@opentelemetry/instrumentation-amqplib": "^0.33.0",
"@opentelemetry/instrumentation-aws-lambda": "^0.36.0",
"@opentelemetry/instrumentation-aws-sdk": "^0.35.0",
Expand All @@ -64,9 +64,9 @@
"@opentelemetry/instrumentation-fastify": "^0.32.0",
"@opentelemetry/instrumentation-generic-pool": "^0.32.0",
"@opentelemetry/instrumentation-graphql": "^0.35.0",
"@opentelemetry/instrumentation-grpc": "^0.41.0",
"@opentelemetry/instrumentation-grpc": "^0.41.2",
"@opentelemetry/instrumentation-hapi": "^0.32.0",
"@opentelemetry/instrumentation-http": "^0.41.0",
"@opentelemetry/instrumentation-http": "^0.41.2",
"@opentelemetry/instrumentation-ioredis": "^0.35.0",
"@opentelemetry/instrumentation-knex": "^0.32.0",
"@opentelemetry/instrumentation-koa": "^0.35.0",
Expand All @@ -92,6 +92,6 @@
"@opentelemetry/resource-detector-container": "^0.3.0",
"@opentelemetry/resource-detector-gcp": "^0.29.0",
"@opentelemetry/resources": "^1.12.0",
"@opentelemetry/sdk-node": "^0.41.0"
"@opentelemetry/sdk-node": "^0.41.2"
}
}
6 changes: 3 additions & 3 deletions metapackages/auto-instrumentations-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@
"webpack-merge": "5.8.0"
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.41.0",
"@opentelemetry/instrumentation": "^0.41.2",
"@opentelemetry/instrumentation-document-load": "^0.33.0",
"@opentelemetry/instrumentation-fetch": "^0.41.0",
"@opentelemetry/instrumentation-fetch": "^0.41.2",
"@opentelemetry/instrumentation-user-interaction": "^0.33.0",
"@opentelemetry/instrumentation-xml-http-request": "^0.41.0"
"@opentelemetry/instrumentation-xml-http-request": "^0.41.2"
}
}
2 changes: 1 addition & 1 deletion packages/opentelemetry-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"dependencies": {
"@opentelemetry/core": "^1.0.0",
"@opentelemetry/exporter-jaeger": "^1.3.1",
"@opentelemetry/instrumentation": "^0.41.0",
"@opentelemetry/instrumentation": "^0.41.2",
"@opentelemetry/sdk-trace-node": "^1.8.0",
"@opentelemetry/resources": "^1.8.0",
"@opentelemetry/sdk-trace-base": "^1.8.0",
Expand Down
2 changes: 1 addition & 1 deletion plugins/node/instrumentation-amqplib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
"dependencies": {
"@opentelemetry/core": "^1.8.0",
"@opentelemetry/instrumentation": "^0.41.0",
"@opentelemetry/instrumentation": "^0.41.2",
"@opentelemetry/semantic-conventions": "^1.0.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions plugins/node/instrumentation-cucumber/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@opentelemetry/api": "^1.0.0"
},
"devDependencies": {
"@cucumber/cucumber": "^8.8.0",
"@cucumber/cucumber": "^9.0.0",
"@opentelemetry/api": "^1.0.0",
"@opentelemetry/core": "^1.3.1",
"@opentelemetry/sdk-trace-base": "^1.3.1",
Expand All @@ -66,7 +66,7 @@
"typescript": "4.4.4"
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.36.0",
"@opentelemetry/instrumentation": "^0.41.2",
"@opentelemetry/semantic-conventions": "^1.0.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-cucumber#readme"
Expand Down
2 changes: 1 addition & 1 deletion plugins/node/instrumentation-dataloader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"typescript": "4.4.4"
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.41.0"
"@opentelemetry/instrumentation": "^0.41.2"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-dataloader#readme"
}
2 changes: 1 addition & 1 deletion plugins/node/instrumentation-fs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
},
"dependencies": {
"@opentelemetry/core": "^1.8.0",
"@opentelemetry/instrumentation": "^0.41.0",
"@opentelemetry/instrumentation": "^0.41.2",
"@opentelemetry/semantic-conventions": "^1.0.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-fs#readme"
Expand Down
2 changes: 1 addition & 1 deletion plugins/node/instrumentation-lru-memoizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"typescript": "4.4.4"
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.41.0"
"@opentelemetry/instrumentation": "^0.41.2"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-lru-memoizer#readme"
}
2 changes: 1 addition & 1 deletion plugins/node/instrumentation-mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"dependencies": {
"@opentelemetry/core": "^1.8.0",
"@opentelemetry/instrumentation": "^0.41.0",
"@opentelemetry/instrumentation": "^0.41.2",
"@opentelemetry/semantic-conventions": "^1.0.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-mongoose#readme"
Expand Down
2 changes: 1 addition & 1 deletion plugins/node/instrumentation-socket.io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"typescript": "4.4.4"
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.41.0",
"@opentelemetry/instrumentation": "^0.41.2",
"@opentelemetry/semantic-conventions": "^1.0.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-socket.io#readme"
Expand Down
2 changes: 1 addition & 1 deletion plugins/node/instrumentation-tedious/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"typescript": "4.4.4"
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.41.0",
"@opentelemetry/instrumentation": "^0.41.2",
"@opentelemetry/semantic-conventions": "^1.0.0",
"@types/tedious": "^4.0.6"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"typescript": "4.4.4"
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.41.0",
"@opentelemetry/instrumentation": "^0.41.2",
"@opentelemetry/propagator-aws-xray": "^1.3.0",
"@opentelemetry/resources": "^1.8.0",
"@opentelemetry/semantic-conventions": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
"dependencies": {
"@opentelemetry/core": "^1.8.0",
"@opentelemetry/instrumentation": "^0.41.0",
"@opentelemetry/instrumentation": "^0.41.2",
"@opentelemetry/semantic-conventions": "^1.0.0",
"@opentelemetry/propagation-utils": "^0.30.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"typescript": "4.4.4"
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.41.0",
"@opentelemetry/instrumentation": "^0.41.2",
"@types/bunyan": "1.8.8"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-bunyan#readme"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"typescript": "4.4.4"
},
"dependencies": {
"@opentelemetry/instrumentation": "^0.41.0",
"@opentelemetry/instrumentation": "^0.41.2",
"@opentelemetry/semantic-conventions": "^1.0.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-cassandra#readme"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
},
"dependencies": {
"@opentelemetry/core": "^1.8.0",
"@opentelemetry/instrumentation": "^0.41.0",
"@opentelemetry/instrumentation": "^0.41.2",
"@opentelemetry/semantic-conventions": "^1.0.0",
"@types/connect": "3.4.35"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
ConnectNames,
ConnectTypes,
} from './enums/AttributeNames';
import { Use, UseArgs, UseArgs2 } from './internal-types';
import { PatchedRequest, Use, UseArgs, UseArgs2 } from './internal-types';
import { VERSION } from './version';
import {
InstrumentationBase,
Expand All @@ -32,6 +32,11 @@ import {
isWrapped,
} from '@opentelemetry/instrumentation';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
replaceCurrentStackRoute,
addNewStackLayer,
generateRoute,
} from './utils';

export const ANONYMOUS_NAME = 'anonymous';

Expand Down Expand Up @@ -65,6 +70,9 @@ export class ConnectInstrumentation extends InstrumentationBase<Server> {
if (!isWrapped(patchedApp.use)) {
this._wrap(patchedApp, 'use', this._patchUse.bind(this));
}
if (!isWrapped(patchedApp.handle)) {
this._wrap(patchedApp, 'handle', this._patchHandle.bind(this));
}
}

private _patchConstructor(original: () => Server): () => Server {
Expand Down Expand Up @@ -120,14 +128,20 @@ export class ConnectInstrumentation extends InstrumentationBase<Server> {
if (!instrumentation.isEnabled()) {
return (middleWare as any).apply(this, arguments);
}
const [resArgIdx, nextArgIdx] = isErrorMiddleware ? [2, 3] : [1, 2];
const [reqArgIdx, resArgIdx, nextArgIdx] = isErrorMiddleware
? [1, 2, 3]
: [0, 1, 2];
const req = arguments[reqArgIdx] as PatchedRequest;
const res = arguments[resArgIdx] as ServerResponse;
const next = arguments[nextArgIdx] as NextFunction;

replaceCurrentStackRoute(req, routeName);

const rpcMetadata = getRPCMetadata(context.active());
if (routeName && rpcMetadata?.type === RPCType.HTTP) {
rpcMetadata.route = routeName;
rpcMetadata.route = generateRoute(req);
}

let spanName = '';
if (routeName) {
spanName = `request handler - ${routeName}`;
Expand Down Expand Up @@ -180,4 +194,30 @@ export class ConnectInstrumentation extends InstrumentationBase<Server> {
return original.apply(this, args as UseArgs2);
};
}

public _patchHandle(original: Server['handle']): Server['handle'] {
const instrumentation = this;
return function (this: Server): ReturnType<Server['handle']> {
const [reqIdx, outIdx] = [0, 2];
const req = arguments[reqIdx] as PatchedRequest;
const out = arguments[outIdx];
const completeStack = addNewStackLayer(req);

if (typeof out === 'function') {
arguments[outIdx] = instrumentation._patchOut(
out as NextFunction,
completeStack
);
}

return (original as any).apply(this, arguments);
};
}

public _patchOut(out: NextFunction, completeStack: () => void): NextFunction {
return function nextFunction(this: NextFunction, ...args: any[]): void {
completeStack();
return Reflect.apply(out, this, args);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@
* limitations under the License.
*/

import type { HandleFunction, Server } from 'connect';
import type { HandleFunction, IncomingMessage, Server } from 'connect';

export const _LAYERS_STORE_PROPERTY: unique symbol = Symbol(
'opentelemetry.instrumentation-connect.request-route-stack'
);

export type UseArgs1 = [HandleFunction];
export type UseArgs2 = [string, HandleFunction];
export type UseArgs = UseArgs1 | UseArgs2;
export type Use = (...args: UseArgs) => Server;
export type PatchedRequest = {
[_LAYERS_STORE_PROPERTY]: string[];
} & IncomingMessage;
55 changes: 55 additions & 0 deletions plugins/node/opentelemetry-instrumentation-connect/src/utils.ts
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
);
};
Loading

0 comments on commit e89d7ad

Please sign in to comment.