0.8.4 • Published 5 years ago

database-metrics-logger v0.8.4

Weekly downloads
6
License
ISC
Repository
github
Last release
5 years ago

This project is no longer supported, please consider using node-service-metrics-logger instead.
The project node-service-metrics-logger does also support other services like RabbitMQ.

divider

divider

❯ Why

You want to log service metrics from your application on Cloud Foundry? - Yes? - Here you are 🙌 !!

divider

❯ Table of Contents

divider

❯ Supported Services

ServiceVersion
MongoDB>=3.6.6
Redis>=2.3.9

divider

❯ Quick Start

Installation

Install library by using npm

npm install database-metrics-logger

or by using yarn

yarn add database-metrics-logger

How to use

Step 1: Import DatabaseMetricsLogger

Using CommonJS module loader:

const DatabaseMetricsLogger = require('database-metrics-logger');

Using ES6 module loader:

import { DatabaseMetricsLogger } from 'database-metrics-logger';

Step 2: Create new instance of CfServiceMetricsLogger

Create new instance of DatabaseMetricsLogger and provide options:

const databaseCredentials = [
  {
    databaseType: 'mongodb',
    host: 'your-mongodb-host',
    username: 'user',
    password: 'this-is-secret',
    port: 27017
    database: 'your-database-name',
  },
  {
    databaseType: 'redis',
    host: 'your-redis-host',
    port: 6379,
    password: 'this-is-secret',
  }
];

const databaseMetricsLogger = new DatabaseMetricsLogger({databaseCredentials});

Step 3: Subscribe to receive service metrics and general logs

Subscribe metrics to receive service metrics data:

databaseMetricsLogger.subscribe('metrics', data => {
  // do some fancy stuff with your metrics
});

Subscribe logs to receive general application logs:

databaseMetricsLogger.subscribe('logs', {message, level} => {
  console[level](message));
});

Step 3: Start and stop service metrics logging

Start database metrics logging:

databaseMetricsLogger.start();

Stop service metrics logging:

databaseMetricsLogger.stop();

divider

❯ API

Credentials Options

OptionDescriptionDefault Value
databaseTypeType of database. Currently only mongodb redis are available
nameName of credential to identify your specific database in a mass of logsyour host
hostHost of database
port (optional)Port of database
uri (optional)Instead of port and host, you can provide an URI
username (optional)Username
password (optional)Password  
interval (optional)Database metrics polling interval in ms10000

Methods

MethodDescription
start()Start service metrics
stop()Stop service metrics
subscribe(eventId, callback)Subscribe an event
unsubscribe(eventId, callback)Unsubscribe an event
unsubscribeAll()Unsubscribe all events

Subscription event id's

IdDescription
metricsService metrics
logsGeneral application logs for levels debug, info, warn and error

divider

❯ Cloud Connectors

You can use a cloud connector to log metrics from your cloud instance databases.

Cloud Foundry

Options

OptionDescriptionDefault Value
vcap (optional)Provide local VCAP_SERVICES and/or VCAP_APPLICATION values{}
vcapFile (optional)Provide local VCAP_SERVICES and/or VCAP_APPLICATION file''

Methods

MethodDescription
getCredentials()Get Cloud Foundry service credentials

Example

// import using `CommonJS` module loader:
const { CloudFoundryConnector, DatabaseMetricsLogger } = require('cf-service-metrics-logger');

// or import using `ES6` module loader:
import { CloudFoundryConnector, DatabaseMetricsLogger } from 'cf-service-metrics-logger';

const options = {
  vcapFile: 'your-vcap-file-path/vcap.json' 
};

const cloudFoundryConnector = new CloudFoundryConnector(options);
const databaseCredentials = cloudFoundryConnector.getCredentials();
const databaseMetricsLogger = new DatabaseMetricsLogger({databaseCredentials});

databaseMetricsLogger.subscribe('metrics', data => {
  // do some fancy stuff with your metrics
});

databaseMetricsLogger.start();

setTimeout(() => {
  databaseMetricsLogger.stop();
}, 30000);

divider

❯ Transports

With transports you can log metrics to metrics services like Datadog. Datadog is currently the one and only implemented transport.

Datadog

Example

// import using `CommonJS` module loader:
const { DatabaseMetricsLogger, DatadogTransport } = require('database-metrics-logger');

// or import using `ES6` module loader:
import { DatabaseMetricsLogger, DatadogTransport } from 'database-metrics-logger';

const datadogTransport = new DatadogTransport({
  apiKey: 'datadog-api-key',
  appKey: 'datadog-app-key',
  host: 'app.datadoghq.com',
  tags: ['env:development']
});

const databaseCredentials = [
  {
    databaseType: 'mongodb',
    host: 'your-mongodb-host',
    username: 'user',
    password: 'this-is-secret',
    port: 27017,
    database: 'your-database-name',
  }
];

const databaseMetricsLogger = new DatabaseMetricsLogger({
  databaseCredentials,
  transports: [datadogTransport],
});

databaseMetricsLogger.start();

❯ Development

Getting Started

Step 1: Set up the Development Environment

You need to set up your development environment before you can do anything.

Install Node.js and NPM

Install yarn globally

yarn install yarn -g

Step 2: Install dependencies

Install all dependencies with yarn.

yarn install

Scripts and Tasks

Install

  • Install all dependencies with yarn install

Linting

  • Run code quality analysis using yarn run lint. This runs tslint.

Tests

  • Run unit test using yarn run test.

Building the project

  • Run yarn run build to generate commonJS and ES6 modules as well as declaration from the TypeScript source.
  • Builded sources are located in dist folder.

Debugger

VS Code

Just set a breakpoint in source or unit test and hit F5 in your Visual Studio Code to execute and debug all unit tests.

divider

❯ Project Structure

NameDescription
.vscode/VSCode tasks, launch configuration and some other settings
dist/Compiled and bundled source files will be placed here
src/Source files
src/types/ *.d.tsCustom type definitions and files that aren't on DefinitelyTyped
test/Tests
test/unit/ *.test.tsUnit tests
rollup.config.jsConfig for Rollup module bundler
0.8.4

5 years ago

0.8.3

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.9

5 years ago

0.6.8

5 years ago

0.6.7

5 years ago

0.6.6

5 years ago

0.6.5

5 years ago

0.6.4

5 years ago

0.6.3

5 years ago

0.6.2

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.5

5 years ago

0.5.4

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.9

5 years ago

0.4.8

5 years ago

0.4.7

5 years ago

0.4.6

5 years ago

0.4.5

5 years ago

0.4.3

5 years ago

0.4.4

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago