@kubesense/kubesense-browser-logs 0.0.0-stage → 2.0.0-beta.1

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.
Files changed (166) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +90 -2
  3. package/bundle/kubesense-logs.js +1 -0
  4. package/cjs/boot/logsPublicApi.d.ts +231 -0
  5. package/cjs/boot/logsPublicApi.js +75 -0
  6. package/cjs/boot/logsPublicApi.js.map +1 -0
  7. package/cjs/boot/preStartLogs.d.ts +8 -0
  8. package/cjs/boot/preStartLogs.js +105 -0
  9. package/cjs/boot/preStartLogs.js.map +1 -0
  10. package/cjs/boot/startLogs.d.ts +35 -0
  11. package/cjs/boot/startLogs.js +54 -0
  12. package/cjs/boot/startLogs.js.map +1 -0
  13. package/cjs/domain/assembly.d.ts +5 -0
  14. package/cjs/domain/assembly.js +35 -0
  15. package/cjs/domain/assembly.js.map +1 -0
  16. package/cjs/domain/configuration.d.ts +306 -0
  17. package/cjs/domain/configuration.js +86 -0
  18. package/cjs/domain/configuration.js.map +1 -0
  19. package/cjs/domain/console/consoleCollection.d.ts +20 -0
  20. package/cjs/domain/console/consoleCollection.js +37 -0
  21. package/cjs/domain/console/consoleCollection.js.map +1 -0
  22. package/cjs/domain/contexts/commonContext.d.ts +2 -0
  23. package/cjs/domain/contexts/commonContext.js +13 -0
  24. package/cjs/domain/contexts/commonContext.js.map +1 -0
  25. package/cjs/domain/contexts/internalContext.d.ts +7 -0
  26. package/cjs/domain/contexts/internalContext.js +11 -0
  27. package/cjs/domain/contexts/internalContext.js.map +1 -0
  28. package/cjs/domain/contexts/rumInternalContext.d.ts +2 -0
  29. package/cjs/domain/contexts/rumInternalContext.js +32 -0
  30. package/cjs/domain/contexts/rumInternalContext.js.map +1 -0
  31. package/cjs/domain/contexts/sessionContext.d.ts +4 -0
  32. package/cjs/domain/contexts/sessionContext.js +20 -0
  33. package/cjs/domain/contexts/sessionContext.js.map +1 -0
  34. package/cjs/domain/contexts/trackingConsentContext.d.ts +3 -0
  35. package/cjs/domain/contexts/trackingConsentContext.js +14 -0
  36. package/cjs/domain/contexts/trackingConsentContext.js.map +1 -0
  37. package/cjs/domain/createErrorFieldFromRawError.d.ts +10 -0
  38. package/cjs/domain/createErrorFieldFromRawError.js +15 -0
  39. package/cjs/domain/createErrorFieldFromRawError.js.map +1 -0
  40. package/cjs/domain/hooks.d.ts +28 -0
  41. package/cjs/domain/hooks.js +13 -0
  42. package/cjs/domain/hooks.js.map +1 -0
  43. package/cjs/domain/lifeCycle.d.ts +24 -0
  44. package/cjs/domain/lifeCycle.js +6 -0
  45. package/cjs/domain/lifeCycle.js.map +1 -0
  46. package/cjs/domain/logger/isAuthorized.d.ts +17 -0
  47. package/cjs/domain/logger/isAuthorized.js +34 -0
  48. package/cjs/domain/logger/isAuthorized.js.map +1 -0
  49. package/cjs/domain/logger/loggerCollection.d.ts +7 -0
  50. package/cjs/domain/logger/loggerCollection.js +47 -0
  51. package/cjs/domain/logger/loggerCollection.js.map +1 -0
  52. package/cjs/domain/logger.d.ts +48 -0
  53. package/cjs/domain/logger.js +112 -0
  54. package/cjs/domain/logger.js.map +1 -0
  55. package/cjs/domain/networkError/networkErrorCollection.d.ts +9 -0
  56. package/cjs/domain/networkError/networkErrorCollection.js +57 -0
  57. package/cjs/domain/networkError/networkErrorCollection.js.map +1 -0
  58. package/cjs/domain/plugins.d.ts +49 -0
  59. package/cjs/domain/plugins.js +12 -0
  60. package/cjs/domain/plugins.js.map +1 -0
  61. package/cjs/domain/remoteSdkConfiguration.d.ts +26 -0
  62. package/cjs/domain/remoteSdkConfiguration.js +67 -0
  63. package/cjs/domain/remoteSdkConfiguration.js.map +1 -0
  64. package/cjs/domain/report/reportCollection.d.ts +13 -0
  65. package/cjs/domain/report/reportCollection.js +30 -0
  66. package/cjs/domain/report/reportCollection.js.map +1 -0
  67. package/cjs/domain/reportError.d.ts +2 -0
  68. package/cjs/domain/reportError.js +20 -0
  69. package/cjs/domain/reportError.js.map +1 -0
  70. package/cjs/domain/runtimeError/runtimeErrorCollection.d.ts +14 -0
  71. package/cjs/domain/runtimeError/runtimeErrorCollection.js +29 -0
  72. package/cjs/domain/runtimeError/runtimeErrorCollection.js.map +1 -0
  73. package/cjs/domainContext.types.d.ts +11 -0
  74. package/cjs/entries/main.d.ts +33 -0
  75. package/cjs/entries/main.js +31 -0
  76. package/cjs/entries/main.js.map +1 -0
  77. package/cjs/logsEvent.types.d.ts +192 -0
  78. package/cjs/rawLogsEvent.types.d.ts +59 -0
  79. package/cjs/transport/startLogsBatch.d.ts +4 -0
  80. package/cjs/transport/startLogsBatch.js +22 -0
  81. package/cjs/transport/startLogsBatch.js.map +1 -0
  82. package/cjs/transport/startLogsBridge.d.ts +2 -0
  83. package/cjs/transport/startLogsBridge.js +13 -0
  84. package/cjs/transport/startLogsBridge.js.map +1 -0
  85. package/esm/boot/logsPublicApi.mjs +75 -0
  86. package/esm/boot/logsPublicApi.mjs.map +1 -0
  87. package/esm/boot/preStartLogs.mjs +105 -0
  88. package/esm/boot/preStartLogs.mjs.map +1 -0
  89. package/esm/boot/startLogs.mjs +54 -0
  90. package/esm/boot/startLogs.mjs.map +1 -0
  91. package/esm/domain/assembly.mjs +35 -0
  92. package/esm/domain/assembly.mjs.map +1 -0
  93. package/esm/domain/configuration.mjs +84 -0
  94. package/esm/domain/configuration.mjs.map +1 -0
  95. package/esm/domain/console/consoleCollection.mjs +36 -0
  96. package/esm/domain/console/consoleCollection.mjs.map +1 -0
  97. package/esm/domain/contexts/commonContext.mjs +13 -0
  98. package/esm/domain/contexts/commonContext.mjs.map +1 -0
  99. package/esm/domain/contexts/internalContext.mjs +11 -0
  100. package/esm/domain/contexts/internalContext.mjs.map +1 -0
  101. package/esm/domain/contexts/rumInternalContext.mjs +32 -0
  102. package/esm/domain/contexts/rumInternalContext.mjs.map +1 -0
  103. package/esm/domain/contexts/sessionContext.mjs +20 -0
  104. package/esm/domain/contexts/sessionContext.mjs.map +1 -0
  105. package/esm/domain/contexts/trackingConsentContext.mjs +14 -0
  106. package/esm/domain/contexts/trackingConsentContext.mjs.map +1 -0
  107. package/esm/domain/createErrorFieldFromRawError.mjs +15 -0
  108. package/esm/domain/createErrorFieldFromRawError.mjs.map +1 -0
  109. package/esm/domain/hooks.mjs +13 -0
  110. package/esm/domain/hooks.mjs.map +1 -0
  111. package/esm/domain/lifeCycle.mjs +7 -0
  112. package/esm/domain/lifeCycle.mjs.map +1 -0
  113. package/esm/domain/logger/isAuthorized.mjs +32 -0
  114. package/esm/domain/logger/isAuthorized.mjs.map +1 -0
  115. package/esm/domain/logger/loggerCollection.mjs +47 -0
  116. package/esm/domain/logger/loggerCollection.mjs.map +1 -0
  117. package/esm/domain/logger.mjs +110 -0
  118. package/esm/domain/logger.mjs.map +1 -0
  119. package/esm/domain/networkError/networkErrorCollection.mjs +56 -0
  120. package/esm/domain/networkError/networkErrorCollection.mjs.map +1 -0
  121. package/esm/domain/plugins.mjs +12 -0
  122. package/esm/domain/plugins.mjs.map +1 -0
  123. package/esm/domain/remoteSdkConfiguration.mjs +65 -0
  124. package/esm/domain/remoteSdkConfiguration.mjs.map +1 -0
  125. package/esm/domain/report/reportCollection.mjs +30 -0
  126. package/esm/domain/report/reportCollection.mjs.map +1 -0
  127. package/esm/domain/reportError.mjs +20 -0
  128. package/esm/domain/reportError.mjs.map +1 -0
  129. package/esm/domain/runtimeError/runtimeErrorCollection.mjs +29 -0
  130. package/esm/domain/runtimeError/runtimeErrorCollection.mjs.map +1 -0
  131. package/esm/entries/main.mjs +27 -0
  132. package/esm/entries/main.mjs.map +1 -0
  133. package/esm/transport/startLogsBatch.mjs +22 -0
  134. package/esm/transport/startLogsBatch.mjs.map +1 -0
  135. package/esm/transport/startLogsBridge.mjs +13 -0
  136. package/esm/transport/startLogsBridge.mjs.map +1 -0
  137. package/package.json +53 -3
  138. package/src/boot/logsPublicApi.ts +427 -0
  139. package/src/boot/preStartLogs.ts +192 -0
  140. package/src/boot/startLogs.ts +83 -0
  141. package/src/domain/assembly.ts +73 -0
  142. package/src/domain/configuration.ts +176 -0
  143. package/src/domain/console/consoleCollection.ts +58 -0
  144. package/src/domain/contexts/commonContext.ts +15 -0
  145. package/src/domain/contexts/internalContext.ts +19 -0
  146. package/src/domain/contexts/rumInternalContext.ts +60 -0
  147. package/src/domain/contexts/sessionContext.ts +34 -0
  148. package/src/domain/contexts/trackingConsentContext.ts +18 -0
  149. package/src/domain/createErrorFieldFromRawError.ts +22 -0
  150. package/src/domain/hooks.ts +40 -0
  151. package/src/domain/lifeCycle.ts +25 -0
  152. package/src/domain/logger/isAuthorized.ts +33 -0
  153. package/src/domain/logger/loggerCollection.ts +69 -0
  154. package/src/domain/logger.ts +202 -0
  155. package/src/domain/networkError/networkErrorCollection.ts +104 -0
  156. package/src/domain/plugins.ts +72 -0
  157. package/src/domain/remoteSdkConfiguration.ts +105 -0
  158. package/src/domain/report/reportCollection.ts +47 -0
  159. package/src/domain/reportError.ts +20 -0
  160. package/src/domain/runtimeError/runtimeErrorCollection.ts +51 -0
  161. package/src/domainContext.types.ts +21 -0
  162. package/src/entries/main.ts +65 -0
  163. package/src/logsEvent.types.ts +198 -0
  164. package/src/rawLogsEvent.types.ts +74 -0
  165. package/src/transport/startLogsBatch.ts +29 -0
  166. package/src/transport/startLogsBridge.ts +13 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"startLogsBatch.mjs","names":[],"sources":["../../src/transport/startLogsBatch.ts"],"sourcesContent":["import type { Context, SessionManager } from '@kubesense/kubesense-browser-core'\nimport { createBatch } from '@kubesense/kubesense-browser-core'\nimport { createEndpointBuilder, createReplicaEndpointBuilder } from '@kubesense/kubesense-js-core/transport'\nimport type { LogsConfiguration } from '../domain/configuration'\nimport type { LifeCycle } from '../domain/lifeCycle'\nimport { LifeCycleEventType } from '../domain/lifeCycle'\nimport type { LogsEvent } from '../logsEvent.types'\n\nexport function startLogsBatch(\n configuration: LogsConfiguration,\n lifeCycle: LifeCycle,\n reportError: (message: string) => void,\n sessionManager: SessionManager\n) {\n const endpoints = [createEndpointBuilder(configuration, 'logs')]\n const replicaEndpoint = createReplicaEndpointBuilder(configuration, 'logs')\n if (replicaEndpoint) {\n endpoints.push(replicaEndpoint)\n }\n\n const batch = createBatch({ endpoints, reportError })\n sessionManager.expireObservable.subscribe(() => batch.forceFlush('session_expire'))\n\n lifeCycle.subscribe(LifeCycleEventType.LOG_COLLECTED, (serverLogsEvent: LogsEvent & Context) => {\n batch.add(serverLogsEvent)\n })\n\n return batch\n}\n"],"mappings":";;;;AAQA,SAAgB,eACd,eACA,WACA,aACA,gBACA;CACA,MAAM,YAAY,CAAC,sBAAsB,eAAe,MAAM,CAAC;CAC/D,MAAM,kBAAkB,6BAA6B,eAAe,MAAM;CAC1E,IAAI,iBACF,UAAU,KAAK,eAAe;CAGhC,MAAM,QAAQ,YAAY;EAAE;EAAW;CAAY,CAAC;CACpD,eAAe,iBAAiB,gBAAgB,MAAM,WAAW,gBAAgB,CAAC;CAElF,UAAU,UAAA,IAA6C,oBAAyC;EAC9F,MAAM,IAAI,eAAe;CAC3B,CAAC;CAED,OAAO;AACT"}
