@justeat/f-stat-client

0.1.0 • Public • Published

f-stat-client

Fozzie Bear

Javascript HTTP client for publishing stats to ElasticSearch


npm version CircleCI Coverage Status Known Vulnerabilities

This client abstracts away the complexity of publishing stats to ElasticSearch, such as API timings so you can then graph on these results. It also provides a standardised approach for to follow that can be used in a suite of features and components, allowing you to use and report in a generic way.

Benefits (Now)

Benefits (Soon)

  • encapsulated batch publishing
  • encapsulated sample publishing
  • encapsulated authentication

Usage

Installation

Install the module using npm or Yarn:

yarn add @justeat/f-stat-client

Initialisation/Construction e.g.

import StatClient from '@justeat/f-stat-client';

const options = {
    statClientUri: 'http://public-elastic-search-endpoint',
    tenant: 'uk',
    featureName: 'checkoutweb'
};

const client = new StatClient(options);

How to use

await client.publish({ verb: 'GET', segment: '/search', status: 200, timing: 611 });

Note; the dynamic model passed to the publish() method will get deconstructed and written out as individual fields on the ElasticSearch document along with the fixed fields of FeatureName, Tenant and a Timestamp.

An example of the final document written to ElasticSearch: Output Example

Constructor

Parameter Description Type Example
options The overrides for the default options json [string] {statClientUri: 'http://localhost:9200',tenant: 'ns',featureName: 'Generic Front End'}(See below the defaults for the options if not overridden via the constructor options)
mock This can be supplied for testing purposes and will be used to return your mock response instead @elastic/elasticsearch-mock (see mock example below)

Options

Parameter Description Type Default
statClientUri The host of the stat publishing endpoint String 'http://localhost:9200'
tenant This is a key identifier to group stats by country, e.g. uk String 'ns'
featureName This is a key identifier to group stats by feature, e.g. salesWebsite String 'Generic Front End'
statClientUser The username to gain access to the stat publishing endpoint (if not supplied then no authentication will be used) String null
statClientPwd The password to gain access to the stat publishing endpoint (ignored if user not supplied) String null
statClientIndexName This is index to write to String 'justeat'

Client Methods

These are all of the methods exposed by the f-stat-client

Method Description Parameters Example
publish Sends a dynamic model (stat details) to the Endpoint json [string] {verb: 'GET',segment: '/search',status: 200,timing: 654}
## How to Test/Mock e.g.
import StatClient from '@justeat/f-stat-client';

const Mock = require('@elastic/elasticsearch-mock');

const mock = new Mock();

// Build a cut down mock reponse
const mockResponse = {
    _index: 'justeat',
    result: 'created',
    statusCode: 201
};

// Mock the action to return the mock response
mock.add({
    method: 'POST',
    path: '*'
}, () => (mockResponse));

const options = {
    statClientUri: 'http://localhost:9200',
    tenant: 'uk',
    featureName: 'checkoutweb_test',
    statClientIndexName: 'justeat'
};

// Supply the mock on the constructor
const client = new StatClient(options, mock);

// Act
const response = await client.publish({ verb: 'GET', segment: '/basket', status: 200, timimg: 654 });

// Assert
expect(response.body.result).toBe('created');

Readme

Keywords

Package Sidebar

Install

npm i @justeat/f-stat-client

Weekly Downloads

0

Version

0.1.0

License

Apache-2.0

Unpacked Size

22.7 kB

Total Files

11

Last publish

Collaborators

  • ilia.chikmarev
  • fidel.montesino
  • anastasiia.horban
  • raouf.sawehli
  • mmakwe-onyeka
  • gregory.palaci
  • arielfdr
  • dawidchar
  • roberto.santana
  • lizzie.turney
  • davidpn.11
  • dandel10n
  • mwh1989
  • kevinrodrigues
  • fozzie-bot
  • ashleynolan
  • simonsje
  • xander-marjoram
  • sergii.semenkiv
  • thomaswilliammcclean
  • benrwhite
  • robertellison
  • zlatin.ivanov
  • joshuang2332
  • oliversweb
  • andymayje
  • jamieomaguire
  • adammorr
  • lsphillips