How to use @instana/aws-lambda - 5 common examples

To help you get started, we’ve selected a few @instana/aws-lambda examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github instana / nodejs-sensor / packages / aws-lambda / lambdas / wrapped-async / index.js View on Github external
'use strict';

// eslint-disable-next-line import/no-extraneous-dependencies
const instana = require('@instana/aws-lambda'); // provided by Lambda layer "instana"

const fetch = require('node-fetch');

exports.handler = instana.wrap(async event => {
  console.log('in actual handler');
  await fetch('https://example.com');
  if (event.error) {
    throw new Error('Boom!');
  } else {
    return {
      message: 'Stan says hi!'
    };
  }
});
github instana / nodejs-sensor / packages / aws-lambda / lambdas / wrapped-callback / index.js View on Github external
'use strict';

// eslint-disable-next-line import/no-extraneous-dependencies
const instana = require('@instana/aws-lambda'); // provided by Lambda layer "instana"

const https = require('https');

exports.handler = instana.wrap((event, context, callback) => {
  console.log('in actual handler');
  const req = https.get('https://example.com', res => {
    res.resume();
    res.on('end', () => {
      if (event.error) {
        callback(new Error('Boom!'));
      } else {
        callback(null, {
          message: 'Stan says hi!'
        });
      }
    });
  });
  req.on('error', e => {
    callback(e);
  });
github instana / nodejs-sensor / packages / aws-lambda / lambdas / demo-cloudwatch-events-processor / index.js View on Github external
const pg = require('pg');

const pgHost = process.env.RDS_HOSTNAME || 'localhost';
const pgPort = process.env.RDS_PORT || '5432';
const pgDatabase = process.env.RDS_DB_NAME || 'lambdademo';
const pgUser = process.env.RDS_USERNAME || 'postgres';
const pgPassword = process.env.RDS_PASSWORD;

console.log(
  `Using PG config: ${pgHost}:${pgPort}/${pgDatabase}, user ${pgUser}, ${
    pgPassword ? 'a password has been provided.' : 'no password has been provided!'
  }`
);

exports.handler = instana.awsLambda.wrap(async event => {
  if (!event.time || typeof event.time !== 'string') {
    // malformed event, probably not triggered by a CloudWatch event
    console.log('Event has no timestamp or timestamp is not a string. Aborting.');
    return;
  }

  const client = await connect();
  try {
    // 1. Delete all items older than 24 hours (just so the table does not grow indefinitely).
    await client.query("DELETE FROM items WHERE timestamp < now()-'12 hours'::interval");
    console.log('Deleted old items.');

    // 2. Create a new item
    const label = `Created by CloudWatch Event at ${event.time}.`;
    await client.query('INSERT INTO items(label) VALUES($1) RETURNING *', [label]);
    console.log(`Created: ${label}.`);
github instana / nodejs-sensor / packages / aws-lambda / lambdas / demo-s3-watcher / index.js View on Github external
const pg = require('pg');

const pgHost = process.env.RDS_HOSTNAME || 'localhost';
const pgPort = process.env.RDS_PORT || '5432';
const pgDatabase = process.env.RDS_DB_NAME || 'lambdademo';
const pgUser = process.env.RDS_USERNAME || 'postgres';
const pgPassword = process.env.RDS_PASSWORD;

console.log(
  `Using PG config: ${pgHost}:${pgPort}/${pgDatabase}, user ${pgUser}, ${
    pgPassword ? 'a password has been provided.' : 'no password has been provided!'
  }`
);

exports.handler = instana.awsLambda.wrap(async event => {
  const label = event.Records.slice(0, 20).map(
    s3Record =>
      `${s3Record.eventName}: ${s3Record.s3 && s3Record.s3.bucket ? s3Record.s3.bucket.name : ''}/${s3RecordToObject(
        s3Record
      )}`
  );

  const client = await connect();
  try {
    await client.query('INSERT INTO items(label) VALUES($1) RETURNING *', [label]);
    console.log(`Created: ${label}.`);
  } catch (err) {
    console.log('Failed to execute insert.', JSON.stringify(err));
  } finally {
    await client.end();
  }
github instana / nodejs-sensor / packages / aws-lambda-auto-wrap / src / index.js View on Github external
const fs = require('fs');
const path = require('path');

const DEFAULT_HANDLER = 'index.handler';
const RUNTIME_PATH = '/var/runtime';
const SPLIT_AT_DOT_REGEX = /^([^.]*)\.(.*)$/;
const TWO_DOTS = '..';

const lambdaRuntimeErrors = require(`${RUNTIME_PATH}/Errors.js`);

let wrappedHandler;

if (!wrappedHandler) {
  const targetHandler = loadTargetHandlerFunction();
  wrappedHandler = instana.wrap(targetHandler);
}

exports.handler = function instanaAutowrapHandler(event, context, callback) {
  return wrappedHandler(event, context, callback);
};

function loadTargetHandlerFunction() {
  let targetHandlerEnvVar = process.env.LAMBDA_HANDLER;
  if (!targetHandlerEnvVar || targetHandlerEnvVar.length === 0) {
    targetHandlerEnvVar = DEFAULT_HANDLER;
  }

  const {
    targetHandlerModuleFolder, //
    targetHandlerModuleName,
    targetHandlerFunctionName

@instana/aws-lambda

Instana tracing and monitoring for Node.js based AWS Lambdas

MIT
Latest version published 6 days ago

Package Health Score

82 / 100
Full package analysis

Popular @instana/aws-lambda functions