@@ -0,0 +1,13 @@
1
+ import "../domain/lifeCycle.mjs";
2
+ import { getEventBridge } from "@kubesense/kubesense-browser-core";
3
+ //#region src/transport/startLogsBridge.ts
4
+ function startLogsBridge(lifeCycle) {
5
+ const bridge = getEventBridge();
6
+ lifeCycle.subscribe(1, (serverLogsEvent) => {
7
+ bridge.send("log", serverLogsEvent);
8
+ });
9
+ }
10
+ //#endregion
11
+ export { startLogsBridge };
12
+
13
+ //# sourceMappingURL=startLogsBridge.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"startLogsBridge.mjs","names":[],"sources":["../../src/transport/startLogsBridge.ts"],"sourcesContent":["import type { Context } from '@kubesense/kubesense-browser-core'\nimport { getEventBridge } from '@kubesense/kubesense-browser-core'\nimport type { LifeCycle } from '../domain/lifeCycle'\nimport { LifeCycleEventType } from '../domain/lifeCycle'\nimport type { LogsEvent } from '../logsEvent.types'\n\nexport function startLogsBridge(lifeCycle: LifeCycle) {\n const bridge = getEventBridge<'log', LogsEvent>()!\n\n lifeCycle.subscribe(LifeCycleEventType.LOG_COLLECTED, (serverLogsEvent: LogsEvent & Context) => {\n bridge.send('log', serverLogsEvent)\n })\n}\n"],"mappings":";;;AAMA,SAAgB,gBAAgB,WAAsB;CACpD,MAAM,SAAS,eAAiC;CAEhD,UAAU,UAAA,IAA6C,oBAAyC;EAC9F,OAAO,KAAK,OAAO,eAAe;CACpC,CAAC;AACH"}
package/package.json CHANGED
@@ -1,6 +1,56 @@
1
1
  {
2
2
  "name": "@kubesense/kubesense-browser-logs",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "2.0.0-beta.1",
4
+ "keywords": [
5
+ "kubesense",
6
+ "browser",
7
+ "logs",
8
+ "logging",
9
+ "log-collection",
10
+ "monitoring",
11
+ "observability",
12
+ "error-tracking",
13
+ "telemetry"
14
+ ],
15
+ "description": "Kubesense Browser Logs SDK — collect and forward browser logs and errors to Kubesense.",
16
+ "license": "Apache-2.0",
17
+ "main": "cjs/entries/main.js",
18
+ "module": "esm/entries/main.mjs",
19
+ "types": "cjs/entries/main.d.ts",
20
+ "files": [
21
+ "bundle/**/*.js",
22
+ "cjs",
23
+ "esm",
24
+ "src",
25
+ "!src/**/*.spec.ts",
26
+ "!src/**/*.specHelper.ts"
27
+ ],
28
+ "scripts": {
29
+ "build": "node ../../scripts/build/build-package.ts --modules --bundle kubesense-logs.js",
30
+ "build:bundle": "node ../../scripts/build/build-package.ts --bundle kubesense-logs.js",
31
+ "prepack": "yarn build"
32
+ },
33
+ "dependencies": {
34
+ "@kubesense/kubesense-browser-core": "2.0.0-beta.1",
35
+ "@kubesense/kubesense-js-core": "2.0.0-beta.1"
36
+ },
37
+ "peerDependencies": {
38
+ "@kubesense/kubesense-browser-rum": "2.0.0-beta.1"
39
+ },
40
+ "peerDependenciesMeta": {
41
+ "@kubesense/kubesense-browser-rum": {
42
+ "optional": true
43
+ }
44
+ },
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "git+https://github.com/kubesense-ai/kubesense-browser-sdk.git",
48
+ "directory": "packages/logs"
49
+ },
50
+ "volta": {
51
+ "extends": "../../package.json"
52
+ },
53
+ "publishConfig": {
54
+ "access": "public"
55
+ }
6
56
  }
