0.1.0 • Published 4 years ago

@justeat/f-stat-client v0.1.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
4 years ago

f-stat-client

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:

Constructor

ParameterDescriptionTypeExample
optionsThe overrides for the default optionsjson 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)
mockThis can be supplied for testing purposes and will be used to return your mock response instead@elastic/elasticsearch-mock(see mock example below)

Options

ParameterDescriptionTypeDefault
statClientUriThe host of the stat publishing endpointString'http://localhost:9200'
tenantThis is a key identifier to group stats by country, e.g. ukString'ns'
featureNameThis is a key identifier to group stats by feature, e.g. salesWebsiteString'Generic Front End'
statClientUserThe username to gain access to the stat publishing endpoint (if not supplied then no authentication will be used)Stringnull
statClientPwdThe password to gain access to the stat publishing endpoint (ignored if user not supplied)Stringnull
statClientIndexNameThis is index to write toString'justeat'

Client Methods

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

MethodDescriptionParametersExample
publishSends a dynamic model (stat details) to the Endpointjson string{verb: 'GET',segment: '/search',status: 200,timing: 654}
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');