agentrail-node 0.0.0 → 0.3.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 +179 -0
- package/dist/client.d.ts +17 -0
- package/dist/client.js +116 -0
- package/dist/http.d.ts +10 -0
- package/dist/http.js +197 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +27 -0
- package/dist/init.d.ts +4 -0
- package/dist/init.js +80 -0
- package/dist/limits.d.ts +18 -0
- package/dist/limits.js +18 -0
- package/dist/mcp.d.ts +51 -0
- package/dist/mcp.js +712 -0
- package/dist/options.d.ts +101 -0
- package/dist/options.js +79 -0
- package/dist/performance.d.ts +4 -0
- package/dist/performance.js +37 -0
- package/dist/pino.d.ts +6 -0
- package/dist/pino.js +35 -0
- package/dist/privacy.d.ts +28 -0
- package/dist/privacy.js +338 -0
- package/dist/processors.d.ts +11 -0
- package/dist/processors.js +64 -0
- package/dist/queue.d.ts +54 -0
- package/dist/queue.js +179 -0
- package/dist/runtime.d.ts +88 -0
- package/dist/runtime.js +67 -0
- package/dist/suppression.d.ts +3 -0
- package/dist/suppression.js +14 -0
- package/package.json +70 -3
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 [yyyy] [name of copyright owner]
|
|
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,179 @@
|
|
|
1
|
+
# agentrail-node
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/agentrail-node)
|
|
4
|
+
[](https://www.npmjs.com/package/agentrail-node)
|
|
5
|
+
[](https://github.com/agentrail-co/agentrail-js/blob/main/LICENSE)
|
|
6
|
+
|
|
7
|
+
The Agentrail SDK for Node servers. It records what happens on your server
|
|
8
|
+
when an agent or a person reaches it: the requests it answers and the HTTP and
|
|
9
|
+
`fetch` calls it makes, the identity your application verified, the milestones
|
|
10
|
+
you name, your logs and your MCP tool calls. [Agentrail](https://www.agentrail.co)
|
|
11
|
+
links these steps with the ones your edge and pages record, into one journey.
|
|
12
|
+
|
|
13
|
+
What leaves your process is each step's facts: a request's method, route,
|
|
14
|
+
status, user agent and URL (its query dropped, but for Agentrail's own
|
|
15
|
+
markers); an MCP call's method, tool or prompt name, resource URI (without its
|
|
16
|
+
userinfo, query or fragment), the client's self-reported name and version,
|
|
17
|
+
message sizes, JSON-RPC id and error code, and, for a call that came over
|
|
18
|
+
HTTP, the names of the request's headers with its `Accept` and
|
|
19
|
+
`Accept-Encoding`; and identities only as keyed hashes. Request and response
|
|
20
|
+
bodies, the values of other headers and raw identities never leave it.
|
|
21
|
+
MCP tool arguments and results are not exported unless you turn on
|
|
22
|
+
`capturePayloads`, and then only as your `redact` hook returns them. The same
|
|
23
|
+
features are in the
|
|
24
|
+
[Go](https://github.com/agentrail-co/agentrail-go) and
|
|
25
|
+
[Python](https://github.com/agentrail-co/agentrail-python) SDKs.
|
|
26
|
+
|
|
27
|
+
## Install
|
|
28
|
+
|
|
29
|
+
Node 22 or later.
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
npm install agentrail-node
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
In Agentrail, open **Journeys → Set up** and choose **Your servers**. Agentrail
|
|
36
|
+
shows the **Secret** and the **Hash key** once: keep them in your server's
|
|
37
|
+
secrets as `AGENTRAIL_SECRET` and `AGENTRAIL_HASH_KEY`, never in a page.
|
|
38
|
+
|
|
39
|
+
## Quick start
|
|
40
|
+
|
|
41
|
+
Start Agentrail before your application's HTTP modules load:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
// instrumentation.ts
|
|
45
|
+
import { init } from 'agentrail-node';
|
|
46
|
+
|
|
47
|
+
const secret = process.env.AGENTRAIL_SECRET;
|
|
48
|
+
const hashKey = process.env.AGENTRAIL_HASH_KEY;
|
|
49
|
+
if (!secret || !hashKey) throw new Error('Agentrail is not configured');
|
|
50
|
+
|
|
51
|
+
export const agentrail = await init({
|
|
52
|
+
collector: { endpoint: 'https://<collector>/ingest/otlp', secret },
|
|
53
|
+
hashKey,
|
|
54
|
+
serviceName: 'orders-api',
|
|
55
|
+
outboundOrigins: ['https://inventory.example.com'], // your own services
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
node --import ./instrumentation.js app.js
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Every request your server answers is now recorded, with the calls it makes.
|
|
64
|
+
In a handler, once your application has authenticated the caller:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
agentrail.identify({ kind: 'user', id: user.id });
|
|
68
|
+
|
|
69
|
+
const order = await agentrail.milestone(
|
|
70
|
+
'checkout.confirm',
|
|
71
|
+
{ surface: 'custom', kind: 'checkout.confirm' },
|
|
72
|
+
async () => confirmOrder(),
|
|
73
|
+
);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
When the process stops, finish your requests, then send what is queued:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
process.on('SIGTERM', () => {
|
|
80
|
+
server.close(() => void agentrail.shutdown());
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## MCP servers
|
|
85
|
+
|
|
86
|
+
`agentrail-node/mcp` records every JSON-RPC message your MCP server receives
|
|
87
|
+
(`initialize`, `tools/list`, `tools/call`, notifications, and requests the SDK
|
|
88
|
+
refuses) as one step, through public APIs only: nothing is patched.
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { createMcpHandler, McpServer } from '@modelcontextprotocol/server';
|
|
92
|
+
import { mcp } from 'agentrail-node/mcp';
|
|
93
|
+
|
|
94
|
+
const capture = mcp({ client: agentrail });
|
|
95
|
+
|
|
96
|
+
// A handler from createMcpHandler: serve the wrapped one in its place.
|
|
97
|
+
export const handler = capture.handler(
|
|
98
|
+
createMcpHandler(() => {
|
|
99
|
+
const server = new McpServer({ name: 'orders', version: '1' });
|
|
100
|
+
server.registerTool(
|
|
101
|
+
'find_order',
|
|
102
|
+
{ inputSchema },
|
|
103
|
+
capture.tool('find_order', findOrder),
|
|
104
|
+
);
|
|
105
|
+
return server;
|
|
106
|
+
}),
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
// A transport you own: connect the wrapped one.
|
|
110
|
+
await server.connect(capture.transport(transport));
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
If you create the handler with a larger `maxRequestBodySize` than
|
|
114
|
+
`createMcpHandler`'s 4 MiB default, give the wrapper the same value, or bodies
|
|
115
|
+
past 4 MiB are served without a step:
|
|
116
|
+
`capture.handler(createMcpHandler(factory, { maxRequestBodySize }), { maxRequestBodySize })`.
|
|
117
|
+
|
|
118
|
+
Each call carries its method and name, the client's self-reported name,
|
|
119
|
+
version and protocol, the keyed hash of its `Mcp-Session-Id` (none for a
|
|
120
|
+
stateless server), the size of the request and response as JSON, and its
|
|
121
|
+
JSON-RPC error code or `isError` result.
|
|
122
|
+
|
|
123
|
+
A call that came over HTTP also carries what its HTTP request said of
|
|
124
|
+
itself: the method, the URL (its query dropped, but for Agentrail's own
|
|
125
|
+
markers), the user agent, `Accept` and `Accept-Encoding`, the names of its
|
|
126
|
+
headers (never a value), and the call's JSON-RPC id; through `capture.handler`,
|
|
127
|
+
the HTTP status of the answer too. Every message of a batch carries its
|
|
128
|
+
request's. A client's HTTP library writes its headers in an order and case of
|
|
129
|
+
its own, which say which client it is: where the SDK's HTTP instrumentation
|
|
130
|
+
saw the request (`init`, or `httpOptions` in your own setup), the names are
|
|
131
|
+
sent as Node received them; otherwise they come from the Fetch `Request`,
|
|
132
|
+
which lower-cases and sorts them. Names are sent as received, those a proxy
|
|
133
|
+
added too: Agentrail leaves those out. A call over stdio carries none of this.
|
|
134
|
+
|
|
135
|
+
`capture.tool`, `capture.resource`
|
|
136
|
+
and `capture.prompt` add the handler's own span inside the call, so a call is
|
|
137
|
+
counted once; on their own, without a wrapped transport, each is the step.
|
|
138
|
+
Inside a wrapped transport, a tool whose callback threw is a failed call: the
|
|
139
|
+
MCP server answers the throw with an `isError` result of its own, and the call
|
|
140
|
+
keeps `isError` but carries the thrown error's type, where a result your tool
|
|
141
|
+
returned with `isError` is a failed result (`tool_error`).
|
|
142
|
+
|
|
143
|
+
Payloads are off by default. To send them, redact first; returning `null`
|
|
144
|
+
sends neither:
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
const capture = mcp({
|
|
148
|
+
client: agentrail,
|
|
149
|
+
capturePayloads: {
|
|
150
|
+
redact: ({ method, name, request, response }) =>
|
|
151
|
+
name === 'find_order'
|
|
152
|
+
? { request, response: redactOrder(response) }
|
|
153
|
+
: null,
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
A payload is sent as JSON text with no length limit of its own, except that a
|
|
159
|
+
call's two payloads together are cut to 14 MiB of UTF-8 (the longer first, on a
|
|
160
|
+
character boundary, and flagged as truncated), so one call always fits an
|
|
161
|
+
export. At most 64 MiB of payload text waits to be sent: past that, calls go
|
|
162
|
+
without their payloads and `onError` is told (`payload_budget`), as it is when
|
|
163
|
+
`redact` throws or returns what JSON cannot hold (`payload_redact_failed`).
|
|
164
|
+
|
|
165
|
+
## Documentation
|
|
166
|
+
|
|
167
|
+
| Topic | Guide |
|
|
168
|
+
| ------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------- |
|
|
169
|
+
| Install, identity, milestones, logs (Pino), MCP, location | [Install the Node SDK](https://www.agentrail.co/docs/guides/journeys-node) |
|
|
170
|
+
| Every option, an existing OpenTelemetry setup (Sentry too), delivery and errors, the MCP and Pino adapters in full | [Node SDK reference](https://www.agentrail.co/docs/guides/journeys-node-reference) |
|
|
171
|
+
| Keeping your own services' calls out | [Leave out your own servers](https://www.agentrail.co/docs/guides/journeys-own-servers) |
|
|
172
|
+
| What is sent, and what never is | [What Agentrail records](https://www.agentrail.co/docs/guides/journeys-data) |
|
|
173
|
+
| Workflow attempts and plugin calls | [Plugin workflow tracking](https://www.agentrail.co/docs/guides/plugin-performance) |
|
|
174
|
+
|
|
175
|
+
Before 1.0, a minor version (0.1 to 0.2) may change what the package offers.
|
|
176
|
+
|
|
177
|
+
## Licence
|
|
178
|
+
|
|
179
|
+
[Apache-2.0](https://github.com/agentrail-co/agentrail-js/blob/main/LICENSE).
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type MilestoneContext, type VerifiedIdentity } from './options.js';
|
|
2
|
+
import { type StructuredLog } from './privacy.js';
|
|
3
|
+
import type { processors } from './processors.js';
|
|
4
|
+
import { type Runtime } from './runtime.js';
|
|
5
|
+
export interface AgentrailNode {
|
|
6
|
+
identify(identity: VerifiedIdentity): void;
|
|
7
|
+
milestone(name: string, details: MilestoneContext): void;
|
|
8
|
+
milestone<T>(name: string, details: MilestoneContext, run: () => T | Promise<T>): Promise<T>;
|
|
9
|
+
log(input: StructuredLog): void;
|
|
10
|
+
flush(): Promise<void>;
|
|
11
|
+
shutdown(): Promise<void>;
|
|
12
|
+
diagnostics(): {
|
|
13
|
+
spans: import('./queue.js').QueueStats;
|
|
14
|
+
logs: import('./queue.js').QueueStats;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export declare function createClient(runtime: Runtime, queues: ReturnType<typeof processors>): AgentrailNode;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { isCaptureSuppressed } from './suppression.js';
|
|
2
|
+
import { context, ROOT_CONTEXT, trace, SpanStatusCode, } from '@opentelemetry/api';
|
|
3
|
+
import { CaptureError, ConfigurationError, } from './options.js';
|
|
4
|
+
import { makeLog } from './privacy.js';
|
|
5
|
+
import { performanceAttributes } from './performance.js';
|
|
6
|
+
import { registerInternal } from './runtime.js';
|
|
7
|
+
export function createClient(runtime, queues) {
|
|
8
|
+
function parent() {
|
|
9
|
+
const active = context.active();
|
|
10
|
+
if (runtime.state(active) !== undefined)
|
|
11
|
+
return active;
|
|
12
|
+
const external = trace.getSpanContext(active);
|
|
13
|
+
return ROOT_CONTEXT.setValue(runtime.stateKey, runtime.newState(false, external === undefined ? [] : [{ context: external }]));
|
|
14
|
+
}
|
|
15
|
+
function start(name, details, identity, extra = {}) {
|
|
16
|
+
if (!name.isWellFormed() ||
|
|
17
|
+
!details.kind.isWellFormed() ||
|
|
18
|
+
name.includes('\u0000') ||
|
|
19
|
+
details.kind.includes('\u0000') ||
|
|
20
|
+
name.length === 0 ||
|
|
21
|
+
name.length > 256 ||
|
|
22
|
+
details.kind.length === 0 ||
|
|
23
|
+
details.kind.length > 256)
|
|
24
|
+
throw new ConfigurationError('Milestone name and kind must contain 1–256 characters.');
|
|
25
|
+
const { performance, ...metadata } = details;
|
|
26
|
+
let normalized;
|
|
27
|
+
if (performance !== undefined) {
|
|
28
|
+
try {
|
|
29
|
+
normalized = performanceAttributes(performance, runtime.options.input.hashKey);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (!(error instanceof ConfigurationError))
|
|
33
|
+
throw error;
|
|
34
|
+
runtime.options.report(new CaptureError('invalid_performance', 'context', 1, error));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const base = parent();
|
|
38
|
+
const marked = base.setValue(runtime.markKey, {
|
|
39
|
+
...metadata,
|
|
40
|
+
...(normalized === undefined ? {} : { performance: normalized }),
|
|
41
|
+
name,
|
|
42
|
+
...(identity === undefined ? {} : { identity }),
|
|
43
|
+
...extra,
|
|
44
|
+
});
|
|
45
|
+
const state = runtime.state(base);
|
|
46
|
+
const span = trace.getTracer('agentrail-node').startSpan(name, {
|
|
47
|
+
links: trace.getSpanContext(base) === undefined ? (state?.links ?? []) : [],
|
|
48
|
+
}, marked);
|
|
49
|
+
if (!span.isRecording() && !isCaptureSuppressed(context.active()))
|
|
50
|
+
runtime.options.report(new CaptureError('entry_not_recording', 'traces', 1));
|
|
51
|
+
return { span, ctx: trace.setSpan(base, span) };
|
|
52
|
+
}
|
|
53
|
+
function milestone(name, details, run) {
|
|
54
|
+
const { span, ctx } = start(name, details);
|
|
55
|
+
if (run === undefined) {
|
|
56
|
+
span.end();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
return context.with(ctx, async () => {
|
|
60
|
+
try {
|
|
61
|
+
return await run();
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
65
|
+
if (error instanceof Error)
|
|
66
|
+
span.setAttribute('error.type', error.name);
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
span.end();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
const client = {
|
|
75
|
+
identify(identity) {
|
|
76
|
+
if (identity.id.length === 0 || identity.actingUserId === '')
|
|
77
|
+
throw new ConfigurationError('Verified identity IDs cannot be empty.');
|
|
78
|
+
const state = runtime.state(context.active());
|
|
79
|
+
if (state === undefined) {
|
|
80
|
+
runtime.options.report(new CaptureError('identity_outside_request', 'context', 1));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const hashed = runtime.identity(identity);
|
|
84
|
+
if (state.identity === undefined)
|
|
85
|
+
state.identity = hashed;
|
|
86
|
+
else if (JSON.stringify(state.identity) !== JSON.stringify(hashed)) {
|
|
87
|
+
// Preserve the first assertion; contradictory assertions are explicit evidence.
|
|
88
|
+
const { span } = start('identity.asserted', { surface: 'auth', kind: 'auth.identity' }, hashed);
|
|
89
|
+
span.end();
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
milestone,
|
|
93
|
+
log(input) {
|
|
94
|
+
if (isCaptureSuppressed(context.active()))
|
|
95
|
+
return;
|
|
96
|
+
const record = makeLog(input, context.active(), runtime);
|
|
97
|
+
if (record !== undefined)
|
|
98
|
+
queues.logs.add(record);
|
|
99
|
+
},
|
|
100
|
+
async flush() {
|
|
101
|
+
await Promise.all([queues.spans.flush(), queues.logs.flush()]);
|
|
102
|
+
},
|
|
103
|
+
async shutdown() {
|
|
104
|
+
await Promise.all([queues.spans.shutdown(), queues.logs.shutdown()]);
|
|
105
|
+
},
|
|
106
|
+
diagnostics: () => ({
|
|
107
|
+
spans: queues.spans.diagnostics(),
|
|
108
|
+
logs: queues.logs.diagnostics(),
|
|
109
|
+
}),
|
|
110
|
+
};
|
|
111
|
+
registerInternal(client, {
|
|
112
|
+
runtime,
|
|
113
|
+
start: (name, details, extra) => start(name, details, undefined, extra),
|
|
114
|
+
});
|
|
115
|
+
return client;
|
|
116
|
+
}
|
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { TextMapPropagator } from '@opentelemetry/api';
|
|
2
|
+
import type { HttpInstrumentationConfig } from '@opentelemetry/instrumentation-http';
|
|
3
|
+
import type { UndiciInstrumentationConfig, UndiciRequest } from '@opentelemetry/instrumentation-undici';
|
|
4
|
+
import type { Runtime } from './runtime.js';
|
|
5
|
+
export declare function httpCapture(runtime: Runtime): {
|
|
6
|
+
propagator: TextMapPropagator<any>;
|
|
7
|
+
httpOptions: HttpInstrumentationConfig;
|
|
8
|
+
undiciOptions: UndiciInstrumentationConfig<UndiciRequest, import("@opentelemetry/instrumentation-undici").UndiciResponse>;
|
|
9
|
+
rememberPort(span: object, value: unknown): void;
|
|
10
|
+
};
|