@crimson-education/browser-logger 2.0.2-cognito.1 → 2.0.2
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 +137 -4
- package/lib/index.d.ts +69 -13
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +130 -57
- package/lib/index.js.map +1 -1
- package/lib/reporters/amplifyReporter.d.ts +40 -14
- package/lib/reporters/amplifyReporter.d.ts.map +1 -1
- package/lib/reporters/amplifyReporter.js +4 -11
- package/lib/reporters/amplifyReporter.js.map +1 -1
- package/lib/reporters/amplifyReporter.test.js +0 -11
- package/lib/reporters/amplifyReporter.test.js.map +1 -1
- package/lib/reporters/datadogReporter.d.ts +58 -10
- package/lib/reporters/datadogReporter.d.ts.map +1 -1
- package/lib/reporters/datadogReporter.js +26 -18
- package/lib/reporters/datadogReporter.js.map +1 -1
- package/lib/reporters/gtmReporter.d.ts +3 -2
- package/lib/reporters/gtmReporter.d.ts.map +1 -1
- package/lib/reporters/gtmReporter.js +24 -5
- package/lib/reporters/gtmReporter.js.map +1 -1
- package/lib/reporters/index.d.ts +3 -28
- package/lib/reporters/index.d.ts.map +1 -1
- package/lib/reporters/index.js +17 -0
- package/lib/reporters/index.js.map +1 -1
- package/lib/types/index.d.ts +3 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +19 -0
- package/lib/types/index.js.map +1 -0
- package/lib/{types.d.ts → types/logger.d.ts} +4 -15
- package/lib/types/logger.d.ts.map +1 -0
- package/lib/{types.js → types/logger.js} +1 -1
- package/lib/types/logger.js.map +1 -0
- package/lib/types/reporter.d.ts +103 -0
- package/lib/types/reporter.d.ts.map +1 -0
- package/lib/types/reporter.js +3 -0
- package/lib/types/reporter.js.map +1 -0
- package/lib/utils.d.ts +5 -1
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +8 -3
- package/lib/utils.js.map +1 -1
- package/lib/utils.test.d.ts +2 -0
- package/lib/utils.test.d.ts.map +1 -0
- package/lib/utils.test.js +19 -0
- package/lib/utils.test.js.map +1 -0
- package/package.json +9 -6
- package/src/index.ts +148 -57
- package/src/reporters/amplifyReporter.test.ts +1 -14
- package/src/reporters/amplifyReporter.ts +53 -23
- package/src/reporters/datadogReporter.ts +93 -17
- package/src/reporters/gtmReporter.ts +39 -7
- package/src/reporters/index.ts +3 -32
- package/src/types/index.ts +2 -0
- package/src/{types.ts → types/logger.ts} +3 -13
- package/src/types/reporter.ts +107 -0
- package/src/utils.test.ts +18 -0
- package/src/utils.ts +16 -1
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js.map +0 -1
|
@@ -1,26 +1,67 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
IReporter,
|
|
3
|
+
Metadata,
|
|
4
|
+
ReporterBreadcrumb,
|
|
5
|
+
ReporterConfigBase,
|
|
6
|
+
ReporterEvent,
|
|
7
|
+
ReportUser,
|
|
8
|
+
ServiceInfo,
|
|
9
|
+
} from '../types';
|
|
3
10
|
import { Auth } from '@aws-amplify/auth';
|
|
4
11
|
import { Analytics } from '@aws-amplify/analytics';
|
|
12
|
+
import { AttributeMap, filterAttributeMap } from '../utils';
|
|
5
13
|
|
|
6
|
-
export
|
|
14
|
+
export interface AmplifyReporterConfig extends ReporterConfigBase {
|
|
15
|
+
/**
|
|
16
|
+
* AWS Region for Amplify.
|
|
17
|
+
*/
|
|
7
18
|
region: string;
|
|
8
19
|
/**
|
|
9
|
-
* The Identity Pool
|
|
20
|
+
* The Identity Pool Id to use for reporting, if set to false, Auth.configure is not called.
|
|
10
21
|
* This must be called manually for the reporter to work.
|
|
11
22
|
*/
|
|
12
23
|
identityPoolId: string | false;
|
|
24
|
+
/**
|
|
25
|
+
* The Pinpoint App Id to report to.
|
|
26
|
+
*/
|
|
13
27
|
analyticsAppId: string;
|
|
28
|
+
/**
|
|
29
|
+
* The Cognito User Pool to configure in Auth.configure.
|
|
30
|
+
* If you are using Cognito, it is better to set identityPoolId to false and configure Auth manually.
|
|
31
|
+
*/
|
|
32
|
+
userPoolId?: string;
|
|
33
|
+
/**
|
|
34
|
+
* The Cognito Web Client Id to configure in Auth.configure.
|
|
35
|
+
* If you are using Cognito, it is better to set identityPoolId to false and configure Auth manually.
|
|
36
|
+
*/
|
|
37
|
+
userPoolWebClientId?: string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* If you want to track which page/url in your webapp is the most frequently viewed one, you can use this feature.
|
|
41
|
+
* It will automatically send events containing url information when the page is visited.
|
|
42
|
+
*/
|
|
14
43
|
autoTrackPageViews?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* If you want to track user interactions with elements on the page, you can use this feature.
|
|
46
|
+
* All you need to do is attach the specified selectors to your dom element and turn on the auto tracking.
|
|
47
|
+
*/
|
|
15
48
|
autoTrackEvents?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* A web session can be defined in different ways.
|
|
51
|
+
* To keep it simple we define that the web session is active when the page is not hidden and inactive when the page is hidden.
|
|
52
|
+
*/
|
|
16
53
|
autoTrackSessions?: boolean;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The data tag prefix to use for attributing HTML elements. Defaults to data-analytics-
|
|
57
|
+
*/
|
|
17
58
|
selectorPrefix?: string;
|
|
18
|
-
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Modify how the reporter sends events to Amplify.
|
|
62
|
+
*/
|
|
19
63
|
buffering?: AmplifyReporterBufferingConfig;
|
|
20
|
-
|
|
21
|
-
userPoolWebClientId?: string;
|
|
22
|
-
ignoreMetadataPatterns?: RegExp[];
|
|
23
|
-
};
|
|
64
|
+
}
|
|
24
65
|
|
|
25
66
|
/**
|
|
26
67
|
* Configuration options for the buffering behavior of Pinpoint's event tracker.
|
|
@@ -52,7 +93,7 @@ export function amplifyReporter(info: ServiceInfo, config: AmplifyReporterConfig
|
|
|
52
93
|
appName: info.service,
|
|
53
94
|
service: info.service,
|
|
54
95
|
domain: window.location.host,
|
|
55
|
-
environment: info.
|
|
96
|
+
environment: info.environment,
|
|
56
97
|
version: info.version,
|
|
57
98
|
});
|
|
58
99
|
|
|
@@ -139,24 +180,19 @@ export function amplifyReporter(info: ServiceInfo, config: AmplifyReporterConfig
|
|
|
139
180
|
setPageName: function (pageName: string): void {
|
|
140
181
|
reporter.addMetadata({ pageName });
|
|
141
182
|
},
|
|
142
|
-
reportError: function (): void {},
|
|
143
|
-
recordSession: function (): void {},
|
|
144
|
-
recordSessionStop: function (): void {},
|
|
145
183
|
};
|
|
146
184
|
|
|
147
185
|
return reporter;
|
|
148
186
|
}
|
|
149
187
|
|
|
150
|
-
type AttributeMap = Record<string, string[] | string | null>;
|
|
151
|
-
|
|
152
188
|
/**
|
|
153
189
|
* Pinpoint has strict attribute name and value length limits
|
|
154
190
|
*/
|
|
155
191
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
156
192
|
export function asAttributeMap(
|
|
157
|
-
values: Record<string,
|
|
193
|
+
values: Record<string, unknown>,
|
|
158
194
|
groupValues = true,
|
|
159
|
-
ignorePatterns: RegExp[] = [],
|
|
195
|
+
ignorePatterns: (string | RegExp)[] = [],
|
|
160
196
|
): AttributeMap {
|
|
161
197
|
const attributeMap = buildAttributeMap(values, undefined, groupValues);
|
|
162
198
|
const filteredAttributeMap = filterAttributeMap(attributeMap, ignorePatterns);
|
|
@@ -206,9 +242,3 @@ export function buildAttributeMap(
|
|
|
206
242
|
|
|
207
243
|
return valuesWithStringArrays;
|
|
208
244
|
}
|
|
209
|
-
|
|
210
|
-
export function filterAttributeMap(attributes: AttributeMap | Record<string, string>, ignorePatterns: RegExp[]) {
|
|
211
|
-
const entries = Object.entries(attributes);
|
|
212
|
-
|
|
213
|
-
return Object.fromEntries(entries.filter(([key]) => !ignorePatterns.some((pattern) => pattern.test(key))));
|
|
214
|
-
}
|
|
@@ -1,28 +1,91 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
IReporter,
|
|
4
|
+
ReporterBreadcrumb,
|
|
5
|
+
ReporterEvent,
|
|
6
|
+
Metadata,
|
|
7
|
+
Profiler,
|
|
8
|
+
ReportError,
|
|
9
|
+
ReportUser,
|
|
10
|
+
ServiceInfo,
|
|
11
|
+
ReporterConfigBase,
|
|
12
|
+
} from '../types';
|
|
4
13
|
import { datadogLogs, HandlerType, StatusType } from '@datadog/browser-logs';
|
|
5
14
|
import { datadogRum, DefaultPrivacyLevel } from '@datadog/browser-rum';
|
|
6
15
|
import { ILogger, LogLevel } from '..';
|
|
16
|
+
import { filterAttributeMap } from '../utils';
|
|
7
17
|
|
|
8
|
-
export
|
|
18
|
+
export interface DatadogReporterConfig extends ReporterConfigBase {
|
|
19
|
+
/** The RUM application ID. */
|
|
9
20
|
applicationId: string;
|
|
21
|
+
/** A Datadog client token (Generated in the RUM Page) */
|
|
10
22
|
clientToken: string;
|
|
23
|
+
/** The Datadog site parameter of your organization. */
|
|
24
|
+
site: string;
|
|
25
|
+
/** The Application's version (Overrides the browser logger version) */
|
|
11
26
|
version?: string;
|
|
12
|
-
|
|
27
|
+
/** Optional proxy URL */
|
|
13
28
|
proxyUrl?: string;
|
|
14
|
-
|
|
15
|
-
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Controls the percentage of overall sessions being tracked. It defaults to 100, so every session is tracked by default.
|
|
32
|
+
*/
|
|
16
33
|
sampleRate?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Applied after the overall sample rate, and controls the percentage of sessions tracked as Browser RUM & Session Replay.
|
|
36
|
+
* It defaults to 100, so every session is tracked as Browser RUM & Session Replay by default.
|
|
37
|
+
*/
|
|
17
38
|
replaySampleRate?: number;
|
|
18
|
-
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Use a secure session cookie. This disables RUM events sent on insecure (non-HTTPS) connections.
|
|
42
|
+
*/
|
|
19
43
|
useSecureSessionCookie?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Use a secure cross-site session cookie.
|
|
46
|
+
* This allows the RUM Browser SDK to run when the site is loaded from another one (iframe). Implies `useSecureSessionCookie`
|
|
47
|
+
*/
|
|
20
48
|
useCrossSiteSessionCookie?: boolean;
|
|
21
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Preserve the session across subdomains for the same site.
|
|
51
|
+
*/
|
|
22
52
|
trackSessionAcrossSubdomains?: boolean;
|
|
23
|
-
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Forward console.error logs, uncaught exceptions and network errors to Datadog.
|
|
56
|
+
* Defaults to false.
|
|
57
|
+
*/
|
|
58
|
+
forwardConsoleLogs?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Enables automatic collection of users actions.
|
|
61
|
+
*/
|
|
62
|
+
trackInteractions?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Enables automatic collection of user frustrations. Implies `trackInteractions`: true.
|
|
65
|
+
*/
|
|
66
|
+
trackFrustrations?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Allows you to control RUM views creation.
|
|
69
|
+
* You will need to call `Logger.setPageName()` to create a new view.
|
|
70
|
+
*/
|
|
24
71
|
trackViewsManually?: boolean;
|
|
25
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Specify your own attribute to be used to name actions.
|
|
74
|
+
* Defaults to `data-analytics-name` to coincide with analytics attributes used across reporters.
|
|
75
|
+
*/
|
|
76
|
+
actionNameAttribute?: string;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* By enabling Session Replay, you can automatically mask sensitive elements from being recorded through the RUM Browser SDK.
|
|
80
|
+
*
|
|
81
|
+
* See https://docs.datadoghq.com/real_user_monitoring/session_replay/privacy_options
|
|
82
|
+
*/
|
|
83
|
+
defaultPrivacyLevel?: DefaultPrivacyLevel;
|
|
84
|
+
/**
|
|
85
|
+
* A list of request origins used to inject tracing headers, to be able to connect RUM and backend tracing.
|
|
86
|
+
*/
|
|
87
|
+
allowedTracingOrigins?: (string | RegExp)[];
|
|
88
|
+
}
|
|
26
89
|
|
|
27
90
|
function getDatadogStatusType(level: LogLevel): StatusType {
|
|
28
91
|
switch (level) {
|
|
@@ -52,6 +115,8 @@ export function datadogReporter(info: ServiceInfo, config: DatadogReporterConfig
|
|
|
52
115
|
env: info.environment,
|
|
53
116
|
version: config.version ?? info.version,
|
|
54
117
|
|
|
118
|
+
sampleRate: config.sampleRate ?? 100,
|
|
119
|
+
|
|
55
120
|
useSecureSessionCookie: config.useSecureSessionCookie ?? !isLocalhost,
|
|
56
121
|
useCrossSiteSessionCookie: config.useCrossSiteSessionCookie ?? false,
|
|
57
122
|
trackSessionAcrossSubdomains: config.trackSessionAcrossSubdomains,
|
|
@@ -68,26 +133,29 @@ export function datadogReporter(info: ServiceInfo, config: DatadogReporterConfig
|
|
|
68
133
|
env: info.environment,
|
|
69
134
|
version: config.version ?? info.version,
|
|
70
135
|
|
|
136
|
+
sampleRate: config.sampleRate ?? 100,
|
|
137
|
+
replaySampleRate: config.replaySampleRate ?? 100,
|
|
138
|
+
|
|
71
139
|
useSecureSessionCookie: config.useSecureSessionCookie ?? !isLocalhost,
|
|
72
140
|
useCrossSiteSessionCookie: config.useCrossSiteSessionCookie ?? false,
|
|
73
141
|
trackSessionAcrossSubdomains: config.trackSessionAcrossSubdomains,
|
|
74
142
|
|
|
75
|
-
// Set sampleRate to 100 to capture 100%
|
|
76
|
-
// of transactions for performance monitoring.
|
|
77
|
-
sampleRate: config.sampleRate ?? 100,
|
|
78
|
-
replaySampleRate: config.replaySampleRate ?? 100,
|
|
79
143
|
trackInteractions: config.trackInteractions ?? false,
|
|
144
|
+
trackFrustrations: config.trackFrustrations ?? false,
|
|
145
|
+
trackViewsManually: config.trackViewsManually ?? false,
|
|
146
|
+
actionNameAttribute: config.actionNameAttribute ?? 'data-analytics-name',
|
|
147
|
+
|
|
80
148
|
defaultPrivacyLevel: config.defaultPrivacyLevel ?? 'mask-user-input',
|
|
81
149
|
allowedTracingOrigins: config.allowedTracingOrigins,
|
|
82
|
-
actionNameAttribute: config.actionNameAttribute ?? 'data-analytics-name',
|
|
83
|
-
trackViewsManually: config.trackViewsManually ?? false,
|
|
84
150
|
});
|
|
85
151
|
|
|
152
|
+
const ignoreMetadataPatterns = config.ignoreMetadataPatterns ?? [];
|
|
153
|
+
|
|
86
154
|
const reporter: IReporter = {
|
|
87
155
|
trackEvent: function (event: ReporterEvent): void {
|
|
88
156
|
const context = {
|
|
89
157
|
level: event.level,
|
|
90
|
-
...event.metadata,
|
|
158
|
+
...filterAttributeMap(event.metadata ?? {}, ignoreMetadataPatterns),
|
|
91
159
|
...event.tags,
|
|
92
160
|
...event.metrics,
|
|
93
161
|
};
|
|
@@ -95,6 +163,10 @@ export function datadogReporter(info: ServiceInfo, config: DatadogReporterConfig
|
|
|
95
163
|
datadogLogs.logger?.log(event.message, context, getDatadogStatusType(event.level ?? LogLevel.Info));
|
|
96
164
|
},
|
|
97
165
|
addBreadcrumb: function (breadcrumb: ReporterBreadcrumb): void {
|
|
166
|
+
if (breadcrumb.category && config.ignoreBreadcrumbCategories?.includes(breadcrumb.category)) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
98
170
|
const context = {
|
|
99
171
|
...breadcrumb.metadata,
|
|
100
172
|
category: breadcrumb.category,
|
|
@@ -102,6 +174,8 @@ export function datadogReporter(info: ServiceInfo, config: DatadogReporterConfig
|
|
|
102
174
|
datadogRum.addAction(breadcrumb.message, context);
|
|
103
175
|
},
|
|
104
176
|
addMetadata: function (metadata: Metadata): void {
|
|
177
|
+
metadata = filterAttributeMap(metadata, ignoreMetadataPatterns);
|
|
178
|
+
|
|
105
179
|
for (const [key, value] of Object.entries(metadata)) {
|
|
106
180
|
if (value !== null) {
|
|
107
181
|
datadogRum.addRumGlobalContext(key, value);
|
|
@@ -142,6 +216,8 @@ export function datadogReporter(info: ServiceInfo, config: DatadogReporterConfig
|
|
|
142
216
|
}
|
|
143
217
|
},
|
|
144
218
|
reportError: function (error: ReportError, metadata?: Metadata): void {
|
|
219
|
+
metadata = filterAttributeMap(metadata ?? {}, ignoreMetadataPatterns);
|
|
220
|
+
|
|
145
221
|
// Note, datadog should pick up the console error above
|
|
146
222
|
datadogRum.addError(error, metadata);
|
|
147
223
|
},
|
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
IReporter,
|
|
4
|
+
ReporterBreadcrumb,
|
|
5
|
+
ReporterEvent,
|
|
6
|
+
Metadata,
|
|
7
|
+
ReportUser,
|
|
8
|
+
ServiceInfo,
|
|
9
|
+
ReporterConfigBase,
|
|
10
|
+
} from '../types';
|
|
11
|
+
import { filterAttributeMap } from '../utils';
|
|
12
|
+
|
|
13
|
+
export type GTMReporterConfig = ReporterConfigBase;
|
|
4
14
|
|
|
5
15
|
declare global {
|
|
6
16
|
interface Window {
|
|
@@ -8,10 +18,26 @@ declare global {
|
|
|
8
18
|
}
|
|
9
19
|
}
|
|
10
20
|
|
|
11
|
-
export function gtmReporter(): IReporter {
|
|
21
|
+
export function gtmReporter(info: ServiceInfo, config: GTMReporterConfig): IReporter {
|
|
22
|
+
let loadedDataLayer = !!window.dataLayer;
|
|
23
|
+
const baseMetadata = {
|
|
24
|
+
service: info.service,
|
|
25
|
+
domain: window.location.host,
|
|
26
|
+
environment: info.environment,
|
|
27
|
+
version: info.version,
|
|
28
|
+
};
|
|
29
|
+
|
|
12
30
|
const reporter: IReporter = {
|
|
13
31
|
addMetadata: function (metadata: Metadata): void {
|
|
14
|
-
window.dataLayer
|
|
32
|
+
if (window.dataLayer) {
|
|
33
|
+
// Lazy load base metadata, e.g. if GTM isn't ready yet.
|
|
34
|
+
if (!loadedDataLayer) {
|
|
35
|
+
window.dataLayer.push(baseMetadata);
|
|
36
|
+
loadedDataLayer = true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
window.dataLayer.push(filterAttributeMap(metadata, config.ignoreMetadataPatterns ?? []));
|
|
40
|
+
}
|
|
15
41
|
},
|
|
16
42
|
trackEvent: function (event: ReporterEvent): void {
|
|
17
43
|
reporter.addMetadata({
|
|
@@ -23,6 +49,10 @@ export function gtmReporter(): IReporter {
|
|
|
23
49
|
});
|
|
24
50
|
},
|
|
25
51
|
addBreadcrumb: function (breadcrumb: ReporterBreadcrumb): void {
|
|
52
|
+
if (config.ignoreBreadcrumbCategories?.includes(breadcrumb.category ?? '')) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
26
56
|
reporter.addMetadata({
|
|
27
57
|
...breadcrumb.metadata,
|
|
28
58
|
event: breadcrumb.message,
|
|
@@ -39,9 +69,11 @@ export function gtmReporter(): IReporter {
|
|
|
39
69
|
setPageName: function (pageName: string): void {
|
|
40
70
|
reporter.addMetadata({ pageName });
|
|
41
71
|
},
|
|
42
|
-
reportError: function (): void {},
|
|
43
|
-
recordSession: function (): void {},
|
|
44
|
-
recordSessionStop: function (): void {},
|
|
45
72
|
};
|
|
73
|
+
|
|
74
|
+
if (loadedDataLayer) {
|
|
75
|
+
reporter.addMetadata(baseMetadata);
|
|
76
|
+
}
|
|
77
|
+
|
|
46
78
|
return reporter;
|
|
47
79
|
}
|
package/src/reporters/index.ts
CHANGED
|
@@ -1,32 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export interface ReporterEvent {
|
|
5
|
-
level?: LogLevel;
|
|
6
|
-
message: string;
|
|
7
|
-
metadata?: Metadata | undefined;
|
|
8
|
-
tags?: Metadata | undefined;
|
|
9
|
-
metrics?: Metrics | undefined;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface TrackedReporterEvent extends ReporterEvent {
|
|
13
|
-
occurred: Date;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface ReporterBreadcrumb {
|
|
17
|
-
message: string;
|
|
18
|
-
category?: string | undefined;
|
|
19
|
-
metadata?: Metadata | undefined;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface IReporter {
|
|
23
|
-
trackEvent(event: ReporterEvent): void;
|
|
24
|
-
addBreadcrumb(breadcrumb: ReporterBreadcrumb): void;
|
|
25
|
-
addMetadata(metadata: Metadata): void;
|
|
26
|
-
setUser(user: ReportUser | null): void;
|
|
27
|
-
setRouteName(routeName: string): void;
|
|
28
|
-
setPageName(pageName: string): void;
|
|
29
|
-
reportError(error: ReportError, metadata?: Metadata): void;
|
|
30
|
-
recordSession(): void;
|
|
31
|
-
recordSessionStop(): void;
|
|
32
|
-
}
|
|
1
|
+
export * from './amplifyReporter';
|
|
2
|
+
export * from './datadogReporter';
|
|
3
|
+
export * from './gtmReporter';
|
|
@@ -1,19 +1,6 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
export type Metadata = Record<string, any>;
|
|
3
3
|
|
|
4
|
-
export type Metrics = Record<string, number>;
|
|
5
|
-
|
|
6
|
-
export type ServiceInfo = {
|
|
7
|
-
service: string;
|
|
8
|
-
environment: string;
|
|
9
|
-
version: string;
|
|
10
|
-
defaultMetadata?: Metadata;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type ReportUser = { id: string; email?: string; username?: string; name?: string };
|
|
14
|
-
|
|
15
|
-
export type ReportError = Error | string;
|
|
16
|
-
|
|
17
4
|
export enum LogLevel {
|
|
18
5
|
Debug = 'debug',
|
|
19
6
|
Info = 'info',
|
|
@@ -38,6 +25,9 @@ export interface Profiler {
|
|
|
38
25
|
done(info?: { message?: string; level?: LogLevel }): boolean;
|
|
39
26
|
}
|
|
40
27
|
|
|
28
|
+
/**
|
|
29
|
+
* A Logger is a service that can receive log messages.
|
|
30
|
+
*/
|
|
41
31
|
export interface ILogger {
|
|
42
32
|
startTimer(): Profiler;
|
|
43
33
|
child(metadata?: Metadata, name?: string): ILogger;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { ReporterType } from '..';
|
|
2
|
+
import { LogLevel, Metadata } from './logger';
|
|
3
|
+
|
|
4
|
+
export type Metrics = Record<string, number>;
|
|
5
|
+
|
|
6
|
+
export type ServiceInfo = {
|
|
7
|
+
/** The Application's name. */
|
|
8
|
+
service: string;
|
|
9
|
+
/** The Application's Environment */
|
|
10
|
+
environment: string;
|
|
11
|
+
/** The Application's Version. */
|
|
12
|
+
version: string;
|
|
13
|
+
/**
|
|
14
|
+
* Default metadata to add to Reporter events.
|
|
15
|
+
*/
|
|
16
|
+
defaultMetadata?: Metadata;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type ReportUser = { id: string; email?: string; username?: string; name?: string };
|
|
20
|
+
|
|
21
|
+
export type ReportError = Error | string;
|
|
22
|
+
|
|
23
|
+
export interface ReporterFilters {
|
|
24
|
+
/**
|
|
25
|
+
* Send only to these Reporters.
|
|
26
|
+
*/
|
|
27
|
+
toReporters?: ReporterType[];
|
|
28
|
+
/**
|
|
29
|
+
* Don't send to these Reporters.
|
|
30
|
+
*/
|
|
31
|
+
excludeReporters?: ReporterType[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface ReporterEvent extends ReporterFilters {
|
|
35
|
+
/**
|
|
36
|
+
* The Severity of this event.
|
|
37
|
+
*/
|
|
38
|
+
level?: LogLevel;
|
|
39
|
+
/**
|
|
40
|
+
* The event message/name.
|
|
41
|
+
*/
|
|
42
|
+
message: string;
|
|
43
|
+
/**
|
|
44
|
+
* Event metadata.
|
|
45
|
+
*/
|
|
46
|
+
metadata?: Metadata | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Event tags, if supported by the Reporter. (Otherwise, they will be added to metadata)
|
|
49
|
+
*/
|
|
50
|
+
tags?: Metadata | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Event metrics, if supported by the Reporter. (Otherwise, they will be added to metadata)
|
|
53
|
+
* Metrics are numbers, and are used for aggregations.
|
|
54
|
+
*/
|
|
55
|
+
metrics?: Metrics | undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface TrackedReporterEvent extends ReporterEvent {
|
|
59
|
+
/**
|
|
60
|
+
* The time this event was created.
|
|
61
|
+
*/
|
|
62
|
+
occurred: Date;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface ReporterBreadcrumb extends ReporterFilters {
|
|
66
|
+
/**
|
|
67
|
+
* The breadcrumb event message/name.
|
|
68
|
+
*/
|
|
69
|
+
message: string;
|
|
70
|
+
/**
|
|
71
|
+
* The category of this breadcrumb (Useful for filtering out in specific reporters).
|
|
72
|
+
*/
|
|
73
|
+
category?: string | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* Breadcrumb metadata.
|
|
76
|
+
*/
|
|
77
|
+
metadata?: Metadata | undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface ReporterConfigBase {
|
|
81
|
+
/**
|
|
82
|
+
* Categories of breadcrumbs to ignore sending to Amplify.
|
|
83
|
+
* This is useful to prevent data of no use for amplify.
|
|
84
|
+
*/
|
|
85
|
+
ignoreBreadcrumbCategories?: string[];
|
|
86
|
+
/**
|
|
87
|
+
* Ignore specific metadata keys from being sent to Amplify.
|
|
88
|
+
* This can be a regular expression, or a string to exact match.
|
|
89
|
+
* This is useful to prevent data of no use for amplify, or if the data keys are too long.
|
|
90
|
+
*/
|
|
91
|
+
ignoreMetadataPatterns?: (string | RegExp)[];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* A Reporter is a service that can receive Analytics data.
|
|
96
|
+
*/
|
|
97
|
+
export interface IReporter {
|
|
98
|
+
trackEvent(event: ReporterEvent): void;
|
|
99
|
+
addBreadcrumb(breadcrumb: ReporterBreadcrumb): void;
|
|
100
|
+
addMetadata(metadata: Metadata): void;
|
|
101
|
+
setUser(user: ReportUser | null): void;
|
|
102
|
+
setRouteName(routeName: string): void;
|
|
103
|
+
setPageName(pageName: string): void;
|
|
104
|
+
reportError?(error: ReportError, metadata?: Metadata): void;
|
|
105
|
+
recordSession?(): void;
|
|
106
|
+
recordSessionStop?(): void;
|
|
107
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { filterAttributeMap } from './utils';
|
|
2
|
+
|
|
3
|
+
describe('utils', () => {
|
|
4
|
+
describe('filterAttributeMap', () => {
|
|
5
|
+
it('should remove attributes which match the ignore patterns', () => {
|
|
6
|
+
const inputAttributeMap = {
|
|
7
|
+
includeme: '5',
|
|
8
|
+
excludeme: 'false',
|
|
9
|
+
differentProp: 'boo',
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const filtered = filterAttributeMap(inputAttributeMap, [/exclude/g, /Prop/g]);
|
|
13
|
+
expect(filtered['includeme']).toBeTruthy();
|
|
14
|
+
expect(filtered['excludeme']).toBeFalsy();
|
|
15
|
+
expect(filtered['differentProp']).toBeFalsy();
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
});
|
package/src/utils.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
-
import { ILogger, Profiler,
|
|
3
|
+
import { ILogger, Profiler, Metadata, LogLevel } from './types';
|
|
4
|
+
|
|
5
|
+
export type AttributeMap = Record<string, string[] | string | null>;
|
|
4
6
|
|
|
5
7
|
export function consoleLogger(name?: string, options?: { metadata?: Metadata }): ILogger {
|
|
6
8
|
const logger: ILogger = {
|
|
@@ -48,3 +50,16 @@ export function consoleLogger(name?: string, options?: { metadata?: Metadata }):
|
|
|
48
50
|
|
|
49
51
|
return logger;
|
|
50
52
|
}
|
|
53
|
+
|
|
54
|
+
export function filterAttributeMap(
|
|
55
|
+
attributes: AttributeMap | Record<string, string>,
|
|
56
|
+
ignorePatterns: (string | RegExp)[],
|
|
57
|
+
) {
|
|
58
|
+
const entries = Object.entries(attributes);
|
|
59
|
+
|
|
60
|
+
return Object.fromEntries(
|
|
61
|
+
entries.filter(
|
|
62
|
+
([key]) => !ignorePatterns.some((pattern) => (typeof pattern === 'string' ? pattern === key : pattern.test(key))),
|
|
63
|
+
),
|
|
64
|
+
);
|
|
65
|
+
}
|
package/lib/types.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AACA,oBAAY,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAE3C,oBAAY,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAE7C,oBAAY,WAAW,GAAG;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,eAAe,CAAC,EAAE,QAAQ,CAAC;CAC5B,CAAC;AAEF,oBAAY,UAAU,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAE1F,oBAAY,WAAW,GAAG,KAAK,GAAG,MAAM,CAAC;AAEzC,oBAAY,QAAQ;IAClB,KAAK,UAAU;IACf,IAAI,SAAS;IACb,IAAI,SAAS;IACb,KAAK,UAAU;CAChB;AAED,MAAM,WAAW,cAAc;IAC7B,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC;IAC3C,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;IAC3C,CAAC,OAAO,EAAE,GAAG,GAAG,OAAO,CAAC;CACzB;AAED,MAAM,WAAW,SAAS;IACxB,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC;IAC5D,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;IAC5D,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,GAAG,OAAO,CAAC;CAC1C;AAED,MAAM,WAAW,QAAQ;IACvB,MAAM,EAAE,OAAO,CAAC;IAChB,IAAI,CAAC,IAAI,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,QAAQ,CAAA;KAAE,GAAG,OAAO,CAAC;CAC9D;AAED,MAAM,WAAW,OAAO;IACtB,UAAU,IAAI,QAAQ,CAAC;IACvB,KAAK,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;IAEnD,GAAG,EAAE,SAAS,CAAC;IACf,KAAK,EAAE,cAAc,CAAC;IACtB,IAAI,EAAE,cAAc,CAAC;IACrB,IAAI,EAAE,cAAc,CAAC;IACrB,KAAK,EAAE,cAAc,CAAC;CACvB"}
|
package/lib/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAgBA,IAAY,QAKX;AALD,WAAY,QAAQ;IAClB,2BAAe,CAAA;IACf,yBAAa,CAAA;IACb,yBAAa,CAAA;IACb,2BAAe,CAAA;AACjB,CAAC,EALW,QAAQ,GAAR,gBAAQ,KAAR,gBAAQ,QAKnB"}
|