@tessar/opentelemetry 0.0.0-stage → 0.1.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 +202 -0
- package/README.md +163 -2
- package/dist/adapters/laminar.d.ts +12 -0
- package/dist/adapters/laminar.js +97 -0
- package/dist/adapters/laminar.js.map +1 -0
- package/dist/adapters/langfuse.d.ts +18 -0
- package/dist/adapters/langfuse.js +112 -0
- package/dist/adapters/langfuse.js.map +1 -0
- package/dist/adapters/otlp.d.ts +14 -0
- package/dist/adapters/otlp.js +50 -0
- package/dist/adapters/otlp.js.map +1 -0
- package/dist/attributes.d.ts +8 -0
- package/dist/attributes.js +106 -0
- package/dist/attributes.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/lifecycle.d.ts +4 -0
- package/dist/lifecycle.js +162 -0
- package/dist/lifecycle.js.map +1 -0
- package/dist/messages.d.ts +75 -0
- package/dist/messages.js +55 -0
- package/dist/messages.js.map +1 -0
- package/dist/models.d.ts +4 -0
- package/dist/models.js +103 -0
- package/dist/models.js.map +1 -0
- package/dist/plugin.d.ts +9 -0
- package/dist/plugin.js +40 -0
- package/dist/plugin.js.map +1 -0
- package/dist/tools.d.ts +4 -0
- package/dist/tools.js +53 -0
- package/dist/tools.js.map +1 -0
- package/dist/trace.d.ts +24 -0
- package/dist/trace.js +211 -0
- package/dist/trace.js.map +1 -0
- package/dist/types.d.ts +54 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +49 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 https://github.com/hphucdev-04
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,164 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tessar/opentelemetry
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Optional tracing for Tessar through the official OpenTelemetry SDK and OTLP/HTTP
|
|
4
|
+
JSON exporter. Requires Node.js >=24.22.1. One plugin records executions; adapters
|
|
5
|
+
configure destinations and platform fields without duplicating hooks.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @tessar/core @tessar/opentelemetry
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { OpenTelemetryPlugin, LangfuseAdapter } from "@tessar/opentelemetry";
|
|
13
|
+
|
|
14
|
+
const tracing = new OpenTelemetryPlugin({
|
|
15
|
+
serviceName: "my-app",
|
|
16
|
+
tags: ["production"],
|
|
17
|
+
adapters: [new LangfuseAdapter({
|
|
18
|
+
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
|
|
19
|
+
secretKey: process.env.LANGFUSE_SECRET_KEY,
|
|
20
|
+
baseUrl: process.env.LANGFUSE_BASE_URL,
|
|
21
|
+
})],
|
|
22
|
+
});
|
|
23
|
+
// new Controller({ id, agent, plugins: [tracing] })
|
|
24
|
+
// await controller.initialize();
|
|
25
|
+
// Use agent.stream()/generate() or a Session.
|
|
26
|
+
// await controller.dispose(); // Flush buffered exports before exit.
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Plugin options
|
|
30
|
+
|
|
31
|
+
| Option | Meaning |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| `adapters` | Required nonempty array of `TraceAdapter` instances. Each destination receives the same trace/span IDs. |
|
|
34
|
+
| `realtime` | Optional boolean, defaults to `false`. Set `true` to export each completed span immediately to every adapter; otherwise use background batching. |
|
|
35
|
+
| `serviceName` | Optional OpenTelemetry `service.name` resource attribute. |
|
|
36
|
+
| `traceName` | Optional shared trace name; otherwise Agent ID, plus Thread ID for Session calls. |
|
|
37
|
+
| `tags` | Optional application-defined tags propagated to each observation. |
|
|
38
|
+
|
|
39
|
+
`realtime: true` uses the native OpenTelemetry `SimpleSpanProcessor`; it does not
|
|
40
|
+
stream individual tokens or publish unfinished spans. Network delivery and the
|
|
41
|
+
destination UI can still add latency. Await Controller disposal in either mode
|
|
42
|
+
to finish pending exports. Omitted `realtime` and explicit `false` both retain
|
|
43
|
+
the default `BatchSpanProcessor` and its SDK batching configuration.
|
|
44
|
+
|
|
45
|
+
## Adapters
|
|
46
|
+
|
|
47
|
+
All classes are named exports from `@tessar/opentelemetry`. Configuration options
|
|
48
|
+
are optional unless a requirement is stated below. Explicit values override
|
|
49
|
+
environment variables. `endpoint` always means a complete HTTP(S) trace URL,
|
|
50
|
+
including `/v1/traces`; `baseUrl` means a platform base URL.
|
|
51
|
+
|
|
52
|
+
| Class | Configuration | Default destination |
|
|
53
|
+
| --- | --- | --- |
|
|
54
|
+
| `OTLPAdapter` | `endpoint?`, `headers?` | Native SDK `OTEL_EXPORTER_OTLP_*` environment configuration, otherwise localhost:4318. |
|
|
55
|
+
| `LangfuseAdapter` | `publicKey?`, `secretKey?`, `baseUrl?`, `prompt?: { name, version }` | Langfuse EU Cloud; supports another region or self-hosting. Both keys are required via options or `LANGFUSE_PUBLIC_KEY` / `LANGFUSE_SECRET_KEY`. `LANGFUSE_BASE_URL` selects deployment. |
|
|
56
|
+
| `LaminarAdapter` | `apiKey?`, `endpoint?` | Laminar Cloud. Project key is required via options or `LMNR_PROJECT_API_KEY`. Default is `https://api.lmnr.ai/v1/traces`; `LMNR_BASE_URL` selects a different base URL, including a self-hosted deployment. |
|
|
57
|
+
|
|
58
|
+
Langfuse maps ingestion-v4 observation attributes and exclusive token buckets.
|
|
59
|
+
Its optional prompt reference links primary generations to an existing Langfuse
|
|
60
|
+
prompt; it does not upload instructions. Laminar maps native span types, I/O and
|
|
61
|
+
trace-wide association metadata. Its Session ID is the Tessar Thread ID; the
|
|
62
|
+
original Tessar Session ID remains in metadata. A project API key selects the
|
|
63
|
+
destination project; no separate space/project-name configuration is needed.
|
|
64
|
+
No platform SDK is required.
|
|
65
|
+
|
|
66
|
+
Use `OTLPAdapter` for an OTel Collector or another compatible OTLP receiver.
|
|
67
|
+
Copy the full trace endpoint and authentication header from your deployment:
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
new OTLPAdapter({
|
|
71
|
+
endpoint: process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT!,
|
|
72
|
+
headers: { Authorization: process.env.OTEL_AUTHORIZATION! },
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
You can provide several adapters to export to several destinations. Adapter
|
|
77
|
+
mappings use independent snapshots and cannot add platform fields to other
|
|
78
|
+
adapters' spans. Implement `TraceAdapter.createExporter()` to supply another
|
|
79
|
+
OpenTelemetry `SpanExporter`, or extend `OTLPAdapter` and override its protected
|
|
80
|
+
`mapSpan()` for a receiver-specific mapping.
|
|
81
|
+
|
|
82
|
+
## Trace contents and conventions
|
|
83
|
+
|
|
84
|
+
Each Agent invocation or Session follow-up creates its own trace with an Agent
|
|
85
|
+
root. Langfuse groups these traces into a Session identified by the Tessar Thread
|
|
86
|
+
ID; the original Tessar Session ID remains in observation metadata. Direct Agent
|
|
87
|
+
calls do not create a Langfuse Session. The hierarchy is Agent -> numbered Turns
|
|
88
|
+
-> model attempts and tools. Auxiliary model calls keep their own input/output and originating tool
|
|
89
|
+
parent. Effective requests include instructions, history, tools and configured
|
|
90
|
+
options. Turn output records committed assistant/tool messages.
|
|
91
|
+
Stream, Turn, model and tool spans use recorded lifecycle start/end times rather
|
|
92
|
+
than notification delivery time. Turn spans include context preparation. Ordered
|
|
93
|
+
tool result publication can happen later than execution completion, especially
|
|
94
|
+
with parallel tools. Auxiliary calls made after a Turn ends attach to the Agent
|
|
95
|
+
root instead of creating an extra Turn. Consumer delivery errors do not fail it.
|
|
96
|
+
|
|
97
|
+
Shared spans use GenAI attributes for operation, provider, requested model,
|
|
98
|
+
Agent/conversation IDs and known input/output, cache and reasoning token counts.
|
|
99
|
+
GenAI system instructions and input/output message parts preserve text, reasoning
|
|
100
|
+
and tool calls/results separately. Media/custom parts retain their original types
|
|
101
|
+
as generic parts. This remains a subset of GenAI conventions, not complete
|
|
102
|
+
conformance. Original requests, responses and usage stay lossless in `tessar.*`.
|
|
103
|
+
Adapter display mappings include ordered instructions as a System message.
|
|
104
|
+
Langfuse omits the duplicate instructions field from Additional Input.
|
|
105
|
+
Laminar uses EXECUTOR for Agent roots, DEFAULT for Turns, LLM for model attempts
|
|
106
|
+
and TOOL for tool executions. Root and Turn I/O are JSON `{ role, content }`
|
|
107
|
+
message arrays. Root input includes the first effective primary request's System
|
|
108
|
+
message and that invocation's input; Turn input includes its full effective history.
|
|
109
|
+
Later Turns and auxiliary calls do not overwrite the root instruction snapshot.
|
|
110
|
+
Laminar model I/O uses GenAI `{ role, parts }` arrays with its parser's `thinking`
|
|
111
|
+
and tool-result `result` fields. This receiver mapping keeps visible reasoning
|
|
112
|
+
separate from answer text and preserves tool-call order/correlation. Original
|
|
113
|
+
Tessar requests, parts and results remain under `tessar.*`.
|
|
114
|
+
Available tool definitions use `gen_ai.tool.definitions`; executed tool spans keep
|
|
115
|
+
description/schema under GenAI/Tessar attributes. Langfuse also exposes
|
|
116
|
+
`toolDescription` / `toolParameters` in observation metadata.
|
|
117
|
+
Tool inputs are arguments and outputs are their values, not a message record.
|
|
118
|
+
Laminar trace-wide association metadata is emitted on roots, so per-attempt and
|
|
119
|
+
per-tool identity is not propagated across the entire trace.
|
|
120
|
+
|
|
121
|
+
The request's `tools` list shows tools available to the model, not tools executed
|
|
122
|
+
in that Turn. Only executed tools get child tool spans under their calling Turn.
|
|
123
|
+
Unknown token counts are omitted. Costs and scores
|
|
124
|
+
are calculated or submitted through the destination's own features; Tessar does
|
|
125
|
+
not infer a routed model's price or generate evaluation scores.
|
|
126
|
+
|
|
127
|
+
Turn counters are checkpointed under plugin ID `opentelemetry`, namespace `trace`,
|
|
128
|
+
with reset on fork. Persistent storage retains Turn numbering and Langfuse Thread
|
|
129
|
+
grouping after restart; each invocation still owns a new root with its own input,
|
|
130
|
+
output, status and timing. Resume/recovery creates another trace segment with the
|
|
131
|
+
same logical `streamId`. A continued Turn retains its logical ID and number but
|
|
132
|
+
gets a fresh observation under that segment's root. Trace IDs include the Stream
|
|
133
|
+
and resumed checkpoint identity; ended parents are never reused or exported again.
|
|
134
|
+
Resumed observations never start before their new segment root; cached results
|
|
135
|
+
predating that segment are published without negative durations. Original record
|
|
136
|
+
times remain in `tessar.metadata.recordStartedAt` / `recordEndedAt`.
|
|
137
|
+
Older `opentelemetry` counter snapshots remain readable; obsolete parent flags
|
|
138
|
+
are ignored. Effective conversation history is recorded on each model attempt.
|
|
139
|
+
|
|
140
|
+
Each plugin owns its tracer and explicit span parents without replacing the
|
|
141
|
+
application's global OTel provider. The SDK exports completed spans asynchronously,
|
|
142
|
+
using background batching by default; export failures do not fail agent execution. Export is best-effort,
|
|
143
|
+
not a durable event journal. Await Controller disposal to drain buffered spans.
|
|
144
|
+
Requests and application metadata are sent to the chosen destinations.
|
|
145
|
+
|
|
146
|
+
## Migration
|
|
147
|
+
|
|
148
|
+
`ArizeAdapter` and `ArizeOptions` have been removed. Replace them with
|
|
149
|
+
`LaminarAdapter` / `LaminarOptions` and configure `LMNR_PROJECT_API_KEY`.
|
|
150
|
+
Existing traces on other destinations are not moved or deleted.
|
|
151
|
+
|
|
152
|
+
This package replaces `@tessar/langfuse` in this repository. Replace
|
|
153
|
+
`new LangfusePlugin(options)` with
|
|
154
|
+
`new OpenTelemetryPlugin({ adapters: [new LangfuseAdapter(platformOptions)], ...sharedOptions })`.
|
|
155
|
+
Move `publicKey`, `secretKey`, `baseUrl` and `prompt` to the adapter; keep
|
|
156
|
+
`traceName` and `tags` on the plugin. Existing `langfuse` checkpoint markers are
|
|
157
|
+
not migrated to the new plugin identity; continuing such a Thread does not
|
|
158
|
+
preserve its previous Turn counter. Use a new Thread when adopting this package.
|
|
159
|
+
|
|
160
|
+
See the [developer guide](../../examples/guide/opentelemetry.md) for complete
|
|
161
|
+
Agent and Session examples and links to platform documentation.
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ReadableSpan } from "@opentelemetry/sdk-trace-base";
|
|
2
|
+
import { OTLPAdapter } from "./otlp.js";
|
|
3
|
+
export interface LaminarOptions {
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
/** Full OTLP/HTTP trace URL; otherwise uses LMNR_BASE_URL plus /v1/traces. */
|
|
6
|
+
endpoint?: string;
|
|
7
|
+
}
|
|
8
|
+
/** Laminar OTLP/HTTP authentication and native span/message mapping. */
|
|
9
|
+
export declare class LaminarAdapter extends OTLPAdapter {
|
|
10
|
+
constructor(options?: LaminarOptions);
|
|
11
|
+
protected mapSpan(span: ReadableSpan): ReadableSpan;
|
|
12
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { mappedSpan, readJson } from "../attributes.js";
|
|
2
|
+
import { genAIMessages, inputMessages } from "../messages.js";
|
|
3
|
+
import { OTLPAdapter } from "./otlp.js";
|
|
4
|
+
function conversation(values) {
|
|
5
|
+
return JSON.stringify(values.map(({ role, content, toolCallId }) => ({
|
|
6
|
+
role, content, ...(toolCallId !== undefined ? { toolCallId } : {}),
|
|
7
|
+
})));
|
|
8
|
+
}
|
|
9
|
+
/** Laminar's GenAI parser uses thinking and result for these part fields. */
|
|
10
|
+
function laminarMessages(values) {
|
|
11
|
+
return genAIMessages(values).map(message => ({ ...message,
|
|
12
|
+
parts: message.parts.map(part => {
|
|
13
|
+
if (part.type === "reasoning")
|
|
14
|
+
return { ...part, type: "thinking" };
|
|
15
|
+
if (part.type === "tool_call_response" && "response" in part) {
|
|
16
|
+
const { response, ...rest } = part;
|
|
17
|
+
return { ...rest, result: response };
|
|
18
|
+
}
|
|
19
|
+
return part;
|
|
20
|
+
}),
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
/** Laminar OTLP/HTTP authentication and native span/message mapping. */
|
|
24
|
+
export class LaminarAdapter extends OTLPAdapter {
|
|
25
|
+
constructor(options = {}) {
|
|
26
|
+
const apiKey = options.apiKey ?? process.env.LMNR_PROJECT_API_KEY;
|
|
27
|
+
if (!apiKey)
|
|
28
|
+
throw Error("Laminar requires apiKey or LMNR_PROJECT_API_KEY");
|
|
29
|
+
const base = (process.env.LMNR_BASE_URL ?? "https://api.lmnr.ai").replace(/\/+$/, "");
|
|
30
|
+
super({ endpoint: options.endpoint ?? base + "/v1/traces", headers: { Authorization: "Bearer " + apiKey } });
|
|
31
|
+
}
|
|
32
|
+
mapSpan(span) {
|
|
33
|
+
const attributes = { ...span.attributes };
|
|
34
|
+
const type = String(span.attributes["tessar.observation.type"]);
|
|
35
|
+
attributes["lmnr.span.type"] = { agent: "EXECUTOR", span: "DEFAULT", generation: "LLM", tool: "TOOL" }[type] ?? "DEFAULT";
|
|
36
|
+
if (span.attributes["tessar.metadata.sessionId"] && typeof span.attributes["gen_ai.conversation.id"] === "string")
|
|
37
|
+
attributes["session.id"] = span.attributes["gen_ai.conversation.id"];
|
|
38
|
+
// Association metadata is trace-wide in Laminar. Keep attempt/tool identity
|
|
39
|
+
// on its original tessar.* fields instead of propagating it to every span.
|
|
40
|
+
if (type === "agent") {
|
|
41
|
+
if (span.attributes["tessar.metadata.sessionId"])
|
|
42
|
+
attributes["lmnr.association.properties.session_id"] = span.attributes["gen_ai.conversation.id"];
|
|
43
|
+
if (span.attributes["tessar.trace.tags"] !== undefined)
|
|
44
|
+
attributes["lmnr.association.properties.tags"] = span.attributes["tessar.trace.tags"];
|
|
45
|
+
if (span.attributes["tessar.trace.name"] !== undefined)
|
|
46
|
+
attributes["trace.name"] = span.attributes["tessar.trace.name"];
|
|
47
|
+
for (const [key, value] of Object.entries(span.attributes)) {
|
|
48
|
+
if (!key.startsWith("tessar.metadata."))
|
|
49
|
+
continue;
|
|
50
|
+
const parsed = typeof value === "string" ? JSON.parse(value) : value;
|
|
51
|
+
if (parsed !== null && parsed !== undefined)
|
|
52
|
+
attributes[key.replace("tessar.metadata.", "lmnr.association.properties.metadata.")] = typeof parsed === "object" ? JSON.stringify(parsed) : parsed;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (const direction of ["input", "output"])
|
|
56
|
+
if (span.attributes["tessar." + direction] !== undefined)
|
|
57
|
+
attributes["lmnr.span." + direction] = span.attributes["tessar." + direction];
|
|
58
|
+
if (type === "agent" || type === "span") {
|
|
59
|
+
const request = readJson(span, "tessar.input");
|
|
60
|
+
const values = Array.isArray(request)
|
|
61
|
+
? inputMessages({ instructions: readJson(span, "tessar.instructions") ?? { sections: [] }, messages: request })
|
|
62
|
+
: request?.messages
|
|
63
|
+
? inputMessages({ instructions: request.instructions ?? { sections: [] }, messages: request.messages })
|
|
64
|
+
: undefined;
|
|
65
|
+
if (values)
|
|
66
|
+
attributes["lmnr.span.input"] = conversation(values);
|
|
67
|
+
const output = readJson(span, "tessar.output");
|
|
68
|
+
if (Array.isArray(output))
|
|
69
|
+
attributes["lmnr.span.output"] = conversation(output);
|
|
70
|
+
}
|
|
71
|
+
if (type === "generation") {
|
|
72
|
+
const request = readJson(span, "tessar.input");
|
|
73
|
+
if (request?.messages) {
|
|
74
|
+
attributes["lmnr.span.input"] = JSON.stringify(laminarMessages(inputMessages(request)));
|
|
75
|
+
attributes["gen_ai.input.messages"] = JSON.stringify(laminarMessages(request.messages));
|
|
76
|
+
}
|
|
77
|
+
const output = readJson(span, "tessar.output");
|
|
78
|
+
if (output?.content) {
|
|
79
|
+
const messages = JSON.stringify(laminarMessages([output]));
|
|
80
|
+
attributes["lmnr.span.output"] = messages;
|
|
81
|
+
attributes["gen_ai.output.messages"] = messages;
|
|
82
|
+
}
|
|
83
|
+
attributes["gen_ai.system"] = span.attributes["gen_ai.provider.name"];
|
|
84
|
+
const usage = readJson(span, "tessar.usage");
|
|
85
|
+
if (usage?.totalTokens !== null && usage?.totalTokens !== undefined)
|
|
86
|
+
attributes["llm.usage.total_tokens"] = usage.totalTokens;
|
|
87
|
+
if (usage?.reasoningOutputTokens !== null && usage?.reasoningOutputTokens !== undefined)
|
|
88
|
+
attributes["gen_ai.usage.reasoning_tokens"] = usage.reasoningOutputTokens;
|
|
89
|
+
}
|
|
90
|
+
if (type === "tool") {
|
|
91
|
+
attributes["lmnr.span.input"] = span.attributes["gen_ai.tool.call.arguments"] ?? attributes["lmnr.span.input"];
|
|
92
|
+
attributes["lmnr.span.output"] = span.attributes["gen_ai.tool.call.result"] ?? attributes["lmnr.span.output"];
|
|
93
|
+
}
|
|
94
|
+
return mappedSpan(span, attributes);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=laminar.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"laminar.js","sourceRoot":"","sources":["../../src/adapters/laminar.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAUxC,SAAS,YAAY,CAAC,MAA+B;IACnD,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC,CAAC;QACnE,IAAI,EAAE,OAAO,EAAE,GAAG,CAAC,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACnE,CAAC,CAAC,CAAC,CAAC;AACP,CAAC;AAED,6EAA6E;AAC7E,SAAS,eAAe,CAAC,MAA+B;IACtD,OAA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B,KAAK,IAAI,IAAI,KAAK,EAAE,qBAAqB,KAAK,SAAS;gBACrF,UAAU,CAAC,+BAA+B,CAAC,GAAG,KAAK,CAAC,qBAAqB,CAAC;QAC9E,CAAC;QACD,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;YACpB,UAAU,CAAC,iBAAiB,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,4BAA4B,CAAC,IAAI,UAAU,CAAC,iBAAiB,CAAC,CAAC;YAC/G,UAAU,CAAC,kBAAkB,CAAC,GAAG,IAAI,CAAC,UAAU,CAAC,yBAAyB,CAAC,IAAI,UAAU,CAAC,kBAAkB,CAAC,CAAC;QAChH,CAAC;QACD,OAAO,UAAU,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;IACtC,CAAC;CACF"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ReadableSpan } from "@opentelemetry/sdk-trace-base";
|
|
2
|
+
import { OTLPAdapter } from "./otlp.js";
|
|
3
|
+
export interface LangfuseOptions {
|
|
4
|
+
publicKey?: string;
|
|
5
|
+
secretKey?: string;
|
|
6
|
+
/** Cloud region or self-hosted base URL. Defaults to LANGFUSE_BASE_URL or EU Cloud. */
|
|
7
|
+
baseUrl?: string;
|
|
8
|
+
prompt?: {
|
|
9
|
+
name: string;
|
|
10
|
+
version: number;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/** Maps shared spans to Langfuse ingestion v4; no Langfuse SDK is required. */
|
|
14
|
+
export declare class LangfuseAdapter extends OTLPAdapter {
|
|
15
|
+
#private;
|
|
16
|
+
constructor(options?: LangfuseOptions);
|
|
17
|
+
protected mapSpan(span: ReadableSpan): ReadableSpan;
|
|
18
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { mappedSpan, readJson } from "../attributes.js";
|
|
2
|
+
import { OTLPAdapter } from "./otlp.js";
|
|
3
|
+
import { inputMessages } from "../messages.js";
|
|
4
|
+
// Langfuse buckets are exclusive; cached/reasoning tokens already belong to totals.
|
|
5
|
+
function usageDetails(usage) {
|
|
6
|
+
const details = {};
|
|
7
|
+
const read = usage.cacheReadInputTokens, write = usage.cacheWriteInputTokens;
|
|
8
|
+
if (usage.inputTokens !== null) {
|
|
9
|
+
if ((read ?? 0) + (write ?? 0) <= usage.inputTokens) {
|
|
10
|
+
details.input = usage.inputTokens - (read ?? 0) - (write ?? 0);
|
|
11
|
+
if (read !== null)
|
|
12
|
+
details.input_cached_tokens = read;
|
|
13
|
+
if (write !== null)
|
|
14
|
+
details.input_cache_write = write;
|
|
15
|
+
}
|
|
16
|
+
else
|
|
17
|
+
details.input = usage.inputTokens;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
if (read !== null)
|
|
21
|
+
details.input_cached_tokens = read;
|
|
22
|
+
if (write !== null)
|
|
23
|
+
details.input_cache_write = write;
|
|
24
|
+
if (usage.uncachedInputTokens !== null)
|
|
25
|
+
details.input = usage.uncachedInputTokens;
|
|
26
|
+
}
|
|
27
|
+
if (usage.outputTokens !== null) {
|
|
28
|
+
const reasoning = usage.reasoningOutputTokens;
|
|
29
|
+
if (reasoning !== null && reasoning <= usage.outputTokens) {
|
|
30
|
+
details.output = usage.outputTokens - reasoning;
|
|
31
|
+
details.output_reasoning_tokens = reasoning;
|
|
32
|
+
}
|
|
33
|
+
else
|
|
34
|
+
details.output = usage.outputTokens;
|
|
35
|
+
}
|
|
36
|
+
else if (usage.reasoningOutputTokens !== null)
|
|
37
|
+
details.output_reasoning_tokens = usage.reasoningOutputTokens;
|
|
38
|
+
if (usage.totalTokens !== null)
|
|
39
|
+
details.total = usage.totalTokens;
|
|
40
|
+
return Object.keys(details).length ? details : undefined;
|
|
41
|
+
}
|
|
42
|
+
/** Maps shared spans to Langfuse ingestion v4; no Langfuse SDK is required. */
|
|
43
|
+
export class LangfuseAdapter extends OTLPAdapter {
|
|
44
|
+
#prompt;
|
|
45
|
+
constructor(options = {}) {
|
|
46
|
+
const publicKey = options.publicKey ?? process.env.LANGFUSE_PUBLIC_KEY;
|
|
47
|
+
const secretKey = options.secretKey ?? process.env.LANGFUSE_SECRET_KEY;
|
|
48
|
+
if (!publicKey || !secretKey)
|
|
49
|
+
throw Error("Langfuse requires publicKey and secretKey or LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY");
|
|
50
|
+
const base = (options.baseUrl ??
|
|
51
|
+
process.env.LANGFUSE_BASE_URL ??
|
|
52
|
+
"https://cloud.langfuse.com").replace(/\/+$/, "");
|
|
53
|
+
super({
|
|
54
|
+
endpoint: base + "/api/public/otel/v1/traces",
|
|
55
|
+
headers: {
|
|
56
|
+
Authorization: "Basic " +
|
|
57
|
+
Buffer.from(publicKey + ":" + secretKey).toString("base64"),
|
|
58
|
+
"x-langfuse-ingestion-version": "4",
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
this.#prompt = options.prompt ? { ...options.prompt } : undefined;
|
|
62
|
+
}
|
|
63
|
+
mapSpan(span) {
|
|
64
|
+
const attributes = { ...span.attributes };
|
|
65
|
+
// A Langfuse Session groups invocation traces for one Tessar Thread.
|
|
66
|
+
// The original Tessar Session ID remains in observation metadata.
|
|
67
|
+
if (span.attributes["tessar.metadata.sessionId"] &&
|
|
68
|
+
typeof span.attributes["gen_ai.conversation.id"] === "string")
|
|
69
|
+
attributes["session.id"] = span.attributes["gen_ai.conversation.id"];
|
|
70
|
+
const keys = {
|
|
71
|
+
"tessar.observation.type": "langfuse.observation.type",
|
|
72
|
+
"tessar.trace.name": "langfuse.trace.name",
|
|
73
|
+
"tessar.trace.tags": "langfuse.trace.tags",
|
|
74
|
+
"tessar.input": "langfuse.observation.input",
|
|
75
|
+
"tessar.output": "langfuse.observation.output",
|
|
76
|
+
"tessar.level": "langfuse.observation.level",
|
|
77
|
+
"tessar.status_message": "langfuse.observation.status_message",
|
|
78
|
+
"tessar.completion_start_time": "langfuse.observation.completion_start_time",
|
|
79
|
+
"tessar.model.parameters": "langfuse.observation.model.parameters",
|
|
80
|
+
"gen_ai.request.model": "langfuse.observation.model.name",
|
|
81
|
+
};
|
|
82
|
+
for (const [key, value] of Object.entries(span.attributes)) {
|
|
83
|
+
const target = keys[key] ??
|
|
84
|
+
(key.startsWith("tessar.metadata.")
|
|
85
|
+
? key.replace("tessar.metadata.", "langfuse.observation.metadata.")
|
|
86
|
+
: undefined);
|
|
87
|
+
if (target && value !== undefined)
|
|
88
|
+
attributes[target] = value;
|
|
89
|
+
}
|
|
90
|
+
const request = readJson(span, "tessar.input");
|
|
91
|
+
if (request?.instructions && request.messages) {
|
|
92
|
+
const { instructions, ...input } = request;
|
|
93
|
+
attributes["langfuse.observation.input"] = JSON.stringify({
|
|
94
|
+
...input,
|
|
95
|
+
messages: inputMessages({ instructions, messages: request.messages }),
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
const usage = readJson(span, "tessar.usage");
|
|
99
|
+
const details = usage && usageDetails(usage);
|
|
100
|
+
if (details)
|
|
101
|
+
attributes["langfuse.observation.usage_details"] =
|
|
102
|
+
JSON.stringify(details);
|
|
103
|
+
if (this.#prompt &&
|
|
104
|
+
span.attributes["tessar.observation.type"] === "generation" &&
|
|
105
|
+
readJson(span, "tessar.metadata.purpose") !== "auxiliary") {
|
|
106
|
+
attributes["langfuse.observation.prompt.name"] = this.#prompt.name;
|
|
107
|
+
attributes["langfuse.observation.prompt.version"] = this.#prompt.version;
|
|
108
|
+
}
|
|
109
|
+
return mappedSpan(span, attributes);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=langfuse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"langfuse.js","sourceRoot":"","sources":["../../src/adapters/langfuse.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAA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AAQ,CAAwB,IAAI,EAAE,cAAc,CAAC,CAAC;QACtE,IAAI,OAAO,EAAE,YAAY,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;YAC9C,MAAM,EAAE,YAAY,EAAE,GAAG,KAAK,EAAE,GAAG,OAAO,CAAC;YAC3C,UAAU,CAAC,4BAA4B,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC;gBACxD,GAAG,KAAK;gBACR,QAAQ,EAAE,aAAa,CAAC,EAAE,YAAY,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,CAAC;aACtE,CAAC,CAAC;QACL,CAAC;QACD,MAAM,KAAK,GAAG,QAAQ,CAAa,IAAI,EAAE,cAAc,CAAC,CAAC;QACzD,MAAM,OAAO,GAAG,KAAK,IAAI,YAAY,CAAC,KAAK,CAAC,CAAC;QAC7C,IAAI,OAAO;YACT,UAAU,CAAC,oCAAoC,CAAC;gBAC9C,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QAC5B,IACE,IAAI,CAAC,OAAO;YACZ,IAAI,CAAC,UAAU,CAAC,yBAAyB,CAAC,KAAK,YAAY;YAC3D,QAAQ,CAAC,IAAI,EAAE,yBAAyB,CAAC,KAAK,WAAW,EACzD,CAAC;YACD,UAAU,CAAC,kCAAkC,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;YACnE,UAAU,CAAC,qCAAqC,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC;QAC3E,CAAC;QACD,OAAO,UAAU,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;IACtC,CAAC;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ReadableSpan, SpanExporter } from "@opentelemetry/sdk-trace-base";
|
|
2
|
+
import type { TraceAdapter } from "../types.js";
|
|
3
|
+
export interface OTLPOptions {
|
|
4
|
+
/** Full OTLP/HTTP trace URL, including /v1/traces. Otherwise uses SDK environment/defaults. */
|
|
5
|
+
endpoint?: string;
|
|
6
|
+
headers?: Readonly<Record<string, string>>;
|
|
7
|
+
}
|
|
8
|
+
/** Standard OTLP/HTTP JSON export, for compatible receivers and collectors. */
|
|
9
|
+
export declare class OTLPAdapter implements TraceAdapter {
|
|
10
|
+
#private;
|
|
11
|
+
constructor(options?: OTLPOptions);
|
|
12
|
+
protected mapSpan(span: ReadableSpan): ReadableSpan;
|
|
13
|
+
createExporter(): SpanExporter;
|
|
14
|
+
}
|