service-metrics2-node
v1.0.0
Published
Metrics microservice in Node.js / ES2017 V2
Readme
Metrics Microservice for Node.js / ES2017 V2
Overview
The Metrics microservice is designed to manage various metrics characterizing the operation of a process. Each metric has the following characteristics:
- Metric name
- Up to 3 types of measurements (in string format)
- Date and time
- Numerical value
When adding or updating a metric, statistics are automatically calculated for different time horizons with the calculation of:
- Average values
- Maximum values
- Minimum values
- Accumulated values
Features
Multiple Deployment Options
- Standalone Process
- HTTP/REST API
- Multiple Persistence Options
Persistence Options
- In-Memory (for development and testing)
- Flat Files (for development and testing)
- MongoDB (for production)
No External Dependencies
- Self-contained service
- Easy to deploy and maintain
Quick Links
Getting Started
Installation
git clone [email protected]:entinco/eic-services-infrastructure2.gitNote: The Pip.Service team is working on implementing packaging and making stable releases available as downloadable archives.
Running the Service
- Add config.json file to the config folder
- Start the microservice:
node ./bin/main.jsAPI Reference
Data Models
// Create or update metric struct
class MetricUpdateV1 {
public name: string;
public year: number;
public month: number;
public day: number;
public hour: number;
public minute?: number;
public dimension1?: string;
public dimension2?: string;
public dimension3?: string;
public value: number;
}
// Metric definition struct
class MetricDefinitionV1 {
public name: string;
public dimension1: string[];
public dimension2: string[];
public dimension3: string[];
}
// Metric value struct
class MetricValueSetV1 {
public name: string;
public time_horizon: number;
public dimension1: string;
public dimension2: string;
public dimension3: string;
public values: MetricValueV1[];
}
// Values of metric
class MetricValueV1 {
public year?: number;
public month?: number;
public day?: number;
public hour?: number;
public minute?: number;
public count: number;
public sum: number;
public max: number;
public min: number;
}
// Time horizons
class TimeHorizonV1 {
public static Total: number = 0;
public static Year: number = 1;
public static Month: number = 2;
public static Day: number = 3;
public static Hour: number = 4;
public static Minute: number = 5;
}Interface
interface IMetricsController {
getMetricDefinitions(context: IContext): Promise<MetricDefinitionV1[]>;
getMetricDefinitionByName(
context: IContext,
name: string
): Promise<MetricDefinitionV1>;
getMetricsByFilter(
context: IContext,
filter: FilterParams,
paging: PagingParams
): Promise<DataPage<MetricValueSetV1>>;
updateMetric(
context: IContext,
update: MetricUpdateV1,
maxTimeHorizon: number
): Promise<void>;
updateMetrics(
context: IContext,
updates: MetricUpdateV1[],
maxTimeHorizon: number
): Promise<void>;
}Configuration
Persistence Configuration
Memory Persistence
- descriptor: "metrics:persistence:memory:default:1.0"
options:
max_page_size: 100File Persistence
- descriptor: "metrics:persistence:file:default:1.0"
path: "./data/metrics"MongoDB Persistence
- descriptor: "metrics:persistence:file:default:1.0"
collection: "metrics"
connection:
uri: "mongodb://localhost/pipservicestest"
host: "localhost"
port: 27017
database: "app"
credential:
username: "user_db"
password: "passwd_db"Service Configuration
- descriptor: "metrics:service:default:default:1.0"HTTP Controller Configuration
- descriptor: "metrics:controller:commandable-http:default:1.0"
connection:
protocol: "http"
host: "0.0.0.0"
port: 8080Development
Prerequisites
- Node.js from https://nodejs.org/en/download/
- TypeScript compiler:
npm install typescript -g - Mocha test runner:
npm install mocha -g - Git from https://git-scm.com/downloads
- MongoDB (optional) from https://www.mongodb.org/downloads
Building and Testing
# Install dependencies
npm install
# Build the project
tsc
# Run tests
npm test
# Run benchmarks
npm run benchmarkExamples
Basic Usage
import { MetricsHttpClientV1 } from "client-metrics2-node";
// Configure client
let httpConfig = ConfigParams.fromTuples(
"connection.protocol",
"http",
"connection.port",
3000,
"connection.host",
"localhost"
);
// Create and open client
let client = new MetricssHttpClientV1();
await client.open(null);
// Example: Update metric
await client.updateMetric(
null,
<MetricUpdateV1>{
name: "metric1",
dimension1: "A",
dimension2: "B",
dimension3: null,
year: 2018,
month: 8,
day: 26,
hour: 12,
value: 123,
},
TimeHorizonV1.Hour
);
// Example: Get metrics by filter
let page = await client.getMetricsByFilter(
null,
FilterParams.fromTuples("name", "metric1"),
new PagingParams()
);Acknowledgements
This client library was created and currently maintained by:
