Skip to content

Commit 34f56e0

Browse files
blumamirJamieDanielsontrentm
authored
fix: remove unuseful patch message from instrumentations (#2161)
* chore(mongoose): remove diag prints in patch * chore(tedious): remove diag prints in patch * chore(dns): remove diag prints in patch * chore(fastify): remove diag prints in patch * chore(hapi): remove diag prints in patch * chore(knex): remove diag prints in patch * chore(mysql): remove diag prints in patch * chore(pg): remove diag prints in patch * chore(redis): remove diag prints in patch * docs: document when to use diag for patch * chore: lint markdown * fix: unused import * chore: remove unused import * Update GUIDELINES.md Co-authored-by: Jamie Danielson <[email protected]> * Update GUIDELINES.md Co-authored-by: Jamie Danielson <[email protected]> * Update GUIDELINES.md Co-authored-by: Jamie Danielson <[email protected]> * Update GUIDELINES.md Co-authored-by: Jamie Danielson <[email protected]> * Update GUIDELINES.md Co-authored-by: Jamie Danielson <[email protected]> * Update GUIDELINES.md Co-authored-by: Trent Mick <[email protected]> * fix: name of diag in CHANGELOG --------- Co-authored-by: Jamie Danielson <[email protected]> Co-authored-by: Trent Mick <[email protected]>
1 parent 2ade5ae commit 34f56e0

File tree

10 files changed

+34
-37
lines changed

10 files changed

+34
-37
lines changed

GUIDELINES.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,36 @@ To support this use case, you can choose one of the following options:
164164
};
165165
...
166166
```
167+
168+
## Diag Logging
169+
170+
The OpenTelemetry diagnostic logging channel can be used to troubleshoot issues with instrumentation packages.
171+
172+
### Patching Messages
173+
174+
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:
175+
176+
- 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.
177+
- 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.
178+
179+
It can also be useful to know when the instrumentation is loaded and patched, to understand the order of operations in the application.
180+
181+
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:
182+
183+
```shell
184+
@opentelemetry/instrumentation-foo Applying instrumentation patch for module on require hook {
185+
module: 'foo',
186+
version: '1.2.3',
187+
baseDir: '<your directory>/node_modules/foo'
188+
}
189+
```
190+
191+
Instrumentation should not add additional debug messages for triggering the patching and unpatching callbacks, as the base class will handle this.
192+
193+
Instrumentation may add additional patch/unpatch messages for specific functions if it is expected to help in troubleshooting issues with the instrumentation. Few examples:
194+
195+
- 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.
196+
- 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`.
197+
- 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.
198+
199+
The cases above are not covered by the base class and offer additional context to the user troubleshooting an issue with the instrumentation.

