New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@locker/instrumentation

Package Overview
Dependencies
Maintainers
8
Versions
212
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@locker/instrumentation

Locker Next Instrumentation Utilities

  • 0.16.1
  • npm
  • Socket score

Version published
Weekly downloads
305
decreased by-13.11%
Maintainers
8
Weekly downloads
 
Created
Source

Lightning Web Security (LWS) instrumentation package

The instrumentation package provides interfaces to facilitate instrumenting Lightning Web Security 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;
} finally {
    activity.stop();
}

Implementation for LWS instrumentation interface

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

function makeLockerInstrumentation(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 Instrumentation {
        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 = makeLockerInstrumentation(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;
} finally {
    // 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 { DataType, DefaultInstrumentation, LockerActivity } 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 DefaultInstrumentation {
    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);

FAQs

Package last updated on 08 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc