0.23.5 • Published 8 months ago

@locker/instrumentation v0.23.5

Weekly downloads
-
License
SEE LICENSE IN LI...
Repository
-
Last release
8 months ago

@locker/instrumentation

Lightning Web Security instrumentation utilities

This package provides interfaces to facilitate instrumenting Lightning Web Security (LWS) code without having to bind the entire codebase to an external interface.

There are three instrumentation functions on the main interface, 1 for instrumenting errors, 1 for generic logging and 1 for metrics (activity).

Consumer of LWS needs to provide actual instrumentation service and the wrapper classes which implements the interfaces defined by this package. This package provides a mock implementation to be used when the consumer does not provide any instrumentation service.

Usage

LWS code base

// The `myInstrumentation` object is a wrapper for the actual instrumentation
// client which implements Locker instrumentation interface.
let { error, log, startActivity } = myInstrumentation;
let activity = startActivity('AwesomeActivity', data);
try {
    foo();
    error({ errorData });
    log({ logData });
} catch (e) {
    activity.error(e);
    throw e;
}
activity.stop();

Implementation for LWS instrumentation interface

import { LockerActivity, LockerInstrumentation } from '@locker/instrumentation';
import { evaluateInSandbox } from '@locker/sandbox';

function createLockerInstrumentation(myInstrumentationClient) {
    const activityStartCallback = (activity, data) => {
        // Call myInstrumentationService to start a performance activity metric.
    };

    const activityStopCallback = (activity, data) => {
        // Call myInstrumentationService to stop a performance activity metric.
    };

    class MyInstrumentation extends LockerInstrumentation {
        startActivity(activityName, data) {
            const activity = new LockerActivity(
                activityName,
                activityStartCallback,
                activityStopCallback
            );
            activity.start(data);
            return activity;
        }

        error(data) {
            // Call myInstrumentationClient to log an error.
        }

        log(data) {
            // Call myInstrumentationClient to make a regular log.
        }
    }

    return new MyInstrumentation();
}

const myInstrumentation = createLockerInstrumentation(myInstrumentationClient);

evaluateInSandbox(key, code, null, null, myInstrumentation);

Instrumentation interface

Due to possible restrictions on the instrumentation service, the high volume of method calls and privacy considerations in sandboxed code, Locker cannot instrument serialized method parameters or method return values. As a consequence, LWS instrumentation interface will invoke the instrumentation client using specific information.

LWS instrumentation interface accepts telemetry data with simple JSON objects or native simple types (string, boolean, number). It is expected that the wrapper of the instrumentation client to dissect and re-format the data into the format that the instrumentation client accepts.

startActivity

const { startActivity } = myInstrumentation;
// The instrumented activity is started for current `sandboxKey`.
const myActivity = startActivity(sandboxKey, 'fooActivity');
try {
    foo();
} catch (e) {
    // Stop the activity with error.
    activity.error(e);
    throw e;
}
// Stop the activity after last instrumented call.
activity.stop();

log

const { log } = const { startActivity } = myInstrumentation;
const foo: Array = bar();
log(`bar method returned ${foo.length} results.`);
log({ sandboxKey, message: `bar method returned ${foo.length} results.` });

error

const { error } = new LockerInstrumentation(myService);

try {
    foo();
} catch (e) {
    error(`${e}`);
    error({ sandboxKey, error: e });
    throw e;
}

Plugging in an external instrumentation service

To plug in an external service in Lightning Web Security the following steps are required:

  1. Extend the class Instrumentation from @locker/instrumentation.
import { LockerActivity , LockerInstrumentation } from '@locker/instrumentation';
import type { DataType } from '@locker/instrumentation';

const activityStartCallback = (activity, data) => {
    // Call external instrumentation service to start a performance activity metric.
};

const activityStopCallback = (activity, data) => {
    // Call external instrumentation service to stop a performance activity metric.
};

class MyInstrumentation extends LockerInstrumentation {
    startActivity(activityName: string, data?: DataType) {
        return new LockerActivity(activityName, startCallback, stopCallback);
    }
    error(data?: DataType) {
        errorCallback(data);
    }
    log(data?: DataType) {
        logCallback(data);
    }
}

LockerActivity will call startCallback and stopCallback respectively when an activity is started or stopped.

  1. Invoke evaluateInSandbox or evaluateInCoreSandbox and provide MyInstrumentation as an argument.
const instrumentation = new MyInstrumentation();
evaluateInSandbox(key, code, null, null, instrumentation);
0.23.5

8 months ago

0.23.4

9 months ago

0.23.3

9 months ago

0.22.6

9 months ago

0.23.2

9 months ago

0.23.1

10 months ago

0.21.8

1 year ago

0.21.7

1 year ago

0.21.9

1 year ago

0.22.5

12 months ago

0.22.4

12 months ago

0.22.3

1 year ago

0.21.10

1 year ago

0.21.6

1 year ago

0.22.2

1 year ago

0.22.1

1 year ago

0.21.5

1 year ago

0.21.4

1 year ago

0.20.17

1 year ago

0.20.16

1 year ago

0.21.3

1 year ago

0.20.15

1 year ago

0.21.2

1 year ago

0.21.2-test.0

1 year ago

0.21.1

1 year ago

0.20.13

1 year ago

