Giter VIP home page Giter VIP logo

iopipe / iopipe-js Goto Github PK

View Code? Open in Web Editor NEW
33.0 3.0 9.0 1.2 MB

Build and run serverless apps with confidence on AWS Lambda with Tracing, Profiling, Metrics, Monitoring, and more.

Home Page: https://www.iopipe.com

License: Apache License 2.0

JavaScript 49.45% Shell 50.55%
serverless tracing aws iopipe iopipe-agent aws-lambda profiling debugging aws-lambda-node serverless-functions

iopipe-js's Introduction

IOpipe Agent for JavaScript


Coverage Status npm version styled with prettier semantic-release

IOpipe is a serverless DevOps platform for organizations building event-driven architectures in AWS Lambda. IOpipe captures crucial, high-fidelity metrics for each Lambda function invocation. This data powers a flexibile and robust development and operations experience with features including tracing, profiling, custom metrics, and low-latency alerts. Get started today to quickly and confidently gain superior observability, identify issues, and discover anomalies in your connected applications.

Note: this library is a lower-level implementation than the package you might likely be looking for. Enjoy pre-bundled plugins like tracing and event info with @iopipe/iopipe

Installation

Install using your package manager of choice,

npm install @iopipe/core

or

yarn add @iopipe/core

If you are using the Serverless Framework to deploy your lambdas, check out our serverless plugin.

Usage

Configure the library with your project token (register for access), and it will automatically monitor and collect metrics from your applications running on AWS Lambda.

Example:

const iopipeLib = require('@iopipe/core');

const iopipe = iopipeLib({ token: 'PROJECT_TOKEN' });

exports.handler = iopipe((event, context) => {
  context.succeed('This is my serverless function!');
});

Custom metrics

You may add custom metrics to an invocation using context.iopipe.metric to add either string or numerical values. Keys have a maximum length of 256 characters, and string values are limited to 1024.

Example:

const iopipeLib = require('@iopipe/core');

const iopipe = iopipeLib({ token: 'PROJECT_TOKEN' });

exports.handler = iopipe((event, context) => {
  context.iopipe.metric('key', 'some-value');
  context.iopipe.metric('another-key', 42);
  context.succeed('This is my serverless function!');
});

Labels

You can label invocations using context.iopipe.label to label an invocation with a string value, with a limit of 128 characters.

Example:

const iopipeLib = require('@iopipe/core');

const iopipe = iopipeLib({ token: 'PROJECT_TOKEN' });

exports.handler = iopipe((event, context) => {
  context.iopipe.label('something-important-happened');
  context.succeed('This is my serverless function!');
});

Configuration

Methods

You can configure your iopipe setup through one or more different methods - that can be mixed, providing a config chain. The current methods are listed below, in order of precendence. The module instantiation object overrides all other config values (if values are provided).

  1. Module instantiation object
  2. IOPIPE_* environment variables
  3. An .iopiperc file
  4. An iopipe package.json entry
  5. An extends key referencing a config package
  6. Default values

Options

token (string: required)

If not supplied, the environment variable $IOPIPE_TOKEN will be used if present. Find your project token.

debug (bool: optional = false)

Debug mode will log all data sent to IOpipe servers to STDOUT. This is also a good way to evaluate the sort of data that IOpipe is receiving from your application. If not supplied, the environment variable $IOPIPE_DEBUG will be used if present.

const iopipe = require('@iopipe/core')({
  token: 'PROJECT_TOKEN',
  debug: true
});

exports.handler = iopipe((event, context, callback) => {
  // Do things here. We'll log info to STDOUT.
});

networkTimeout (int: optional = 5000)

The number of milliseconds IOpipe will wait while sending a report before timing out. If not supplied, the environment variable $IOPIPE_NETWORK_TIMEOUT will be used if present.

const iopipe = require('@iopipe/core')({ token: 'PROJECT_TOKEN', networkTimeout: 30000})