@@ -0,0 +1,427 @@
1
+ import type {
2
+ TrackingConsent,
3
+ PublicApi,
4
+ ContextManager,
5
+ Account,
6
+ Context,
7
+ User,
8
+ } from '@kubesense/kubesense-browser-core'
9
+ import {
10
+ ContextManagerMethod,
11
+ CustomerContextKey,
12
+ addTelemetryUsage,
13
+ makePublicApi,
14
+ sanitize,
15
+ displayAlreadyInitializedError,
16
+ createTrackingConsentState,
17
+ defineContextMethod,
18
+ startBufferingData,
19
+ mockable,
20
+ } from '@kubesense/kubesense-browser-core'
21
+ import { monitor, callMonitored } from '@kubesense/kubesense-js-core/monitor'
22
+ import { deepClone } from '@kubesense/kubesense-js-core/util'
23
+ import type { LogsInitConfiguration } from '../domain/configuration'
24
+ import type { HandlerType } from '../domain/logger'
25
+ import type { StatusType } from '../domain/logger/isAuthorized'
26
+ import { Logger } from '../domain/logger'
27
+ import { buildCommonContext } from '../domain/contexts/commonContext'
28
+ import { callPluginsMethod } from '../domain/plugins'
29
+ import type { InternalContext } from '../domain/contexts/internalContext'
30
+ import type { StartLogsResult } from './startLogs'
31
+ import { startLogs } from './startLogs'
32
+ import { createPreStartStrategy } from './preStartLogs'
33
+
34
+ export interface LoggerConfiguration {
35
+ level?: StatusType
36
+ handler?: HandlerType | HandlerType[]
37
+ context?: object
38
+ }
39
+
40
+ /**
41
+ * Public API for the Logs browser SDK.
42
+ *
43
+ * See [Browser Log Collection](https://docs.kubesense.ai/logs/log_collection/javascript) for further information.
44
+ *
45
+ * @category Main
46
+ */
47
+ export interface LogsPublicApi extends PublicApi {
48
+ /**
49
+ * The default logger
50
+ *
51
+ * @category Logger
52
+ */
53
+ logger: Logger
54
+
55
+ /**
56
+ * Init the Logs browser SDK.
57
+ *
58
+ * See [Browser Log Collection](https://docs.kubesense.ai/logs/log_collection/javascript) for further information.
59
+ *
60
+ * @category Init
61
+ * @param initConfiguration - Configuration options of the SDK
62
+ */
63
+ init: (initConfiguration: LogsInitConfiguration) => void
64
+
65
+ /**
66
+ * Set the tracking consent of the current user.
67
+ *
68
+ * Logs will be sent only if it is set to "granted". This value won't be stored by the library
69
+ * across page loads: you will need to call this method or set the appropriate `trackingConsent`
70
+ * field in the init() method at each page load.
71
+ *
72
+ * If this method is called before the init() method, the provided value will take precedence
73
+ * over the one provided as initialization parameter.
74
+ *
75
+ * See [User tracking consent](https://docs.kubesense.ai/logs/log_collection/javascript/#user-tracking-consent) for further information.
76
+ *
77
+ * @category Privacy
78
+ * @param trackingConsent - The user tracking consent
79
+ */
80
+ setTrackingConsent: (trackingConsent: TrackingConsent) => void
81
+
82
+ /**
83
+ * Set the global context information to all events, stored in `@context`
84
+ * See [Global context](https://docs.kubesense.ai/logs/log_collection/javascript/#overwrite-context) for further information.
85
+ *
86
+ * @category Context - Global Context
87
+ * @param context - Global context
88
+ */
89
+ setGlobalContext: (context: any) => void
90
+
91
+ /**
92
+ * Get the global Context
93
+ *
94
+ * See [Global context](https://docs.kubesense.ai/logs/log_collection/javascript/#overwrite-context) for further information.
95
+ *
96
+ * @category Context - Global Context
97
+ */
98
+ getGlobalContext: () => Context
99
+
100
+ /**
101
+ * Set or update a global context property, stored in `@context.<key>`
102
+ *
103
+ * See [Global context](https://docs.kubesense.ai/logs/log_collection/javascript/#overwrite-context) for further information.
104
+ *
105
+ * @category Context - Global Context
106
+ * @param key - Key of the property
107
+ * @param value - Value of the property
108
+ */
109
+ setGlobalContextProperty: (key: any, value: any) => void
110
+
111
+ /**
112
+ * Remove a global context property
113
+ *
114
+ * See [Global context](https://docs.kubesense.ai/logs/log_collection/javascript/#overwrite-context) for further information.
115
+ *
116
+ * @category Context - Global Context
117
+ */
118
+ removeGlobalContextProperty: (key: any) => void
119
+
120
+ /**
121
+ * Clear the global context
122
+ *
123
+ * See [Global context](https://docs.kubesense.ai/logs/log_collection/javascript/#overwrite-context) for further information.
124
+ *
125
+ * @category Context - Global Context
126
+ */
127
+ clearGlobalContext: () => void
128
+
129
+ /**
130
+ * Set user information to all events, stored in `@usr`
131
+ *
132
+ * See [User session](https://docs.kubesense.ai/logs/log_collection/javascript/#user-context) for further information.
133
+ *
134
+ * @category Context - User
135
+ * @param newUser - User information
136
+ */
137
+ setUser(newUser: User & { id: string }): void
138
+
139
+ /**
140
+ * Set user information to all events, stored in `@usr`
141
+ *
142
+ * @category Context - User
143
+ * @deprecated You must specify a user id, favor using {@link setUser} instead
144
+ * @param newUser - User information with optional id
145
+ */
146
+ setUser(newUser: User): void
147
+
148
+ /**
149
+ * Get user information
150
+ *
151
+ * See [User session](https://docs.kubesense.ai/logs/log_collection/javascript/#user-context) for further information.
152
+ *
153
+ * @category Context - User
154
+ * @returns User information
155
+ */
156
+ getUser: () => Context
157
+
158
+ /**
159
+ * Set or update the user property, stored in `@usr.<key>`
160
+ *
161
+ * See [User session](https://docs.kubesense.ai/logs/log_collection/javascript/#user-context) for further information.
162
+ *
163
+ * @category Context - User
164
+ * @param key - Key of the property
165
+ * @param property - Value of the property
166
+ */
167
+ setUserProperty: (key: any, property: any) => void
168
+
169
+ /**
170
+ * Remove a user property
171
+ *
172
+ * @category Context - User
173
+ * @param key - Key of the property to remove
174
+ * @see [User session](https://docs.kubesense.ai/logs/log_collection/javascript/#user-context) for further information.
175
+ */
176
+ removeUserProperty: (key: any) => void
177
+
178
+ /**
179
+ * Clear all user information
180
+ *
181
+ * See [User session](https://docs.kubesense.ai/logs/log_collection/javascript/#user-context) for further information.
182
+ *
183
+ * @category Context - User
184
+ */
185
+ clearUser: () => void
186
+
187
+ /**
188
+ * Set account information to all events, stored in `@account`
189
+ *
190
+ * @category Context - Account
191
+ * @param newAccount - Account information
192
+ */
193
+ setAccount: (newAccount: Account) => void
194
+
195
+ /**
196
+ * Get account information
197
+ *
198
+ * @category Context - Account
199
+ * @returns Account information
200
+ */
201
+ getAccount: () => Context
202
+
203
+ /**
204
+ * Set or update the account property, stored in `@account.<key>`
205
+ *
206
+ * @category Context - Account
207
+ * @param key - Key of the property
208
+ * @param property - Value of the property
209
+ */
210
+ setAccountProperty: (key: string, property: any) => void
211
+
212
+ /**
213
+ * Remove an account property
214
+ *
215
+ * @category Context - Account
216
+ * @param key - Key of the property to remove
217
+ */
218
+ removeAccountProperty: (key: string) => void
219
+
220
+ /**
221
+ * Clear all account information
222
+ *
223
+ * @category Context - Account
224
+ * @returns Clear all account information
225
+ */
226
+ clearAccount: () => void
227
+
228
+ /**
229
+ * The KubeSense browser logs SDK contains a default logger `KUBESENSE_LOGS.logger`, but this API allows to create different ones.
230
+ *
231
+ * See [Define multiple loggers](https://docs.kubesense.ai/logs/log_collection/javascript/#define-multiple-loggers) for further information.
232
+ *
233
+ * @category Logger
234
+ * @param name - Name of the logger
235
+ * @param conf - Configuration of the logger (level, handler, context)
236
+ */
237
+ createLogger: (name: string, conf?: LoggerConfiguration) => Logger
238
+
239
+ /**
240
+ * Get a logger
241
+ *
242
+ * See [Define multiple loggers](https://docs.kubesense.ai/logs/log_collection/javascript/#define-multiple-loggers) for further information.
243
+ *
244
+ * @category Logger
245
+ * @param name - Name of the logger
246
+ */
247
+ getLogger: (name: string) => Logger | undefined
248
+
249
+ /**
250
+ * Get the init configuration
251
+ *
252
+ * @category Init
253
+ * @returns The init configuration
254
+ */
255
+ getInitConfiguration: () => LogsInitConfiguration | undefined
256
+
257
+ /**
258
+ * [Internal API] Get the internal SDK context
259
+ *
260
+ * See [Access internal context](https://docs.kubesense.ai/logs/log_collection/javascript/#access-internal-context) for further information.
261
+ *
262
+ * @internal
263
+ */
264
+ getInternalContext: (startTime?: number) => InternalContext | undefined
265
+ }
266
+
267
+ export interface Strategy {
268
+ init: (initConfiguration: LogsInitConfiguration, publicApi: LogsPublicApi, errorStack?: string) => void
269
+ initConfiguration: LogsInitConfiguration | undefined
270
+ globalContext: ContextManager
271
+ accountContext: ContextManager
272
+ userContext: ContextManager
273
+ getInternalContext: StartLogsResult['getInternalContext']
274
+ handleLog: StartLogsResult['handleLog']
275
+ }
276
+
277
+ export interface LogsPublicApiOptions {
278
+ sdkName?: string
279
+ }
280
+
281
+ export function makeLogsPublicApi(options: LogsPublicApiOptions = {}): LogsPublicApi {
282
+ const trackingConsentState = createTrackingConsentState()
283
+ const bufferedDataObservable = startBufferingData().observable
284
+
285
+ let strategy = createPreStartStrategy(
286
+ buildCommonContext,
287
+ trackingConsentState,
288
+ (configuration, logsSessionManager, hooks) => {
289
+ const startLogsResult = mockable(startLogs)(
290
+ configuration,
291
+ logsSessionManager,
292
+ buildCommonContext,
293
+ bufferedDataObservable,
294
+ hooks
295
+ )
296
+
297
+ strategy = createPostStartStrategy(strategy, startLogsResult)
298
+ callPluginsMethod(configuration.plugins, 'onLogsStart', {
299
+ handleLog: startLogsResult.handleLog,
300
+ })
301
+ return startLogsResult
302
+ },
303
+ options.sdkName
304
+ )
305
+
306
+ const getStrategy = () => strategy
307
+
308
+ const customLoggers: { [name: string]: Logger | undefined } = {}
309
+
310
+ const mainLogger = new Logger((...params) => strategy.handleLog(...params))
311
+
312
+ const logsPublicApi: LogsPublicApi = makePublicApi<LogsPublicApi>({
313
+ logger: mainLogger,
314
+
315
+ init: (initConfiguration) => {
316
+ const errorStack = new Error().stack
317
+ callMonitored(() => strategy.init(initConfiguration, logsPublicApi, errorStack))
318
+ },
319
+
320
+ setTrackingConsent: monitor((trackingConsent) => {
321
+ trackingConsentState.update(trackingConsent)
322
+ addTelemetryUsage({ feature: 'set-tracking-consent', tracking_consent: trackingConsent })
323
+ }),
324
+
325
+ getGlobalContext: defineContextMethod(
326
+ getStrategy,
327
+ CustomerContextKey.globalContext,
328
+ ContextManagerMethod.getContext
329
+ ),
330
+ setGlobalContext: defineContextMethod(
331
+ getStrategy,
332
+ CustomerContextKey.globalContext,
333
+ ContextManagerMethod.setContext
334
+ ),
335
+
336
+ setGlobalContextProperty: defineContextMethod(
337
+ getStrategy,
338
+ CustomerContextKey.globalContext,
339
+ ContextManagerMethod.setContextProperty
340
+ ),
341
+
342
+ removeGlobalContextProperty: defineContextMethod(
343
+ getStrategy,
344
+ CustomerContextKey.globalContext,
345
+ ContextManagerMethod.removeContextProperty
346
+ ),
347
+
348
+ clearGlobalContext: defineContextMethod(
349
+ getStrategy,
350
+ CustomerContextKey.globalContext,
351
+ ContextManagerMethod.clearContext
352
+ ),
353
+
354
+ createLogger: monitor((name, conf = {}) => {
355
+ customLoggers[name] = new Logger(
356
+ (...params) => strategy.handleLog(...params),
357
+ sanitize(name),
358
+ conf.handler,
359
+ conf.level,
360
+ sanitize(conf.context) as object
361
+ )
362
+
363
+ return customLoggers[name]
364
+ }),
365
+
366
+ getLogger: monitor((name) => customLoggers[name]),
367
+
368
+ getInitConfiguration: monitor(() => deepClone(strategy.initConfiguration)),
369
+
370
+ getInternalContext: monitor((startTime) => strategy.getInternalContext(startTime)),
371
+
372
+ setUser: defineContextMethod(getStrategy, CustomerContextKey.userContext, ContextManagerMethod.setContext),
373
+
374
+ getUser: defineContextMethod(getStrategy, CustomerContextKey.userContext, ContextManagerMethod.getContext),
375
+
376
+ setUserProperty: defineContextMethod(
377
+ getStrategy,
378
+ CustomerContextKey.userContext,
379
+ ContextManagerMethod.setContextProperty
380
+ ),
381
+
382
+ removeUserProperty: defineContextMethod(
383
+ getStrategy,
384
+ CustomerContextKey.userContext,
385
+ ContextManagerMethod.removeContextProperty
386
+ ),
387
+
388
+ clearUser: defineContextMethod(getStrategy, CustomerContextKey.userContext, ContextManagerMethod.clearContext),
389
+
390
+ setAccount: defineContextMethod(getStrategy, CustomerContextKey.accountContext, ContextManagerMethod.setContext),
391
+
392
+ getAccount: defineContextMethod(getStrategy, CustomerContextKey.accountContext, ContextManagerMethod.getContext),
393
+
394
+ setAccountProperty: defineContextMethod(
395
+ getStrategy,
396
+ CustomerContextKey.accountContext,
397
+ ContextManagerMethod.setContextProperty
398
+ ),
399
+
400
+ removeAccountProperty: defineContextMethod(
401
+ getStrategy,
402
+ CustomerContextKey.accountContext,
403
+ ContextManagerMethod.removeContextProperty
404
+ ),
405
+
406
+ clearAccount: defineContextMethod(
407
+ getStrategy,
408
+ CustomerContextKey.accountContext,
409
+ ContextManagerMethod.clearContext
410
+ ),
411
+ })
412
+
413
+ return logsPublicApi
414
+ }
415
+
416
+ function createPostStartStrategy(preStartStrategy: Strategy, startLogsResult: StartLogsResult): Strategy {
417
+ return {
418
+ ...preStartStrategy,
419
+ init: (initConfiguration: LogsInitConfiguration) => {
420
+ displayAlreadyInitializedError('KUBESENSE_LOGS', initConfiguration)
421
+ },
422
+ getInternalContext: startLogsResult.getInternalContext,
423
+ globalContext: startLogsResult.globalContext,
424
+ userContext: startLogsResult.userContext,
425
+ accountContext: startLogsResult.accountContext,
426
+ }
427
+ }
@@ -0,0 +1,192 @@
1
+ import { timeStampNow } from '@kubesense/kubesense-js-core/time'
2
+ import { monitorError } from '@kubesense/kubesense-js-core/monitor'
3
+ import type { TrackingConsentState, SessionManager } from '@kubesense/kubesense-browser-core'
4
+ import {
5
+ BufferedObservable,
6
+ canUseEventBridge,
7
+ display,
8
+ displayAlreadyInitializedError,
9
+ initFeatureFlags,
10
+ noop,
11
+ buildAccountContextManager,
12
+ CustomerContextKey,
13
+ bufferContextCalls,
14
+ addTelemetryConfiguration,
15
+ addTelemetryDebug,
16
+ buildGlobalContextManager,
17
+ buildUserContextManager,
18
+ startSessionManager,
19
+ startSessionManagerStub,
20
+ startTelemetry,
21
+ TelemetryService,
22
+ mockable,
23
+ startTelemetrySessionContext,
24
+ setAllowUntrustedEvents,
25
+ isAllowedTrackingOrigins,
26
+ } from '@kubesense/kubesense-browser-core'
27
+ import type { Hooks } from '../domain/hooks'
28
+ import { createHooks } from '../domain/hooks'
29
+ import type { LogsConfiguration, LogsInitConfiguration } from '../domain/configuration'
30
+ import { serializeLogsConfiguration, validateAndBuildLogsConfiguration } from '../domain/configuration'
31
+ import { applyCachedLogsSdkConfiguration, scheduleLogsSdkConfigurationRefresh } from '../domain/remoteSdkConfiguration'
32
+ import type { CommonContext } from '../rawLogsEvent.types'
33
+ import { startTrackingConsentContext } from '../domain/contexts/trackingConsentContext'
34
+ import { callPluginsMethod } from '../domain/plugins'
35
+ import type { Strategy } from './logsPublicApi'
36
+ import type { StartLogsResult } from './startLogs'
37
+
38
+ export type DoStartLogs = (
39
+ configuration: LogsConfiguration,
40
+ sessionManager: SessionManager,
41
+ hooks: Hooks
42
+ ) => StartLogsResult
43
+
44
+ export function createPreStartStrategy(
45
+ getCommonContext: () => CommonContext,
46
+ trackingConsentState: TrackingConsentState,
47
+ doStartLogs: DoStartLogs,
48
+ sdkName?: string
49
+ ): Strategy {
50
+ const BUFFER_LIMIT = 500
51
+ const bufferApiCalls = new BufferedObservable<(startLogsResult: StartLogsResult) => void>(BUFFER_LIMIT, (count) => {
52
+ // monitor-until: 2026-10-14
53
+ addTelemetryDebug('preStartLogs buffer data lost', { count })
54
+ })
55
+
56
+ // TODO next major: remove the globalContext, accountContextManager, userContext from preStartStrategy and use an empty context instead
57
+ const globalContext = buildGlobalContextManager()
58
+ bufferContextCalls(globalContext, CustomerContextKey.globalContext, bufferApiCalls)
59
+
60
+ const accountContext = buildAccountContextManager()
61
+ bufferContextCalls(accountContext, CustomerContextKey.accountContext, bufferApiCalls)
62
+
63
+ const userContext = buildUserContextManager()
64
+ bufferContextCalls(userContext, CustomerContextKey.userContext, bufferApiCalls)
65
+
66
+ let cachedInitConfiguration: LogsInitConfiguration | undefined
67
+ let cachedConfiguration: LogsConfiguration | undefined
68
+ let sessionManager: SessionManager | undefined
69
+ const hooks = createHooks()
70
+ const trackingConsentStateSubscription = trackingConsentState.observable.subscribe(tryStartLogs)
71
+
72
+ function tryStartLogs() {
73
+ if (!cachedConfiguration || !cachedInitConfiguration || !sessionManager) {
74
+ return
75
+ }
76
+
77
+ trackingConsentStateSubscription.unsubscribe()
78
+ const startLogsResult = doStartLogs(cachedConfiguration, sessionManager, hooks)
79
+
80
+ bufferApiCalls.subscribe((callback) => callback(startLogsResult))
81
+ bufferApiCalls.unbuffer()
82
+ }
83
+
84
+ return {
85
+ init(initConfiguration, publicApi, errorStack) {
86
+ if (!initConfiguration) {
87
+ display.error('Missing configuration')
88
+ return
89
+ }
90
+ // Set the experimental feature flags as early as possible, so we can use them in most places
91
+ initFeatureFlags(initConfiguration.enableExperimentalFeatures)
92
+ setAllowUntrustedEvents(initConfiguration.allowUntrustedEvents)
93
+
94
+ if (canUseEventBridge()) {
95
+ initConfiguration = overrideInitConfigurationForBridge(initConfiguration)
96
+ }
97
+
98
+ const remoteSdkConfigurationEnabled = !!initConfiguration.enableRemoteSdkConfiguration
99
+
100
+ // A cached document that stops Logs from starting must never be a one-way door: without a
101
+ // refresh on those paths the browser replays the same bad document on every load and can
102
+ // never learn about a corrected one.
103
+ const refreshRemoteSdkConfiguration = () => {
104
+ if (remoteSdkConfigurationEnabled) {
105
+ scheduleLogsSdkConfigurationRefresh(initConfiguration).catch(noop)
106
+ }
107
+ }
108
+
109
+ if (remoteSdkConfigurationEnabled) {
110
+ // Reading the cached document is synchronous and network-free, so it does not delay
111
+ // initialization. The document fetched below only applies from the next page load.
112
+ const cachedSdkConfiguration = applyCachedLogsSdkConfiguration(initConfiguration)
113
+ initConfiguration = cachedSdkConfiguration.initConfiguration
114
+
115
+ if (!cachedSdkConfiguration.logsEnabled) {
116
+ // Logs is switched off from the dashboard. Still refresh the document, otherwise the
117
+ // switch would be one-way: the SDK could never learn it has been turned back on.
118
+ cachedInitConfiguration = initConfiguration
119
+ refreshRemoteSdkConfiguration()
120
+ return
121
+ }
122
+ }
123
+
124
+ // Expose the initial configuration regardless of initialization success.
125
+ cachedInitConfiguration = initConfiguration
126
+
127
+ if (cachedConfiguration) {
128
+ displayAlreadyInitializedError('KUBESENSE_LOGS', initConfiguration)
129
+ return
130
+ }
131
+
132
+ callPluginsMethod(initConfiguration.plugins, 'onInit', {
133
+ initConfiguration,
134
+ publicApi,
135
+ registerAssembleEventHook: hooks.assembleEvent.register,
136
+ })
137
+
138
+ const configuration = validateAndBuildLogsConfiguration(initConfiguration)
139
+ if (!configuration || !isAllowedTrackingOrigins(configuration, errorStack ?? '')) {
140
+ refreshRemoteSdkConfiguration()
141
+ return
142
+ }
143
+
144
+ cachedConfiguration = configuration
145
+
146
+ trackingConsentState.tryToInit(configuration.trackingConsent)
147
+
148
+ trackingConsentState.onGrantedOnce(() => {
149
+ startTrackingConsentContext(hooks, trackingConsentState)
150
+ mockable(startTelemetry)(TelemetryService.LOGS, configuration, hooks.assembleTelemetry, sdkName)
151
+ const sessionManagerPromise = canUseEventBridge()
152
+ ? startSessionManagerStub()
153
+ : mockable(startSessionManager)(configuration, trackingConsentState)
154
+
155
+ void sessionManagerPromise
156
+ .then((newSessionManager) => {
157
+ if (!newSessionManager) {
158
+ return
159
+ }
160
+ sessionManager = newSessionManager
161
+ startTelemetrySessionContext(hooks.assembleTelemetry, sessionManager)
162
+ addTelemetryConfiguration(serializeLogsConfiguration(initConfiguration))
163
+ tryStartLogs()
164
+
165
+ // Fire-and-forget, after the SDK is running: refreshes the cache for the next page load.
166
+ refreshRemoteSdkConfiguration()
167
+ })
168
+ .catch(monitorError)
169
+ })
170
+ },
171
+
172
+ get initConfiguration() {
173
+ return cachedInitConfiguration
174
+ },
175
+
176
+ globalContext,
177
+ accountContext,
178
+ userContext,
179
+
180
+ getInternalContext: noop as () => undefined,
181
+
182
+ handleLog(message, statusType, handlingStack, context = getCommonContext(), date = timeStampNow()) {
183
+ bufferApiCalls.notify((startLogsResult) =>
184
+ startLogsResult.handleLog(message, statusType, handlingStack, context, date)
185
+ )
186
+ },
187
+ }
188
+ }
189
+
190
+ function overrideInitConfigurationForBridge(initConfiguration: LogsInitConfiguration): LogsInitConfiguration {
191
+ return { ...initConfiguration, clientToken: 'empty' }
192
+ }