primers

The use of Source Map is discussed in front-end exception resolution: Source Map, and then the way exceptions are reported.

Factors affecting the

Abnormal reporting may affect the following factors:

  1. Frequency of reporting. It’s almost like a DDOS attack when there’s an endless loop of exceptions being reported.
  2. The amount of data reported. The amount of data that can be carried is limited by different request methods. If you want to record the user’s actions, the amount of data generated will vary from case to case.
  3. Across domains. Some log servers are separate.
  4. Different Web servers for the requestedbodyDifferent size limits, seeCan HTTP POST be limitless? 。
  5. Response mode of reporting a request. The response also consumes resources.

The above are some factors that are easy to think of. In actual situations, other influencing factors may appear according to different monitoring needs.

Report the way

Image SRC

Advantages:

  • There are no cross-domain restrictions.
  • You don’t have to respond.

Inadequate:

  • The length of the SRC URL is limited. F. In MDN img, we say that SRC is a URL. Are the same as web addresses. .
  • May compete for resources with other higher-priority network requests.

XHR/Fetch

Advantages:

  • POSTCan transmit more data, theoretically without limiting requestsbodyBut depending on the server, the body size of the request accepted may be limited.

Inadequate:

  • Subject to cross-domain constraints.
  • May compete for resources with other higher-priority network requests.

Beacon

The Beacon interface is used to arrange asynchronous and non-blocking data transfers to minimize resource contention with other critical operations while sending requests to their destinations. Features are:

  • A beacon request uses POST and does not require a response.
  • Beacon requests avoid competing for resources with critical operations and high-priority network requests.
  • User agents can effectively combine beacon requests to optimize energy use on mobile devices.
  • Beacon requests are guaranteed to be initialized before the page is unloaded and allow the run to complete without blocking the request or other user interaction event processing.
window.navigator.sendBeacon(url,data)
Copy the code
  • Url: The URL to send data.
  • Data: Specifies the data to transmit. This parameter is optional. ArrayBufferView, Blob, DOMString, and FormData are supported.

For compatibility, see Can I Use Beacon.

Inadequate:

  • This method does not provide any information about whether the data transfer was successful.
  • Subject to cross-domain constraints.

frequency

The frequency of reporting can be manually controlled for different exceptions and purposes. Generally, it can be divided into three categories: instant reporting, batch reporting and user active reporting.

Real-time reporting

Instant reporting means that exceptions are triggered and reported immediately. Such exceptions seriously affect users.

When a large number of exceptions are triggered consecutively or in an infinite loop, reported requests also need to be managed. Here is one way to manage @sentry/utils version 5.8.0:

import { SentryError } from './error';
import { SyncPromise } from './syncpromise';
/** A simple queue that holds promises. */
export class PromiseBuffer {
    constructor(_limit) {
        this._limit = _limit;
        /** Internal set of queued Promises */
        this._buffer = [];
    }
    /** * Says if the buffer is ready to take more requests */
    isReady() {
        return this._limit === undefined || this.length() < this._limit;
    }
    /**
     * Add a promise to the queue.
     *
     * @param task Can be any PromiseLike<T>
     * @returns The original promise.
     */
    add(task) {
        if (!this.isReady()) {
            return SyncPromise.reject(new SentryError('Not adding Promise due to buffer limit reached.'));
        }
        if (this._buffer.indexOf(task) === -1) {
            this._buffer.push(task);
        }
        task
            .then(() = > this.remove(task))
            .then(null.() = > this.remove(task).then(null.() = > {
            // We have to add this catch here otherwise we have an unhandledPromiseRejection
            // because it's a new Promise chain.
        }));
        return task;
    }
    /**
     * Remove a promise to the queue.
     *
     * @param task Can be any PromiseLike<T>
     * @returns Removed promise.
     */
    remove(task) {
        const removedTask = this._buffer.splice(this._buffer.indexOf(task), 1) [0];
        return removedTask;
    }
    /** * This function returns the number of unresolved promises in the queue. */
    length() {
        return this._buffer.length;
    }
    /**
     * This will drain the whole queue, returns true if queue is empty or drained.
     * If timeout is provided and the queue takes longer to drain, the promise still resolves but with false.
     *
     * @param timeout Number in ms to wait until it resolves with false.
     */
    drain(timeout) {
        return new SyncPromise(resolve= > {
            const capturedSetTimeout = setTimeout(() = > {
                if (timeout && timeout > 0) {
                    resolve(false);
                }
            }, timeout);
            SyncPromise.all(this._buffer)
                .then(() = > {
                clearTimeout(capturedSetTimeout);
                resolve(true);
            })
                .then(null.() = > {
                resolve(true); }); }); }}Copy the code

Main ideas:

  • When initialized, an array is supplied_bufferAnd threshold_limit
  • All requests will be made similarlyPromiseMechanism package through methodaddPut it in an array. Requests are asynchronous, and new requests can be continuously added based on an event loop.
  • When the number of added requests exceeds_limit, will not continue to add, so that you can achieve a certain control effect.

The batch report

The collected information is stored locally first. When the data amount or interval reaches a certain threshold, the local information is packaged and uploaded at a time. Tracking the user’s path is a good way to do this.

User active reporting

Provide a portal for reporting exceptions, so that users can report their problems.

If you consider the privacy policy, when you enter the system, you need to be informed that some information will be collected.

The resources

Sin Crown is beautifully visualized, and the soundtrack tends to burn, but when you watch it, it doesn’t feel that way.