timeoutWindow (int: optional = 150)

By default, IOpipe will capture timeouts by exiting your function 150ms early from the AWS configured timeout, to allow time for reporting. You can disable this feature by setting timeoutWindow to 0 in your configuration. If not supplied, the environment variable $IOPIPE_TIMEOUT_WINDOW will be used if present.

const iopipe = require('@iopipe/core')({ token: 'PROJECT_TOKEN', timeoutWindow: 0})

plugins (array: optional)

Note that if you use the @iopipe/iopipe package, you get our recommended plugin set-up right away. Plugins can extend the functionality of IOpipe in ways that best work for you. Follow the guides for the plugins listed below for proper installation and usage on the @iopipe/core library:

Example:

const tracePlugin = require('@iopipe/trace');

const iopipe = require('@iopipe/core')({
  token: 'PROJECT_TOKEN',
  plugins: [tracePlugin()]
});

exports.handler = iopipe((event, context, callback) => {
  // Run your fn here
});

enabled (boolean: optional = True)

Conditionally enable/disable the agent. The environment variable $IOPIPE_ENABLED will also be checked.

url (string: optional)

Sets an alternative URL to use for the IOpipe collector. The environment variable $IOPIPE_COLLECTOR_URL will be used if present.

RC File Configuration

Not recommended for webpack/bundlers due to dynamic require.

You can configure iopipe via an .iopiperc RC file. An example of that is here. Config options are the same as the module instantiation object, except for plugins. Plugins should be an array containing mixed-type values. A plugin value can be a:

  • String that is the name of the plugin
  • Or an array with plugin name first, and plugin options second
{
  "token": "wow_token",
  "plugins": [
    "@iopipe/trace",
    ["@iopipe/profiler", {"enabled": true}]
  ]
}

IMPORTANT: You must install the plugins as dependencies for them to load properly in your environment.

package.json Configuration

Not recommended for webpack/bundlers due to dynamic require.

You can configure iopipe within a iopipe package.json entry. An example of that is here. Config options are the same as the module instantiation object, except for plugins. Plugins should be an array containing mixed-type values. A plugin value can be a:

  • String that is the name of the plugin
  • Or an array with plugin name first, and plugin options second
{
  "name": "my-great-package",
  "dependencies": {
    "@iopipe/trace": "^0.2.0",
    "@iopipe/profiler": "^0.1.0"
  },
  "iopipe": {
    "token": "wow_token",
    "plugins": [
      "@iopipe/trace",
      ["@iopipe/profiler", {"enabled": true}]
    ]
  }
}

IMPORTANT: You must install the plugins as dependencies for them to load properly in your environment.

Extends Configuration

Not recommended for webpack/bundlers due to dynamic require.

You can configure iopipe within a package.json or rc file by referencing a extends config package. An example of that is here. Config options are the same as the module instantiation object, except for plugins. Plugins should be an array containing mixed-type values. A plugin value can be a:

  • String that is the name of the plugin
  • Or an array with plugin name first, and plugin options second

For an example of a config package, check out @iopipe/config.

IMPORTANT: You must install the config package and plugins as dependencies for them to load properly in your environment.

License

Apache 2.0

iopipe-js's People

Contributors

coreylight avatar dependabot[bot] avatar ewindisch avatar javonharper avatar katiebayes avatar kolanos avatar mrickard avatar pselle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

iopipe-js's Issues

Bundling doesn't work (ioredis)