plugins/node/instrumentation-mongoose/src/mongoose.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ export class MongooseInstrumentation extends InstrumentationBase {
137137

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

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

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

287283
private patchAndCaptureSpanContext(funcName: string) {
288284
const self = this;
289-
this._diag.debug(`patching mongoose query ${funcName} function`);
290285
return (original: Function) => {
291286
return function captureSpanContext(this: any) {
292287
this[_STORED_PARENT_SPAN] = trace.getSpan(context.active());

plugins/node/instrumentation-tedious/src/instrumentation.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ export class TediousInstrumentation extends InstrumentationBase {
129129
private _patchQuery(operation: string) {
130130
return (originalMethod: UnknownFunction): UnknownFunction => {
131131
const thisPlugin = this;
132-
this._diag.debug(
133-
`TediousInstrumentation: patched Connection.prototype.${operation}`
134-
);
135132

136133
function patchedMethod(this: ApproxConnection, request: ApproxRequest) {
137134
if (!(request instanceof EventEmitter)) {

plugins/node/opentelemetry-instrumentation-dns/src/instrumentation.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ export class DnsInstrumentation extends InstrumentationBase {
105105
private _getPatchLookupFunction(
106106
original: (hostname: string, ...args: unknown[]) => void
107107
) {
108-
diag.debug('patch lookup function');
109108
const plugin = this;
110109
return function patchedLookup(
111110
this: {},

plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ export class FastifyInstrumentation extends InstrumentationBase {
194194
fastify: () => FastifyInstance;
195195
}): () => FastifyInstance {
196196
const instrumentation = this;
197-
this._diag.debug('Patching fastify constructor function');
198197

199198
function fastify(this: FastifyInstance, ...args: any) {
200199
const app: FastifyInstance = moduleExports.fastify.apply(this, args);

plugins/node/opentelemetry-instrumentation-hapi/src/instrumentation.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ export class HapiInstrumentation extends InstrumentationBase {
135135
original: RegisterFunction<T>
136136
): RegisterFunction<T> {
137137
const instrumentation: HapiInstrumentation = this;
138-
api.diag.debug('Patching Hapi.Server register function');
139138
return function register(
140139
this: Hapi.Server,
141140
pluginInput: HapiPluginInput<T>,
@@ -169,7 +168,6 @@ export class HapiInstrumentation extends InstrumentationBase {
169168
pluginName?: string
170169
) {
171170
const instrumentation: HapiInstrumentation = this;
172-
api.diag.debug('Patching Hapi.Server ext function');
173171

174172
return function ext(
175173
this: ThisParameterType<typeof original>,
@@ -231,7 +229,6 @@ export class HapiInstrumentation extends InstrumentationBase {
231229
pluginName?: string
232230
) {
233231
const instrumentation: HapiInstrumentation = this;
234-
api.diag.debug('Patching Hapi.Server route function');
235232
return function route(
236233
this: Hapi.Server,
237234
route: HapiServerRouteInput

plugins/node/opentelemetry-instrumentation-knex/src/instrumentation.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ export class KnexInstrumentation extends InstrumentationBase {
109109
);
110110
return Client;
111111
},
112-
(Client: any, moduleVersion) => {
113-
api.diag.debug(
114-
`Removing ${basePath}/client.js patch for ${constants.MODULE_NAME}@${moduleVersion}`
115-
);
112+
(Client: any) => {
116113
this._unwrap(Client.prototype, 'queryBuilder');
117114
this._unwrap(Client.prototype, 'schemaBuilder');
118115
this._unwrap(Client.prototype, 'raw');

plugins/node/opentelemetry-instrumentation-mysql/src/instrumentation.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import {
1818
context,
1919
Context,
20-
diag,
2120
trace,
2221
Span,
2322
SpanKind,
@@ -84,7 +83,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
8483
'mysql',
8584
['2.*'],
8685
(moduleExports: typeof mysqlTypes) => {
87-
diag.debug('Patching mysql.createConnection');
8886
if (isWrapped(moduleExports.createConnection)) {
8987
this._unwrap(moduleExports, 'createConnection');
9088
}
@@ -94,7 +92,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
9492
this._patchCreateConnection() as any
9593
);
9694

97-
diag.debug('Patching mysql.createPool');
9895
if (isWrapped(moduleExports.createPool)) {
9996
this._unwrap(moduleExports, 'createPool');
10097
}
@@ -104,7 +101,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
104101
this._patchCreatePool() as any
105102
);
106103

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

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

@@ -173,7 +167,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
173167
private _patchPoolEnd(pool: any) {
174168
return (originalPoolEnd: Function) => {
175169
const thisPlugin = this;
176-
diag.debug('MySQLInstrumentation#patch: patched mysql pool end');
177170
return function end(callback?: unknown) {
178171
const nAll = (pool as any)._allConnections.length;
179172
const nFree = (pool as any)._freeConnections.length;
@@ -196,7 +189,6 @@ export class MySQLInstrumentation extends InstrumentationBase {
196189
private _patchCreatePoolCluster() {
197190
return (originalCreatePoolCluster: Function) => {
198191
const thisPlugin = this;
199-
diag.debug('MySQLInstrumentation#patch: patched mysql createPoolCluster');
200192
return function createPool(_config: string | mysqlTypes.PoolConfig) {
201193
const cluster = originalCreatePoolCluster(...arguments);
202194

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

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

313300
return function query(
314301
query: string | mysqlTypes.Query | mysqlTypes.QueryOptions,

plugins/node/opentelemetry-instrumentation-mysql2/src/instrumentation.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export class MySQL2Instrumentation extends InstrumentationBase {
5656
(moduleExports: any) => {
5757
const ConnectionPrototype: mysqlTypes.Connection =
5858
moduleExports.Connection.prototype;
59-
api.diag.debug('Patching Connection.prototype.query');
6059
if (isWrapped(ConnectionPrototype.query)) {
6160
this._unwrap(ConnectionPrototype, 'query');
6261
}
@@ -91,8 +90,6 @@ export class MySQL2Instrumentation extends InstrumentationBase {
9190
private _patchQuery(format: formatType, isPrepared: boolean) {
9291
return (originalQuery: Function): Function => {
9392
const thisPlugin = this;
94-
api.diag.debug('MySQL2Instrumentation: patched mysql query/execute');
95-
9693
return function query(
9794
this: mysqlTypes.Connection,
9895
query: string | mysqlTypes.Query | mysqlTypes.QueryOptions,

plugins/node/opentelemetry-instrumentation-redis/src/instrumentation.ts

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

17-
import { diag } from '@opentelemetry/api';
1817
import {
1918
isWrapped,
2019
InstrumentationBase,
@@ -51,7 +50,6 @@ export class RedisInstrumentation extends InstrumentationBase {
5150
'redis',
5251
['^2.6.0', '^3.0.0'],
5352
moduleExports => {
54-
diag.debug('Patching redis.RedisClient.internal_send_command');
5553
if (
5654
isWrapped(
5755
moduleExports.RedisClient.prototype['internal_send_command']
@@ -68,7 +66,6 @@ export class RedisInstrumentation extends InstrumentationBase {
6866
this._getPatchInternalSendCommand()
6967
);
7068

71-
diag.debug('patching redis.RedisClient.create_stream');
7269
if (isWrapped(moduleExports.RedisClient.prototype['create_stream'])) {
7370
this._unwrap(moduleExports.RedisClient.prototype, 'create_stream');
7471
}
@@ -78,7 +75,6 @@ export class RedisInstrumentation extends InstrumentationBase {
7875
this._getPatchCreateStream()
7976
);
8077

81-
diag.debug('patching redis.createClient');
8278
if (isWrapped(moduleExports.createClient)) {
8379
this._unwrap(moduleExports, 'createClient');
8480
}

0 commit comments

Comments
 (0)