@sebspark/opentelemetry-instrumentation-opensearch 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +90 -0
- package/dist/index.d.mts +57 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +99 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [2026] [SEB]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# `@sebspark/opentelemetry-instrumentation-opensearch`
|
|
2
|
+
|
|
3
|
+
OTEL auto instrumentation for OpenSearch
|
|
4
|
+
|
|
5
|
+
Patches `@opensearch-project/opensearch`'s `Transport` to create `db` CLIENT spans for every request. By default the db span is set as the active context so HTTP child spans are properly parented and W3C trace headers are propagated outbound.
|
|
6
|
+
|
|
7
|
+
## Use
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { OpenSearchInstrumentation } from '@sebspark/opentelemetry-instrumentation-opensearch'
|
|
11
|
+
import { registerInstrumentations } from '@opentelemetry/instrumentation'
|
|
12
|
+
|
|
13
|
+
registerInstrumentations({
|
|
14
|
+
instrumentations: [new OpenSearchInstrumentation()],
|
|
15
|
+
})
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### Disabling query capture
|
|
19
|
+
|
|
20
|
+
By default the request body is captured as `db.query.text`. Disable it if queries contain sensitive data:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
new OpenSearchInstrumentation({ dbStatementSerializer: false })
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Or provide a custom serializer to redact specific fields:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
new OpenSearchInstrumentation({
|
|
30
|
+
dbStatementSerializer: (params) => {
|
|
31
|
+
const body = params.body as Record<string, unknown>
|
|
32
|
+
return JSON.stringify({ ...body, password: '[redacted]' })
|
|
33
|
+
},
|
|
34
|
+
})
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Adding custom attributes before the request
|
|
38
|
+
|
|
39
|
+
Use `requestHook` to attach attributes to each span before the request is sent:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
new OpenSearchInstrumentation({
|
|
43
|
+
requestHook: (span, params) => {
|
|
44
|
+
span.setAttribute('app.opensearch.method', params.method)
|
|
45
|
+
},
|
|
46
|
+
})
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Adding custom attributes from the response
|
|
50
|
+
|
|
51
|
+
Use `responseHook` to attach attributes derived from the response (only called on success):
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
new OpenSearchInstrumentation({
|
|
55
|
+
responseHook: (span, response) => {
|
|
56
|
+
span.setAttribute('app.opensearch.hits', response.body?.hits?.total?.value ?? 0)
|
|
57
|
+
},
|
|
58
|
+
})
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Recording the client library version
|
|
62
|
+
|
|
63
|
+
Set `moduleVersionAttributeName` to record which version of `@opensearch-project/opensearch` made the request:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
new OpenSearchInstrumentation({
|
|
67
|
+
moduleVersionAttributeName: 'db.opensearch.client.version',
|
|
68
|
+
})
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Suppressing HTTP child spans
|
|
72
|
+
|
|
73
|
+
By default the db span is set as the active context, so an HTTP instrumentation layer (if registered) will create a child span under it. To suppress all child spans instead:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
new OpenSearchInstrumentation({ suppressInternalInstrumentation: true })
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Produced spans
|
|
80
|
+
|
|
81
|
+
| Attribute | Example | Notes |
|
|
82
|
+
|---|---|---|
|
|
83
|
+
| `db.system.name` | `opensearch` | Always set |
|
|
84
|
+
| `db.operation.name` | `search` | Parsed from path (`_search` → `search`) |
|
|
85
|
+
| `db.opensearch.index` | `asset_v0.1.3` | Omitted for cluster-level operations like `bulk` |
|
|
86
|
+
| `db.query.text` | `{"query":{"match_all":{}}}` | Omitted when `dbStatementSerializer: false` |
|
|
87
|
+
| `server.address` | `localhost` | Set from the connection used for the request |
|
|
88
|
+
| `server.port` | `9200` | Omitted when using the default port for the protocol |
|
|
89
|
+
|
|
90
|
+
Span name follows the pattern `<operation> <index>` (e.g. `search asset_v0.1.3`) or just `<operation>` for index-less operations like `bulk`. Falls back to the raw path when the path does not contain an underscore-prefixed operation segment.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { ApiResponse, TransportRequestParams } from "@opensearch-project/opensearch/lib/Transport.js";
|
|
2
|
+
import { Span } from "@opentelemetry/api";
|
|
3
|
+
import { InstrumentationBase, InstrumentationConfig } from "@opentelemetry/instrumentation";
|
|
4
|
+
|
|
5
|
+
//#region src/index.d.ts
|
|
6
|
+
/** Configuration for {@link OpenSearchInstrumentation}. */
|
|
7
|
+
interface OpenSearchInstrumentationConfig extends InstrumentationConfig {
|
|
8
|
+
/**
|
|
9
|
+
* Serialize the request body into `db.query.text`.
|
|
10
|
+
* Set to `false` to disable capture entirely.
|
|
11
|
+
* Defaults to `JSON.stringify`.
|
|
12
|
+
*/
|
|
13
|
+
dbStatementSerializer?: false | ((params: TransportRequestParams) => string);
|
|
14
|
+
/**
|
|
15
|
+
* Hook called after span attributes are set, before the request is made.
|
|
16
|
+
* Use to add custom attributes.
|
|
17
|
+
*/
|
|
18
|
+
requestHook?: (span: Span, params: TransportRequestParams) => void;
|
|
19
|
+
/**
|
|
20
|
+
* Hook called after a successful response is received.
|
|
21
|
+
* Use to add custom attributes derived from the response.
|
|
22
|
+
*/
|
|
23
|
+
responseHook?: (span: Span, response: ApiResponse) => void;
|
|
24
|
+
/**
|
|
25
|
+
* When `true`, child span creation inside the Transport call is suppressed so
|
|
26
|
+
* HTTP instrumentation cannot produce a duplicate span.
|
|
27
|
+
* When `false` (default), the db span is set as the active context so HTTP
|
|
28
|
+
* child spans are properly parented and W3C trace headers are propagated.
|
|
29
|
+
*/
|
|
30
|
+
suppressInternalInstrumentation?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* If set, the value of the `@opensearch-project/opensearch` client version is
|
|
33
|
+
* recorded as a span attribute under this name.
|
|
34
|
+
* e.g. `'db.opensearch.client.version'`
|
|
35
|
+
*/
|
|
36
|
+
moduleVersionAttributeName?: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* OpenTelemetry instrumentation for `@opensearch-project/opensearch`.
|
|
40
|
+
*
|
|
41
|
+
* Patches `Transport.prototype.request` to create a `db` CLIENT span for every
|
|
42
|
+
* request. Tracing is suppressed inside the call so the HTTP instrumentation
|
|
43
|
+
* does not produce a duplicate child span.
|
|
44
|
+
*/
|
|
45
|
+
declare class OpenSearchInstrumentation extends InstrumentationBase {
|
|
46
|
+
private readonly osConfig;
|
|
47
|
+
private _original;
|
|
48
|
+
constructor(config?: OpenSearchInstrumentationConfig);
|
|
49
|
+
protected init(): void;
|
|
50
|
+
/** Patches `Transport.prototype.request` with span instrumentation. */
|
|
51
|
+
enable(): void;
|
|
52
|
+
/** Restores the original `Transport.prototype.request`. */
|
|
53
|
+
disable(): void;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
export { OpenSearchInstrumentation, OpenSearchInstrumentationConfig };
|
|
57
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;;;UA+CiB,+BAAA,SAAwC,qBAAA;EAAxC;;;;;EAMf,qBAAA,aAAkC,MAAA,EAAQ,sBAAA;EAYpB;;;;EANtB,WAAA,IAAe,IAAA,EAAM,IAAA,EAAM,MAAA,EAAQ,sBAAA;EAZoB;;;;EAkBvD,YAAA,IAAgB,IAAA,EAAM,IAAA,EAAM,QAAA,EAAU,WAAA;EANjB;;;;;;EAcrB,+BAAA;EARsC;;;;;EAetC,0BAAA;AAAA;;;;;;;;cAUW,yBAAA,SAAkC,mBAAA;EAAA,iBAC5B,QAAA;EAAA,QACD,SAAA;cAEJ,MAAA,GAAQ,+BAAA;EAAA,UASV,IAAA,CAAA;EA8GM;EA3GP,MAAA,CAAA;;EA2GA,OAAA,CAAA;AAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import Transport from "@opensearch-project/opensearch/lib/Transport.js";
|
|
2
|
+
import opensearchPkg from "@opensearch-project/opensearch/package.json" with { type: "json" };
|
|
3
|
+
import { SpanKind, SpanStatusCode, context, trace } from "@opentelemetry/api";
|
|
4
|
+
import { suppressTracing } from "@opentelemetry/core";
|
|
5
|
+
import { InstrumentationBase } from "@opentelemetry/instrumentation";
|
|
6
|
+
import { ATTR_DB_OPERATION_NAME, ATTR_DB_QUERY_TEXT, ATTR_DB_SYSTEM_NAME, ATTR_SERVER_ADDRESS, ATTR_SERVER_PORT } from "@opentelemetry/semantic-conventions";
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/index.ts
|
|
9
|
+
const PACKAGE_VERSION = "0.0.0";
|
|
10
|
+
const OPENSEARCH_CLIENT_VERSION = opensearchPkg.version;
|
|
11
|
+
/**
|
|
12
|
+
* Parses an OpenSearch path into its index and operation components.
|
|
13
|
+
* e.g. `/my_index/_search` → `{ index: 'my_index', operation: 'search' }`
|
|
14
|
+
* e.g. `/_bulk` → `{ operation: 'bulk' }`
|
|
15
|
+
*/
|
|
16
|
+
function parsePath(path) {
|
|
17
|
+
const match = path.match(/^\/(?:([^/_][^/]*)\/)?_([^/]+)/);
|
|
18
|
+
if (!match) return {};
|
|
19
|
+
return {
|
|
20
|
+
index: match[1],
|
|
21
|
+
operation: match[2]
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* OpenTelemetry instrumentation for `@opensearch-project/opensearch`.
|
|
26
|
+
*
|
|
27
|
+
* Patches `Transport.prototype.request` to create a `db` CLIENT span for every
|
|
28
|
+
* request. Tracing is suppressed inside the call so the HTTP instrumentation
|
|
29
|
+
* does not produce a duplicate child span.
|
|
30
|
+
*/
|
|
31
|
+
var OpenSearchInstrumentation = class extends InstrumentationBase {
|
|
32
|
+
osConfig;
|
|
33
|
+
constructor(config = {}) {
|
|
34
|
+
super("@sebspark/opentelemetry-instrumentation-opensearch", PACKAGE_VERSION, config);
|
|
35
|
+
this.osConfig = config;
|
|
36
|
+
}
|
|
37
|
+
init() {}
|
|
38
|
+
/** Patches `Transport.prototype.request` with span instrumentation. */
|
|
39
|
+
enable() {
|
|
40
|
+
if (this._original) return;
|
|
41
|
+
const self = this;
|
|
42
|
+
const original = Transport.prototype.request;
|
|
43
|
+
this._original = original;
|
|
44
|
+
function patchedRequest(params, options, callback) {
|
|
45
|
+
const { index, operation } = parsePath(params.path);
|
|
46
|
+
const spanName = [operation, index].filter(Boolean).join(" ") || params.path;
|
|
47
|
+
const span = self.tracer.startSpan(spanName, {
|
|
48
|
+
kind: SpanKind.CLIENT,
|
|
49
|
+
attributes: {
|
|
50
|
+
[ATTR_DB_SYSTEM_NAME]: "opensearch",
|
|
51
|
+
...operation && { [ATTR_DB_OPERATION_NAME]: operation },
|
|
52
|
+
...index && { "db.opensearch.index": index },
|
|
53
|
+
...params.body && self.osConfig?.dbStatementSerializer !== false && { [ATTR_DB_QUERY_TEXT]: typeof self.osConfig?.dbStatementSerializer === "function" ? self.osConfig.dbStatementSerializer(params) : typeof params.body === "string" ? params.body : JSON.stringify(params.body) }
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
self.osConfig?.requestHook?.(span, params);
|
|
57
|
+
const endSpanOnSuccess = (res) => {
|
|
58
|
+
const { hostname, port } = res.meta.connection.url;
|
|
59
|
+
span.setAttribute(ATTR_SERVER_ADDRESS, hostname);
|
|
60
|
+
if (port) span.setAttribute(ATTR_SERVER_PORT, Number(port));
|
|
61
|
+
if (self.osConfig?.moduleVersionAttributeName) span.setAttribute(self.osConfig.moduleVersionAttributeName, OPENSEARCH_CLIENT_VERSION);
|
|
62
|
+
self.osConfig?.responseHook?.(span, res);
|
|
63
|
+
span.end();
|
|
64
|
+
};
|
|
65
|
+
const endSpanOnError = (err) => {
|
|
66
|
+
if (err instanceof Error) span.recordException(err);
|
|
67
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
68
|
+
span.end();
|
|
69
|
+
};
|
|
70
|
+
const callCtx = self.osConfig?.suppressInternalInstrumentation ? suppressTracing(context.active()) : trace.setSpan(context.active(), span);
|
|
71
|
+
if (typeof callback === "function") return context.with(callCtx, () => original.call(this, params, options, (err, res) => {
|
|
72
|
+
if (err) endSpanOnError(err);
|
|
73
|
+
else endSpanOnSuccess(res);
|
|
74
|
+
callback(err, res);
|
|
75
|
+
}));
|
|
76
|
+
const result = context.with(callCtx, () => original.call(this, params, options));
|
|
77
|
+
const settled = result.then((res) => {
|
|
78
|
+
endSpanOnSuccess(res);
|
|
79
|
+
return res;
|
|
80
|
+
}, (err) => {
|
|
81
|
+
endSpanOnError(err);
|
|
82
|
+
throw err;
|
|
83
|
+
});
|
|
84
|
+
return Object.assign(settled, { abort: result.abort });
|
|
85
|
+
}
|
|
86
|
+
Transport.prototype.request = patchedRequest;
|
|
87
|
+
}
|
|
88
|
+
/** Restores the original `Transport.prototype.request`. */
|
|
89
|
+
disable() {
|
|
90
|
+
if (this._original) {
|
|
91
|
+
Transport.prototype.request = this._original;
|
|
92
|
+
this._original = void 0;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
//#endregion
|
|
97
|
+
export { OpenSearchInstrumentation };
|
|
98
|
+
|
|
99
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["pkg.version"],"sources":["../package.json","../src/index.ts"],"sourcesContent":["","import Transport, {\n type ApiError,\n type ApiResponse,\n type TransportRequestCallback,\n type TransportRequestOptions,\n type TransportRequestParams,\n type TransportRequestPromise,\n} from '@opensearch-project/opensearch/lib/Transport.js'\nimport opensearchPkg from '@opensearch-project/opensearch/package.json' with {\n type: 'json',\n}\nimport {\n context,\n type Span,\n SpanKind,\n SpanStatusCode,\n trace,\n} from '@opentelemetry/api'\nimport { suppressTracing } from '@opentelemetry/core'\nimport {\n InstrumentationBase,\n type InstrumentationConfig,\n} from '@opentelemetry/instrumentation'\nimport {\n ATTR_DB_OPERATION_NAME,\n ATTR_DB_QUERY_TEXT,\n ATTR_DB_SYSTEM_NAME,\n ATTR_SERVER_ADDRESS,\n ATTR_SERVER_PORT,\n} from '@opentelemetry/semantic-conventions'\nimport pkg from '../package.json' with { type: 'json' }\n\nconst PACKAGE_VERSION: string = pkg.version\nconst OPENSEARCH_CLIENT_VERSION: string = opensearchPkg.version\n\n/**\n * Parses an OpenSearch path into its index and operation components.\n * e.g. `/my_index/_search` → `{ index: 'my_index', operation: 'search' }`\n * e.g. `/_bulk` → `{ operation: 'bulk' }`\n */\nfunction parsePath(path: string): { index?: string; operation?: string } {\n const match = path.match(/^\\/(?:([^/_][^/]*)\\/)?_([^/]+)/)\n if (!match) return {}\n return { index: match[1], operation: match[2] }\n}\n\n/** Configuration for {@link OpenSearchInstrumentation}. */\nexport interface OpenSearchInstrumentationConfig extends InstrumentationConfig {\n /**\n * Serialize the request body into `db.query.text`.\n * Set to `false` to disable capture entirely.\n * Defaults to `JSON.stringify`.\n */\n dbStatementSerializer?: false | ((params: TransportRequestParams) => string)\n\n /**\n * Hook called after span attributes are set, before the request is made.\n * Use to add custom attributes.\n */\n requestHook?: (span: Span, params: TransportRequestParams) => void\n\n /**\n * Hook called after a successful response is received.\n * Use to add custom attributes derived from the response.\n */\n responseHook?: (span: Span, response: ApiResponse) => void\n\n /**\n * When `true`, child span creation inside the Transport call is suppressed so\n * HTTP instrumentation cannot produce a duplicate span.\n * When `false` (default), the db span is set as the active context so HTTP\n * child spans are properly parented and W3C trace headers are propagated.\n */\n suppressInternalInstrumentation?: boolean\n\n /**\n * If set, the value of the `@opensearch-project/opensearch` client version is\n * recorded as a span attribute under this name.\n * e.g. `'db.opensearch.client.version'`\n */\n moduleVersionAttributeName?: string\n}\n\n/**\n * OpenTelemetry instrumentation for `@opensearch-project/opensearch`.\n *\n * Patches `Transport.prototype.request` to create a `db` CLIENT span for every\n * request. Tracing is suppressed inside the call so the HTTP instrumentation\n * does not produce a duplicate child span.\n */\nexport class OpenSearchInstrumentation extends InstrumentationBase {\n private readonly osConfig: OpenSearchInstrumentationConfig\n private declare _original: typeof Transport.prototype.request | undefined\n\n constructor(config: OpenSearchInstrumentationConfig = {}) {\n super(\n '@sebspark/opentelemetry-instrumentation-opensearch',\n PACKAGE_VERSION,\n config\n )\n this.osConfig = config\n }\n\n protected init() {}\n\n /** Patches `Transport.prototype.request` with span instrumentation. */\n override enable() {\n if (this._original) return\n const self = this\n const original: (\n this: Transport,\n params: TransportRequestParams,\n options?: TransportRequestOptions\n ) => TransportRequestPromise<ApiResponse> = Transport.prototype.request\n this._original = original\n\n function patchedRequest(\n params: TransportRequestParams,\n options?: TransportRequestOptions\n ): TransportRequestPromise<ApiResponse>\n function patchedRequest(\n params: TransportRequestParams,\n options: TransportRequestOptions | undefined,\n callback: (err: ApiError, result: ApiResponse) => void\n ): TransportRequestCallback\n function patchedRequest(\n this: Transport,\n params: TransportRequestParams,\n options?: TransportRequestOptions,\n callback?: (err: ApiError, result: ApiResponse) => void\n ): TransportRequestPromise<ApiResponse> | TransportRequestCallback {\n const { index, operation } = parsePath(params.path)\n const spanName =\n [operation, index].filter(Boolean).join(' ') || params.path\n const span = self.tracer.startSpan(spanName, {\n kind: SpanKind.CLIENT,\n attributes: {\n [ATTR_DB_SYSTEM_NAME]: 'opensearch',\n ...(operation && { [ATTR_DB_OPERATION_NAME]: operation }),\n ...(index && { 'db.opensearch.index': index }),\n ...(params.body &&\n self.osConfig?.dbStatementSerializer !== false && {\n [ATTR_DB_QUERY_TEXT]:\n typeof self.osConfig?.dbStatementSerializer === 'function'\n ? self.osConfig.dbStatementSerializer(params)\n : typeof params.body === 'string'\n ? params.body\n : JSON.stringify(params.body),\n }),\n },\n })\n self.osConfig?.requestHook?.(span, params)\n\n const endSpanOnSuccess = (res: ApiResponse) => {\n const { hostname, port } = res.meta.connection.url\n span.setAttribute(ATTR_SERVER_ADDRESS, hostname)\n if (port) span.setAttribute(ATTR_SERVER_PORT, Number(port))\n if (self.osConfig?.moduleVersionAttributeName) {\n span.setAttribute(\n self.osConfig.moduleVersionAttributeName,\n OPENSEARCH_CLIENT_VERSION\n )\n }\n self.osConfig?.responseHook?.(span, res)\n span.end()\n }\n const endSpanOnError = (err: unknown) => {\n if (err instanceof Error) span.recordException(err)\n span.setStatus({ code: SpanStatusCode.ERROR })\n span.end()\n }\n\n const callCtx = self.osConfig?.suppressInternalInstrumentation\n ? suppressTracing(context.active())\n : trace.setSpan(context.active(), span)\n\n if (typeof callback === 'function') {\n return context.with(callCtx, () =>\n (\n original as (\n this: Transport,\n params: TransportRequestParams,\n options: TransportRequestOptions | undefined,\n callback: (err: ApiError, result: ApiResponse) => void\n ) => TransportRequestCallback\n ).call(this, params, options, (err, res) => {\n if (err) endSpanOnError(err)\n else endSpanOnSuccess(res)\n callback(err, res)\n })\n )\n }\n\n const result = context.with(callCtx, () =>\n original.call(this, params, options)\n )\n const settled = result.then(\n (res: ApiResponse) => {\n endSpanOnSuccess(res)\n return res\n },\n (err: unknown) => {\n endSpanOnError(err)\n throw err\n }\n )\n return Object.assign(settled, { abort: result.abort })\n }\n\n Transport.prototype.request = patchedRequest\n }\n\n /** Restores the original `Transport.prototype.request`. */\n override disable() {\n if (this._original) {\n Transport.prototype.request = this._original\n this._original = undefined\n }\n }\n}\n"],"mappings":";;;;;;;;ACgCA,MAAM;AACN,MAAM,4BAAoC,cAAc;;;;;;AAOxD,SAAS,UAAU,MAAsD;CACvE,MAAM,QAAQ,KAAK,MAAM,iCAAiC;AAC1D,KAAI,CAAC,MAAO,QAAO,EAAE;AACrB,QAAO;EAAE,OAAO,MAAM;EAAI,WAAW,MAAM;EAAI;;;;;;;;;AA+CjD,IAAa,4BAAb,cAA+C,oBAAoB;CACjE;CAGA,YAAY,SAA0C,EAAE,EAAE;AACxD,QACE,sDACA,iBACA,OACD;AACD,OAAK,WAAW;;CAGlB,OAAiB;;CAGjB,SAAkB;AAChB,MAAI,KAAK,UAAW;EACpB,MAAM,OAAO;EACb,MAAM,WAIsC,UAAU,UAAU;AAChE,OAAK,YAAY;EAWjB,SAAS,eAEP,QACA,SACA,UACiE;GACjE,MAAM,EAAE,OAAO,cAAc,UAAU,OAAO,KAAK;GACnD,MAAM,WACJ,CAAC,WAAW,MAAM,CAAC,OAAO,QAAQ,CAAC,KAAK,IAAI,IAAI,OAAO;GACzD,MAAM,OAAO,KAAK,OAAO,UAAU,UAAU;IAC3C,MAAM,SAAS;IACf,YAAY;MACT,sBAAsB;KACvB,GAAI,aAAa,GAAG,yBAAyB,WAAW;KACxD,GAAI,SAAS,EAAE,uBAAuB,OAAO;KAC7C,GAAI,OAAO,QACT,KAAK,UAAU,0BAA0B,SAAS,GAC/C,qBACC,OAAO,KAAK,UAAU,0BAA0B,aAC5C,KAAK,SAAS,sBAAsB,OAAO,GAC3C,OAAO,OAAO,SAAS,WACrB,OAAO,OACP,KAAK,UAAU,OAAO,KAAK,EACpC;KACJ;IACF,CAAC;AACF,QAAK,UAAU,cAAc,MAAM,OAAO;GAE1C,MAAM,oBAAoB,QAAqB;IAC7C,MAAM,EAAE,UAAU,SAAS,IAAI,KAAK,WAAW;AAC/C,SAAK,aAAa,qBAAqB,SAAS;AAChD,QAAI,KAAM,MAAK,aAAa,kBAAkB,OAAO,KAAK,CAAC;AAC3D,QAAI,KAAK,UAAU,2BACjB,MAAK,aACH,KAAK,SAAS,4BACd,0BACD;AAEH,SAAK,UAAU,eAAe,MAAM,IAAI;AACxC,SAAK,KAAK;;GAEZ,MAAM,kBAAkB,QAAiB;AACvC,QAAI,eAAe,MAAO,MAAK,gBAAgB,IAAI;AACnD,SAAK,UAAU,EAAE,MAAM,eAAe,OAAO,CAAC;AAC9C,SAAK,KAAK;;GAGZ,MAAM,UAAU,KAAK,UAAU,kCAC3B,gBAAgB,QAAQ,QAAQ,CAAC,GACjC,MAAM,QAAQ,QAAQ,QAAQ,EAAE,KAAK;AAEzC,OAAI,OAAO,aAAa,WACtB,QAAO,QAAQ,KAAK,eAEhB,SAMA,KAAK,MAAM,QAAQ,UAAU,KAAK,QAAQ;AAC1C,QAAI,IAAK,gBAAe,IAAI;QACvB,kBAAiB,IAAI;AAC1B,aAAS,KAAK,IAAI;KAClB,CACH;GAGH,MAAM,SAAS,QAAQ,KAAK,eAC1B,SAAS,KAAK,MAAM,QAAQ,QAAQ,CACrC;GACD,MAAM,UAAU,OAAO,MACpB,QAAqB;AACpB,qBAAiB,IAAI;AACrB,WAAO;OAER,QAAiB;AAChB,mBAAe,IAAI;AACnB,UAAM;KAET;AACD,UAAO,OAAO,OAAO,SAAS,EAAE,OAAO,OAAO,OAAO,CAAC;;AAGxD,YAAU,UAAU,UAAU;;;CAIhC,UAAmB;AACjB,MAAI,KAAK,WAAW;AAClB,aAAU,UAAU,UAAU,KAAK;AACnC,QAAK,YAAY,KAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sebspark/opentelemetry-instrumentation-opensearch",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.mts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"import": "./dist/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "spark-build src/index.ts",
|
|
19
|
+
"dev": "tsc --watch --noEmit",
|
|
20
|
+
"lint": "biome check .",
|
|
21
|
+
"test": "vitest run --passWithNoTests --coverage",
|
|
22
|
+
"test:e2e": "vitest --config vitest.config.e2e.ts --run",
|
|
23
|
+
"typecheck": "vitest --typecheck.only --passWithNoTests"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@opensearch-project/opensearch": ">=2",
|
|
27
|
+
"@opentelemetry/api": ">=1.9.0"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@opentelemetry/core": "2.6.0",
|
|
31
|
+
"@opentelemetry/instrumentation": "0.213.0",
|
|
32
|
+
"@opentelemetry/semantic-conventions": "1.40.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@opensearch-project/opensearch": "3.5.1",
|
|
36
|
+
"@opentelemetry/api": "1.9.0",
|
|
37
|
+
"@opentelemetry/sdk-trace-base": "2.6.0",
|
|
38
|
+
"@opentelemetry/sdk-trace-node": "2.6.0",
|
|
39
|
+
"@testcontainers/opensearch": "11.13.0"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "https://github.com/seb-oss/Spark-packages.git"
|
|
44
|
+
}
|
|
45
|
+
}
|