@datalayer/core 1.0.2 → 1.0.11
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/README.md +1 -1
- package/lib/api/constants.d.ts +6 -0
- package/lib/api/constants.js +6 -0
- package/lib/api/index.d.ts +1 -0
- package/lib/api/index.js +1 -0
- package/lib/api/otel/index.d.ts +12 -0
- package/lib/api/otel/index.js +16 -0
- package/lib/api/otel/logs.d.ts +19 -0
- package/lib/api/otel/logs.js +43 -0
- package/lib/api/otel/metrics.d.ts +31 -0
- package/lib/api/otel/metrics.js +65 -0
- package/lib/api/otel/query.d.ts +16 -0
- package/lib/api/otel/query.js +37 -0
- package/lib/api/otel/services.d.ts +39 -0
- package/lib/api/otel/services.js +81 -0
- package/lib/api/otel/traces.d.ts +24 -0
- package/lib/api/otel/traces.js +53 -0
- package/lib/api/otel/types.d.ts +112 -0
- package/lib/api/otel/types.js +5 -0
- package/lib/api/runtimes/checkpoints.d.ts +122 -0
- package/lib/api/runtimes/checkpoints.js +118 -0
- package/lib/api/runtimes/index.d.ts +1 -0
- package/lib/api/runtimes/index.js +1 -0
- package/lib/api/runtimes/runtimes.d.ts +84 -0
- package/lib/api/runtimes/runtimes.js +50 -0
- package/lib/components/auth/Login.js +1 -1
- package/lib/components/display/BusyDots.d.ts +9 -0
- package/lib/components/display/BusyDots.js +31 -0
- package/lib/components/display/LiveRelativeTime.d.ts +10 -0
- package/lib/components/display/LiveRelativeTime.js +21 -0
- package/lib/components/display/index.d.ts +2 -0
- package/lib/components/display/index.js +2 -0
- package/lib/components/flashes/FlashSurveys.js +1 -1
- package/lib/components/index.d.ts +1 -0
- package/lib/components/index.js +1 -0
- package/lib/components/navbar/SubdomainNavBar.js +1 -1
- package/lib/components/progress/ConsumptionBar.js +6 -7
- package/lib/components/progress/CreditsIndicator.js +2 -2
- package/lib/components/progress/consumption.d.ts +12 -0
- package/lib/components/progress/consumption.js +31 -0
- package/lib/components/progress/index.d.ts +1 -0
- package/lib/components/progress/index.js +1 -0
- package/lib/components/sparklines/Sparklines.d.ts +16 -0
- package/lib/components/sparklines/Sparklines.js +65 -0
- package/lib/components/sparklines/SparklinesLine.d.ts +8 -0
- package/lib/components/sparklines/SparklinesLine.js +37 -0
- package/lib/components/sparklines/dataProcessing.d.ts +25 -0
- package/lib/components/sparklines/dataProcessing.js +35 -0
- package/lib/components/sparklines/index.d.ts +4 -0
- package/lib/components/sparklines/index.js +7 -0
- package/lib/components/sparklines/types.d.ts +36 -0
- package/lib/components/sparklines/types.js +5 -0
- package/lib/components/storage/ContentsBrowser.js +17 -1
- package/lib/components/subnav/SubNav.js +1 -1
- package/lib/config/Configuration.d.ts +4 -0
- package/lib/hooks/useCache.d.ts +6 -63
- package/lib/hooks/useCache.js +35 -205
- package/lib/hooks/useProjects.d.ts +1 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.js +4 -0
- package/lib/models/ItemDTO.js +1 -1
- package/lib/models/RolesPlatform.js +2 -2
- package/lib/models/User.d.ts +2 -0
- package/lib/models/User.js +4 -1
- package/lib/otel/client/OtelClient.d.ts +93 -0
- package/lib/otel/client/OtelClient.js +232 -0
- package/lib/otel/client/index.d.ts +2 -0
- package/lib/otel/client/index.js +5 -0
- package/lib/otel/hooks/index.d.ts +186 -0
- package/lib/otel/hooks/index.js +532 -0
- package/lib/otel/index.d.ts +34 -0
- package/lib/otel/index.js +23 -0
- package/lib/otel/types.d.ts +190 -0
- package/lib/otel/types.js +5 -0
- package/lib/otel/utils.d.ts +33 -0
- package/lib/otel/utils.js +181 -0
- package/lib/otel/views/OtelLive.d.ts +12 -0
- package/lib/otel/views/OtelLive.js +372 -0
- package/lib/otel/views/OtelLogsList.d.ts +11 -0
- package/lib/otel/views/OtelLogsList.js +137 -0
- package/lib/otel/views/OtelMetricsChart.d.ts +22 -0
- package/lib/otel/views/OtelMetricsChart.js +300 -0
- package/lib/otel/views/OtelMetricsList.d.ts +15 -0
- package/lib/otel/views/OtelMetricsList.js +213 -0
- package/lib/otel/views/OtelSearchBar.d.ts +11 -0
- package/lib/otel/views/OtelSearchBar.js +22 -0
- package/lib/otel/views/OtelSpanDetail.d.ts +11 -0
- package/lib/otel/views/OtelSpanDetail.js +172 -0
- package/lib/otel/views/OtelSpanTree.d.ts +11 -0
- package/lib/otel/views/OtelSpanTree.js +176 -0
- package/lib/otel/views/OtelSqlView.d.ts +16 -0
- package/lib/otel/views/OtelSqlView.js +239 -0
- package/lib/otel/views/OtelSystemView.d.ts +15 -0
- package/lib/otel/views/OtelSystemView.js +75 -0
- package/lib/otel/views/OtelTimeline.d.ts +11 -0
- package/lib/otel/views/OtelTimeline.js +101 -0
- package/lib/otel/views/OtelTimelineRangeSlider.d.ts +16 -0
- package/lib/otel/views/OtelTimelineRangeSlider.js +338 -0
- package/lib/otel/views/OtelTracesList.d.ts +13 -0
- package/lib/otel/views/OtelTracesList.js +199 -0
- package/lib/otel/views/index.d.ts +20 -0
- package/lib/otel/views/index.js +21 -0
- package/lib/state/storage/IAMStorage.d.ts +2 -1
- package/lib/state/substates/CoreState.js +7 -6
- package/lib/utils/Date.d.ts +6 -0
- package/lib/utils/Date.js +37 -0
- package/lib/utils/Jwt.d.ts +42 -0
- package/lib/utils/Jwt.js +44 -0
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/index.js +1 -0
- package/lib/views/iam/SignInSimple.d.ts +43 -0
- package/lib/views/iam/SignInSimple.js +113 -0
- package/lib/views/iam/index.d.ts +2 -0
- package/lib/views/iam/index.js +5 -0
- package/lib/views/iam-tokens/IAMTokenEdit.d.ts +5 -1
- package/lib/views/iam-tokens/IAMTokenEdit.js +54 -5
- package/lib/views/iam-tokens/IAMTokenNew.js +2 -2
- package/lib/views/iam-tokens/IAMTokens.d.ts +4 -2
- package/lib/views/iam-tokens/IAMTokens.js +68 -36
- package/lib/views/iam-tokens/Tokens.js +63 -31
- package/lib/views/index.d.ts +3 -1
- package/lib/views/index.js +3 -1
- package/lib/views/otel/DashboardView.d.ts +16 -0
- package/lib/views/otel/DashboardView.js +4 -0
- package/lib/views/otel/LogsView.d.ts +12 -0
- package/lib/views/otel/LogsView.js +4 -0
- package/lib/views/otel/MetricsView.d.ts +12 -0
- package/lib/views/otel/MetricsView.js +4 -0
- package/lib/views/otel/OtelHeader.d.ts +33 -0
- package/lib/views/otel/OtelHeader.js +105 -0
- package/lib/views/otel/SqlView.d.ts +9 -0
- package/lib/views/otel/SqlView.js +4 -0
- package/lib/views/otel/SystemView.d.ts +9 -0
- package/lib/views/otel/SystemView.js +4 -0
- package/lib/views/otel/TracesView.d.ts +12 -0
- package/lib/views/otel/TracesView.js +4 -0
- package/lib/views/otel/index.d.ts +16 -0
- package/lib/views/otel/index.js +12 -0
- package/lib/views/otel/simpleAuthStore.d.ts +21 -0
- package/lib/views/otel/simpleAuthStore.js +22 -0
- package/lib/views/profile/UserBadge.d.ts +20 -0
- package/lib/views/profile/UserBadge.js +101 -0
- package/lib/views/profile/index.d.ts +2 -0
- package/lib/views/profile/index.js +5 -0
- package/package.json +3 -4
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2023-2025 Datalayer, Inc.
|
|
3
|
+
* Distributed under the terms of the Modified BSD License.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* React hooks for fetching OTEL signals from the REST API.
|
|
7
|
+
*
|
|
8
|
+
* @module otel/hooks
|
|
9
|
+
*/
|
|
10
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
11
|
+
import { coreStore } from '../../state/substates/CoreState';
|
|
12
|
+
// ── Global 401 handler ──────────────────────────────────────────────
|
|
13
|
+
let _onUnauthorized = null;
|
|
14
|
+
/**
|
|
15
|
+
* Register a callback invoked whenever an OTEL API call receives a
|
|
16
|
+
* **401 Unauthorized** response. Typically used to clear auth state
|
|
17
|
+
* (i.e. log the user out) when the token has expired.
|
|
18
|
+
*
|
|
19
|
+
* Pass `null` to unregister.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { setOtelOnUnauthorized } from '@datalayer/core/lib/otel';
|
|
24
|
+
* setOtelOnUnauthorized(() => authStore.getState().clearAuth());
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function setOtelOnUnauthorized(cb) {
|
|
28
|
+
_onUnauthorized = cb;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Lightweight fetch helper for OTEL API calls.
|
|
32
|
+
* Uses plain `fetch` to avoid pulling in `@jupyterlab/coreutils` / axios.
|
|
33
|
+
*
|
|
34
|
+
* If the response is **401 Unauthorized** and a global `onUnauthorized`
|
|
35
|
+
* handler has been registered via {@link setOtelOnUnauthorized}, the
|
|
36
|
+
* handler is called before the error is thrown.
|
|
37
|
+
*/
|
|
38
|
+
async function otelFetch(url, token, options) {
|
|
39
|
+
const headers = {
|
|
40
|
+
Accept: 'application/json',
|
|
41
|
+
'Cache-Control': 'no-store, no-cache, must-revalidate',
|
|
42
|
+
};
|
|
43
|
+
if (token) {
|
|
44
|
+
headers['Authorization'] = `Bearer ${token}`;
|
|
45
|
+
}
|
|
46
|
+
const isPost = options?.method === 'POST';
|
|
47
|
+
if (isPost) {
|
|
48
|
+
headers['Content-Type'] = 'application/json';
|
|
49
|
+
}
|
|
50
|
+
const res = await fetch(url, {
|
|
51
|
+
method: options?.method ?? 'GET',
|
|
52
|
+
headers,
|
|
53
|
+
credentials: 'include',
|
|
54
|
+
body: isPost && options?.body !== undefined
|
|
55
|
+
? JSON.stringify(options.body)
|
|
56
|
+
: undefined,
|
|
57
|
+
});
|
|
58
|
+
if (!res.ok) {
|
|
59
|
+
if (res.status === 401 && _onUnauthorized) {
|
|
60
|
+
_onUnauthorized();
|
|
61
|
+
}
|
|
62
|
+
throw new Error(`HTTP ${res.status} ${res.statusText}`);
|
|
63
|
+
}
|
|
64
|
+
return res.json();
|
|
65
|
+
}
|
|
66
|
+
/** Convert nanosecond epoch to ISO string. Returns the input if already a string. */
|
|
67
|
+
function nanoToIso(val) {
|
|
68
|
+
if (typeof val === 'string' && val.length > 0) {
|
|
69
|
+
// Already a date string — use as-is.
|
|
70
|
+
return val;
|
|
71
|
+
}
|
|
72
|
+
const n = Number(val);
|
|
73
|
+
if (!isNaN(n) && n > 1e15) {
|
|
74
|
+
// Nanosecond epoch → milliseconds.
|
|
75
|
+
return new Date(n / 1e6).toISOString();
|
|
76
|
+
}
|
|
77
|
+
if (!isNaN(n) && n > 1e12) {
|
|
78
|
+
// Microsecond epoch.
|
|
79
|
+
return new Date(n / 1e3).toISOString();
|
|
80
|
+
}
|
|
81
|
+
if (!isNaN(n) && n > 0) {
|
|
82
|
+
// Millisecond epoch.
|
|
83
|
+
return new Date(n).toISOString();
|
|
84
|
+
}
|
|
85
|
+
return String(val ?? '');
|
|
86
|
+
}
|
|
87
|
+
/** Normalise a raw API span row into the OtelSpan shape components expect. */
|
|
88
|
+
function normalizeSpan(raw) {
|
|
89
|
+
return {
|
|
90
|
+
trace_id: String(raw.trace_id ?? raw.TraceId ?? ''),
|
|
91
|
+
span_id: String(raw.span_id ?? raw.SpanId ?? raw.trace_id ?? raw.TraceId ?? ''),
|
|
92
|
+
parent_span_id: (raw.parent_span_id ?? raw.ParentSpanId)
|
|
93
|
+
? String(raw.parent_span_id ?? raw.ParentSpanId)
|
|
94
|
+
: undefined,
|
|
95
|
+
span_name: String(raw.span_name ?? raw.SpanName ?? raw.operation_name ?? raw.name ?? ''),
|
|
96
|
+
service_name: String(raw.service_name ?? raw.ServiceName ?? ''),
|
|
97
|
+
kind: String(raw.kind ?? raw.SpanKind ?? raw.span_kind ?? 'INTERNAL'),
|
|
98
|
+
start_time: nanoToIso(raw.start_time ?? raw.Timestamp ?? raw.start_time_unix_nano),
|
|
99
|
+
end_time: nanoToIso(raw.end_time ?? raw.end_time_unix_nano),
|
|
100
|
+
duration_ms: raw.duration_ms != null
|
|
101
|
+
? Number(raw.duration_ms)
|
|
102
|
+
: raw.Duration != null
|
|
103
|
+
? Number(raw.Duration) / 1e6
|
|
104
|
+
: raw.duration_ns != null
|
|
105
|
+
? Number(raw.duration_ns) / 1e6
|
|
106
|
+
: 0,
|
|
107
|
+
status_code: raw.status_code ? String(raw.status_code) : undefined,
|
|
108
|
+
status_message: raw.status_message ? String(raw.status_message) : undefined,
|
|
109
|
+
otel_scope_name: raw.otel_scope_name
|
|
110
|
+
? String(raw.otel_scope_name)
|
|
111
|
+
: undefined,
|
|
112
|
+
attributes: typeof raw.attributes === 'object' && raw.attributes !== null
|
|
113
|
+
? raw.attributes
|
|
114
|
+
: typeof raw.attributes === 'string'
|
|
115
|
+
? (() => {
|
|
116
|
+
try {
|
|
117
|
+
return JSON.parse(raw.attributes);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
})()
|
|
123
|
+
: undefined,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/** Normalise a raw API log row into the OtelLog shape components expect. */
|
|
127
|
+
function normalizeLog(raw) {
|
|
128
|
+
return {
|
|
129
|
+
timestamp: nanoToIso(raw.timestamp ?? raw.Timestamp ?? raw.timestamp_unix_nano),
|
|
130
|
+
severity_text: String(raw.severity_text ?? raw.SeverityText ?? ''),
|
|
131
|
+
severity_number: (raw.severity_number ?? raw.SeverityNumber) != null
|
|
132
|
+
? Number(raw.severity_number ?? raw.SeverityNumber)
|
|
133
|
+
: undefined,
|
|
134
|
+
body: String(raw.body ?? raw.Body ?? ''),
|
|
135
|
+
service_name: String(raw.service_name ?? raw.ServiceName ?? ''),
|
|
136
|
+
trace_id: (raw.trace_id ?? raw.TraceId)
|
|
137
|
+
? String(raw.trace_id ?? raw.TraceId)
|
|
138
|
+
: undefined,
|
|
139
|
+
span_id: (raw.span_id ?? raw.SpanId)
|
|
140
|
+
? String(raw.span_id ?? raw.SpanId)
|
|
141
|
+
: undefined,
|
|
142
|
+
attributes: typeof raw.attributes === 'object' && raw.attributes !== null
|
|
143
|
+
? raw.attributes
|
|
144
|
+
: typeof raw.attributes === 'string'
|
|
145
|
+
? (() => {
|
|
146
|
+
try {
|
|
147
|
+
return JSON.parse(raw.attributes);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
})()
|
|
153
|
+
: undefined,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/** Normalise a raw API metric row into the OtelMetric shape components expect. */
|
|
157
|
+
function normalizeMetric(raw) {
|
|
158
|
+
return {
|
|
159
|
+
metric_name: String(raw.metric_name ?? raw.MetricName ?? raw.name ?? raw.Name ?? ''),
|
|
160
|
+
service_name: String(raw.service_name ?? raw.ServiceName ?? ''),
|
|
161
|
+
value: Number(raw.value ?? raw.Value ?? raw.value_double ?? raw.value_int ?? 0),
|
|
162
|
+
unit: (raw.metric_unit ?? raw.MetricUnit)
|
|
163
|
+
? String(raw.metric_unit ?? raw.MetricUnit)
|
|
164
|
+
: (raw.unit ?? raw.Unit)
|
|
165
|
+
? String(raw.unit ?? raw.Unit)
|
|
166
|
+
: undefined,
|
|
167
|
+
timestamp: nanoToIso(raw.timestamp ??
|
|
168
|
+
raw.Timestamp ??
|
|
169
|
+
raw.timestamp_unix_nano ??
|
|
170
|
+
raw.start_time_unix_nano),
|
|
171
|
+
metric_type: (raw.metric_type ?? raw.MetricType)
|
|
172
|
+
? String(raw.metric_type ?? raw.MetricType)
|
|
173
|
+
: undefined,
|
|
174
|
+
attributes: typeof raw.attributes === 'object' && raw.attributes !== null
|
|
175
|
+
? raw.attributes
|
|
176
|
+
: typeof raw.attributes === 'string'
|
|
177
|
+
? (() => {
|
|
178
|
+
try {
|
|
179
|
+
return JSON.parse(raw.attributes);
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
})()
|
|
185
|
+
: undefined,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
// ── useOtelTraces ───────────────────────────────────────────────────
|
|
189
|
+
/** Fetch a list of traces / spans from the OTEL service. */
|
|
190
|
+
export function useOtelTraces(options) {
|
|
191
|
+
const { token, baseUrl = coreStore.getState().configuration.otelRunUrl, serviceName, limit = 50, autoRefreshMs, } = options;
|
|
192
|
+
const [traces, setTraces] = useState([]);
|
|
193
|
+
const [loading, setLoading] = useState(true);
|
|
194
|
+
const [error, setError] = useState(null);
|
|
195
|
+
const fetchTraces = useCallback(async () => {
|
|
196
|
+
try {
|
|
197
|
+
const params = new URLSearchParams({ limit: String(limit) });
|
|
198
|
+
if (serviceName)
|
|
199
|
+
params.set('service_name', serviceName);
|
|
200
|
+
const data = await otelFetch(`${baseUrl}/api/otel/v1/traces/?${params}`, token);
|
|
201
|
+
const rows = data.data ?? data ?? [];
|
|
202
|
+
setTraces(Array.isArray(rows) ? rows.map(normalizeSpan) : []);
|
|
203
|
+
setError(null);
|
|
204
|
+
}
|
|
205
|
+
catch (err) {
|
|
206
|
+
setError(err.message);
|
|
207
|
+
}
|
|
208
|
+
finally {
|
|
209
|
+
setLoading(false);
|
|
210
|
+
}
|
|
211
|
+
}, [token, baseUrl, serviceName, limit]);
|
|
212
|
+
useEffect(() => {
|
|
213
|
+
fetchTraces();
|
|
214
|
+
if (autoRefreshMs && autoRefreshMs > 0) {
|
|
215
|
+
const id = setInterval(fetchTraces, autoRefreshMs);
|
|
216
|
+
return () => clearInterval(id);
|
|
217
|
+
}
|
|
218
|
+
}, [fetchTraces, autoRefreshMs]);
|
|
219
|
+
return { traces, loading, error, refetch: fetchTraces };
|
|
220
|
+
}
|
|
221
|
+
// ── useOtelTrace ────────────────────────────────────────────────────
|
|
222
|
+
/** Fetch all spans for a single trace. */
|
|
223
|
+
export function useOtelTrace(options) {
|
|
224
|
+
const { traceId, token, baseUrl = '' } = options;
|
|
225
|
+
const [spans, setSpans] = useState([]);
|
|
226
|
+
const [loading, setLoading] = useState(false);
|
|
227
|
+
const [error, setError] = useState(null);
|
|
228
|
+
useEffect(() => {
|
|
229
|
+
if (!traceId) {
|
|
230
|
+
setSpans([]);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
setLoading(true);
|
|
234
|
+
otelFetch(`${baseUrl}/api/otel/v1/traces/${traceId}`, token)
|
|
235
|
+
.then(data => {
|
|
236
|
+
const rows = data.data ?? data ?? [];
|
|
237
|
+
setSpans(Array.isArray(rows) ? rows.map(normalizeSpan) : []);
|
|
238
|
+
setError(null);
|
|
239
|
+
})
|
|
240
|
+
.catch((err) => setError(err.message))
|
|
241
|
+
.finally(() => setLoading(false));
|
|
242
|
+
}, [traceId, token, baseUrl]);
|
|
243
|
+
return { spans, loading, error };
|
|
244
|
+
}
|
|
245
|
+
// ── useOtelLogs ─────────────────────────────────────────────────────
|
|
246
|
+
/** Fetch log records from the OTEL service. */
|
|
247
|
+
export function useOtelLogs(options) {
|
|
248
|
+
const { token, baseUrl = coreStore.getState().configuration.otelRunUrl, serviceName, severity, traceId, limit = 100, autoRefreshMs, } = options;
|
|
249
|
+
const [logs, setLogs] = useState([]);
|
|
250
|
+
const [loading, setLoading] = useState(true);
|
|
251
|
+
const [error, setError] = useState(null);
|
|
252
|
+
const fetchLogs = useCallback(async () => {
|
|
253
|
+
try {
|
|
254
|
+
const params = new URLSearchParams({ limit: String(limit) });
|
|
255
|
+
if (serviceName)
|
|
256
|
+
params.set('service_name', serviceName);
|
|
257
|
+
if (severity)
|
|
258
|
+
params.set('severity', severity);
|
|
259
|
+
if (traceId)
|
|
260
|
+
params.set('trace_id', traceId);
|
|
261
|
+
const data = await otelFetch(`${baseUrl}/api/otel/v1/logs/?${params}`, token);
|
|
262
|
+
const rows = data.data ?? data ?? [];
|
|
263
|
+
setLogs(Array.isArray(rows) ? rows.map(normalizeLog) : []);
|
|
264
|
+
setError(null);
|
|
265
|
+
}
|
|
266
|
+
catch (err) {
|
|
267
|
+
setError(err.message);
|
|
268
|
+
}
|
|
269
|
+
finally {
|
|
270
|
+
setLoading(false);
|
|
271
|
+
}
|
|
272
|
+
}, [token, baseUrl, serviceName, severity, traceId, limit]);
|
|
273
|
+
useEffect(() => {
|
|
274
|
+
fetchLogs();
|
|
275
|
+
if (autoRefreshMs && autoRefreshMs > 0) {
|
|
276
|
+
const id = setInterval(fetchLogs, autoRefreshMs);
|
|
277
|
+
return () => clearInterval(id);
|
|
278
|
+
}
|
|
279
|
+
}, [fetchLogs, autoRefreshMs]);
|
|
280
|
+
return { logs, loading, error, refetch: fetchLogs };
|
|
281
|
+
}
|
|
282
|
+
// ── useOtelMetrics ──────────────────────────────────────────────────
|
|
283
|
+
/** Fetch metrics from the OTEL service. */
|
|
284
|
+
export function useOtelMetrics(options) {
|
|
285
|
+
const { token, baseUrl = coreStore.getState().configuration.otelRunUrl, serviceName, metricName, limit = 50, autoRefreshMs, } = options;
|
|
286
|
+
const [metrics, setMetrics] = useState([]);
|
|
287
|
+
const [loading, setLoading] = useState(true);
|
|
288
|
+
const [error, setError] = useState(null);
|
|
289
|
+
const fetchMetrics = useCallback(async () => {
|
|
290
|
+
try {
|
|
291
|
+
const params = new URLSearchParams({ limit: String(limit) });
|
|
292
|
+
if (serviceName)
|
|
293
|
+
params.set('service_name', serviceName);
|
|
294
|
+
if (metricName)
|
|
295
|
+
params.set('metric_name', metricName);
|
|
296
|
+
const data = await otelFetch(`${baseUrl}/api/otel/v1/metrics/?${params}`, token);
|
|
297
|
+
const rows = data.data ?? data ?? [];
|
|
298
|
+
setMetrics(Array.isArray(rows) ? rows.map(normalizeMetric) : []);
|
|
299
|
+
setError(null);
|
|
300
|
+
}
|
|
301
|
+
catch (err) {
|
|
302
|
+
setError(err.message);
|
|
303
|
+
}
|
|
304
|
+
finally {
|
|
305
|
+
setLoading(false);
|
|
306
|
+
}
|
|
307
|
+
}, [token, baseUrl, serviceName, metricName, limit]);
|
|
308
|
+
useEffect(() => {
|
|
309
|
+
fetchMetrics();
|
|
310
|
+
if (autoRefreshMs && autoRefreshMs > 0) {
|
|
311
|
+
const id = setInterval(fetchMetrics, autoRefreshMs);
|
|
312
|
+
return () => clearInterval(id);
|
|
313
|
+
}
|
|
314
|
+
}, [fetchMetrics, autoRefreshMs]);
|
|
315
|
+
return { metrics, loading, error, refetch: fetchMetrics };
|
|
316
|
+
}
|
|
317
|
+
// ── useOtelServices ─────────────────────────────────────────────────
|
|
318
|
+
/** Fetch list of observed service names. */
|
|
319
|
+
export function useOtelServices(options) {
|
|
320
|
+
const { token, baseUrl = coreStore.getState().configuration.otelRunUrl } = options;
|
|
321
|
+
const [services, setServices] = useState([]);
|
|
322
|
+
const [loading, setLoading] = useState(true);
|
|
323
|
+
useEffect(() => {
|
|
324
|
+
otelFetch(`${baseUrl}/api/otel/v1/traces/services/list/`, token)
|
|
325
|
+
.then(data => {
|
|
326
|
+
// Accept multiple backend response shapes and normalize to unique names.
|
|
327
|
+
// Examples:
|
|
328
|
+
// { services: ["a","b"] }
|
|
329
|
+
// { data: [{ service_name: "a" }] }
|
|
330
|
+
// { data: [{ service: "a" }] }
|
|
331
|
+
// ["a", "b"]
|
|
332
|
+
// [{ serviceName: "a" }]
|
|
333
|
+
const raw = data.services ?? data.data ?? data;
|
|
334
|
+
const rows = Array.isArray(raw) ? raw : [];
|
|
335
|
+
const names = rows
|
|
336
|
+
.map(row => {
|
|
337
|
+
if (typeof row === 'string') {
|
|
338
|
+
return row.trim();
|
|
339
|
+
}
|
|
340
|
+
if (row && typeof row === 'object') {
|
|
341
|
+
const record = row;
|
|
342
|
+
const value = record.service_name ??
|
|
343
|
+
record.serviceName ??
|
|
344
|
+
record.service ??
|
|
345
|
+
record.name;
|
|
346
|
+
return value == null ? '' : String(value).trim();
|
|
347
|
+
}
|
|
348
|
+
return '';
|
|
349
|
+
})
|
|
350
|
+
.filter(Boolean);
|
|
351
|
+
setServices(Array.from(new Set(names)).sort((a, b) => a.localeCompare(b)));
|
|
352
|
+
})
|
|
353
|
+
.catch(() => { })
|
|
354
|
+
.finally(() => setLoading(false));
|
|
355
|
+
}, [token, baseUrl]);
|
|
356
|
+
return { services, loading };
|
|
357
|
+
}
|
|
358
|
+
// ── useOtelStats ────────────────────────────────────────────────────
|
|
359
|
+
/** Fetch storage stats from the OTEL service. */
|
|
360
|
+
export function useOtelStats(options) {
|
|
361
|
+
const { token, baseUrl = '' } = options;
|
|
362
|
+
const [stats, setStats] = useState({});
|
|
363
|
+
const [loading, setLoading] = useState(true);
|
|
364
|
+
useEffect(() => {
|
|
365
|
+
otelFetch(`${baseUrl}/api/otel/v1/stats/`, token)
|
|
366
|
+
.then(data => setStats(data))
|
|
367
|
+
.catch(() => { })
|
|
368
|
+
.finally(() => setLoading(false));
|
|
369
|
+
}, [token, baseUrl]);
|
|
370
|
+
return { stats, loading };
|
|
371
|
+
}
|
|
372
|
+
/** Fetch system statistics from /api/otel/v1/system/ (platform_admin). */
|
|
373
|
+
export function useOtelSystem(options) {
|
|
374
|
+
const { token, baseUrl = '' } = options;
|
|
375
|
+
const [data, setData] = useState(null);
|
|
376
|
+
const [loading, setLoading] = useState(true);
|
|
377
|
+
const [error, setError] = useState(null);
|
|
378
|
+
const refresh = useCallback(() => {
|
|
379
|
+
setLoading(true);
|
|
380
|
+
setError(null);
|
|
381
|
+
otelFetch(`${baseUrl}/api/otel/v1/system/`, token)
|
|
382
|
+
.then((resp) => setData(resp.data ?? resp))
|
|
383
|
+
.catch((err) => setError(err?.message ?? 'Failed to load system info'))
|
|
384
|
+
.finally(() => setLoading(false));
|
|
385
|
+
}, [token, baseUrl]);
|
|
386
|
+
useEffect(() => {
|
|
387
|
+
refresh();
|
|
388
|
+
}, [refresh]);
|
|
389
|
+
return { data, loading, error, refresh };
|
|
390
|
+
}
|
|
391
|
+
// ── useOtelQuery ────────────────────────────────────────────────────
|
|
392
|
+
/**
|
|
393
|
+
* Execute an ad-hoc DataFusion SQL query against the OTEL service.
|
|
394
|
+
*
|
|
395
|
+
* Usage:
|
|
396
|
+
* ```tsx
|
|
397
|
+
* const { execute, rows, loading, error } = useOtelQuery({ baseUrl, token });
|
|
398
|
+
* await execute('SELECT trace_id FROM spans LIMIT 10');
|
|
399
|
+
* ```
|
|
400
|
+
*/
|
|
401
|
+
export function useOtelQuery(options) {
|
|
402
|
+
const { token, baseUrl = '' } = options;
|
|
403
|
+
const [rows, setRows] = useState([]);
|
|
404
|
+
const [loading, setLoading] = useState(false);
|
|
405
|
+
const [error, setError] = useState(null);
|
|
406
|
+
const execute = useCallback(async (sql) => {
|
|
407
|
+
setLoading(true);
|
|
408
|
+
setError(null);
|
|
409
|
+
try {
|
|
410
|
+
const data = await otelFetch(`${baseUrl}/api/otel/v1/query/`, token, {
|
|
411
|
+
method: 'POST',
|
|
412
|
+
body: { sql },
|
|
413
|
+
});
|
|
414
|
+
setRows(data.data ?? []);
|
|
415
|
+
}
|
|
416
|
+
catch (err) {
|
|
417
|
+
setError(err.message ?? 'Query failed');
|
|
418
|
+
setRows([]);
|
|
419
|
+
}
|
|
420
|
+
finally {
|
|
421
|
+
setLoading(false);
|
|
422
|
+
}
|
|
423
|
+
}, [token, baseUrl]);
|
|
424
|
+
const clear = useCallback(() => {
|
|
425
|
+
setRows([]);
|
|
426
|
+
setError(null);
|
|
427
|
+
}, []);
|
|
428
|
+
return { rows, loading, error, execute, clear };
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Connect to the OTEL service WebSocket for live telemetry streaming.
|
|
432
|
+
*
|
|
433
|
+
* The server pushes JSON messages whenever new data is flushed to storage.
|
|
434
|
+
* Authentication is via the `token` query parameter (JWT or API key).
|
|
435
|
+
*
|
|
436
|
+
* @returns `{ connected, error, close }` – reactive connection state.
|
|
437
|
+
*/
|
|
438
|
+
export function useOtelWebSocket(options) {
|
|
439
|
+
const { baseUrl = coreStore.getState().configuration.otelRunUrl, token, autoReconnect = true, reconnectDelayMs = 3000, callbacks, } = options;
|
|
440
|
+
const [connected, setConnected] = useState(false);
|
|
441
|
+
const [error, setError] = useState(null);
|
|
442
|
+
const wsRef = useRef(null);
|
|
443
|
+
const reconnectTimerRef = useRef(null);
|
|
444
|
+
// Store callbacks in a ref so we don't reconnect when they change.
|
|
445
|
+
const cbRef = useRef(callbacks);
|
|
446
|
+
cbRef.current = callbacks;
|
|
447
|
+
const close = useCallback(() => {
|
|
448
|
+
if (reconnectTimerRef.current) {
|
|
449
|
+
clearTimeout(reconnectTimerRef.current);
|
|
450
|
+
reconnectTimerRef.current = null;
|
|
451
|
+
}
|
|
452
|
+
if (wsRef.current) {
|
|
453
|
+
wsRef.current.close();
|
|
454
|
+
wsRef.current = null;
|
|
455
|
+
}
|
|
456
|
+
}, []);
|
|
457
|
+
useEffect(() => {
|
|
458
|
+
if (!token) {
|
|
459
|
+
setConnected(false);
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
// Derive ws(s):// URL from http(s):// baseUrl.
|
|
463
|
+
let wsUrl;
|
|
464
|
+
if (baseUrl.startsWith('http://')) {
|
|
465
|
+
wsUrl = `ws://${baseUrl.slice(7)}`;
|
|
466
|
+
}
|
|
467
|
+
else if (baseUrl.startsWith('https://')) {
|
|
468
|
+
wsUrl = `wss://${baseUrl.slice(8)}`;
|
|
469
|
+
}
|
|
470
|
+
else if (baseUrl.startsWith('ws://') || baseUrl.startsWith('wss://')) {
|
|
471
|
+
wsUrl = baseUrl;
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
// Relative URL – use current page's protocol.
|
|
475
|
+
const proto = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
476
|
+
wsUrl = `${proto}//${window.location.host}${baseUrl}`;
|
|
477
|
+
}
|
|
478
|
+
wsUrl = `${wsUrl.replace(/\/$/, '')}/api/otel/v1/ws?token=${encodeURIComponent(token)}`;
|
|
479
|
+
function connect() {
|
|
480
|
+
const ws = new WebSocket(wsUrl);
|
|
481
|
+
wsRef.current = ws;
|
|
482
|
+
ws.onopen = () => {
|
|
483
|
+
setConnected(true);
|
|
484
|
+
setError(null);
|
|
485
|
+
cbRef.current?.onOpen?.();
|
|
486
|
+
};
|
|
487
|
+
ws.onmessage = event => {
|
|
488
|
+
try {
|
|
489
|
+
const msg = JSON.parse(event.data);
|
|
490
|
+
cbRef.current?.onMessage?.(msg);
|
|
491
|
+
if (msg.signal === 'traces' && cbRef.current?.onTraces) {
|
|
492
|
+
cbRef.current.onTraces(Array.isArray(msg.data) ? msg.data.map(normalizeSpan) : []);
|
|
493
|
+
}
|
|
494
|
+
if (msg.signal === 'logs' && cbRef.current?.onLogs) {
|
|
495
|
+
cbRef.current.onLogs(Array.isArray(msg.data) ? msg.data.map(normalizeLog) : []);
|
|
496
|
+
}
|
|
497
|
+
if (msg.signal === 'metrics' && cbRef.current?.onMetrics) {
|
|
498
|
+
cbRef.current.onMetrics(Array.isArray(msg.data) ? msg.data.map(normalizeMetric) : []);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
catch {
|
|
502
|
+
// Ignore malformed messages.
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
ws.onclose = event => {
|
|
506
|
+
setConnected(false);
|
|
507
|
+
cbRef.current?.onClose?.(event);
|
|
508
|
+
if (autoReconnect && event.code !== 1008) {
|
|
509
|
+
// 1008 = policy violation (auth failure) – don't retry.
|
|
510
|
+
reconnectTimerRef.current = setTimeout(connect, reconnectDelayMs);
|
|
511
|
+
}
|
|
512
|
+
};
|
|
513
|
+
ws.onerror = event => {
|
|
514
|
+
setError('WebSocket error');
|
|
515
|
+
cbRef.current?.onError?.(event);
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
connect();
|
|
519
|
+
return () => {
|
|
520
|
+
// Disable auto-reconnect during teardown.
|
|
521
|
+
if (reconnectTimerRef.current) {
|
|
522
|
+
clearTimeout(reconnectTimerRef.current);
|
|
523
|
+
reconnectTimerRef.current = null;
|
|
524
|
+
}
|
|
525
|
+
if (wsRef.current) {
|
|
526
|
+
wsRef.current.close();
|
|
527
|
+
wsRef.current = null;
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
}, [baseUrl, token, autoReconnect, reconnectDelayMs]);
|
|
531
|
+
return { connected, error, close };
|
|
532
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OTEL React components, hooks, and client for visualizing OpenTelemetry
|
|
3
|
+
* signals (traces, logs, metrics) in a Logfire-inspired Live view.
|
|
4
|
+
*
|
|
5
|
+
* Structure:
|
|
6
|
+
* - `types` – shared TypeScript types
|
|
7
|
+
* - `utils` – pure helpers (formatDuration, buildSpanTree, …)
|
|
8
|
+
* - `hooks/` – React hooks that read from Datalayer config by default
|
|
9
|
+
* - `client/`– Typed HTTP client (non-React, reads Datalayer config by default)
|
|
10
|
+
* - `views/` – React view components
|
|
11
|
+
*
|
|
12
|
+
* @module otel
|
|
13
|
+
*/
|
|
14
|
+
export type { OtelSpan, OtelLog, OtelMetric, OtelSpanEvent, OtelSpanLink, SignalType, OtelLiveProps, OtelTracesListProps, OtelSpanDetailProps, OtelTimelineProps, OtelLogsListProps, OtelMetricsListProps, OtelSpanTreeProps, OtelSearchBarProps, OtelTimelineRangeSliderProps, OtelQueryRow, OtelQueryResult, } from './types';
|
|
15
|
+
export { toMs, formatDuration, formatTime, serviceColor, kindColor, severityColor, severityVariant, buildSpanTree, flattenSpanTree, } from './utils';
|
|
16
|
+
export { useOtelTraces, useOtelTrace, useOtelLogs, useOtelMetrics, useOtelServices, useOtelStats, useOtelQuery, useOtelSystem, useOtelWebSocket, setOtelOnUnauthorized, } from './hooks';
|
|
17
|
+
export type { OtelWsMessage, OtelWsCallbacks, OtelSystemData, OtelSystemProcess, OtelSystemDisk, OtelSystemTable, } from './hooks';
|
|
18
|
+
export { OtelClient, createOtelClient } from './client';
|
|
19
|
+
export type { OtelClientOptions, FetchTracesOptions, FetchLogsOptions, FetchMetricsOptions, FetchMetricTotalOptions, } from './client';
|
|
20
|
+
export { OtelLive } from './views';
|
|
21
|
+
export { OtelTracesList } from './views';
|
|
22
|
+
export { OtelSpanDetail } from './views';
|
|
23
|
+
export { OtelSpanTree } from './views';
|
|
24
|
+
export { OtelLogsList } from './views';
|
|
25
|
+
export { OtelSearchBar } from './views';
|
|
26
|
+
export { OtelMetricsList } from './views';
|
|
27
|
+
export { OtelMetricsChart } from './views';
|
|
28
|
+
export type { OtelMetricsChartProps } from './views';
|
|
29
|
+
export { OtelTimeline } from './views';
|
|
30
|
+
export { OtelTimelineRangeSlider } from './views';
|
|
31
|
+
export { OtelSqlView } from './views';
|
|
32
|
+
export type { OtelSqlViewProps } from './views';
|
|
33
|
+
export { OtelSystemView } from './views';
|
|
34
|
+
export type { OtelSystemViewProps } from './views';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2023-2025 Datalayer, Inc.
|
|
3
|
+
* Distributed under the terms of the Modified BSD License.
|
|
4
|
+
*/
|
|
5
|
+
// ── Utils ─────────────────────────────────────────────────────────────────────
|
|
6
|
+
export { toMs, formatDuration, formatTime, serviceColor, kindColor, severityColor, severityVariant, buildSpanTree, flattenSpanTree, } from './utils';
|
|
7
|
+
// ── Hooks ─────────────────────────────────────────────────────────────────────
|
|
8
|
+
export { useOtelTraces, useOtelTrace, useOtelLogs, useOtelMetrics, useOtelServices, useOtelStats, useOtelQuery, useOtelSystem, useOtelWebSocket, setOtelOnUnauthorized, } from './hooks';
|
|
9
|
+
// ── Client ────────────────────────────────────────────────────────────────────
|
|
10
|
+
export { OtelClient, createOtelClient } from './client';
|
|
11
|
+
// ── Views ─────────────────────────────────────────────────────────────────────
|
|
12
|
+
export { OtelLive } from './views';
|
|
13
|
+
export { OtelTracesList } from './views';
|
|
14
|
+
export { OtelSpanDetail } from './views';
|
|
15
|
+
export { OtelSpanTree } from './views';
|
|
16
|
+
export { OtelLogsList } from './views';
|
|
17
|
+
export { OtelSearchBar } from './views';
|
|
18
|
+
export { OtelMetricsList } from './views';
|
|
19
|
+
export { OtelMetricsChart } from './views';
|
|
20
|
+
export { OtelTimeline } from './views';
|
|
21
|
+
export { OtelTimelineRangeSlider } from './views';
|
|
22
|
+
export { OtelSqlView } from './views';
|
|
23
|
+
export { OtelSystemView } from './views';
|