Skip to content

Commit 7102a65

Browse files
committed
sdk-core: add BacktraceAttachmentProvider
1 parent 8a6cdcb commit 7102a65

File tree

5 files changed

+74
-12
lines changed

5 files changed

+74
-12
lines changed

packages/sdk-core/src/BacktraceCoreClient.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,10 @@ export abstract class BacktraceCoreClient<O extends BacktraceConfiguration = Bac
259259
* Add attachment to the client
260260
* @param attachment attachment
261261
*/
262-
public addAttachment(attachment: BacktraceAttachment): void {
263-
this.attachmentManager.add(attachment);
262+
public addAttachment(...attachments: BacktraceAttachment[]): void;
263+
public addAttachment(...attachments: Array<() => BacktraceAttachment>): void;
264+
public addAttachment(...attachments: Array<BacktraceAttachment | (() => BacktraceAttachment)>): void {
265+
this.attachmentManager.add(...attachments);
264266
}
265267

266268
/**
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BacktraceAttachment } from '../model/attachment';
22

33
export type AttachmentEvents = {
4-
'attachments-updated'(attachments: BacktraceAttachment[]): void;
4+
'scoped-attachments-updated'(attachments: BacktraceAttachment[]): void;
55
};
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,62 @@
11
import { Events } from '../../common/Events';
22
import { AttachmentEvents } from '../../events/AttachmentEvents';
33
import { BacktraceAttachment } from '../../model/attachment';
4+
import { BacktraceAttachmentProvider } from './BacktraceAttachmentProvider';
45

56
export class AttachmentManager {
67
public readonly attachmentEvents: Events<AttachmentEvents>;
78

8-
private readonly _attachments: BacktraceAttachment[] = [];
9+
private readonly _attachmentProviders: BacktraceAttachmentProvider[] = [];
910

1011
constructor() {
1112
this.attachmentEvents = new Events();
1213
}
1314

14-
public add(...attachments: BacktraceAttachment[]) {
15-
this._attachments.push(...attachments);
16-
this.attachmentEvents.emit('attachments-updated', this.get());
15+
public add(...attachments: Array<BacktraceAttachment | (() => BacktraceAttachment)>) {
16+
this.addProviders(
17+
...attachments.map<BacktraceAttachmentProvider>((a) =>
18+
typeof a === 'function'
19+
? {
20+
type: 'dynamic',
21+
get: a,
22+
}
23+
: {
24+
type: 'scoped',
25+
get: () => a,
26+
},
27+
),
28+
);
1729
}
1830

19-
public get(): BacktraceAttachment[] {
20-
return [...this._attachments];
31+
public addProviders(...attachmentProviders: BacktraceAttachmentProvider[]) {
32+
let anyScoped = false;
33+
for (const provider of attachmentProviders) {
34+
if (provider.type === 'dynamic') {
35+
this._attachmentProviders.push(provider);
36+
} else {
37+
const attachment = provider.get();
38+
this._attachmentProviders.push({
39+
type: 'scoped',
40+
get: () => attachment,
41+
});
42+
anyScoped = true;
43+
}
44+
}
45+
46+
if (anyScoped) {
47+
this.attachmentEvents.emit('scoped-attachments-updated', this.get('scoped'));
48+
}
49+
}
50+
51+
public get(type?: 'scoped' | 'dynamic'): BacktraceAttachment[] {
52+
const result: BacktraceAttachment[] = [];
53+
for (const provider of this._attachmentProviders) {
54+
if (type && provider.type !== type) {
55+
continue;
56+
}
57+
58+
result.push(provider.get());
59+
}
60+
return result;
2161
}
2262
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { BacktraceAttachment } from '../../model/attachment';
2+
3+
export interface BacktraceAttachmentProvider {
4+
/**
5+
* Return attachment provider type. Based on the type, attachment provider is being invoked
6+
* once or per every report.
7+
* - scoped: means the attachment provider will be invoked only once just after adding the
8+
* provider to the client.
9+
* - dynamic: means the attachment provider will be invoked every time before the report data generation
10+
*/
11+
get type(): 'scoped' | 'dynamic';
12+
13+
/**
14+
* Generate provider attributes
15+
*/
16+
get(): BacktraceAttachment;
17+
}

packages/sdk-core/src/modules/breadcrumbs/BreadcrumbsManager.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,13 @@ export class BreadcrumbsManager implements BacktraceBreadcrumbs, BacktraceModule
6666
}
6767
}
6868

69-
public bind({ client, reportEvents }: BacktraceModuleBindData): void {
70-
for (const attachment of this._storage.getAttachments()) {
71-
client.addAttachment(attachment);
69+
public bind({ client, reportEvents, attachmentManager }: BacktraceModuleBindData): void {
70+
if (this._storage.getAttachmentProviders) {
71+
attachmentManager.addProviders(...this._storage.getAttachmentProviders());
72+
} else {
73+
attachmentManager.add(...this._storage.getAttachments());
7274
}
75+
7376
client.addAttribute(() => ({
7477
[BREADCRUMB_ATTRIBUTE_NAME]: this._storage.lastBreadcrumbId,
7578
}));

0 commit comments

Comments
 (0)