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: remove unuseful patch message from instrumentations #2161

Merged
merged 30 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8a85f1c
chore(mongoose): remove diag prints in patch
blumamir Apr 27, 2024
5351954
chore(tedious): remove diag prints in patch
blumamir Apr 27, 2024
eba86f2
chore(dns): remove diag prints in patch
blumamir Apr 27, 2024
ab58270
chore(fastify): remove diag prints in patch
blumamir Apr 27, 2024
9811baa
chore(hapi): remove diag prints in patch
blumamir Apr 27, 2024
53a4811
chore(knex): remove diag prints in patch
blumamir Apr 27, 2024
203f82d
chore(mysql): remove diag prints in patch
blumamir Apr 27, 2024
2b105cc
chore(pg): remove diag prints in patch
blumamir Apr 27, 2024
d3e00f2
chore(redis): remove diag prints in patch
blumamir Apr 27, 2024
182405f
docs: document when to use diag for patch
blumamir Apr 27, 2024
e602c3b
chore: lint markdown
blumamir Apr 27, 2024
5cf1a43
fix: unused import
blumamir Apr 27, 2024
c547835
chore: remove unused import
blumamir Apr 27, 2024
b3cf029
Merge branch 'main' into no-patch-diag
blumamir Apr 29, 2024
7a7f69f
Merge branch 'main' into no-patch-diag
blumamir Apr 29, 2024
628471d
Merge branch 'main' into no-patch-diag
blumamir Apr 29, 2024
3c5ca76
Merge branch 'main' into no-patch-diag
blumamir Apr 29, 2024
124d1aa
Update GUIDELINES.md
blumamir Apr 30, 2024
00e3416
Update GUIDELINES.md
blumamir Apr 30, 2024
ee1e0fa
Update GUIDELINES.md
blumamir Apr 30, 2024
4d5050c
Update GUIDELINES.md
blumamir Apr 30, 2024
855d264
Update GUIDELINES.md
blumamir Apr 30, 2024
f84890d
Merge branch 'main' into no-patch-diag
blumamir Apr 30, 2024
5b22de0
Merge branch 'main' into no-patch-diag
blumamir Apr 30, 2024
0aa2cfe
Merge branch 'main' into no-patch-diag
blumamir Apr 30, 2024
cccc9ba
Merge branch 'main' into no-patch-diag
blumamir Apr 30, 2024
4bb5e16
Update GUIDELINES.md
blumamir May 1, 2024
928fe6a
Merge remote-tracking branch 'upstream/main' into no-patch-diag
blumamir May 1, 2024
0ade21f
fix: name of diag in CHANGELOG
blumamir May 1, 2024
8185fbd
Merge branch 'main' into no-patch-diag
blumamir May 2, 2024
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
33 changes: 33 additions & 0 deletions GUIDELINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,36 @@ To support this use case, you can choose one of the following options:
};
...
```

## Diag Logging

The OpenTelemetry diagnostic logging channel can be used to troubleshoot issues with instrumentation packages.

### Patching Messages

When OpenTelemetry is installed in a user application, and expected spans are missing from generated traces, it is often useful to differentiate between the following scenarios:

- The instrumentation is not auto loaded - due to issue with the require/import interception, an unsupported version of the instrumented package, or some other issue. This knowledge can pin-point the issue to the instrumentation package.
- The instrumentation patch was applied but expected spans are missing -- this can suggest an issue with instrumented package logic, configuration, limits, otel sdk, or other issues.

It can also be useful to know when the instrumentation is loaded and patched, to understand the order of operations in the application.

Instrumentation packages should use the `@opentelemetry/instrumentation` package `InstrumentationBase` class to register patches and unpatch callbacks for specific require/import of the instrumented package, it's dependency or an internal module file. When this mechanism is used, the base class will automatically emit a debug message on instrumentation diag component logger, looking like this:

```shell
@opentelemetry/instrumentation-foo Applying instrumentation patch for module on require hook {
module: 'foo',
version: '1.2.3',
baseDir: '<your directory>/node_modules/foo'
}
```

Instrumentation should not add additional debug messages for triggering the patching and unpatching callbacks, as the base class will handle this.

Instrumentation may add additional patch/unpatch messages for specific functions if it is expected to help in troubleshooting issues with the instrumentation. Few examples:
Copy link
Member

Choose a reason for hiding this comment

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

Oh, another thought I had that I think will probably be updated in the follow-up PR that replaces inconsistent usages of api.diag.debug and similar that aren't using the instrumentation this to allow for this._diag.debug. When we make that change we should update guidelines for that as well, though it will already be easier to follow suit once these are all more consistent 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

certainly, I aim to first remove or hoist unneeded diag messages, and then will transform all remaining diag message to use this._diag and update the GUIDELINES.md 👍

open-telemetry/opentelemetry-js#4663 will also help in hosting many diag prints and remove the repeated code from dozens of places in contrib.


- If the patch logic is conditional, and user can benefit from ensuring the condition is met and the patch happened. `koa` patching logic examine an object and branch between patching it as router vs middleware, which is applied at runtime. `aws-lambda` will abort patching if the environment is not configured properly.
- When the patch is not applied directly on a `moduleExports` object in the `InstrumentationBase` callbacks, but rather from an event in the package, like creating new client instance, registering a listener, etc. `fastify` instrumentation applies a patch when a hook is added to the fastify app instance, which is patched from `moduleExports`.
- In situations where the patch logic is not trivial and it helps to specify patch events in the right context and nuances. `aws-lambda` logs additional properties extracted from the lambda framework and exposes them for troubleshooting.

The cases above are not covered by the base class and offer additional context to the user troubleshooting an issue with the instrumentation.
5 changes: 0 additions & 5 deletions plugins/node/instrumentation-mongoose/src/mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ export class MongooseInstrumentation extends InstrumentationBase {

private patchAggregateExec(moduleVersion: string | undefined) {
const self = this;
this._diag.debug('patched mongoose Aggregate exec function');
return (originalAggregate: Function) => {
return function exec(this: any, callback?: Function) {
if (
Expand Down Expand Up @@ -179,7 +178,6 @@ export class MongooseInstrumentation extends InstrumentationBase {

private patchQueryExec(moduleVersion: string | undefined) {
const self = this;
this._diag.debug('patched mongoose Query exec function');
return (originalExec: Function) => {
return function exec(this: any, callback?: Function) {
if (
Expand Down Expand Up @@ -222,7 +220,6 @@ export class MongooseInstrumentation extends InstrumentationBase {

private patchOnModelMethods(op: string, moduleVersion: string | undefined) {
const self = this;
this._diag.debug(`patching mongoose Model '${op}' operation`);
return (originalOnModelFunction: Function) => {
return function method(this: any, options?: any, callback?: Function) {
if (
Expand Down Expand Up @@ -271,7 +268,6 @@ export class MongooseInstrumentation extends InstrumentationBase {
// the aggregate of Model, and set the context on the Aggregate object
private patchModelAggregate() {
const self = this;
this._diag.debug('patched mongoose model aggregate function');
return (original: Function) => {
return function captureSpanContext(this: any) {
const currentSpan = trace.getSpan(context.active());
Expand All @@ -286,7 +282,6 @@ export class MongooseInstrumentation extends InstrumentationBase {

private patchAndCaptureSpanContext(funcName: string) {
const self = this;
this._diag.debug(`patching mongoose query ${funcName} function`);
return (original: Function) => {
return function captureSpanContext(this: any) {
this[_STORED_PARENT_SPAN] = trace.getSpan(context.active());
Expand Down
3 changes: 0 additions & 3 deletions plugins/node/instrumentation-tedious/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ export class TediousInstrumentation extends InstrumentationBase {
private _patchQuery(operation: string) {
return (originalMethod: UnknownFunction): UnknownFunction => {
const thisPlugin = this;
this._diag.debug(
`TediousInstrumentation: patched Connection.prototype.${operation}`
);

function patchedMethod(this: ApproxConnection, request: ApproxRequest) {
if (!(request instanceof EventEmitter)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ export class DnsInstrumentation extends InstrumentationBase {
private _getPatchLookupFunction(
original: (hostname: string, ...args: unknown[]) => void
) {
diag.debug('patch lookup function');
const plugin = this;
return function patchedLookup(
this: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ export class FastifyInstrumentation extends InstrumentationBase {
fastify: () => FastifyInstance;
}): () => FastifyInstance {
const instrumentation = this;
this._diag.debug('Patching fastify constructor function');

function fastify(this: FastifyInstance, ...args: any) {
const app: FastifyInstance = moduleExports.fastify.apply(this, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ export class HapiInstrumentation extends InstrumentationBase {
original: RegisterFunction<T>
): RegisterFunction<T> {
const instrumentation: HapiInstrumentation = this;
api.diag.debug('Patching Hapi.Server register function');
return function register(
this: Hapi.Server,
pluginInput: HapiPluginInput<T>,
Expand Down Expand Up @@ -169,7 +168,6 @@ export class HapiInstrumentation extends InstrumentationBase {
pluginName?: string
) {
const instrumentation: HapiInstrumentation = this;
api.diag.debug('Patching Hapi.Server ext function');

return function ext(
this: ThisParameterType<typeof original>,
Expand Down Expand Up @@ -231,7 +229,6 @@ export class HapiInstrumentation extends InstrumentationBase {
pluginName?: string
) {
const instrumentation: HapiInstrumentation = this;
api.diag.debug('Patching Hapi.Server route function');
return function route(
this: Hapi.Server,
route: HapiServerRouteInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ export class KnexInstrumentation extends InstrumentationBase {
);
return Client;
},
(Client: any, moduleVersion) => {
api.diag.debug(
`Removing ${basePath}/client.js patch for ${constants.MODULE_NAME}@${moduleVersion}`
);
(Client: any) => {
this._unwrap(Client.prototype, 'queryBuilder');
this._unwrap(Client.prototype, 'schemaBuilder');
this._unwrap(Client.prototype, 'raw');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import {
context,
Context,
diag,
trace,
Span,
SpanKind,
Expand Down Expand Up @@ -84,7 +83,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
'mysql',
['2.*'],
(moduleExports: typeof mysqlTypes) => {
diag.debug('Patching mysql.createConnection');
if (isWrapped(moduleExports.createConnection)) {
this._unwrap(moduleExports, 'createConnection');
}
Expand All @@ -94,7 +92,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
this._patchCreateConnection() as any
);

diag.debug('Patching mysql.createPool');
if (isWrapped(moduleExports.createPool)) {
this._unwrap(moduleExports, 'createPool');
}
Expand All @@ -104,7 +101,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
this._patchCreatePool() as any
);

diag.debug('Patching mysql.createPoolCluster');
if (isWrapped(moduleExports.createPoolCluster)) {
this._unwrap(moduleExports, 'createPoolCluster');
}
Expand All @@ -130,7 +126,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
private _patchCreateConnection() {
return (originalCreateConnection: Function) => {
const thisPlugin = this;
diag.debug('MySQLInstrumentation#patch: patched mysql createConnection');

return function createConnection(
_connectionUri: string | mysqlTypes.ConnectionConfig
Expand All @@ -153,7 +148,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
private _patchCreatePool() {
return (originalCreatePool: Function) => {
const thisPlugin = this;
diag.debug('MySQLInstrumentation#patch: patched mysql createPool');
return function createPool(_config: string | mysqlTypes.PoolConfig) {
const pool = originalCreatePool(...arguments);

Expand All @@ -173,7 +167,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
private _patchPoolEnd(pool: any) {
return (originalPoolEnd: Function) => {
const thisPlugin = this;
diag.debug('MySQLInstrumentation#patch: patched mysql pool end');
return function end(callback?: unknown) {
const nAll = (pool as any)._allConnections.length;
const nFree = (pool as any)._freeConnections.length;
Expand All @@ -196,7 +189,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
private _patchCreatePoolCluster() {
return (originalCreatePoolCluster: Function) => {
const thisPlugin = this;
diag.debug('MySQLInstrumentation#patch: patched mysql createPoolCluster');
return function createPool(_config: string | mysqlTypes.PoolConfig) {
const cluster = originalCreatePoolCluster(...arguments);

Expand All @@ -215,7 +207,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
private _patchAdd(cluster: mysqlTypes.PoolCluster) {
return (originalAdd: Function) => {
const thisPlugin = this;
diag.debug('MySQLInstrumentation#patch: patched mysql pool cluster add');
return function add(id: string, config: unknown) {
// Unwrap if unpatch has been called
if (!thisPlugin['_enabled']) {
Expand All @@ -241,9 +232,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
private _patchGetConnection(pool: mysqlTypes.Pool | mysqlTypes.PoolCluster) {
return (originalGetConnection: Function) => {
const thisPlugin = this;
diag.debug(
'MySQLInstrumentation#patch: patched mysql pool getConnection'
);

return function getConnection(
arg1?: unknown,
Expand Down Expand Up @@ -308,7 +296,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
private _patchQuery(connection: mysqlTypes.Connection | mysqlTypes.Pool) {
return (originalQuery: Function): mysqlTypes.QueryFunction => {
const thisPlugin = this;
diag.debug('MySQLInstrumentation: patched mysql query');

return function query(
query: string | mysqlTypes.Query | mysqlTypes.QueryOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export class MySQL2Instrumentation extends InstrumentationBase {
(moduleExports: any) => {
const ConnectionPrototype: mysqlTypes.Connection =
moduleExports.Connection.prototype;
api.diag.debug('Patching Connection.prototype.query');
if (isWrapped(ConnectionPrototype.query)) {
this._unwrap(ConnectionPrototype, 'query');
}
Expand Down Expand Up @@ -91,8 +90,6 @@ export class MySQL2Instrumentation extends InstrumentationBase {
private _patchQuery(format: formatType, isPrepared: boolean) {
return (originalQuery: Function): Function => {
const thisPlugin = this;
api.diag.debug('MySQL2Instrumentation: patched mysql query/execute');

return function query(
this: mysqlTypes.Connection,
query: string | mysqlTypes.Query | mysqlTypes.QueryOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import { diag } from '@opentelemetry/api';
import {
isWrapped,
InstrumentationBase,
Expand Down Expand Up @@ -51,7 +50,6 @@ export class RedisInstrumentation extends InstrumentationBase {
'redis',
['^2.6.0', '^3.0.0'],
moduleExports => {
diag.debug('Patching redis.RedisClient.internal_send_command');
if (
isWrapped(
moduleExports.RedisClient.prototype['internal_send_command']
Expand All @@ -68,7 +66,6 @@ export class RedisInstrumentation extends InstrumentationBase {
this._getPatchInternalSendCommand()
);

diag.debug('patching redis.RedisClient.create_stream');
if (isWrapped(moduleExports.RedisClient.prototype['create_stream'])) {
this._unwrap(moduleExports.RedisClient.prototype, 'create_stream');
}
Expand All @@ -78,7 +75,6 @@ export class RedisInstrumentation extends InstrumentationBase {
this._getPatchCreateStream()
);

diag.debug('patching redis.createClient');
if (isWrapped(moduleExports.createClient)) {
this._unwrap(moduleExports, 'createClient');
}
Expand Down
Loading