@navikt/apm 0.0.0-stage → 0.7.3
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 +21 -0
- package/README.md +527 -2
- package/dist/api.d.ts +108 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +220 -0
- package/dist/api.js.map +1 -0
- package/dist/config.d.ts +150 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +268 -0
- package/dist/config.js.map +1 -0
- package/dist/console.d.ts +28 -0
- package/dist/console.d.ts.map +1 -0
- package/dist/console.js +71 -0
- package/dist/console.js.map +1 -0
- package/dist/errors.d.ts +40 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +177 -0
- package/dist/errors.js.map +1 -0
- package/dist/feedback.d.ts +42 -0
- package/dist/feedback.d.ts.map +1 -0
- package/dist/feedback.js +72 -0
- package/dist/feedback.js.map +1 -0
- package/dist/index.d.ts +144 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +249 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +47 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +129 -0
- package/dist/internal.js.map +1 -0
- package/dist/metaTags.d.ts +34 -0
- package/dist/metaTags.d.ts.map +1 -0
- package/dist/metaTags.js +76 -0
- package/dist/metaTags.js.map +1 -0
- package/dist/react/ErrorBoundary.d.ts +59 -0
- package/dist/react/ErrorBoundary.d.ts.map +1 -0
- package/dist/react/ErrorBoundary.js +92 -0
- package/dist/react/ErrorBoundary.js.map +1 -0
- package/dist/react/NaisMetaTags.d.ts +25 -0
- package/dist/react/NaisMetaTags.d.ts.map +1 -0
- package/dist/react/NaisMetaTags.js +6 -0
- package/dist/react/NaisMetaTags.js.map +1 -0
- package/dist/react/index.d.ts +17 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +14 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/nextClient.d.ts +25 -0
- package/dist/react/nextClient.d.ts.map +1 -0
- package/dist/react/nextClient.js +29 -0
- package/dist/react/nextClient.js.map +1 -0
- package/dist/react/routing.d.ts +56 -0
- package/dist/react/routing.d.ts.map +1 -0
- package/dist/react/routing.js +89 -0
- package/dist/react/routing.js.map +1 -0
- package/dist/replay/constants.d.ts +26 -0
- package/dist/replay/constants.d.ts.map +1 -0
- package/dist/replay/constants.js +24 -0
- package/dist/replay/constants.js.map +1 -0
- package/dist/replay/events.d.ts +74 -0
- package/dist/replay/events.d.ts.map +1 -0
- package/dist/replay/events.js +258 -0
- package/dist/replay/events.js.map +1 -0
- package/dist/replay/hash.d.ts +8 -0
- package/dist/replay/hash.d.ts.map +1 -0
- package/dist/replay/hash.js +14 -0
- package/dist/replay/hash.js.map +1 -0
- package/dist/replay/masking.d.ts +89 -0
- package/dist/replay/masking.d.ts.map +1 -0
- package/dist/replay/masking.js +199 -0
- package/dist/replay/masking.js.map +1 -0
- package/dist/replay/options.d.ts +52 -0
- package/dist/replay/options.d.ts.map +1 -0
- package/dist/replay/options.js +66 -0
- package/dist/replay/options.js.map +1 -0
- package/dist/replay/recording.d.ts +60 -0
- package/dist/replay/recording.d.ts.map +1 -0
- package/dist/replay/recording.js +120 -0
- package/dist/replay/recording.js.map +1 -0
- package/dist/replay/snapshot.d.ts +41 -0
- package/dist/replay/snapshot.d.ts.map +1 -0
- package/dist/replay/snapshot.js +97 -0
- package/dist/replay/snapshot.js.map +1 -0
- package/dist/replay/transport.d.ts +82 -0
- package/dist/replay/transport.d.ts.map +1 -0
- package/dist/replay/transport.js +188 -0
- package/dist/replay/transport.js.map +1 -0
- package/dist/scrub.d.ts +69 -0
- package/dist/scrub.d.ts.map +1 -0
- package/dist/scrub.js +317 -0
- package/dist/scrub.js.map +1 -0
- package/dist/serialize.d.ts +15 -0
- package/dist/serialize.d.ts.map +1 -0
- package/dist/serialize.js +68 -0
- package/dist/serialize.js.map +1 -0
- package/dist/tracing.d.ts +37 -0
- package/dist/tracing.d.ts.map +1 -0
- package/dist/tracing.js +70 -0
- package/dist/tracing.js.map +1 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +3 -0
- package/dist/version.js.map +1 -0
- package/package.json +94 -4
package/dist/api.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sentry-compatible convenience API on top of Faro.
|
|
3
|
+
*
|
|
4
|
+
* Mapping:
|
|
5
|
+
* - `captureException(err, { context, fingerprint })` → `faro.api.pushError`
|
|
6
|
+
* (`fingerprint` becomes `context.fingerprint`, consumed by the owned
|
|
7
|
+
* fingerprinting pipeline, nais/grafana-apm-app#62)
|
|
8
|
+
* - `captureMessage(msg, level)` → `faro.api.pushLog`
|
|
9
|
+
* - `setUser` / `clearUser` → `faro.api.setUser` / `faro.api.resetUser`
|
|
10
|
+
* - `setTag` / `setContext` → module-level context merged into every
|
|
11
|
+
* pushError context. Faro has no first-class tag concept, so this is an
|
|
12
|
+
* approximation: values ride along as exception context, they are not
|
|
13
|
+
* indexed labels.
|
|
14
|
+
*/
|
|
15
|
+
import { LogLevel } from '@grafana/faro-web-sdk';
|
|
16
|
+
import { getGlobalContext, runOrBuffer } from './internal.js';
|
|
17
|
+
import { looksLikePii } from './scrub.js';
|
|
18
|
+
const SEVERITY_TO_LOG_LEVEL = {
|
|
19
|
+
fatal: LogLevel.ERROR, // Faro has no `fatal`; documented approximation
|
|
20
|
+
error: LogLevel.ERROR,
|
|
21
|
+
warning: LogLevel.WARN,
|
|
22
|
+
log: LogLevel.LOG,
|
|
23
|
+
info: LogLevel.INFO,
|
|
24
|
+
debug: LogLevel.DEBUG,
|
|
25
|
+
};
|
|
26
|
+
function stringifyValue(value) {
|
|
27
|
+
if (typeof value === 'string') {
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
return JSON.stringify(value) ?? String(value);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return String(value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function stringifyContext(context) {
|
|
38
|
+
const out = {};
|
|
39
|
+
for (const [key, value] of Object.entries(context ?? {})) {
|
|
40
|
+
if (value !== undefined) {
|
|
41
|
+
out[key] = stringifyValue(value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
function toError(value) {
|
|
47
|
+
if (value instanceof Error) {
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
if (typeof value === 'string') {
|
|
51
|
+
return new Error(value);
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
return new Error(JSON.stringify(value));
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return new Error(String(value));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Capture an exception. Sentry-compatible replacement for
|
|
62
|
+
* `Sentry.captureException`. Note: Faro's `pushError` returns no event ID.
|
|
63
|
+
*
|
|
64
|
+
* When `error` is an `Error` instance, it is also forwarded as Faro's
|
|
65
|
+
* `originalError` pushError option. Combined with `init({ faro: {
|
|
66
|
+
* preserveOriginalError: true } })`, the real Error instance rides along on
|
|
67
|
+
* `item.payload.originalError` and is visible to a custom `beforeSend` hook
|
|
68
|
+
* before Faro strips it — a single place to branch on error type/shape before
|
|
69
|
+
* reporting.
|
|
70
|
+
*/
|
|
71
|
+
export function captureException(error, options = {}) {
|
|
72
|
+
// Context and error are snapshotted at call time so buffered calls (an async
|
|
73
|
+
// init in flight) capture the state the caller saw, not the flush-time state.
|
|
74
|
+
const context = {
|
|
75
|
+
...getGlobalContext(),
|
|
76
|
+
...stringifyContext(options.context),
|
|
77
|
+
};
|
|
78
|
+
if (options.fingerprint !== undefined) {
|
|
79
|
+
context['fingerprint'] = options.fingerprint;
|
|
80
|
+
}
|
|
81
|
+
const error_ = toError(error);
|
|
82
|
+
// Only a real Error instance can carry `originalError` (Faro types it as
|
|
83
|
+
// `Error`); coerced strings/objects have nothing more original than `error_`
|
|
84
|
+
// itself. With `preserveOriginalError: true` in the Faro escape hatch, this
|
|
85
|
+
// survives into the `beforeSend` payload (`item.payload.originalError`) so
|
|
86
|
+
// callers can inspect the actual thrown instance before it is stripped.
|
|
87
|
+
const originalError = error instanceof Error ? error : undefined;
|
|
88
|
+
const hasContext = Object.keys(context).length > 0;
|
|
89
|
+
const options_ = hasContext || originalError ? { ...(hasContext && { context }), ...(originalError && { originalError }) } : undefined;
|
|
90
|
+
runOrBuffer((faro) => {
|
|
91
|
+
faro.api.pushError(error_, options_);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/** Capture a message as a log line. Replacement for `Sentry.captureMessage`. */
|
|
95
|
+
export function captureMessage(message, level = 'info') {
|
|
96
|
+
const globalContext = { ...getGlobalContext() };
|
|
97
|
+
runOrBuffer((faro) => {
|
|
98
|
+
faro.api.pushLog([message], {
|
|
99
|
+
level: SEVERITY_TO_LOG_LEVEL[level] ?? LogLevel.INFO,
|
|
100
|
+
context: Object.keys(globalContext).length > 0 ? globalContext : undefined,
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
let warnedUserPii = false;
|
|
105
|
+
/** @internal test helper — resets the once-only setUser PII warning. */
|
|
106
|
+
export function _resetUserPiiWarning() {
|
|
107
|
+
warnedUserPii = false;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Set the active user. Replacement for `Sentry.setUser(user)`.
|
|
111
|
+
*
|
|
112
|
+
* Structured Faro user fields BYPASS the transport-level PII scrubber, so this
|
|
113
|
+
* guards defensively: any `id`/`username`/`attributes` value that looks like
|
|
114
|
+
* PII (fødselsnummer, email, or a raw NAV ident) is dropped, and `email` is
|
|
115
|
+
* dropped unconditionally. Pass an opaque, non-identifying id — never a raw
|
|
116
|
+
* ident, fnr, email, or name.
|
|
117
|
+
*
|
|
118
|
+
* @example setUser({ id: hashedSubject }) // hashedSubject = a salted hash, not an ident
|
|
119
|
+
*/
|
|
120
|
+
export function setUser(user) {
|
|
121
|
+
if (user === null) {
|
|
122
|
+
runOrBuffer((faro) => faro.api.resetUser());
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const safe = {};
|
|
126
|
+
let dropped = false;
|
|
127
|
+
for (const key of ['id', 'username']) {
|
|
128
|
+
const value = user[key];
|
|
129
|
+
if (value === undefined) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (looksLikePii(value)) {
|
|
133
|
+
dropped = true;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
safe[key] = value;
|
|
137
|
+
}
|
|
138
|
+
// email is PII by definition — never forward it (the field is deprecated).
|
|
139
|
+
if (user.email !== undefined) {
|
|
140
|
+
dropped = true;
|
|
141
|
+
}
|
|
142
|
+
if (user.attributes) {
|
|
143
|
+
const attributes = {};
|
|
144
|
+
for (const [key, value] of Object.entries(user.attributes)) {
|
|
145
|
+
if (looksLikePii(value)) {
|
|
146
|
+
dropped = true;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
attributes[key] = value;
|
|
150
|
+
}
|
|
151
|
+
if (Object.keys(attributes).length > 0) {
|
|
152
|
+
safe.attributes = attributes;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (dropped && !warnedUserPii) {
|
|
156
|
+
warnedUserPii = true;
|
|
157
|
+
// eslint-disable-next-line no-console
|
|
158
|
+
console.warn('[@nais/apm] setUser value looks like PII (fnr/email) and was dropped — pass an opaque id instead');
|
|
159
|
+
}
|
|
160
|
+
runOrBuffer((faro) => faro.api.setUser(safe));
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Record a custom numeric measurement (a metric). Safe, documented entry point
|
|
164
|
+
* over `faro.api.pushMeasurement` so teams don't drop to raw Faro. It rides the
|
|
165
|
+
* same guarded transport, so the mandatory PII scrubber runs on the way out —
|
|
166
|
+
* including NAV-ident redaction on `context`. No-op before `init()`.
|
|
167
|
+
*
|
|
168
|
+
* @example pushMeasurement('checkout_latency', { ms: 812 })
|
|
169
|
+
*/
|
|
170
|
+
export function pushMeasurement(type, values, options = {}) {
|
|
171
|
+
runOrBuffer((faro) => {
|
|
172
|
+
faro.api.pushMeasurement({
|
|
173
|
+
type,
|
|
174
|
+
values,
|
|
175
|
+
...(options.context ? { context: options.context } : {}),
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Record a custom event with string attributes. Safe, documented entry point
|
|
181
|
+
* over `faro.api.pushEvent` so teams don't drop to raw Faro. It rides the same
|
|
182
|
+
* guarded transport, so the mandatory PII scrubber runs on the way out —
|
|
183
|
+
* including NAV-ident redaction on `attributes`. No-op before `init()`.
|
|
184
|
+
*
|
|
185
|
+
* @example pushEvent('feature_flag_evaluated', { flag: 'new-checkout', value: 'on' })
|
|
186
|
+
*/
|
|
187
|
+
export function pushEvent(name, attributes, domain) {
|
|
188
|
+
runOrBuffer((faro) => faro.api.pushEvent(name, attributes, domain));
|
|
189
|
+
}
|
|
190
|
+
/** Clear the active user. Replacement for `Sentry.setUser(null)`. */
|
|
191
|
+
export function clearUser() {
|
|
192
|
+
runOrBuffer((faro) => faro.api.resetUser());
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Set a tag merged into every subsequent `captureException`/`captureMessage`
|
|
196
|
+
* context. Approximation of `Sentry.setTag` — Faro has no tag concept.
|
|
197
|
+
*/
|
|
198
|
+
export function setTag(key, value) {
|
|
199
|
+
getGlobalContext()[key] = String(value);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Attach named context merged (flattened as `name.key`) into every subsequent
|
|
203
|
+
* capture. Approximation of `Sentry.setContext`. Pass `null` to remove.
|
|
204
|
+
*/
|
|
205
|
+
export function setContext(name, context) {
|
|
206
|
+
const globalContext = getGlobalContext();
|
|
207
|
+
if (context === null) {
|
|
208
|
+
const prefix = `${name}.`;
|
|
209
|
+
for (const key of Object.keys(globalContext)) {
|
|
210
|
+
if (key.startsWith(prefix)) {
|
|
211
|
+
delete globalContext[key];
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
for (const [key, value] of Object.entries(stringifyContext(context))) {
|
|
217
|
+
globalContext[`${name}.${key}`] = value;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
//# sourceMappingURL=api.js.map
|
package/dist/api.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAGjD,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAW1C,MAAM,qBAAqB,GAAoC;IAC7D,KAAK,EAAE,QAAQ,CAAC,KAAK,EAAE,gDAAgD;IACvE,KAAK,EAAE,QAAQ,CAAC,KAAK;IACrB,OAAO,EAAE,QAAQ,CAAC,IAAI;IACtB,GAAG,EAAE,QAAQ,CAAC,GAAG;IACjB,IAAI,EAAE,QAAQ,CAAC,IAAI;IACnB,KAAK,EAAE,QAAQ,CAAC,KAAK;CACtB,CAAC;AAEF,SAAS,cAAc,CAAC,KAAc;IACpC,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC;IAChD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;AACH,CAAC;AAED,SAAS,gBAAgB,CAAC,OAA4C;IACpE,MAAM,GAAG,GAA2B,EAAE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AK,IAAI,EAAE,CAAC;QACrB,MAAM,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC;QAC1B,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,EAAE,CAAC;YAC7C,IAAI,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;gBAC3B,OAAO,aAAa,CAAC,GAAG,CAAC,CAAC;YAC5B,CAAC;QACH,CAAC;QACD,OAAO;IACT,CAAC;IACD,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC;QACrE,aAAa,CAAC,GAAG,IAAI,IAAI,GAAG,EAAE,CAAC,GAAG,KAAK,CAAC;IAC1C,CAAC;AACH,CAAC"}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config resolution for @nais/apm (ADR-0001: docs/adr/0001-frontend-config-contract.md).
|
|
3
|
+
*
|
|
4
|
+
* Priority order (per field):
|
|
5
|
+
* 1. Explicit `init()` options
|
|
6
|
+
* 2. `<meta name="nais-app|nais-team|nais-cluster|nais-version|nais-telemetry-url">` tags
|
|
7
|
+
* (a server→browser transport rendered by the app's own server or the
|
|
8
|
+
* `@nais/apm/react` helpers — the platform never injects them)
|
|
9
|
+
* 3. Environment variables — `NAIS_APP_NAME`, `NAIS_TEAM`/`NAIS_NAMESPACE`,
|
|
10
|
+
* `NAIS_FRONTEND_TELEMETRY_COLLECTOR_URL` (pod runtime, SSR), and a version
|
|
11
|
+
* derived from `GITHUB_SHA`/`NAIS_APP_IMAGE`. In browser bundles these only
|
|
12
|
+
* work when the bundler inlines `process.env.*` — and note that
|
|
13
|
+
* `NAIS_CLUSTER_NAME` can NEVER be inlined correctly (one image deploys to
|
|
14
|
+
* many clusters); only `version` is safe to inline.
|
|
15
|
+
* 4. Collector fallback derived from the cluster name (nav tenant only).
|
|
16
|
+
* 5. No collector URL resolved → on a local host, console-echo dev mode
|
|
17
|
+
* (warn once); on a non-local host this is a misconfiguration and a
|
|
18
|
+
* loud, specific `console.error` is emitted instead.
|
|
19
|
+
*/
|
|
20
|
+
export interface ConfigOptions {
|
|
21
|
+
/** Application name; maps to Faro `app.name`. */
|
|
22
|
+
app?: string;
|
|
23
|
+
/**
|
|
24
|
+
* The nais team that owns this app (a.k.a. the Kubernetes namespace); maps to
|
|
25
|
+
* Faro `app.namespace`. The plugin groups and attributes all telemetry by
|
|
26
|
+
* team, so this is effectively required — browser telemetry without it cannot
|
|
27
|
+
* be attributed to a team. Resolves from `<meta name="nais-team">` /
|
|
28
|
+
* `nais-namespace` or the `NAIS_TEAM` / `NAIS_NAMESPACE` env when omitted.
|
|
29
|
+
*/
|
|
30
|
+
namespace?: string;
|
|
31
|
+
/** Application version / release; maps to Faro `app.version`. */
|
|
32
|
+
version?: string;
|
|
33
|
+
/** Environment (nais cluster, e.g. `prod-gcp`); maps to Faro `app.environment`. */
|
|
34
|
+
environment?: string;
|
|
35
|
+
/** Faro collector URL (Alloy `faro.receiver`). */
|
|
36
|
+
telemetryUrl?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Print the per-field resolution table (which source won for each field) to
|
|
39
|
+
* the console. Use this to diagnose "why is nothing being sent".
|
|
40
|
+
*/
|
|
41
|
+
debug?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Echo telemetry to the console when running in dev mode (no collector
|
|
44
|
+
* resolved on a local host). Defaults to `true`. Set `false` to silence
|
|
45
|
+
* both the echo and the once-only dev-mode notice during local
|
|
46
|
+
* development — telemetry is still dropped, nothing is ever sent. Has no
|
|
47
|
+
* effect when a collector is resolved, and does not silence the loud
|
|
48
|
+
* misconfiguration error on non-local hosts.
|
|
49
|
+
*/
|
|
50
|
+
devConsoleEcho?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* The tenant profile supplying last-resort collector derivation (ADR-0001
|
|
53
|
+
* decision 7). Defaults to the built-in nav profile ({@link navTenant});
|
|
54
|
+
* pass `false` to disable derivation entirely, or your own profile for a
|
|
55
|
+
* different nais tenant. INTERIM: scheduled for demotion once the
|
|
56
|
+
* platform-served well-known config URL ships (nais/grafana-apm-app#134
|
|
57
|
+
* phase 3) — never rely on it for a tenant other than nav.
|
|
58
|
+
*/
|
|
59
|
+
tenant?: TenantProfile | false;
|
|
60
|
+
}
|
|
61
|
+
export interface ResolvedConfig {
|
|
62
|
+
app: string;
|
|
63
|
+
/** The nais team (namespace). Falls back to `'unknown-team'` when unresolved. */
|
|
64
|
+
namespace: string;
|
|
65
|
+
version?: string;
|
|
66
|
+
environment?: string;
|
|
67
|
+
telemetryUrl?: string;
|
|
68
|
+
/** True when no collector URL could be resolved → console-echo mode. */
|
|
69
|
+
devMode: boolean;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The shape of the nais platform's generated frontend config — the `nais.js`
|
|
73
|
+
* module (and upcoming `nais.json`) that naiserator mounts into the pod when
|
|
74
|
+
* `spec.frontend.generatedConfig` is set, with `environment`/`schemaVersion`
|
|
75
|
+
* arriving as the platform contract completes (nais/grafana-apm-app#134).
|
|
76
|
+
*/
|
|
77
|
+
export interface NaisGeneratedConfig {
|
|
78
|
+
/** Contract generation; absent on the original payload. */
|
|
79
|
+
schemaVersion?: number;
|
|
80
|
+
/** Faro collector URL for the cluster this pod runs in. */
|
|
81
|
+
telemetryCollectorURL?: string;
|
|
82
|
+
app?: {
|
|
83
|
+
name?: string;
|
|
84
|
+
/** The owning team; absent until nais/grafana-apm-app#134 phase 1 lands. */
|
|
85
|
+
namespace?: string;
|
|
86
|
+
version?: string;
|
|
87
|
+
};
|
|
88
|
+
/** Cluster/environment; absent until nais/grafana-apm-app#134 phase 1 lands. */
|
|
89
|
+
environment?: string;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Map the nais generated frontend config (naiserator `generatedConfig`) to
|
|
93
|
+
* `init()` options. SSR servers can `import(mountPath)` the mounted module and
|
|
94
|
+
* pass it through; browser apps that serve the file from their web root should
|
|
95
|
+
* prefer {@link initFromConfigUrl} (which fetches and applies it in one step).
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* const naisConfig = (await import('/app/nais.js')).default;
|
|
99
|
+
* init({ ...fromNaisConfig(naisConfig), namespace: 'my-team' });
|
|
100
|
+
*/
|
|
101
|
+
export declare function fromNaisConfig(config: NaisGeneratedConfig | null | undefined): ConfigOptions;
|
|
102
|
+
/**
|
|
103
|
+
* Derive a version from a container image reference, e.g.
|
|
104
|
+
* `europe-north1-docker.pkg.dev/nais/app:2026.07.03-abc1234` → `2026.07.03-abc1234`.
|
|
105
|
+
*/
|
|
106
|
+
export declare function versionFromImage(image: string | undefined): string | undefined;
|
|
107
|
+
/**
|
|
108
|
+
* A tenant's last-resort collector derivation (ADR-0001 decision 7). Every
|
|
109
|
+
* nais tenant is a physically isolated installation (own clusters, own
|
|
110
|
+
* naiserator, own top-level domain), so anything domain-shaped is per-tenant
|
|
111
|
+
* knowledge and lives behind this interface — never in the shared resolution
|
|
112
|
+
* code. The tenant-agnostic paths (meta tags, generatedConfig, env) always
|
|
113
|
+
* win; a profile only fires when nothing else resolved.
|
|
114
|
+
*/
|
|
115
|
+
export interface TenantProfile {
|
|
116
|
+
/** Short tenant name, used in debug/source labels (e.g. `'nav'`). */
|
|
117
|
+
name: string;
|
|
118
|
+
/** Derive the collector URL from a cluster name (e.g. `prod-gcp`). */
|
|
119
|
+
telemetryUrlFromCluster?(cluster: string): string | undefined;
|
|
120
|
+
/**
|
|
121
|
+
* Derive the collector URL from the page's hostname. Only consulted on
|
|
122
|
+
* non-local hosts. This is what makes bare `init({ app, namespace })` send
|
|
123
|
+
* telemetry from a static bundle when no runtime channel is wired up yet.
|
|
124
|
+
*/
|
|
125
|
+
telemetryUrlFromHostname?(hostname: string): string | undefined;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The built-in nav tenant profile (https://docs.nais.io/observability/frontend/).
|
|
129
|
+
* Nais APM is currently installed for the nav tenant only; other tenants must
|
|
130
|
+
* rely on the platform channels (meta tag / generatedConfig / env) or supply
|
|
131
|
+
* their own profile. INTERIM per ADR-0001: both derivations are demoted once
|
|
132
|
+
* the platform-served well-known config URL ships (nais/grafana-apm-app#134
|
|
133
|
+
* phase 3, tracked with #86).
|
|
134
|
+
*
|
|
135
|
+
* Note: only the collector URL is derived — never `environment`. The hostname
|
|
136
|
+
* cannot tell prod-gcp from prod-fss, and a fabricated cluster name is worse
|
|
137
|
+
* than an absent one.
|
|
138
|
+
*/
|
|
139
|
+
export declare const navTenant: TenantProfile;
|
|
140
|
+
/**
|
|
141
|
+
* True when the page is served from a genuinely local host — the only case
|
|
142
|
+
* where silently entering console-echo dev mode is the right behavior. On any
|
|
143
|
+
* other host a missing collector URL is a production misconfiguration and must
|
|
144
|
+
* be loud (ADR-0001 decision 6).
|
|
145
|
+
*/
|
|
146
|
+
export declare function isLocalHost(hostname?: string | undefined): boolean;
|
|
147
|
+
/** @internal test helper — resets the once-only dev-mode and namespace warnings. */
|
|
148
|
+
export declare function _resetDevModeWarning(): void;
|
|
149
|
+
export declare function resolveConfig(options?: ConfigOptions): ResolvedConfig;
|
|
150
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,MAAM,WAAW,aAAa;IAC5B,iDAAiD;IACjD,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iEAAiE;IACjE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,mFAAmF;IACnF,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,kDAAkD;IAClD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IAC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G,MAAM,GAAG,SAAS,CAAC;CACjE;AAKD;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,SAAS,EAAE,aA0BvB,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,WAAW,CACzB,QAAQ,GAAE,MAAM,GAAG,SAEN,GACZ,OAAO,CAcT;AAkBD,oFAAoF;AACpF,wBAAgB,oBAAoB,IAAI,IAAI,CAI3C;AAED,wBAAgB,aAAa,CAAC,OAAO,GAAE,aAAkB,GAAG,cAAc,CAyIzE"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config resolution for @nais/apm (ADR-0001: docs/adr/0001-frontend-config-contract.md).
|
|
3
|
+
*
|
|
4
|
+
* Priority order (per field):
|
|
5
|
+
* 1. Explicit `init()` options
|
|
6
|
+
* 2. `<meta name="nais-app|nais-team|nais-cluster|nais-version|nais-telemetry-url">` tags
|
|
7
|
+
* (a server→browser transport rendered by the app's own server or the
|
|
8
|
+
* `@nais/apm/react` helpers — the platform never injects them)
|
|
9
|
+
* 3. Environment variables — `NAIS_APP_NAME`, `NAIS_TEAM`/`NAIS_NAMESPACE`,
|
|
10
|
+
* `NAIS_FRONTEND_TELEMETRY_COLLECTOR_URL` (pod runtime, SSR), and a version
|
|
11
|
+
* derived from `GITHUB_SHA`/`NAIS_APP_IMAGE`. In browser bundles these only
|
|
12
|
+
* work when the bundler inlines `process.env.*` — and note that
|
|
13
|
+
* `NAIS_CLUSTER_NAME` can NEVER be inlined correctly (one image deploys to
|
|
14
|
+
* many clusters); only `version` is safe to inline.
|
|
15
|
+
* 4. Collector fallback derived from the cluster name (nav tenant only).
|
|
16
|
+
* 5. No collector URL resolved → on a local host, console-echo dev mode
|
|
17
|
+
* (warn once); on a non-local host this is a misconfiguration and a
|
|
18
|
+
* loud, specific `console.error` is emitted instead.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Map the nais generated frontend config (naiserator `generatedConfig`) to
|
|
22
|
+
* `init()` options. SSR servers can `import(mountPath)` the mounted module and
|
|
23
|
+
* pass it through; browser apps that serve the file from their web root should
|
|
24
|
+
* prefer {@link initFromConfigUrl} (which fetches and applies it in one step).
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* const naisConfig = (await import('/app/nais.js')).default;
|
|
28
|
+
* init({ ...fromNaisConfig(naisConfig), namespace: 'my-team' });
|
|
29
|
+
*/
|
|
30
|
+
export function fromNaisConfig(config) {
|
|
31
|
+
if (config == null || typeof config !== 'object') {
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
const options = {};
|
|
35
|
+
if (config.app?.name)
|
|
36
|
+
options.app = config.app.name;
|
|
37
|
+
if (config.app?.namespace)
|
|
38
|
+
options.namespace = config.app.namespace;
|
|
39
|
+
if (config.app?.version)
|
|
40
|
+
options.version = config.app.version;
|
|
41
|
+
if (config.environment)
|
|
42
|
+
options.environment = config.environment;
|
|
43
|
+
if (config.telemetryCollectorURL)
|
|
44
|
+
options.telemetryUrl = config.telemetryCollectorURL;
|
|
45
|
+
return options;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Read an environment value defensively. The callback must contain the literal
|
|
49
|
+
* `process.env.NAME` expression so bundlers can substitute it; in browsers
|
|
50
|
+
* without such substitution the ReferenceError is swallowed.
|
|
51
|
+
*/
|
|
52
|
+
function safeEnv(get) {
|
|
53
|
+
try {
|
|
54
|
+
const value = get();
|
|
55
|
+
return value === '' ? undefined : value;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function readMeta(name) {
|
|
62
|
+
if (typeof document === 'undefined') {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
const content = document.querySelector(`meta[name="${name}"]`)?.getAttribute('content');
|
|
66
|
+
return content == null || content === '' ? undefined : content;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Derive a version from a container image reference, e.g.
|
|
70
|
+
* `europe-north1-docker.pkg.dev/nais/app:2026.07.03-abc1234` → `2026.07.03-abc1234`.
|
|
71
|
+
*/
|
|
72
|
+
export function versionFromImage(image) {
|
|
73
|
+
if (!image) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
// A digest suffix is not a tag: `app@sha256:…` must yield no version, and
|
|
77
|
+
// `app:v1@sha256:…` must yield `v1` — without this, the digest's colon wins
|
|
78
|
+
// and 64 hex chars leak into app.version (found by the nais/naiserator#687
|
|
79
|
+
// adversarial review; naiserator's generator mirrors this logic).
|
|
80
|
+
const at = image.indexOf('@');
|
|
81
|
+
if (at !== -1) {
|
|
82
|
+
image = image.slice(0, at);
|
|
83
|
+
}
|
|
84
|
+
const colon = image.lastIndexOf(':');
|
|
85
|
+
if (colon === -1 || colon < image.lastIndexOf('/')) {
|
|
86
|
+
return undefined; // no tag, or the colon belongs to a registry port
|
|
87
|
+
}
|
|
88
|
+
const tag = image.slice(colon + 1);
|
|
89
|
+
return tag === '' ? undefined : tag;
|
|
90
|
+
}
|
|
91
|
+
const NAV_PROD_COLLECTOR = 'https://telemetry.nav.no/collect';
|
|
92
|
+
const NAV_DEV_COLLECTOR = 'https://telemetry.ekstern.dev.nav.no/collect';
|
|
93
|
+
/**
|
|
94
|
+
* The built-in nav tenant profile (https://docs.nais.io/observability/frontend/).
|
|
95
|
+
* Nais APM is currently installed for the nav tenant only; other tenants must
|
|
96
|
+
* rely on the platform channels (meta tag / generatedConfig / env) or supply
|
|
97
|
+
* their own profile. INTERIM per ADR-0001: both derivations are demoted once
|
|
98
|
+
* the platform-served well-known config URL ships (nais/grafana-apm-app#134
|
|
99
|
+
* phase 3, tracked with #86).
|
|
100
|
+
*
|
|
101
|
+
* Note: only the collector URL is derived — never `environment`. The hostname
|
|
102
|
+
* cannot tell prod-gcp from prod-fss, and a fabricated cluster name is worse
|
|
103
|
+
* than an absent one.
|
|
104
|
+
*/
|
|
105
|
+
export const navTenant = {
|
|
106
|
+
name: 'nav',
|
|
107
|
+
telemetryUrlFromCluster(cluster) {
|
|
108
|
+
if (cluster.startsWith('prod-')) {
|
|
109
|
+
return NAV_PROD_COLLECTOR;
|
|
110
|
+
}
|
|
111
|
+
if (cluster.startsWith('dev-')) {
|
|
112
|
+
return NAV_DEV_COLLECTOR;
|
|
113
|
+
}
|
|
114
|
+
return undefined;
|
|
115
|
+
},
|
|
116
|
+
telemetryUrlFromHostname(hostname) {
|
|
117
|
+
const host = hostname.toLowerCase();
|
|
118
|
+
if (host !== 'nav.no' && !host.endsWith('.nav.no')) {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
// A literal `dev` DNS label anywhere in the name marks a dev-cluster
|
|
122
|
+
// ingress. The modern pattern puts it late (*.intern.dev.nav.no), but
|
|
123
|
+
// legacy dev-fss ingresses still serve *.dev.intern.nav.no — a suffix
|
|
124
|
+
// check on `.dev.nav.no` alone misrouted those to the PROD collector.
|
|
125
|
+
// The label must match exactly: devtools.intern.nav.no stays prod.
|
|
126
|
+
if (host.split('.').includes('dev')) {
|
|
127
|
+
return NAV_DEV_COLLECTOR;
|
|
128
|
+
}
|
|
129
|
+
return NAV_PROD_COLLECTOR;
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* True when the page is served from a genuinely local host — the only case
|
|
134
|
+
* where silently entering console-echo dev mode is the right behavior. On any
|
|
135
|
+
* other host a missing collector URL is a production misconfiguration and must
|
|
136
|
+
* be loud (ADR-0001 decision 6).
|
|
137
|
+
*/
|
|
138
|
+
export function isLocalHost(hostname = typeof window !== 'undefined' && window.location
|
|
139
|
+
? window.location.hostname
|
|
140
|
+
: undefined) {
|
|
141
|
+
if (hostname === undefined) {
|
|
142
|
+
// Non-browser (SSR import, tests without jsdom): treat as local — the
|
|
143
|
+
// browser-side init is where the loud path matters.
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
return (hostname === '' ||
|
|
147
|
+
hostname === 'localhost' ||
|
|
148
|
+
hostname === '127.0.0.1' ||
|
|
149
|
+
hostname === '[::1]' ||
|
|
150
|
+
hostname.endsWith('.localhost') ||
|
|
151
|
+
hostname.endsWith('.local'));
|
|
152
|
+
}
|
|
153
|
+
function pick(...candidates) {
|
|
154
|
+
for (const [value, source] of candidates) {
|
|
155
|
+
if (value != null) {
|
|
156
|
+
return [value, source];
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return [undefined, 'unresolved'];
|
|
160
|
+
}
|
|
161
|
+
let warnedDevMode = false;
|
|
162
|
+
let warnedMissingCollector = false;
|
|
163
|
+
let warnedMissingNamespace = false;
|
|
164
|
+
/** @internal test helper — resets the once-only dev-mode and namespace warnings. */
|
|
165
|
+
export function _resetDevModeWarning() {
|
|
166
|
+
warnedDevMode = false;
|
|
167
|
+
warnedMissingCollector = false;
|
|
168
|
+
warnedMissingNamespace = false;
|
|
169
|
+
}
|
|
170
|
+
export function resolveConfig(options = {}) {
|
|
171
|
+
const envApp = safeEnv(() => process.env.NAIS_APP_NAME);
|
|
172
|
+
const envCluster = safeEnv(() => process.env.NAIS_CLUSTER_NAME);
|
|
173
|
+
const envNamespace = safeEnv(() => process.env.NAIS_TEAM) ?? safeEnv(() => process.env.NAIS_NAMESPACE);
|
|
174
|
+
// The pod-runtime collector URL naiserator sets alongside generatedConfig.
|
|
175
|
+
// Runtime-only (SSR); in a CI-built browser bundle it does not exist.
|
|
176
|
+
const envTelemetryUrl = safeEnv(() => process.env.NAIS_FRONTEND_TELEMETRY_COLLECTOR_URL);
|
|
177
|
+
// The commit SHA is the release identity that deploy annotations carry
|
|
178
|
+
// (nais/grafana-apm-app#64: the deploy-annotation action defaults to
|
|
179
|
+
// github.sha), so preferring GITHUB_SHA makes app.version join deploy
|
|
180
|
+
// markers and release tracking without per-team convention work. The
|
|
181
|
+
// image-tag derivation stays as the fallback.
|
|
182
|
+
const envSha = safeEnv(() => process.env.GITHUB_SHA);
|
|
183
|
+
const envVersion = envSha ?? versionFromImage(safeEnv(() => process.env.NAIS_APP_IMAGE));
|
|
184
|
+
const [app, appSource] = pick([options.app, 'init option'], [readMeta('nais-app'), 'meta nais-app'], [envApp, 'env NAIS_APP_NAME']);
|
|
185
|
+
// Accept either `nais-team` (preferred, matches the product term) or the
|
|
186
|
+
// literal `nais-namespace` meta/env for teams that mirror the k8s name.
|
|
187
|
+
const [resolvedNamespace, namespaceSource] = pick([options.namespace, 'init option'], [readMeta('nais-team'), 'meta nais-team'], [readMeta('nais-namespace'), 'meta nais-namespace'], [envNamespace, 'env NAIS_TEAM/NAIS_NAMESPACE']);
|
|
188
|
+
const [environment, environmentSource] = pick([options.environment, 'init option'], [readMeta('nais-cluster'), 'meta nais-cluster'], [envCluster, 'env NAIS_CLUSTER_NAME']);
|
|
189
|
+
const [version, versionSource] = pick([options.version, 'init option'], [readMeta('nais-version'), 'meta nais-version'], [envVersion, envSha ? 'env GITHUB_SHA' : 'env NAIS_APP_IMAGE tag']);
|
|
190
|
+
const tenant = options.tenant === false ? undefined : (options.tenant ?? navTenant);
|
|
191
|
+
const hostname = typeof window !== 'undefined' && window.location ? window.location.hostname : undefined;
|
|
192
|
+
const local = isLocalHost(hostname);
|
|
193
|
+
const [telemetryUrl, telemetryUrlSource] = pick([options.telemetryUrl, 'init option'], [readMeta('nais-telemetry-url'), 'meta nais-telemetry-url'], [envTelemetryUrl, 'env NAIS_FRONTEND_TELEMETRY_COLLECTOR_URL'], [
|
|
194
|
+
environment != null ? tenant?.telemetryUrlFromCluster?.(environment) : undefined,
|
|
195
|
+
`derived from cluster '${environment ?? ''}' (${tenant?.name ?? 'no'} tenant fallback)`,
|
|
196
|
+
], [
|
|
197
|
+
// Hostname derivation only fires on real (non-local) hosts — locally the
|
|
198
|
+
// console-echo dev mode is the right outcome, not a network transport.
|
|
199
|
+
!local && hostname != null ? tenant?.telemetryUrlFromHostname?.(hostname) : undefined,
|
|
200
|
+
`derived from hostname '${hostname ?? ''}' (${tenant?.name ?? 'no'} tenant fallback)`,
|
|
201
|
+
]);
|
|
202
|
+
const devMode = telemetryUrl == null;
|
|
203
|
+
if (options.debug) {
|
|
204
|
+
// eslint-disable-next-line no-console
|
|
205
|
+
console.info('[@nais/apm] config resolution:\n' +
|
|
206
|
+
[
|
|
207
|
+
['app', app, appSource],
|
|
208
|
+
['namespace', resolvedNamespace, namespaceSource],
|
|
209
|
+
['version', version, versionSource],
|
|
210
|
+
['environment', environment, environmentSource],
|
|
211
|
+
['telemetryUrl', telemetryUrl, telemetryUrlSource],
|
|
212
|
+
]
|
|
213
|
+
.map(([field, value, source]) => ` ${field} = ${value ?? '(unresolved)'} ← ${source}`)
|
|
214
|
+
.join('\n') +
|
|
215
|
+
`\n mode = ${devMode
|
|
216
|
+
? local
|
|
217
|
+
? options.devConsoleEcho === false
|
|
218
|
+
? 'dev (silent)'
|
|
219
|
+
: 'dev (console echo)'
|
|
220
|
+
: 'MISCONFIGURED (nothing sent)'
|
|
221
|
+
: 'sending'}`);
|
|
222
|
+
}
|
|
223
|
+
if (devMode) {
|
|
224
|
+
// With devConsoleEcho: false the developer asked for silence, so the
|
|
225
|
+
// dev-mode notice is skipped too — but the non-local misconfiguration
|
|
226
|
+
// error below stays loud regardless (it signals a broken deploy, not
|
|
227
|
+
// local development).
|
|
228
|
+
if (local && options.devConsoleEcho !== false && !warnedDevMode) {
|
|
229
|
+
warnedDevMode = true;
|
|
230
|
+
// eslint-disable-next-line no-console
|
|
231
|
+
console.warn('[@nais/apm] No telemetry collector URL resolved (no init option, nais meta tag, or NAIS_* env). ' +
|
|
232
|
+
'Running in dev mode: telemetry is echoed to the console and nothing is sent. ' +
|
|
233
|
+
'Silence this with init({ devConsoleEcho: false }).');
|
|
234
|
+
}
|
|
235
|
+
else if (!local && !warnedMissingCollector) {
|
|
236
|
+
warnedMissingCollector = true;
|
|
237
|
+
// This page is served from a real (non-local) host with no collector —
|
|
238
|
+
// a misconfiguration, not local development. Be loud and specific
|
|
239
|
+
// (ADR-0001 decision 6): name what is missing, what will not flow, and
|
|
240
|
+
// how to fix it. Never throw — that would take down the host app.
|
|
241
|
+
// eslint-disable-next-line no-console
|
|
242
|
+
console.error('[@nais/apm] No telemetry collector URL resolved on a non-local host — telemetry will NOT be sent. ' +
|
|
243
|
+
'Fix one of: pass init({ telemetryUrl }), serve a <meta name="nais-telemetry-url"> tag, ' +
|
|
244
|
+
'or mount the nais generatedConfig into your web root and use initFromConfigUrl(). ' +
|
|
245
|
+
'Diagnose with init({ debug: true }). https://github.com/nais/apm#configuration-resolution');
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
// Team (namespace) is required for the plugin to attribute telemetry. We do
|
|
249
|
+
// NOT throw — a thrown init() would take down the host app — so we loud-warn
|
|
250
|
+
// (error in prod, warn in dev/console mode) and fall back to 'unknown-team'.
|
|
251
|
+
if (resolvedNamespace == null && !warnedMissingNamespace) {
|
|
252
|
+
warnedMissingNamespace = true;
|
|
253
|
+
const message = '[@nais/apm] namespace (team) is required — telemetry cannot be attributed to your team and ' +
|
|
254
|
+
'will be grouped under "unknown-team". Fix one of: pass init({ namespace }), serve a ' +
|
|
255
|
+
'<meta name="nais-team"> tag, or set the NAIS_TEAM env.';
|
|
256
|
+
// eslint-disable-next-line no-console
|
|
257
|
+
(devMode && local ? console.warn : console.error)(message);
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
app: app ?? 'unknown-app',
|
|
261
|
+
namespace: resolvedNamespace ?? 'unknown-team',
|
|
262
|
+
version,
|
|
263
|
+
environment,
|
|
264
|
+
telemetryUrl,
|
|
265
|
+
devMode,
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
//# sourceMappingURL=config.js.map
|