Skip to content

Library to Track Events to Kinesis Ingress Events Queue

License

Notifications You must be signed in to change notification settings

babbel/miza-kinesis

Repository files navigation

Miza-Kinesis

Provides an interface to create tracking events which are sent to AWS Kinesis.

Requirements

  • Nodejs >= 16 with NPM version >= 8

Development

Install dependencies:

npm install @babbel/miza-kinesis --save

Build

npm run build

Tests

npm test

Usage

Config to track single event:

const config = {
  appName: 'application-name',
  kinesisStream: {
    arn: 'Kinesis arn',
    connectTimeout: 1000,
    maxRetries: 10,
  },
  ipv4: '127.0.0.1', // optional
  endpoint: 'http://localhost:4568', // localstack only
};

Code Example:

const events = require('@babbel/miza-kinesis');

const emitEvent = events(config);

const event = {
  name: 'request:performed',
  meta: {
    // ...
  },
  // ... more
};

try {
  const data = await emitEvent(event);
  console.log(data)
}
catch(error) {
  console.error(error);
}

Config to track multiple events:

const config = {
  appName: 'application-name',
  kinesisStream: {
    arn: 'Kinesis arn',
    httpOptions: {
      connectTimeout: 1000,
      timeout: 1000,
    },
    maxRetries: 10,
  },
  ipv4: '127.0.0.1', // optional
  endpoint: 'http://localhost:4568', // localstack only
  type: 'BATCH',
};

Code Example:

const events = require('@babbel/miza-kinesis');

const emitEvent = events(config);

const events = [
  {
    name: 'request:performed',
    meta: {
      // ...
    },
  },
  {
    name: 'data:saved',
    meta: {
      // ...
    },
  },
];

try {
  const data = await emitEvent(event);
  console.log(data)
}
catch(error) {
  console.error(error);
}

Config has a following format:

  • appName - required added to the events meta data to give notice of it's origin
  • kinesisStream.arn - required Kinesis ARN where the events will be send
  • kinesisStream.httpOptions - optional specified in AWS SDK https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Kinesis.html
  • kinesisStream.maxRetries - optional the maximum amount of retries to attempt with a request. See AWS.Kinesis.maxRetries for more information.
  • config.maxRetries the maximum amount of retries to attempt for failed requests.
  • ipv4 - optional ip of the machine that is sending the event
  • endpoint - localstack-only we recommend to run the service in development environment using Localstack. Kinesis (from Localstack) will respond at the location http://localhost:4568. In order to work with Kinesis, you need to provide the location(endpoint) to the AWS-sdk configuration.
  • partitionKey - optional the key used to group data by shard within a stream.

emitEvent returns data in either of the following format: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Kinesis.html#putRecord-property or https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Kinesis.html#putRecords-property

event specification you can check here: https://confluence.internal.babbel.com/wiki/display/PM/Event+Specifications

events array of events

Releasing new versions

In order to create a release:

  1. Add details about changes in CHANGELOG.md
  2. Version the new changes
    • Manual version update:
      1. Update the version in package.json of your feature branch
      2. Tag the last commit with the new version
        1. git tag v1.x.x
        2. git push origin v1.x.x
    • Automatic version update:
      1. Use npm version
  3. Merge to master!
    1. New versions are automatically published to npm on every merge to master

License

MIT Licensed. See LICENSE for full details.