@lucaapp/service-utils 1.2.3 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './lib/kafka';
2
2
  export * from './lib/serviceIdentity';
3
+ export * from './lib/metrics';
package/dist/index.js CHANGED
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./lib/kafka"), exports);
18
18
  __exportStar(require("./lib/serviceIdentity"), exports);
19
+ __exportStar(require("./lib/metrics"), exports);
@@ -30,7 +30,24 @@ const util_1 = require("util");
30
30
  const serviceIdentity_1 = require("../serviceIdentity");
31
31
  const events_1 = require("./events");
32
32
  const utils_1 = require("../../utils/utils");
33
+ const metrics_1 = require("../metrics");
33
34
  const KEY_ALG = 'ES256';
35
+ const messageProducedSizeCounter = new metrics_1.metricsClient.Histogram({
36
+ name: 'kafka_message_produce_size_bytes',
37
+ help: 'Size of messages produced',
38
+ labelNames: ['topic'],
39
+ buckets: [64, 128, 256, 512, 1024, 2048, 4096, 8182],
40
+ });
41
+ const messageConsumedCounter = new metrics_1.metricsClient.Counter({
42
+ name: 'kafka_message_consume_count',
43
+ help: 'Total number of messages consumed',
44
+ labelNames: ['topic', 'groupId'],
45
+ });
46
+ const messageAcknowledgedCounter = new metrics_1.metricsClient.Counter({
47
+ name: 'kafka_message_consume_ack_count',
48
+ help: 'Total number of messages acknowledged',
49
+ labelNames: ['topic', 'groupId'],
50
+ });
34
51
  const getIssuer = (kafkaTopic) => {
35
52
  return events_1.MessageIssuer[kafkaTopic].valueOf();
36
53
  };
@@ -46,7 +63,7 @@ class KafkaClient {
46
63
  }
47
64
  };
48
65
  this.getTopic = async (kafkaTopic) => {
49
- const topic = `${this.environment}_${this.serviceIdentity.identityName}_${kafkaTopic}`;
66
+ const topic = `${this.environment}_${getIssuer(kafkaTopic)}_${kafkaTopic}`;
50
67
  await this.ensureTopics(topic);
51
68
  return topic;
52
69
  };
@@ -136,9 +153,15 @@ class KafkaClient {
136
153
  await consumer.run({
137
154
  autoCommit: true,
138
155
  eachMessage: async ({ message }) => {
156
+ messageConsumedCounter
157
+ .labels({ topic: kafkaTopic.valueOf(), groupId })
158
+ .inc();
139
159
  const decryptedValue = await this.decryptValue(kafkaTopic, message.value);
140
160
  await this.verifySignature(kafkaTopic, decryptedValue, message.headers);
141
161
  const value = this.parseValue(decryptedValue);
162
+ messageAcknowledgedCounter
163
+ .labels({ topic: kafkaTopic.valueOf(), groupId })
164
+ .inc();
142
165
  this.logger.debug({ key: message.key, value, timestamp: message.timestamp }, 'Record received');
143
166
  await handler({ ...message, value });
144
167
  },
@@ -167,6 +190,9 @@ class KafkaClient {
167
190
  };
168
191
  await this.producer.send(producerRecord);
169
192
  this.logger.debug(producerRecord, 'Record sent');
193
+ messageProducedSizeCounter
194
+ .labels({ topic })
195
+ .observe(Buffer.byteLength(encryptedValue));
170
196
  }
171
197
  catch (error) {
172
198
  throw (0, utils_1.logAndGetError)(this.logger, `Could not create producer for topic=${topic}`, error);
@@ -0,0 +1 @@
1
+ export * from './metricsClient';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./metricsClient"), exports);
@@ -0,0 +1 @@
1
+ export * as metricsClient from 'prom-client';
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.metricsClient = void 0;
27
+ exports.metricsClient = __importStar(require("prom-client"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lucaapp/service-utils",
3
- "version": "1.2.3",
3
+ "version": "1.3.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -34,11 +34,12 @@
34
34
  "typescript": "^4.7.4"
35
35
  },
36
36
  "dependencies": {
37
- "@types/express": "4.17.13",
38
37
  "@hapi/boom": "^10.0.0",
38
+ "@types/express": "4.17.13",
39
39
  "axios": "^0.27.2",
40
40
  "jose": "4.9.2",
41
41
  "kafkajs": "2.1.0",
42
- "moment": "^2.29.4"
42
+ "moment": "^2.29.4",
43
+ "prom-client": "14.1.0"
43
44
  }
44
45
  }