metrics-sender
Advanced tools
Comparing version 0.0.8 to 0.0.10
@@ -10,2 +10,6 @@ export interface IMetrics { | ||
/** | ||
* Close UDP connection | ||
*/ | ||
close(): void; | ||
/** | ||
* Add new tag to list of tags | ||
@@ -12,0 +16,0 @@ * |
@@ -9,3 +9,3 @@ import { IMetrics } from "./IMetrics"; | ||
export declare class Metrics implements IMetrics { | ||
private measurement; | ||
private readonly measurement; | ||
private tags; | ||
@@ -43,2 +43,6 @@ private sender; | ||
/** | ||
* Close UDP connection | ||
*/ | ||
close(): void; | ||
/** | ||
* Creates line protocol compliant message | ||
@@ -52,3 +56,3 @@ * | ||
*/ | ||
private createLine(fieldSet, includeTime?); | ||
private createLine; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Metrics = void 0; | ||
const ObjectUtils_1 = require("hb-utils/dist/lib/ObjectUtils"); | ||
@@ -55,2 +56,8 @@ const StringUtils_1 = require("hb-utils/dist/lib/StringUtils"); | ||
/** | ||
* Close UDP connection | ||
*/ | ||
close() { | ||
this.sender.close(); | ||
} | ||
/** | ||
* Creates line protocol compliant message | ||
@@ -57,0 +64,0 @@ * |
@@ -30,3 +30,3 @@ export declare class Sender { | ||
*/ | ||
private refreshServerIp(self); | ||
private refreshServerIp; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Sender = void 0; | ||
const dgram = require("dgram"); | ||
@@ -4,0 +5,0 @@ const dns = require("dns"); |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
@@ -15,26 +16,37 @@ }); | ||
require("mocha"); | ||
let server = null; | ||
describe("Metrics", () => { | ||
it("should create line in defined format", () => __awaiter(this, void 0, void 0, function* () { | ||
const tags = { name: "appname", host: "10.28.30.45" }; | ||
const m = new Metrics_1.Metrics("measurementName", tags); | ||
const lineWithTime = yield m.send({ value1: 10000, value2: "some value" }); | ||
const lineWithoutTime = yield m.send({ value1: 10000, value2: "some value" }, false); | ||
const expectedWithoutTime = "measurementName,name=appname,host=10.28.30.45 value1=10000,value2=some\\ value"; | ||
chai_1.assert.equal(expectedWithoutTime, lineWithoutTime); | ||
// Check if line contains timestamp at the end if generated with it | ||
chai_1.assert.lengthOf(lineWithTime, expectedWithoutTime.length + 20); | ||
chai_1.assert.lengthOf(lineWithTime.substr(0, expectedWithoutTime.length), expectedWithoutTime.length); | ||
})); | ||
it("should create line in defined format with lately added and removed tag", () => __awaiter(this, void 0, void 0, function* () { | ||
const tags = { name: "appname", host: "10.28.30.45" }; | ||
const m = new Metrics_1.Metrics("measurementName", tags); | ||
m.addTag("added", "foo"); | ||
m.removeTag("invalid"); | ||
m.removeTag("host"); | ||
const line = yield m.send({ value1: 10000, value2: "some value" }, false); | ||
const withoutTime = "measurementName,name=appname,added=foo value1=10000,value2=some\\ value"; | ||
chai_1.assert.equal(line, withoutTime); | ||
})); | ||
beforeEach(() => { | ||
server = dgram.createSocket("udp4"); | ||
server.bind(3333, "127.0.0.1"); | ||
}); | ||
afterEach(() => { | ||
server.close(); | ||
}); | ||
it("should create line in defined format", () => { | ||
server.on("listening", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const tags = { name: "appname", host: "10.28.30.45" }; | ||
const m = new Metrics_1.Metrics("measurementName", tags); | ||
const lineWithTime = yield m.send({ value1: 10000, value2: "some value" }); | ||
const lineWithoutTime = yield m.send({ value1: 10000, value2: "some value" }, false); | ||
const expectedWithoutTime = "measurementName,name=appname,host=10.28.30.45 value1=10000,value2=some\\ value"; | ||
chai_1.assert.equal(expectedWithoutTime, lineWithoutTime); | ||
// Check if line contains timestamp at the end if generated with it | ||
chai_1.assert.lengthOf(lineWithTime, expectedWithoutTime.length + 20); | ||
chai_1.assert.lengthOf(lineWithTime.substr(0, expectedWithoutTime.length), expectedWithoutTime.length); | ||
})); | ||
}); | ||
it("should create line in defined format with lately added and removed tag", () => { | ||
server.on("listening", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const tags = { name: "appname", host: "10.28.30.45" }; | ||
const m = new Metrics_1.Metrics("measurementName", tags, "127.0.0.1", 3333); | ||
m.addTag("added", "foo"); | ||
m.removeTag("invalid"); | ||
m.removeTag("host"); | ||
const line = yield m.send({ value1: 10000, value2: "some value" }, false); | ||
const withoutTime = "measurementName,name=appname,added=foo value1=10000,value2=some\\ value"; | ||
chai_1.assert.equal(line, withoutTime); | ||
})); | ||
}); | ||
it("should send and receive udp packet", (done) => { | ||
const server = dgram.createSocket("udp4"); | ||
server.on("listening", () => { | ||
@@ -51,5 +63,4 @@ const tags = { name: "appname", host: "10.28.30.45" }; | ||
}); | ||
server.bind(3333, "127.0.0.1"); | ||
}); | ||
}); | ||
//# sourceMappingURL=Metrics.test.js.map |
{ | ||
"name": "metrics-sender", | ||
"version": "0.0.8", | ||
"version": "0.0.10", | ||
"description": "Library for sending application metrics", | ||
@@ -27,11 +27,11 @@ "main": "./dist/src/Metrics.js", | ||
"devDependencies": { | ||
"@types/chai": "4.0.4", | ||
"@types/mocha": "2.2.43", | ||
"@types/node": "8.0.20", | ||
"chai": "3.5.0", | ||
"mocha": "3.5.2", | ||
"ts-node": "3.3.0", | ||
"tslint": "5.7.0", | ||
"typescript": "2.5.2" | ||
"@types/chai": "4.2.18", | ||
"@types/mocha": "8.2.2", | ||
"@types/node": "15.12.2", | ||
"chai": "4.3.4", | ||
"mocha": "9.0.0", | ||
"ts-node": "10.0.0", | ||
"tslint": "6.1.3", | ||
"typescript": "4.3.2" | ||
} | ||
} |
141
README.md
@@ -1,45 +0,6 @@ | ||
#amqplib-plus | ||
#metrics-sender | ||
Amqplib-plus amplifies the original npm amqplib library (https://www.npmjs.com/package/amqplib) with OOP approach. | ||
Contains d.ts files to be easily used in your typescript code. | ||
Amqplib-plus adds following features: | ||
- amqp connection, publisher and consumer object oriented classes | ||
- connection auto-reconnect | ||
- easy to use publisher | ||
- easy to use consumer that runs user defined function on every consumed message | ||
## How to install: | ||
`$ npm install amqplib-plus` | ||
`$ npm install metrics-sender` | ||
## Basic classes to be used in your code: | ||
- Connection: | ||
- amqplib-plus/dist/lib/Connection | ||
- wraps connection to rabbitmq instance and auto-reconnecting | ||
- Connection instance to be passed to Publisher, Consumer | ||
- Publisher: | ||
- amqplib-plus/dist/lib/Publisher | ||
- wraps logic for publishing messages to broker (publish and sedToQueue methods can be used) | ||
- params: | ||
- connection instance | ||
- prepare function for queues/exchanges/binds etc. creation using channel which is called righ after publisher instance creation | ||
- AssertPublisher: | ||
- amqplib-plus/dist/lib/AssertPublisher | ||
- extends Publisher and asserts queue before every publish/sendToQueue | ||
- params: | ||
- connection instance | ||
- prepare function for queues/exchanges/binds etc. creation using channel which is called righ after publisher instance creation | ||
- params for new queues assertion | ||
- Consumer: | ||
- amqplib-plus/dist/lib/Consumer | ||
- abstract class for your custom consumer classes to be built on | ||
- SimpleConsumer: | ||
- amqplib-plus/dist/lib/SimpleConsumer | ||
- basic example consumer implementation that calls process function for every received messages and acks it afterwards | ||
- params: | ||
- connection instance | ||
- prepare function for queues/exchanges/binds etc. creation using channel which is called righ after publisher instance creation | ||
- process message function | ||
## How to use (Typescript) | ||
@@ -49,96 +10,24 @@ | ||
```typescript | ||
import {Channel, Message} from "amqplib"; | ||
import Connection from "amqplib-plus/dist/lib/Connection"; | ||
import Publisher from "amqplib-plus/dist/lib/Publisher"; | ||
import SimpleConsumer from "amqplib-plus/dist/lib/SimpleConsumer"; | ||
import {Metrics} from "metrics-sender"; | ||
// Define initial values | ||
const tags = {name: "app", host: "1.1.1.1", foo: "bar"}; | ||
const fields = {value1: 10000, value2: "some value"}; | ||
// Set connection details | ||
const conn = new Connection({ | ||
host: "localhost", | ||
port: 5672, | ||
user: "guest", | ||
pass: "guest", | ||
vhost: "/", | ||
heartbeat: 60, | ||
}); | ||
const m = new Metrics("measurementName", tags, "localhost", 3333); | ||
// Create queues, exchanges and whatever else you want to have prepared before first publish | ||
const prePublish = async (ch: Channel) => { | ||
// set up your queues, exchanges etc. here | ||
await ch.assertQueue("queueName", {}); | ||
await ch.assertExchange("exName", "direct", {}); | ||
await ch.bindQueue("queueName", "exName", "rk"); | ||
// ... | ||
} | ||
// Add more tag | ||
m.addTag("added", "foo"); | ||
// Creates new publisher instance and publish message using it | ||
const publisher = new Publisher(conn, prePublish); | ||
publisher.publish("exName", "rk", new Buffer("test"), {}); | ||
// Or remove some unnecessary tag | ||
m.removeTag("host"); | ||
// Create new consumer instance and start consumption from queue | ||
// Simple consumer calls process method on every received message and then always acks the message. | ||
// If you need further logic for ack/nack/reject you must implement custom consumer that inherits basic Consumer class like SimpleConsumer does. | ||
const preConsume = async (ch: Channel) => { | ||
// set up your queues, exchanges etc. here | ||
await ch.assertQueue("queueName", {}); | ||
// ... | ||
}; | ||
const process = (msg: Message) => { | ||
// do whatever you want with the message | ||
console.log(msg); | ||
}; | ||
const consumer = new SimpleConsumer(conn, preConsume, process); | ||
consumer.consume("queueName", {}); | ||
// And finally send UDP packet | ||
m.send(fields); | ||
``` | ||
## How to use (ES6, Node 8 and above) | ||
```ecmascript 6 | ||
const Connection = require("amqplib-plus/dist/lib/Connection"); | ||
const Publisher = require("amqplib-plus/dist/lib/Publisher"); | ||
const SimpleConsumer = require("amqplib-plus/dist/lib/SimpleConsumer"); | ||
const conn = new Connection.default({ | ||
host: "localhost", | ||
port: 5672, | ||
user: "guest", | ||
pass: "guest", | ||
vhost: "/", | ||
heartbeat: 60, | ||
}); | ||
// Create queues, exchanges and whatever else you want to have prepared before first publish | ||
const prePublish = async (ch) => { | ||
// set up your queues, exchanges etc. here | ||
return ch.assertQueue("queueName", {}); | ||
// ... | ||
} | ||
// Creates new publisher instance and publish message using it | ||
const publisher = new Publisher.default(conn, prePublish); | ||
publisher.sendToQueue("queueName", new Buffer("test"), {}); | ||
// Create new consumer instance and start consumption from queue | ||
// Simple consumer calls process method on every received message and then always acks the message. | ||
// If you need further logic for ack/nack/reject you must implement custom consumer that inherits basic Consumer class like SimpleConsumer does. | ||
const preConsume = async (ch) => { | ||
// set up your queues, exchanges etc. here | ||
return ch.assertQueue("queueName", {}); | ||
// ... | ||
}; | ||
const process = (msg) => { | ||
// do whatever you want with the message | ||
console.log(msg); | ||
}; | ||
const consumer = new SimpleConsumer.default(conn, preConsume, process); | ||
consumer.consume("queueName", {}); | ||
``` | ||
## How to contribute: | ||
Create pull request to `https://github.com/hanaboso/amqplib-plus` repository. | ||
Create pull request to `https://github.com/hanaboso/metrics-sender` repository. | ||
Please note that this lib is written in typescript. | ||
## How to test | ||
If you have running rabbitmq instance, set env variable values defined used in test/config.ts and run: `$ npm test`. | ||
Alternatively you can run: `$ make test` which will start rabbitmq instance for you and run tests in docker containers. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
340
54197
33