UNPKG

4.95 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const __1 = require("..");
4/**
5 * Tracks metrics.
6 * @category Metrics
7 */
8class Metrics {
9 /**
10 * Creates a new Metrics handler with optional parent handler. When
11 * a parent handler is defined, metrics will be automatically published
12 * upwards to the parent.
13 * @param {Metrics} parent Optional parent for upstream metrics.
14 */
15 constructor(parent = null) {
16 this.listeners = [];
17 this.requestStartTimes = {};
18 this.uid = 0;
19 if (parent !== null) {
20 this.registerListener({
21 onIncrement(metricName, context, amount) {
22 parent.listeners.forEach(h => h.onIncrement(metricName, context, amount));
23 },
24 onDecrement(metricName, context, amount) {
25 parent.listeners.forEach(h => h.onDecrement(metricName, context, amount));
26 },
27 onReset(metricName, context) {
28 parent.listeners.forEach(h => h.onReset(metricName, context));
29 },
30 onStartMetric(metricName, context) {
31 parent.listeners.forEach(h => h.onStartMetric(metricName, context));
32 },
33 onEndMetric(metricName, context, timeMs) {
34 parent.listeners.forEach(h => h.onEndMetric(metricName, context, timeMs));
35 },
36 });
37 }
38 }
39 /**
40 * Registers a metric listener.
41 * @param {IMetricListener} listener The listener.
42 */
43 registerListener(listener) {
44 this.listeners.push(listener);
45 }
46 /**
47 * De-registers a metric listener.
48 * @param {IMetricListener} listener The listener.
49 */
50 unregisterListener(listener) {
51 const idx = this.listeners.indexOf(listener);
52 if (idx !== -1)
53 this.listeners.splice(idx, 1);
54 }
55 /**
56 * Starts a timer on a metric.
57 * @param {string} metricName The metric name.
58 * @param {IMetricContext} context The metric context. Expected to have a unique ID.
59 */
60 start(metricName, context) {
61 this.requestStartTimes[context.uniqueId] = new Date().getTime();
62 this.listeners.forEach(h => h.onStartMetric(metricName, context));
63 }
64 /**
65 * Ends a timer on a metric.
66 * @param {string} metricName The metric name.
67 * @param {IMetricContext} context The metric context. Expected to have a unique ID.
68 */
69 end(metricName, context) {
70 const timeMs = (new Date().getTime()) - this.requestStartTimes[context.uniqueId];
71 delete this.requestStartTimes[context.uniqueId];
72 this.listeners.forEach(h => h.onEndMetric(metricName, context, timeMs));
73 // Trim the context for logging
74 const trimmedContext = {};
75 for (const key of Object.keys(context)) {
76 if (key === 'client') {
77 const client = context[key];
78 trimmedContext[key] = `<MatrixClient ${client['userId'] || 'NoCachedUserID'}>`;
79 }
80 else if (key === 'intent') {
81 const intent = context[key];
82 trimmedContext[key] = `<Intent ${intent['userId'] || 'NoImpersonatedUserID'}>`;
83 }
84 else {
85 trimmedContext[key] = context[key];
86 }
87 }
88 __1.LogService.debug("Metrics", metricName, trimmedContext, timeMs);
89 }
90 /**
91 * Increments a metric.
92 * @param {string} metricName The metric name.
93 * @param {IMetricContext} context The metric context. Expected to have a unique ID.
94 * @param {number} amount The amount.
95 */
96 increment(metricName, context, amount) {
97 this.listeners.forEach(h => h.onIncrement(metricName, context, amount));
98 }
99 /**
100 * Decrements a metric.
101 * @param {string} metricName The metric name.
102 * @param {IMetricContext} context The metric context. Expected to have a unique ID.
103 * @param {number} amount The amount.
104 */
105 decrement(metricName, context, amount) {
106 this.listeners.forEach(h => h.onDecrement(metricName, context, amount));
107 }
108 /**
109 * Resets a metric.
110 * @param {string} metricName The metric name.
111 * @param {IMetricContext} context The metric context. Expected to have a unique ID.
112 */
113 reset(metricName, context) {
114 this.listeners.forEach(h => h.onReset(metricName, context));
115 }
116 /**
117 * Assigns a unique ID to the context object, returning it back.
118 * @param {IMetricContext} context The context to modify.
119 * @returns {IMetricContext} The provided context.
120 */
121 assignUniqueContextId(context) {
122 context.uniqueId = `${new Date().getTime()}-${this.uid++}`;
123 return context;
124 }
125}
126exports.Metrics = Metrics;