-
Notifications
You must be signed in to change notification settings - Fork 552
/
Copy pathinstrumentation.ts
216 lines (199 loc) · 7.3 KB
/
instrumentation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* 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 * as api from '@opentelemetry/api';
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
import * as constants from './constants';
import {
InstrumentationBase,
InstrumentationNodeModuleDefinition,
InstrumentationNodeModuleFile,
isWrapped,
} from '@opentelemetry/instrumentation';
import {
SEMATTRS_DB_NAME,
SEMATTRS_DB_OPERATION,
SEMATTRS_DB_SQL_TABLE,
SEMATTRS_DB_STATEMENT,
SEMATTRS_DB_SYSTEM,
SEMATTRS_DB_USER,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_PORT,
SEMATTRS_NET_TRANSPORT,
} from '@opentelemetry/semantic-conventions';
import * as utils from './utils';
import { KnexInstrumentationConfig } from './types';
const contextSymbol = Symbol('opentelemetry.instrumentation-knex.context');
const DEFAULT_CONFIG: KnexInstrumentationConfig = {
maxQueryLength: 1022,
requireParentSpan: false,
};
export class KnexInstrumentation extends InstrumentationBase<KnexInstrumentationConfig> {
constructor(config: KnexInstrumentationConfig = {}) {
super(PACKAGE_NAME, PACKAGE_VERSION, { ...DEFAULT_CONFIG, ...config });
}
override setConfig(config: KnexInstrumentationConfig = {}) {
super.setConfig({ ...DEFAULT_CONFIG, ...config });
}
init() {
const module = new InstrumentationNodeModuleDefinition(
constants.MODULE_NAME,
constants.SUPPORTED_VERSIONS
);
module.files.push(
this.getClientNodeModuleFileInstrumentation('src'),
this.getClientNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('src'),
this.getRunnerNodeModuleFileInstrumentation('lib'),
this.getRunnerNodeModuleFileInstrumentation('lib/execution')
);
return module;
}
private getRunnerNodeModuleFileInstrumentation(basePath: string) {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/runner.js`,
constants.SUPPORTED_VERSIONS,
(Runner: any, moduleVersion) => {
this.ensureWrapped(
Runner.prototype,
'query',
this.createQueryWrapper(moduleVersion)
);
return Runner;
},
(Runner: any, moduleVersion) => {
this._unwrap(Runner.prototype, 'query');
return Runner;
}
);
}
private getClientNodeModuleFileInstrumentation(basePath: string) {
return new InstrumentationNodeModuleFile(
`knex/${basePath}/client.js`,
constants.SUPPORTED_VERSIONS,
(Client: any) => {
this.ensureWrapped(
Client.prototype,
'queryBuilder',
this.storeContext.bind(this)
);
this.ensureWrapped(
Client.prototype,
'schemaBuilder',
this.storeContext.bind(this)
);
this.ensureWrapped(
Client.prototype,
'raw',
this.storeContext.bind(this)
);
return Client;
},
(Client: any) => {
this._unwrap(Client.prototype, 'queryBuilder');
this._unwrap(Client.prototype, 'schemaBuilder');
this._unwrap(Client.prototype, 'raw');
return Client;
}
);
}
private createQueryWrapper(moduleVersion?: string) {
const instrumentation = this;
return function wrapQuery(original: (...args: any[]) => any) {
return function wrapped_logging_method(this: any, query: any) {
const config = this.client.config;
const table = utils.extractTableName(this.builder);
// `method` actually refers to the knex API method - Not exactly "operation"
// in the spec sense, but matches most of the time.
const operation = query?.method;
const name =
config?.connection?.filename || config?.connection?.database;
const { maxQueryLength } = instrumentation.getConfig();
const attributes: api.SpanAttributes = {
'knex.version': moduleVersion,
[SEMATTRS_DB_SYSTEM]: utils.mapSystem(config.client),
[SEMATTRS_DB_SQL_TABLE]: table,
[SEMATTRS_DB_OPERATION]: operation,
[SEMATTRS_DB_USER]: config?.connection?.user,
[SEMATTRS_DB_NAME]: name,
[SEMATTRS_NET_PEER_NAME]: config?.connection?.host,
[SEMATTRS_NET_PEER_PORT]: config?.connection?.port,
[SEMATTRS_NET_TRANSPORT]:
config?.connection?.filename === ':memory:' ? 'inproc' : undefined,
};
if (maxQueryLength) {
// filters both undefined and 0
attributes[SEMATTRS_DB_STATEMENT] = utils.limitLength(
query?.sql,
maxQueryLength
);
}
const parentContext =
this.builder[contextSymbol] || api.context.active();
const parentSpan = api.trace.getSpan(parentContext);
const hasActiveParent =
parentSpan && api.trace.isSpanContextValid(parentSpan.spanContext());
if (instrumentation._config.requireParentSpan && !hasActiveParent) {
return original.bind(this)(...arguments);
}
const span = instrumentation.tracer.startSpan(
utils.getName(name, operation, table),
{
kind: api.SpanKind.CLIENT,
attributes,
},
parentContext
);
const spanContext = api.trace.setSpan(api.context.active(), span);
return api.context
.with(spanContext, original, this, ...arguments)
.then((result: unknown) => {
span.end();
return result;
})
.catch((err: any) => {
// knex adds full query with all the binding values to the message,
// we want to undo that without changing the original error
const formatter = utils.getFormatter(this);
const fullQuery = formatter(query.sql, query.bindings || []);
const message = err.message.replace(fullQuery + ' - ', '');
const clonedError = utils.cloneErrorWithNewMessage(err, message);
span.recordException(clonedError);
span.setStatus({ code: api.SpanStatusCode.ERROR, message });
span.end();
throw err;
});
};
};
}
private storeContext(original: Function) {
return function wrapped_logging_method(this: any) {
const builder = original.apply(this, arguments);
// Builder is a custom promise type and when awaited it fails to propagate context.
// We store the parent context at the moment of initiating the builder
// otherwise we'd have nothing to attach the span as a child for in `query`.
Object.defineProperty(builder, contextSymbol, {
value: api.context.active(),
});
return builder;
};
}
ensureWrapped(obj: any, methodName: string, wrapper: (original: any) => any) {
if (isWrapped(obj[methodName])) {
this._unwrap(obj, methodName);
}
this._wrap(obj, methodName, wrapper);
}
}