{ ModuleNotFoundError: Module not found: Error: Can't resolve 'ioredis' in '/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/dist/plugins'
at factory.create (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/webpack/lib/Compilation.js:888:10)
at factory (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/webpack/lib/NormalModuleFactory.js:401:22)
at resolver (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/webpack/lib/NormalModuleFactory.js:130:21)
at asyncLib.parallel (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/webpack/lib/NormalModuleFactory.js:224:22)
at /var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/neo-async/async.js:2830:7
at /var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/neo-async/async.js:6877:13
at normalResolver.resolve (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/webpack/lib/NormalModuleFactory.js:214:25)
at doResolve (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/enhanced-resolve/lib/Resolver.js:184:12)
at hook.callAsync (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
at _fn0 (eval at create (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/tapable/lib/HookCodeFactory.js:33:10), :15:1)
name: 'BuildError',
isBuilderError: true,
isSilent: undefined,
isCancellation: undefined,
broccoliPayload:
{ originalError:
ModuleNotFoundError: Module not found: Error: Can't resolve 'ioredis' in '/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/dist/plugins'
at factory.create (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/webpack/lib/Compilation.js:888:10)
at factory (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/webpack/lib/NormalModuleFactory.js:401:22)
at resolver (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/webpack/lib/NormalModuleFactory.js:130:21)
at asyncLib.parallel (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/webpack/lib/NormalModuleFactory.js:224:22)
at /var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/neo-async/async.js:2830:7
at /var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/neo-async/async.js:6877:13
at normalResolver.resolve (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/webpack/lib/NormalModuleFactory.js:214:25)
at doResolve (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/enhanced-resolve/lib/Resolver.js:184:12)
at hook.callAsync (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
at _fn0 (eval at create (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/tapable/lib/HookCodeFactory.js:33:10), :15:1)
resolve 'ioredis' in '/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/dist/plugins'
Parsed request is a module
using description file: /var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/package.json (relative path: ./dist/plugins)
resolve as module
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/dist/plugins/node_modules doesn't exist or is not a directory
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/dist/node_modules doesn't exist or is not a directory
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/node_modules doesn't exist or is not a directory
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/node_modules doesn't exist or is not a directory
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/node_modules doesn't exist or is not a directory
/var/lib/shippable/build/IN/node_modules doesn't exist or is not a directory
/var/lib/shippable/build/node_modules doesn't exist or is not a directory
/var/lib/shippable/node_modules doesn't exist or is not a directory
/var/lib/node_modules doesn't exist or is not a directory
/var/node_modules doesn't exist or is not a directory
/node_modules doesn't exist or is not a directory
looking for modules in /var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/node_modules
using description file: /var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/package.json (relative path: ./node_modules)
using description file: /var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/package.json (relative path: ./node_modules/ioredis)
no extension
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/node_modules/ioredis doesn't exist
looking for modules in /var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules
using description file: /var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/package.json (relative path: ./node_modules)
using description file: /var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/package.json (relative path: ./node_modules/ioredis)
no extension
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/ioredis doesn't exist
.wasm
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/node_modules/ioredis.wasm doesn't exist
.wasm
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/ioredis.wasm doesn't exist
.mjs
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/node_modules/ioredis.mjs doesn't exist
.mjs
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/ioredis.mjs doesn't exist
.js
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/node_modules/ioredis.js doesn't exist
.js
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/ioredis.js doesn't exist
.json
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/node_modules/ioredis.json doesn't exist
.json
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/ioredis.json doesn't exist
as directory
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/node_modules/ioredis doesn't exist
as directory
/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/ioredis doesn't exist,
originalMessage:
'Module not found: Error: Can't resolve 'ioredis' in '/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@iopipe/trace/dist/plugins'',
nodeId: 20,
nodeLabel: 'WebpackWriter',
nodeName: 'WebpackWriter',
nodeAnnotation: undefined,
instantiationStack:
' at WebpackWriter.Plugin (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/broccoli-plugin/index.js:7:31)\n at WebpackWriter.CachingWriter [as constructor] (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/broccoli-caching-writer/index.js:18:10)\n at new WebpackWriter (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/broccoli-webpack/index.js:13:16)\n at webpackLambdas (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@silvercurve/lambda-server-build-utils/dist/builder.js:188:16)\n at LambdaServerBuilder.buildForDeploy (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@silvercurve/lambda-server-build-utils/dist/builder.js:269:26)\n at LambdaServerBuilder.build (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/@silvercurve/lambda-server-build-utils/dist/builder.js:300:54)\n at resolve (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/broccoli/lib/wrappers/transform-node.js:29:35)\n at new Promise ()\n at TransformNodeWrapper.build (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/broccoli/lib/wrappers/transform-node.js:21:12)\n at Promise.resolve.then.then.then (/var/lib/shippable/build/IN/sc-pp-backend-repo-master/gitRepo/node_modules/broccoli/lib/builder.js:116:28)',
location:
{ file: undefined,
treeDir: undefined,
line: undefined,
column: undefined } } }

Ability to turn off GA ping when offline

Trying to work offline locally (no internet connection ... caltrain), i couldn't hit my functions because Google Analytics dns lookup fails.

I tried setting iopipeNoStats in serverless.yaml but there seems to be a code issue (i ended up just removing iopipe temporary to work).

Easy to reproduce by turning off your Internet connection and run with serverless offline.

NodeJS support for Lambda Layers

Lambda layers: Lambda can't find the file @iopipe/iopipe.js

A readme section was added describing how to manually integrate IOpipe to resolve this error, but we need to be able to integrate w/o code modifications.

I think this used to work... I'm not sure what changed, but we can no longer import the @iopipe/iopipe module as a handler directly.

Detect errors when using API Gateway Proxy integration

We use the Lambda proxy integration in AWS API Gateway and it seems that iopipe can't detect errors within our functions.

Due to the way that integration works our lambdas don't use things like context.fail() nor can we throw the error. Instead, the integration uses the JSON body to determine the response to the client; e.g:

exports.handler = iopipe(async (event, context) => {
try {
   <code here throws exception>
  } catch (err) {
    logger.error('------ There was an error');
    logger.error(err);

    response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
    response.body = 'There was an erro';

    return response;  //
  } 
}

iopipe does not report this an error

Allow users to disable null method warnings

mark.end(foo) was called from @iopipe/iopipe but this method was not available. You may have called this method outside of an invocation or @iopipe/core needs to be upgraded to satisfy version ^1.11.0
label(bar) was called from @iopipe/iopipe but this method was not available. You may have called this method outside of an invocation or @iopipe/core needs to be upgraded to satisfy version ^1.11.0

When a user is say, unit testing outside of the main invocation, these warnings could be expected/noisy.

errors have wrong callstack

Reported error on a lambda log uses the callstack of the iopipe catch block, instead of the call stack of the original error, ie where it was thrown from.

Error: "user code error message here"
    at Report._callee$ (/opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:4982:62)
    at tryCatch (/opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:2230:40)
    at Generator.invoke [as _invoke] (/opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:2464:22)
    at Generator.prototype.(anonymous function) [as next] (/opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:2282:21)
    at step (/opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:1011:30)
    at /opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:1029:14
    at new Promise (<anonymous>)
    at new F (/opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:143:28)
    at Report.<anonymous> (/opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:1008:12)
    at Report.prepare (/opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:5061:21)
    at IOpipeWrapperClass._callee2$ (/opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:1903:36)
    at tryCatch (/opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:2230:40)
    at Generator.invoke [as _invoke] (/opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:2464:22)
    at Generator.prototype.(anonymous function) [as next] (/opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:2282:21)
    at step (/opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:1011:30)
    at /opt/nodejs/node_modules/@iopipe/core/dist/iopipe.js:1022:13
    at <anonymous>
    at runMicrotasksCallback (internal/process/next_tick.js:121:5)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)

No matching version found for [email protected]

Hi, I am not sure why we started seeing this error when doing an npm install, I wonder if there's some problem with npmjs.com ...

npm ERR! code ETARGET
npm ERR! notarget No matching version found for [email protected]
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.
npm ERR! notarget
npm ERR! notarget It was specified as a dependency of 'lab-pull-google-reports'
npm ERR! notarget

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.