0.20.12

1 year ago

0.20.11

1 year ago

0.21.0

2 years ago

0.19.17

2 years ago

0.20.10

2 years ago

0.20.9

2 years ago

0.20.1

2 years ago

0.20.0

2 years ago

0.18.24

2 years ago

0.19.11

2 years ago

0.19.12

2 years ago

0.19.13

2 years ago

0.19.14

2 years ago

0.19.15

2 years ago

0.19.16

2 years ago

0.20.8

2 years ago

0.20.7

2 years ago

0.20.6

2 years ago

0.20.5

2 years ago

0.20.4

2 years ago

0.20.3

2 years ago

0.18.23

2 years ago

0.19.9

2 years ago

0.18.22

2 years ago

0.19.10

2 years ago

0.19.8

2 years ago

0.19.3

2 years ago

0.19.4

2 years ago

0.19.5

2 years ago

0.19.6

2 years ago

0.19.7

2 years ago

0.18.21

2 years ago

0.18.20

2 years ago

0.17.23

2 years ago

0.17.25

2 years ago

0.17.24

2 years ago

0.17.27

2 years ago

0.17.26

2 years ago

0.18.9

2 years ago

0.18.4

2 years ago

0.18.5

2 years ago

0.18.6

2 years ago

0.18.7

2 years ago

0.18.8

2 years ago

0.17.29

2 years ago

0.17.28

2 years ago

0.17.30

2 years ago

0.18.11

2 years ago

0.18.10

2 years ago

0.18.13

2 years ago

0.18.12

2 years ago

0.18.15

2 years ago

0.18.14

2 years ago

0.18.17

2 years ago

0.18.16

2 years ago

0.19.0

2 years ago

0.19.1

2 years ago

0.19.2

2 years ago

0.18.19

2 years ago

0.18.18

2 years ago

0.17.18

3 years ago

0.17.17

3 years ago

0.17.19

3 years ago

0.17.21

2 years ago

0.17.20

3 years ago

0.17.22

2 years ago

0.18.1

3 years ago

0.18.2

2 years ago

0.18.3

2 years ago

0.18.0

3 years ago

0.16.30

3 years ago

0.17.7

3 years ago

0.17.8

3 years ago

0.17.9

3 years ago

0.17.10

3 years ago

0.17.12

3 years ago

0.17.11

3 years ago

0.17.14

3 years ago

0.17.13

3 years ago

0.17.16

3 years ago

0.17.15

3 years ago

0.17.9-test.4

3 years ago

0.17.9-test.3

3 years ago

0.17.9-test.0

3 years ago

0.17.9-test.1

3 years ago

0.16.29

3 years ago

0.16.28

3 years ago

0.17.4

3 years ago

0.17.5

3 years ago

0.17.6

3 years ago

0.16.25

3 years ago

0.16.26

3 years ago

0.16.27

3 years ago

0.17.2

3 years ago

0.17.3

3 years ago

0.17.1-test.0

3 years ago

0.17.0

3 years ago

0.17.1

3 years ago

0.16.19

3 years ago

0.16.21-test.2

3 years ago

0.16.21-test.1

3 years ago

0.16.21

3 years ago

0.16.22

3 years ago

0.16.20

3 years ago

0.16.23

3 years ago

0.16.24

3 years ago

0.16.10

3 years ago

0.16.11

3 years ago

0.16.14

3 years ago

0.16.15

3 years ago

0.16.12

3 years ago

0.16.13

3 years ago

0.16.18

3 years ago

0.16.16

3 years ago

0.16.17

3 years ago

0.15.17

3 years ago

0.16.3

3 years ago

0.16.4

3 years ago

0.16.5

3 years ago

0.16.6

3 years ago

0.16.7

3 years ago

0.16.8

3 years ago

0.16.9

3 years ago

0.16.1

3 years ago

0.16.2

3 years ago

0.15.13

3 years ago

0.15.14

3 years ago

0.15.12

3 years ago

0.15.15

3 years ago

0.15.16

3 years ago

0.15.11

3 years ago

0.14.28

3 years ago

0.15.4

3 years ago

0.15.5

3 years ago

0.15.6

3 years ago

0.15.7

3 years ago

0.15.8

3 years ago

0.15.9

3 years ago

0.15.3

3 years ago

0.15.10

3 years ago

0.14.24

3 years ago

0.14.23

3 years ago

0.14.22

3 years ago

0.14.27

3 years ago

0.14.26

3 years ago

0.14.25

3 years ago

0.15.0

4 years ago

0.15.2

4 years ago

0.14.20

4 years ago

0.14.21

4 years ago

0.14.17

4 years ago

0.14.16

4 years ago

0.14.15

4 years ago

0.14.14

4 years ago

0.14.19

4 years ago

0.14.18

4 years ago

0.14.13

4 years ago

0.14.12

4 years ago

0.14.11

4 years ago

0.13.10

4 years ago

0.14.6

4 years ago

0.14.7

4 years ago

0.14.5

4 years ago

0.13.9

4 years ago

0.14.2

4 years ago

0.14.3

4 years ago

0.14.4

4 years ago

0.14.1

4 years ago

0.14.0

4 years ago

0.13.7

4 years ago

0.13.8

4 years ago

0.13.6

4 years ago