Duration return post register new histogram metric this type
};
Options
Default | |||
---|---|---|---|
|
|
||
Object or Array<Object> | |||
colletrics |
|
|
|
|
|||
|
|||
|
|
||
|
|
||
|
|
|
---|
metrics:{
enabled:true,
reporter:[
{
type:"Console",
options:{
includes:["moleculer.**.total"],
excludes:["moleculer.broker.**","moleculer.request.**"],// moleculer.config.js
module.exports ={
metrics:{
enabled:true,
reporter:[
{
type:"Console",
options:{
// Printing interval in seconds interval:5,
// Custom logger.logger:null,
// Using colors
colors:true,
// Prints only changed metrics, not the full list.// - "metric" - save metrics to individual files
// - "label" - save metrics by labels to individual files mode:"metric",
// Saved metrics types.types:null,
// Saving interval in seconds
interval:5,
// Custom filename formatter
filenameFormatter:null,
// Custom CSV row formatter.
};
{ |
---|
};
// moleculer.config.js
module.exports ={
metrics:{
enabled:true,
reporter:[
{
type:"StatsD",
options:{
// Server host
host:"localhost",
// Server port
port:8125,
// Maximum payload size.maxPayloadSize:1300
}}]}};
} |
|
---|
Use custom metrics
A counter is a cumulative metric that represents a single monotonically increasing counter whose value can only increase or be reset to zero. For example, you can use a counter to represent the number of requests served, tasks completed, or errors. It can also provide 1-minute rate.
Counter provides the following methods
Histogram
A histogram samples observations (usually things like request durations or response sizes) and counts them in configurable buckets. It also provides a sum of all observed values and calculates configurable quantiles over a sliding time window. It can also provide 1-minute rate.
set(value: any |null, labels?: GenericObject, timestamp?: number)
Built-in Internal Metrics
• | |
• | |
• | |
• |
|
• |
|
• | |
• | |
• | |
• | |
• |
|
• |
|
• | |
• | |
• | |
• | |
• |
|
• |
|
• | |
• | |
• | |
• | |
• |
|
• |
|
• | |
• | |
• | |
• | |
• |
|
• |
|
• | |
• | |
• | |
• |
|
• |
|
• | |
• | |
• | |
• | |
• |
|
• |
|
• | |
• | |
• |
|
|
---|
actions:{
// Get posts.get(ctx){
// Increment metric
this.broker.metrics.increment("posts.get.total",1);
}; |
---|
Create a gauge with labels
// posts.service.js
module.exports ={
name:"posts",actions:{
// Create a new post
create(ctx){
// Update metrics
}, |
---|
created(){
// Register new gauge metric
this.broker.metrics.register({
type:"gauge",
name:"posts.total",
labelNames:["userID"]
description:"Number of posts by user",
unit:"post"
this.logger.debug("Post created. Elapsed time: ", duration,"ms"); return post;
}, |
|
---|
} |
---|
};
Errors
|
|
||
---|---|---|---|
|
|
||
|
|||
|
Error for client error which is not retryable. Parameters are same as .
Internal error classes
RequestTimeoutError
Throw it if your request is timed out.Error code: 504
Retryable: true
Type: REQUEST_TIMEOUTQueueIsFullError
Throw it if there are too many active requests.Error code: 429
Retryable: true
Type: QUEUE_FULLServiceSchemaError
Throw it if your service schema is not valid.Error code: 500
Error code: 500
Retryable: false
Type: GRACEFUL_STOP_TIMEOUTProtocolVersionMismatchError
Throw it if an old nodeID connected with older protocol version.const{ MoleculerError }= require("moleculer").Errors;
class MyBusinessError extends MoleculerError {
constructor(msg, data){
super(msg ||`This is my business error.`,500,"MY_BUSINESS_ERROR", data);
|
|
|
---|---|---|
|
|
|
Error or undefined |
const{ Regenerator, MoleculerError }= require("moleculer").Errors; const{ ServiceBroker }= require("moleculer");
class TimestampedError extends MoleculerError {
constructor(message, code, type, data, timestamp){ super(message, code, type, data);
} |
---|
module.exports = CustomRegenerator;
} |
---|
Production-ready
Use the moleculer.config.js during development or store common options. In production, you can overwrite the values with the environment variables!
Syntax |
---|
Note: It runs in this format in NPM scripts only. To call it directly from your console, use the ./node_modules/.bin/moleculer-runner --repl or node
./node_modules/moleculer/bin/moleculer-runner.js--replformat.
}
1. Load the config file defined in MOLECULER_CONFIG environment variable. If it does not exist, it throws an error. 2. It loads config file defined in CLI options. If it does not exist, it throws an error. Note that MOLECULER_CONFIG has priority over CLI meaning that if both are defined MOLECULER_CONFIG is the one that’s going to be used.
3. If not defined, it loads the moleculer.config.js file from the current directory. If it does not exist, it loads the moleculer.config.json file.
environment variables, use the MOL_ prefix and double underscore for nested properties in .env file. For example, to set the cacher prefix to MOL you should declare as MOL_CACHEROPTIONSPREFIX=MOL.
Configuration file
circuitBreaker:{
enabled:true
},
}; |
|
---|
This function runs with the MoleculerRunner instance as the this context. Useful if you need to access the flags passed to the runner. Check the MoleculerRunner source more details.
Environment variables
# Nested property
CIRCUITBREAKER_ENABLED=trueMETRICS=true
2. If SERVICEDIR & SERVICES env found, it loads the specified services from the SERVICEDIR directory.
3. If no SERVICEDIR, but SERVICES env found, it loads the specified services from the current directory.
SERVICEDIR=services SERVICES=math,post,user
It loads the math.service.js, post.service.js and user.service.js files from the services folder.
$ moleculer-runner services !services/others/**/*.service.js services/others/mandatory/main.service.js
Explanations:
Built-in clustering
Moleculer Runner has a built-in clustering function to start multiple instances from your broker. Example to start all services from the services folder in 4 instances.
-e,--env - Load environment variables from the ‘.env’ file from the current folder .-E,--envfile <filename>- Load environment variables from the specified file.
$ moleculer-runner --env # Load the default .env file from current directory
$ moleculer-runner --envfile .my-env # Load the specified .my-env file
-To use this feature, install the dotenv module with npm install dotenv --save command.