-
Notifications
You must be signed in to change notification settings - Fork 4
Breadcrumbs support #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { OverwritingArrayIterator } from './OverwritingArrayIterator'; | ||
|
|
||
| export class OverwritingArray<T> { | ||
| private _array: T[]; | ||
| private _index = 0; | ||
| private _size = 0; | ||
| private _startIndex = 0; | ||
| constructor(public readonly capacity: number) { | ||
| this._array = this.createArray(); | ||
| } | ||
| public add(value: T): this { | ||
| this._array[this._index] = value; | ||
| this._index = this.incrementIndex(this._index); | ||
| this._startIndex = this.incrementStartingIndex(); | ||
| this._size = this.incrementSize(); | ||
| return this; | ||
| } | ||
|
|
||
| public clear(): void { | ||
| this._array = this.createArray(); | ||
| } | ||
|
|
||
| public values(): IterableIterator<T> { | ||
| return new OverwritingArrayIterator<T>(this._array, this._startIndex, this._size); | ||
| } | ||
|
|
||
| [Symbol.iterator](): IterableIterator<T> { | ||
| return new OverwritingArrayIterator<T>(this._array, this._startIndex, this._size); | ||
| } | ||
|
|
||
| private incrementIndex(index: number) { | ||
| return (index + 1) % this.capacity; | ||
| } | ||
|
|
||
| private incrementStartingIndex() { | ||
| if (this._size !== this.capacity) { | ||
| return this._startIndex; | ||
| } | ||
| return this.incrementIndex(this._startIndex); | ||
| } | ||
| private incrementSize() { | ||
| return Math.min(this.capacity, this._size + 1); | ||
| } | ||
| private createArray() { | ||
| return new Array(this.capacity); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
packages/sdk-core/src/dataStructures/OverwritingArrayIterator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| export class OverwritingArrayIterator<T> implements IterableIterator<T> { | ||
| private _index?: number; | ||
|
|
||
| constructor(private readonly _source: T[], private readonly _offset: number, private readonly _size: number) {} | ||
|
|
||
| [Symbol.iterator](): IterableIterator<T> { | ||
| return new OverwritingArrayIterator(this._source, this._offset, this._size); | ||
| } | ||
| next(): IteratorResult<T> { | ||
| if (this._size === 0) { | ||
| return { | ||
| done: true, | ||
| value: undefined, | ||
| }; | ||
| } | ||
| if (this._index === undefined) { | ||
| this._index = 0; | ||
| } else if (this._index === this._size - 1) { | ||
| return { | ||
| done: true, | ||
| value: undefined, | ||
| }; | ||
| } else { | ||
| this._index++; | ||
| } | ||
| return { | ||
| done: false, | ||
| value: this._source[(this._index + this._offset) % this._size], | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/sdk-core/src/modules/breadcrumbs/BacktraceBreadcrumbs.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { AttributeType } from '../../model/data/BacktraceData'; | ||
| import { BreadcrumbLogLevel } from './model/BreadcrumbLogLevel'; | ||
| import { BreadcrumbType } from './model/BreadcrumbType'; | ||
|
|
||
| export interface BacktraceBreadcrumbs { | ||
| /** | ||
| * Breadcrumbs type | ||
| */ | ||
| readonly breadcrumbsType: BreadcrumbType; | ||
|
|
||
| /** | ||
| * Breadcrumbs Log level | ||
| */ | ||
| readonly logLevel: BreadcrumbLogLevel; | ||
|
|
||
| /** | ||
| * Dispose breadcrumbs integration | ||
| */ | ||
| dispose(): void; | ||
|
|
||
| verbose(message: string, attributes?: Record<string, AttributeType>): void; | ||
| debug(message: string, attributes?: Record<string, AttributeType>): void; | ||
| info(message: string, attributes?: Record<string, AttributeType>): void; | ||
| warn(message: string, attributes?: Record<string, AttributeType>): void; | ||
| error(message: string, attributes?: Record<string, AttributeType>): void; | ||
| log(message: string, level: BreadcrumbLogLevel, attributes?: Record<string, AttributeType>): void; | ||
| } | ||
118 changes: 118 additions & 0 deletions
118
packages/sdk-core/src/modules/breadcrumbs/BreadcrumbsManager.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { | ||
| BacktraceBreadcrumbs, | ||
| BreadcrumbLogLevel, | ||
| BreadcrumbType, | ||
| defaultBreadcrumbsLogLevel, | ||
| defaultBreadcurmbType, | ||
| } from '.'; | ||
| import { BacktraceBreadcrumbsSettings } from '../../model/configuration/BacktraceConfiguration'; | ||
| import { AttributeType } from '../../model/data/BacktraceData'; | ||
| import { BacktraceReport } from '../../model/report/BacktraceReport'; | ||
| import { BreadcrumbsSetup } from './BreadcrumbsSetup'; | ||
| import { BreadcrumbsEventSubscriber } from './events/BreadcrurmbsEventSubscriber'; | ||
| import { ConsoleEventSubscriber } from './events/ConsoleEventSubscriber'; | ||
| import { BreadcrumbsStorage } from './storage/BreadcrumbsStorage'; | ||
| import { InMemoryBreadcrumbsStorage } from './storage/InMemoryBreadcrumbsStorage'; | ||
|
|
||
| export class BreadcrumbsManager implements BacktraceBreadcrumbs { | ||
| /** | ||
| * Breadcrumbs type | ||
| */ | ||
| public readonly breadcrumbsType: BreadcrumbType; | ||
|
|
||
| public readonly BREADCRUMB_ATTRIBUTE_NAME = 'breadcrumbs.lastId'; | ||
|
|
||
| /** | ||
| * Breadcrumbs Log level | ||
| */ | ||
| public readonly logLevel: BreadcrumbLogLevel; | ||
|
|
||
| get type(): 'scoped' | 'dynamic' { | ||
| return 'dynamic'; | ||
| } | ||
|
|
||
| public readonly breadcrumbsStorage: BreadcrumbsStorage; | ||
|
|
||
| /** | ||
| * Determines if the breadcrumb manager is enabled. | ||
| */ | ||
| private _enabled = true; | ||
| private readonly _eventSubscribers: BreadcrumbsEventSubscriber[] = [new ConsoleEventSubscriber()]; | ||
|
|
||
| constructor(configuration?: BacktraceBreadcrumbsSettings, options?: BreadcrumbsSetup) { | ||
| this.breadcrumbsType = configuration?.eventType ?? defaultBreadcurmbType; | ||
| this.logLevel = configuration?.logLevel ?? defaultBreadcrumbsLogLevel; | ||
| this.breadcrumbsStorage = options?.storage ?? new InMemoryBreadcrumbsStorage(configuration?.maximumBreadcrumbs); | ||
| if (options?.subscribers) { | ||
| this._eventSubscribers.push(...options.subscribers); | ||
| } | ||
| } | ||
|
|
||
| public dispose(): void { | ||
| this._enabled = false; | ||
| for (const subscriber of this._eventSubscribers) { | ||
| subscriber.dispose(); | ||
| } | ||
| } | ||
|
|
||
| public get(): Record<string, number> { | ||
| return { | ||
| [this.BREADCRUMB_ATTRIBUTE_NAME]: this.breadcrumbsStorage.lastBreadcrumbId, | ||
| }; | ||
| } | ||
|
|
||
| public start() { | ||
| for (const subscriber of this._eventSubscribers) { | ||
| subscriber.start(this); | ||
| } | ||
| } | ||
|
|
||
| public verbose(message: string, attributes?: Record<string, AttributeType> | undefined): boolean { | ||
| return this.log(message, BreadcrumbLogLevel.Verbose, attributes); | ||
| } | ||
| public debug(message: string, attributes?: Record<string, AttributeType> | undefined): boolean { | ||
| return this.log(message, BreadcrumbLogLevel.Debug, attributes); | ||
| } | ||
| public info(message: string, attributes?: Record<string, AttributeType> | undefined): boolean { | ||
| return this.log(message, BreadcrumbLogLevel.Info, attributes); | ||
| } | ||
| public warn(message: string, attributes?: Record<string, AttributeType> | undefined): boolean { | ||
| return this.log(message, BreadcrumbLogLevel.Warning, attributes); | ||
| } | ||
| public error(message: string, attributes?: Record<string, AttributeType> | undefined): boolean { | ||
| return this.log(message, BreadcrumbLogLevel.Error, attributes); | ||
| } | ||
| public log( | ||
| message: string, | ||
| level: BreadcrumbLogLevel, | ||
| attributes?: Record<string, AttributeType> | undefined, | ||
| ): boolean { | ||
| return this.addBreadcrumb(message, level, BreadcrumbType.Manual, attributes); | ||
| } | ||
|
|
||
| public logReport(report: BacktraceReport) { | ||
| const level = report.data instanceof Error ? BreadcrumbLogLevel.Error : BreadcrumbLogLevel.Warning; | ||
| return this.addBreadcrumb(report.message, level, BreadcrumbType.System); | ||
| } | ||
|
|
||
| public addBreadcrumb( | ||
| message: string, | ||
| level: BreadcrumbLogLevel, | ||
| type: BreadcrumbType, | ||
| attributes?: Record<string, AttributeType> | undefined, | ||
| ): boolean { | ||
| if (!this._enabled) { | ||
| return false; | ||
| } | ||
| if ((this.logLevel & level) !== level) { | ||
| return false; | ||
| } | ||
perf2711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if ((this.breadcrumbsType & type) !== type) { | ||
| return false; | ||
| } | ||
|
|
||
| this.breadcrumbsStorage.add(message, level, type, attributes); | ||
| return true; | ||
| } | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
packages/sdk-core/src/modules/breadcrumbs/BreadcrumbsSetup.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { BreadcrumbsEventSubscriber } from './events/BreadcrurmbsEventSubscriber'; | ||
| import { BreadcrumbsStorage } from './storage/BreadcrumbsStorage'; | ||
|
|
||
| export interface BreadcrumbsSetup { | ||
| storage?: BreadcrumbsStorage; | ||
| subscribers?: BreadcrumbsEventSubscriber[]; | ||
| } |
14 changes: 14 additions & 0 deletions
14
packages/sdk-core/src/modules/breadcrumbs/events/BreadcrurmbsEventSubscriber.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { BreadcrumbsManager } from '../BreadcrumbsManager'; | ||
|
|
||
| export interface BreadcrumbsEventSubscriber { | ||
| /** | ||
| * Set up breadcrumbs listener | ||
| * @param breadcrumbsManager breadcrumbs manager | ||
| */ | ||
| start(breadcrumbsManager: BreadcrumbsManager): void; | ||
|
|
||
| /** | ||
| * Dispose all breadcrumbs events | ||
| */ | ||
| dispose(): void; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.