@meltwater/conversations-api-services 1.0.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.
@@ -0,0 +1,107 @@
1
+ import Prometheus from 'prom-client';
2
+ import logger from './logger.js';
3
+
4
+ const podMetricsByName = new Map();
5
+
6
+ const metricNames = ['irGetByDocumentId', 'irRatelimited', 'irSearch'];
7
+
8
+ // todo: make this a dynamic process
9
+ const irGetByDocumentIdHistogram = new Prometheus.Histogram({
10
+ name: 'irGetByDocumentId_Avg_Reponse_Time',
11
+ help: 'metric_help',
12
+ buckets: Prometheus.linearBuckets(0, 200, 40),
13
+ });
14
+
15
+ const irGetByDocumentIdCountHistogram = new Prometheus.Histogram({
16
+ name: 'irGetByDocumentId_Count',
17
+ help: 'metric_help',
18
+ buckets: Prometheus.linearBuckets(0, 1, 40),
19
+ });
20
+
21
+ metricNames.forEach((metricName) => {
22
+ podMetricsByName.set(
23
+ metricName,
24
+ new Prometheus.Counter({
25
+ name: metricName,
26
+ help: `${metricName}_help`,
27
+ })
28
+ );
29
+ });
30
+
31
+ class Metrics {
32
+ constructor() {
33
+ this.requestMetricsByName = new Map();
34
+ metricNames.forEach((metricName) => {
35
+ this.requestMetricsByName.set(metricName, []);
36
+ });
37
+ this.getMetrics = (metricName) => {
38
+ return {
39
+ podMetrics: podMetricsByName.get(metricName),
40
+ requestMetrics: this.requestMetricsByName.get(metricName),
41
+ };
42
+ };
43
+ }
44
+
45
+ count(metricName) {
46
+ const { podMetrics, requestMetrics } = this.getMetrics(metricName);
47
+
48
+ if (podMetrics) {
49
+ podMetrics.inc();
50
+ } else {
51
+ logger.error(`No pod level metric exists for ${metricName}`);
52
+ }
53
+
54
+ const metricObject = {
55
+ type: 'count',
56
+ timestamp: Date.now(),
57
+ };
58
+ if (requestMetrics) {
59
+ requestMetrics.push(metricObject);
60
+ }
61
+ return metricObject;
62
+ }
63
+
64
+ finishOperation(metricName, metricObject) {
65
+ const { requestMetrics } = this.getMetrics(metricName);
66
+
67
+ if (requestMetrics) {
68
+ const matchingMetric = requestMetrics.find(
69
+ (requestMetricExiting) =>
70
+ requestMetricExiting.timestamp === metricObject.timestamp
71
+ );
72
+ matchingMetric.duration = Date.now() - matchingMetric.timestamp;
73
+ }
74
+ }
75
+
76
+ async somethingCompleted(name = 'IR.getByDocumentId', start = true) {
77
+ // duration math
78
+ }
79
+
80
+ async finishRequest() {
81
+ const { requestMetrics } = this.getMetrics('irGetByDocumentId');
82
+ let arrHasDuration = requestMetrics.filter((metric) => metric.duration);
83
+ if (arrHasDuration.length) {
84
+ let avgDuration =
85
+ arrHasDuration.reduce(
86
+ (avg, metric) => avg + metric.duration,
87
+ 0
88
+ ) / arrHasDuration.length;
89
+ irGetByDocumentIdHistogram.observe(avgDuration);
90
+ }
91
+ if (requestMetrics.length > 0) {
92
+ irGetByDocumentIdCountHistogram.observe(requestMetrics.length);
93
+ }
94
+ }
95
+ }
96
+
97
+ function createEnum(values) {
98
+ const enumObject = {};
99
+ for (const val of values) {
100
+ enumObject[val] = val;
101
+ }
102
+ return Object.freeze(enumObject);
103
+ }
104
+
105
+ const metricN = createEnum(metricNames);
106
+
107
+ export { Metrics, metricN };