UNPKG

3.21 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const names_1 = require("./names");
4/**
5 * Times a MatrixClient function call for metrics.
6 * @category Metrics
7 */
8function timedMatrixClientFunctionCall() {
9 return function (target, propertyKey, descriptor) {
10 const originalMethod = descriptor.value;
11 descriptor.value = function (...args) {
12 const metrics = this.metrics;
13 const context = metrics.assignUniqueContextId({
14 functionName: propertyKey,
15 client: this,
16 });
17 metrics.start(names_1.METRIC_MATRIX_CLIENT_FUNCTION_CALL, context);
18 let result;
19 let exception;
20 try {
21 result = originalMethod.apply(this, args);
22 }
23 catch (e) {
24 exception = e;
25 result = Promise.reject(e);
26 }
27 let promise = result;
28 if (!(result instanceof Promise) && result !== null && result !== undefined) {
29 promise = Promise.resolve(result);
30 }
31 promise
32 .then(() => metrics.increment(names_1.METRIC_MATRIX_CLIENT_SUCCESSFUL_FUNCTION_CALL, context, 1))
33 .catch(() => metrics.increment(names_1.METRIC_MATRIX_CLIENT_FAILED_FUNCTION_CALL, context, 1))
34 .finally(() => metrics.end(names_1.METRIC_MATRIX_CLIENT_FUNCTION_CALL, context));
35 if (exception)
36 throw exception;
37 return result;
38 };
39 };
40}
41exports.timedMatrixClientFunctionCall = timedMatrixClientFunctionCall;
42/**
43 * Times an Intent function call for metrics.
44 * @category Metrics
45 */
46function timedIntentFunctionCall() {
47 return function (target, propertyKey, descriptor) {
48 const originalMethod = descriptor.value;
49 descriptor.value = function (...args) {
50 const metrics = this.metrics;
51 const context = metrics.assignUniqueContextId({
52 functionName: propertyKey,
53 client: this.client,
54 intent: this,
55 });
56 metrics.start(names_1.METRIC_INTENT_FUNCTION_CALL, context);
57 let result;
58 let exception;
59 try {
60 result = originalMethod.apply(this, args);
61 }
62 catch (e) {
63 exception = e;
64 result = Promise.reject(e);
65 }
66 let promise = result;
67 if (!(result instanceof Promise) && result !== null && result !== undefined) {
68 promise = Promise.resolve(result);
69 }
70 promise
71 .then(() => metrics.increment(names_1.METRIC_INTENT_SUCCESSFUL_FUNCTION_CALL, context, 1))
72 .catch(() => metrics.increment(names_1.METRIC_INTENT_FAILED_FUNCTION_CALL, context, 1))
73 .finally(() => metrics.end(names_1.METRIC_INTENT_FUNCTION_CALL, context));
74 if (exception)
75 throw exception;
76 return result;
77 };
78 };
79}
80exports.timedIntentFunctionCall = timedIntentFunctionCall;