npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

service-metrics-node

v1.1.7

Published

Metrics microservice in Node.js / ES2017

Downloads

4

Readme

Metrics microservice

This is the metrics microservice. It keeps list of metrics.

This 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 is a numerical value characterizing the metric

When adding or updating a metric, statistics on the metric are automatically calculated for different time horizons (you can specify the depth of the horizon) with the calculation of the average, maximum, minimum and accumulated values ​​within each of them.

Data access is provided through a set of API functions

The microservice currently supports the following deployment options:

  • Deployment platforms: Standalone Process
  • External APIs: HTTP/REST
  • Persistence: Memory, Flat Files, MongoDB

This microservice has no dependencies on other microservices. Quick Links:

Contract

Logical contract of the microservice is presented below. For physical implementation (HTTP/REST, GRPC, Lambda, etc.), please, refer to documentation of the specific protocol.

// 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 IMetricsController {
    getMetricDefinitions(correlationId: string): Promise<MetricDefinitionV1[]>;
    getMetricDefinitionByName(correlationId: string, name: string): Promise<MetricDefinitionV1>;
    getMetricsByFilter(correlationId: string, filter: FilterParams, paging: PagingParams): Promise<DataPage<MetricValueSetV1>>;
    updateMetric(correlationId: string, update: MetricUpdateV1, maxTimeHorizon: number): Promise<void>;
    updateMetrics(correlationId: string, updates: MetricUpdateV1[], maxTimeHorizon: number): Promise<void>;
}

Download

Right now the only way to get the microservice is to check it out directly from github repository

git clone [email protected]:pip-services-infrastructure2/service-metrics-node.git

Pip.Service team is working to implement packaging and make stable releases available for your as zip downloadable archieves.

Run

Add config.yaml file to the root of the microservice folder and set configuration parameters. As the starting point you can use example configuration from config.example.yaml file.

Example of microservice configuration

{    
---
- descriptor: "pip-services-commons:logger:console:default:1.0"
  level: "trace"

- descriptor: "service-metrics:persistence:file:default:1.0"
  path: "./data/blobs"

- descriptor: "service-metrics:controller:default:default:1.0"

- descriptor: "service-metrics:service:commandable-http:default:1.0"
  connection:
    protocol: "http"
    host: "0.0.0.0"
    port: 3000
}

For more information on the microservice configuration see Configuration Guide.

Start the microservice using the command:

node run

Use

Inside your code get the reference to the client SDK

 import { MetricsHttpClientV1 } from 'client-metrics-node';

Define client configuration parameters.

// Client configuration
let httpConfig = ConfigParams.fromTuples(
            'connection.protocol', 'http',
            'connection.port', 3000,
            'connection.host', 'localhost'
        );
client.configure(httpConfig);

Instantiate the client and open connection to the microservice

// Create the client instance
client = new MetricssHttpClientV1();

// Connect to the microservice
try {
    await client.open(null);

    // Work with the microservice
    ...
} catch(err) {
    console.error('Connection to the microservice failed');
    console.error(err);
}

Now the client is ready to perform operations:

Update if exist metric or create otherwise:

try {
    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
    );
} catch(err) {
    console.error('Update/create metric are failed');
    console.error(err);
}
    

Update if exist metrics or create otherwise::

try {
    await client.updateMetrics(
        null,
        [
            <MetricUpdateV1> {
                name: "metric1",
                dimension1: "A",
                dimension2: "B",
                dimension3: null,
                year: 2018,
                month: 8,
                day: 26,
                hour: 13,
                value: 321
            },
            <MetricUpdateV1> {
                name: "metric2",
                dimension1: "A",
                dimension2: null,
                dimension3: "C",
                year: 2018,
                month: 8,
                day: 26,
                hour: 13,
                value: 321
            }        
        ],
        TimeHorizonV1.Hour  
    );
} catch(err) {
    console.error('Update/create metric are failed');
    console.error(err);
}
    

Get metrics by filter:

try {
    let page = await client.getMetricsByFilter(null,
        FilterParams.fromTuples("name", "metric1"),
        new PagingParams()
    );
    console.log("Metrics:");
    console.log(page.data);
} catch(err) {
    console.error("Can\'t get metrics by filter");
    console.error(err);
}

    

Get all metrics definitions:

try {
    let definitions = await client.getMetricDefinitions(null);
    console.log("All metrics definition:");
    console.log(definitions);
} catch(err) {
    console.error("Can\'t get metrics definitions");
    console.error(err);
}
    

Get metric definition by name:

try {
    let definition = await client.getMetricDefinitionByName(null, "metric2");
    console.log("Metric definition name %s:", definition.name);
    console.log(definition);
} catch(err) {
    console.error("Can\'t get metrics definition by name");
    console.error(err);
} 
              
    

Acknowledgements

This client SDK was created and currently maintained by Sergey Seroukhov and Levichev Dmitry.