1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//! OpenTelemetry trace collector for the dashboard.
//!
//! [`OtelTracingPlugin`] turns the dashboard into a standalone OTLP viewer.
//! It mounts an OTLP/HTTP **JSON** ingest endpoint and serves the same
//! `/v1/tracing/*` read surface the SPA already consumes, backed by its own
//! [`TraceStore`]:
//!
//! | Method & path | Role |
//! |------------------------------|---------------------------------------------|
//! | `POST /v1/traces` | OTLP/HTTP JSON ingest (any number of sources) |
//! | `GET /v1/tracing/runs` | Runs list (one run per trace id) |
//! | `GET /v1/tracing/runs/{id}` | Span tree for one run |
//!
//! Every emitter — an arbitrary microservice, or Polaris itself —
//! points its OTLP exporter at `POST /v1/traces`. The collector copies all
//! attributes into the generic run-label / span-field bags and hardcodes
//! nothing about any source; the dashboard derives the "Source" column from
//! the OTLP resource attributes client-side.
//!
//! ## Relationship to the upstream tracing plugin
//!
//! This plugin **replaces** [`TracingPlugin`](polaris_ai::plugins::TracingPlugin):
//! both mount `GET /v1/tracing/runs`, so registering both panics axum on the duplicate
//! route. Use this plugin to view arbitrary `OTel` sources; to also see native
//! Polaris runs, have the Polaris server export OTLP to `POST /v1/traces`.
//!
//! See [`OtelTracingPlugin`] for the full route table, lifecycle and security
//! notes, and a worked registration example.
use PathBuf;
use Arc;
use Duration;
use Json;
use Router;
use ;
use StatusCode;
use ;
use ;
use ;
use ;
use Server;
use json;
// The output contract (`/v1/tracing/*` response shapes) is re-exported so
// [`TraceStore`]'s public method signatures are nameable and navigable on
// docs.rs, not dead-end types in a private module. The OTLP ingest types stay
// internal — consumers feed a JSON payload via `TraceStore::ingest_otlp_json`,
// never hand-build the envelope.
use ExportTraceServiceRequest;
pub use ;
pub use ;
/// Maximum accepted OTLP ingest body size. Bounds memory use from a single
/// `POST /v1/traces` so an oversized (or hostile) payload can't exhaust the
/// process before deserialization. Generous enough for batched OTLP exports;
/// override upstream by layering a different [`DefaultBodyLimit`] if needed.
const MAX_INGEST_BODY_BYTES: usize = 4 * 1024 * 1024;
/// Turns the dashboard into a standalone OTLP viewer.
///
/// Reach for this plugin when you want to view traces from **arbitrary `OTel`
/// sources** (a microservice, or Polaris exporting OTLP) rather than only
/// native Polaris runs. It mounts an OTLP/HTTP JSON ingest endpoint plus the
/// `/v1/tracing/*` read surface the SPA already consumes, backed by its own
/// in-memory [`TraceStore`].
///
/// **Conflicts with the upstream tracing surface:** this plugin and
/// [`TracingPlugin`](polaris_ai::plugins::TracingPlugin) both mount
/// `GET /v1/tracing/runs`, so registering both panics axum on the duplicate
/// route. Pick one — use this plugin for `OTel` sources, or point a Polaris
/// server's OTLP exporter at `POST /v1/traces` to see native runs here too.
///
/// # Routes Provided
///
/// | Method | Path | Description |
/// |--------|------|-------------|
/// | `POST` | `/v1/traces` | OTLP/HTTP JSON trace ingest (any number of sources). |
/// | `GET` | `/v1/tracing/runs` | Runs list — one run per trace id. |
/// | `GET` | `/v1/tracing/runs/{id}` | Span tree for one run. |
///
/// # Resources Provided
///
/// | Resource | Scope | Description |
/// |----------|-------|-------------|
/// | _none_ | — | The collector's [`TraceStore`] is held as axum router state, not registered as a Polaris resource. |
///
/// # APIs Provided
///
/// None.
///
/// # Dependencies
///
/// - [`AppPlugin`] — provides the [`HttpRouter`] the routes are mounted on.
///
/// # Lifecycle
///
/// - **Requires `default-features = false`** on `polaris_dashboard`: the
/// default `native-tracing` feature mounts the upstream `TracingPlugin`'s
/// `/v1/tracing/runs`, which collides with this plugin's identical route and
/// panics axum on the duplicate. Disable it so this plugin owns the trace
/// surface.
/// - **`build()` panics** if no [`AppPlugin`] (the [`HttpRouter`] provider) has
/// been registered before this plugin.
///
/// # Security
///
/// `POST /v1/traces` is a state-mutating ingest endpoint that inherits whatever
/// `AuthProvider` the host installs on [`HttpRouter`] — the same (possibly
/// absent) auth as the read routes. If the host registers no provider, treat it
/// as an open write sink and keep it on a trusted network. Ingested attributes
/// are stored and re-served verbatim; the SPA is responsible for escaping them
/// on render.
///
/// # Example
///
/// ```no_run
/// use std::sync::Arc;
/// use polaris_ai::app::{AppConfig, AppPlugin};
/// use polaris_ai::sessions::{HttpPlugin, InMemoryStore, SessionsPlugin};
/// use polaris_ai::system::server::Server;
/// use polaris_dashboard::{DashboardPlugin, OtelTracingPlugin};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let mut server = Server::new();
/// server
/// .add_plugins(AppPlugin::new(AppConfig::new()))
/// .add_plugins(SessionsPlugin::new(Arc::new(InMemoryStore::new())))
/// .add_plugins(HttpPlugin::new())
/// .add_plugins(OtelTracingPlugin::new())
/// .add_plugins(DashboardPlugin::default());
/// server.run().await?;
/// # Ok(())
/// # }
/// ```
/// Retention configuration accumulated while a plugin is [`Configuring`],
/// applied to a fresh [`TraceStore`] by [`build`](OtelTracingPlugin::build).
///
/// Public only because it is the [`Configuring`] state's
/// [`StoreState::Data`]; it is opaque (no public fields or constructors) and
/// callers never name it — configure through the plugin's `with_*` methods.
/// Typestate of an [`OtelTracingPlugin`] — whether its [`TraceStore`] has been
/// built yet. Sealed: the only states are [`Configuring`] and [`Ready`].
/// Typestate marker: the store is still being configured. Retention knobs
/// ([`with_capacity`](OtelTracingPlugin::with_capacity),
/// [`with_ttl`](OtelTracingPlugin::with_ttl),
/// [`with_persistence`](OtelTracingPlugin::with_persistence)) are available, but
/// the plugin is **not** a [`Plugin`] yet and cannot be mounted until
/// [`build`](OtelTracingPlugin::build) seals it into [`Ready`].
;
/// Typestate marker: the store is built and the plugin is ready to mount. This
/// is the default state — `OtelTracingPlugin` means `OtelTracingPlugin<Ready>`,
/// so every existing reference and `add_plugins` call is unaffected.
;
// `Data` is `Debug + Clone` in every state, so both impls are unconditional —
// hand-written because `derive` would wrongly demand `S: Debug`/`S: Clone`.
/// OTLP/HTTP JSON ingest. Returns the OTLP success envelope. Non-JSON bodies
/// (e.g. protobuf) are rejected by the `Json` extractor with a 4xx — this
/// collector speaks OTLP/JSON only.
async
async
async