From b289f9ba62c931bc1900c286e26c0432eda33548 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Wed, 13 Jul 2022 09:31:34 +0100 Subject: [PATCH 1/4] Update to matrix-appservice-bridge 4.0.1 --- package.json | 2 +- yarn.lock | 118 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 96 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 25dea0c0..9647d842 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "escape-string-regexp": "^4.0.0", "js-yaml": "^3.14.0", "marked": "^1.2.2", - "matrix-appservice-bridge": "^3.1.0", + "matrix-appservice-bridge": "^4.0.1", "matrix-discord-parser": "0.1.5", "mime": "^2.4.6", "node-html-parser": "^1.2.19", diff --git a/yarn.lock b/yarn.lock index 19292986..ecef8afa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,6 +2,11 @@ # yarn lockfile v1 +"@alloc/quick-lru@^5.2.0": + version "5.2.0" + resolved "https://registry.yarnpkg.com/@alloc/quick-lru/-/quick-lru-5.2.0.tgz#7bf68b20c0a350f936915fcae06f58e32007ce30" + integrity sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw== + "@babel/code-frame@7.12.11": version "7.12.11" resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.12.11.tgz#f4ad435aa263db935b8f10f2c552d23fb716a63f" @@ -476,6 +481,11 @@ dependencies: "@types/node" "*" +"@types/retry@0.12.0": + version "0.12.0" + resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d" + integrity sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA== + "@types/scheduler@*": version "0.16.2" resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.2.tgz#1a62f89525723dde24ba1b01b092bf5df8ad4d39" @@ -783,6 +793,13 @@ aws4@^1.8.0: resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.11.0.tgz#d61f46d83b2519250e2784daf5b09479a8b41c59" integrity sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA== +axios@^0.23.0: + version "0.23.0" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.23.0.tgz#b0fa5d0948a8d1d75e3d5635238b6c4625b05149" + integrity sha512-NmvAE4i0YAv5cKq8zlDoPd1VLKAqX5oLuZKs8xkJa4qi6RGn0uhCYFjWtHHC9EM/MwOwYWOs53W+V0aqEXq1sg== + dependencies: + follow-redirects "^1.14.4" + balanced-match@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" @@ -1673,6 +1690,11 @@ expand-template@^2.0.3: resolved "https://registry.yarnpkg.com/expand-template/-/expand-template-2.0.3.tgz#6e14b3fcee0f3a6340ecb57d2e8918692052a47c" integrity sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg== +express-rate-limit@^6.2.0: + version "6.4.0" + resolved "https://registry.yarnpkg.com/express-rate-limit/-/express-rate-limit-6.4.0.tgz#b7066afe21157a012ed2b7c9adde386e712485cd" + integrity sha512-lxQRZI4gi3qAWTf0/Uqsyugsz57h8bd7QyllXBgJvd6DJKokzW7C5DTaNvwzvAQzwHGFaItybfYGhC8gpu0V2A== + express@^4.17.1: version "4.17.1" resolved "https://registry.yarnpkg.com/express/-/express-4.17.1.tgz#4491fc38605cf51f8629d39c2b5d026f98a4c134" @@ -1864,6 +1886,11 @@ fn.name@1.x.x: resolved "https://registry.yarnpkg.com/fn.name/-/fn.name-1.1.0.tgz#26cad8017967aea8731bc42961d04a3d5988accc" integrity sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw== +follow-redirects@^1.14.4: + version "1.15.1" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.1.tgz#0ca6a452306c9b276e4d3127483e29575e207ad5" + integrity sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA== + foreground-child@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/foreground-child/-/foreground-child-2.0.0.tgz#71b32800c9f15aa8f2f83f4a6bd9bff35d861a53" @@ -2315,6 +2342,22 @@ ini@~1.3.0: resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.8.tgz#a29da425b48806f34767a4efce397269af28432c" integrity sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew== +ip-address@^7.1.0: + version "7.1.0" + resolved "https://registry.yarnpkg.com/ip-address/-/ip-address-7.1.0.tgz#4a9c699e75b51cbeb18b38de8ed216efa1a490c5" + integrity sha512-V9pWC/VJf2lsXqP7IWJ+pe3P1/HCYGBMZrrnT62niLGjAfCbeiwXMUxaeHvnVlz19O27pvXP4azs+Pj/A0x+SQ== + dependencies: + jsbn "1.1.0" + sprintf-js "1.1.2" + +ip-cidr@^3.0.4: + version "3.0.10" + resolved "https://registry.yarnpkg.com/ip-cidr/-/ip-cidr-3.0.10.tgz#e1a039705196d84b43858f81a243fd70def9cefc" + integrity sha512-PXSsrRYirsuaCI1qBVyVXRLUIpNzxm76eHd3UvN5NXTMUG85GWGZpr6P+70mimc5e7Nfh/tShmjk0oSywErMWg== + dependencies: + ip-address "^7.1.0" + jsbn "^1.1.0" + ipaddr.js@1.9.1: version "1.9.1" resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-1.9.1.tgz#bff38543eeb8984825079ff3a2a8e6cbd46781b3" @@ -2518,13 +2561,18 @@ js-yaml@^3.13.1, js-yaml@^3.14.0: argparse "^1.0.7" esprima "^4.0.0" -js-yaml@^4.0.0: +js-yaml@^4.0.0, js-yaml@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602" integrity sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA== dependencies: argparse "^2.0.1" +jsbn@1.1.0, jsbn@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-1.1.0.tgz#b01307cb29b618a1ed26ec79e911f803c4da0040" + integrity sha512-4bYVV3aAMtDTTu4+xsDYa6sy9GyJ69/amsu9sYF2zqjiEoZA5xJi3BrfX3uY+/IekIu7MwdObdbDWpoZdBv3/A== + jsbn@~0.1.0: version "0.1.1" resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513" @@ -2733,34 +2781,39 @@ marked@^1.2.2: resolved "https://registry.yarnpkg.com/marked/-/marked-1.2.9.tgz#53786f8b05d4c01a2a5a76b7d1ec9943d29d72dc" integrity sha512-H8lIX2SvyitGX+TRdtS06m1jHMijKN/XjfH6Ooii9fvxMlh8QdqBfBDkGUpMWH2kQNrtixjzYUa3SH8ROTgRRw== -matrix-appservice-bridge@^3.1.0: - version "3.1.0" - resolved "https://registry.yarnpkg.com/matrix-appservice-bridge/-/matrix-appservice-bridge-3.1.0.tgz#ee7bed257d40bca1f47195c8111b5cb2d9703a3c" - integrity sha512-mNLWyqLigy5TboZw+EJ38mtsVlJUKa50HEjtyzx0aJLySNvnCgDFFlMahJWedOrZ3ipultnhtaRbtU1ZVsu6yA== +matrix-appservice-bridge@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/matrix-appservice-bridge/-/matrix-appservice-bridge-4.0.1.tgz#e12166dee2d25088ca5de2c5970bebfe6f4dea37" + integrity sha512-MIuApkRuS/y6gXJWPUVZETeb7Wpr2Oz7MgLL6v8kQp+4/VZWEPiqxiaXLZnbSnW4xiA2hxN3iVgajcHjf0Hcyg== dependencies: + "@alloc/quick-lru" "^5.2.0" + axios "^0.23.0" chalk "^4.1.0" + express-rate-limit "^6.2.0" extend "^3.0.2" + ip-cidr "^3.0.4" is-my-json-valid "^2.20.5" js-yaml "^4.0.0" - matrix-appservice "^0.8.0" + matrix-appservice "^0.10.0" matrix-bot-sdk "^0.6.0-beta.2" - matrix-js-sdk "^9.9.0" + matrix-js-sdk "^12.4.1" nedb "^1.8.0" nopt "^5.0.0" p-queue "^6.6.2" - prom-client "^13.1.0" + prom-client "^14.0.0" + uuid "^8.3.2" winston "^3.3.3" winston-daily-rotate-file "^4.5.1" -matrix-appservice@^0.8.0: - version "0.8.0" - resolved "https://registry.yarnpkg.com/matrix-appservice/-/matrix-appservice-0.8.0.tgz#c366cd9e38ff1c02594f37d5851a7fc57761ccae" - integrity sha512-mfgMpmV3dWLtzrd4V/3XtqUD0P44I/mTgsRreW5jMhSaUnnRGZbpptBw2q4/axbLjw2FarlWtOVgertDGMtccA== +matrix-appservice@^0.10.0: + version "0.10.0" + resolved "https://registry.yarnpkg.com/matrix-appservice/-/matrix-appservice-0.10.0.tgz#11b96dfda4567c04c8d14cd7b51110a3c6991c29" + integrity sha512-bxkvPaFXzuuRfqSQgIBbA6M+nKXeRJEeZlJfzjhP0RBBMl62HQTXqNLSVHhLRCdzKbr1OayrbDKL+BnmoghDDA== dependencies: "@types/express" "^4.17.8" body-parser "^1.19.0" express "^4.17.1" - js-yaml "^3.14.0" + js-yaml "^4.1.0" morgan "^1.10.0" matrix-bot-sdk@^0.6.0-beta.2: @@ -2792,17 +2845,17 @@ matrix-discord-parser@0.1.5: resolved "https://registry.yarnpkg.com/matrix-discord-parser/-/matrix-discord-parser-0.1.5.tgz#dd6a481a569567e8e30d70599d4dcb173261504c" integrity sha512-SilBNcNeJCrL6XEHVfbOhupNJICFdyFWfwv85Wtyh1Xxc0XPv4zK/gpEPogVeuKJD3TE0KT4iHq0nLgUJ8GBiQ== dependencies: - discord-markdown "git+https://github.com/Sorunome/discord-markdown.git#7958a03a952ed02cbd588b09eb04bc070b3a11f2" + discord-markdown "git://github.com/Sorunome/discord-markdown.git#7958a03a952ed02cbd588b09eb04bc070b3a11f2" escape-html "^1.0.3" got "^11.6.0" highlight.js "^9.18.1" node-html-parser "^1.4.5" unescape-html "^1.1.0" -matrix-js-sdk@^9.9.0: - version "9.11.0" - resolved "https://registry.yarnpkg.com/matrix-js-sdk/-/matrix-js-sdk-9.11.0.tgz#57ba60dfbcf2b32917eb6d7232d0e81cf0de0d3a" - integrity sha512-wP28ybOxyQ7lbC48QddRORYr8atEwbTqDOsu8H6u9jTTgB2qqczI/bkSoXHtutODuSeLY5x0UuwLcxVCy4yxVQ== +matrix-js-sdk@^12.4.1: + version "12.5.0" + resolved "https://registry.yarnpkg.com/matrix-js-sdk/-/matrix-js-sdk-12.5.0.tgz#3899f9d323c457d15a1fe436a2dfa07ae131cce2" + integrity sha512-HnEXoEhqpNp9/W9Ep7ZNZAubFlUssFyVpjgKfMOxxg+dYbBk5NWToHmAPQxlRUgrZ/rIMLVyMJROSCIthDbo2A== dependencies: "@babel/runtime" "^7.12.5" another-json "^0.2.0" @@ -2810,6 +2863,7 @@ matrix-js-sdk@^9.9.0: bs58 "^4.0.1" content-type "^1.0.4" loglevel "^1.7.1" + p-retry "^4.5.0" qs "^6.9.6" request "^2.88.2" unhomoglyph "^1.0.6" @@ -3257,6 +3311,14 @@ p-queue@^6.4.0, p-queue@^6.6.2: eventemitter3 "^4.0.4" p-timeout "^3.2.0" +p-retry@^4.5.0: + version "4.6.2" + resolved "https://registry.yarnpkg.com/p-retry/-/p-retry-4.6.2.tgz#9baae7184057edd4e17231cee04264106e092a16" + integrity sha512-312Id396EbJdvRONlngUx0NydfrIQ5lsYu0znKVUzVvArzEIt08V1qhtyESbGVd1FGX7UKtiFp5uwKZdM8wIuQ== + dependencies: + "@types/retry" "0.12.0" + retry "^0.13.1" + p-timeout@^3.2.0: version "3.2.0" resolved "https://registry.yarnpkg.com/p-timeout/-/p-timeout-3.2.0.tgz#c7e17abc971d2a7962ef83626b35d635acf23dfe" @@ -3508,10 +3570,10 @@ prom-client@^12.0.0: dependencies: tdigest "^0.1.1" -prom-client@^13.1.0: - version "13.2.0" - resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-13.2.0.tgz#99d13357912dd400f8911b77df19f7b328a93e92" - integrity sha512-wGr5mlNNdRNzEhRYXgboUU2LxHWIojxscJKmtG3R8f4/KiWqyYgXTLHs0+Ted7tG3zFT7pgHJbtomzZ1L0ARaQ== +prom-client@^14.0.0: + version "14.0.1" + resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-14.0.1.tgz#bdd9583e02ec95429677c0e013712d42ef1f86a8" + integrity sha512-HxTArb6fkOntQHoRGvv4qd/BkorjliiuO2uSWC2KC17MUTKYttWdDoXX/vxOhQdkoECEM9BBH0pj2l8G8kev6w== dependencies: tdigest "^0.1.1" @@ -3745,6 +3807,11 @@ responselike@^2.0.0: dependencies: lowercase-keys "^2.0.0" +retry@^0.13.1: + version "0.13.1" + resolved "https://registry.yarnpkg.com/retry/-/retry-0.13.1.tgz#185b1587acf67919d63b357349e03537b2484658" + integrity sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg== + reusify@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76" @@ -3992,6 +4059,11 @@ split2@^3.1.1: dependencies: readable-stream "^3.0.0" +sprintf-js@1.1.2: + version "1.1.2" + resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.1.2.tgz#da1765262bf8c0f571749f2ad6c26300207ae673" + integrity sha512-VE0SOVEHCk7Qc8ulkWw3ntAzXuqf7S2lvwQaDLRnUeIEaKNQJzV6BwmLKhOqT61aGhfUMrXeaBk+oDGCzvhcug== + sprintf-js@~1.0.2: version "1.0.3" resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" @@ -4395,7 +4467,7 @@ uuid@^3.3.2, uuid@^3.3.3: resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee" integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A== -uuid@^8.3.1: +uuid@^8.3.1, uuid@^8.3.2: version "8.3.2" resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== From 331d4878c676e594a5efbc722eb089a2a50ee491 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Wed, 13 Jul 2022 10:05:53 +0100 Subject: [PATCH 2/4] Update metrics to be functional --- src/discordas.ts | 2 +- src/metrics.ts | 62 +++++++++++++++++++++++++++++------------------- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/discordas.ts b/src/discordas.ts index 951b4fb9..3d0e4453 100644 --- a/src/discordas.ts +++ b/src/discordas.ts @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -import { Appservice, IAppserviceRegistration, LogService, MatrixClient } from "matrix-bot-sdk"; +import { Appservice, IAppserviceRegistration, LogService } from "matrix-bot-sdk"; import * as yaml from "js-yaml"; import * as fs from "fs"; import { DiscordBridgeConfig } from "./config"; diff --git a/src/metrics.ts b/src/metrics.ts index 4be179a3..d9b890e6 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -16,17 +16,16 @@ limitations under the License. /* eslint-disable max-classes-per-file, @typescript-eslint/no-empty-function */ import { Gauge, Counter, Histogram, collectDefaultMetrics, register } from "prom-client"; -import { Log } from "./log"; import { Appservice, IMetricContext, METRIC_MATRIX_CLIENT_FAILED_FUNCTION_CALL, METRIC_MATRIX_CLIENT_SUCCESSFUL_FUNCTION_CALL, FunctionCallContext, - METRIC_MATRIX_CLIENT_FUNCTION_CALL} from "matrix-bot-sdk"; + METRIC_MATRIX_CLIENT_FUNCTION_CALL, +} from "matrix-bot-sdk"; import { DiscordBridgeConfigMetrics } from "./config"; import * as http from "http"; -const log = new Log("BridgeMetrics"); const REQUEST_EXPIRE_TIME_MS = 30000; export interface IBridgeMetrics { @@ -37,6 +36,7 @@ export interface IBridgeMetrics { storeCall(method: string, cached: boolean); setRemoteMonthlyActiveUsers(rmau: number); setBridgeBlocked(isBlocked: boolean); + stop(); } export class DummyBridgeMetrics implements IBridgeMetrics { @@ -47,6 +47,7 @@ export class DummyBridgeMetrics implements IBridgeMetrics { public storeCall() {} public setRemoteMonthlyActiveUsers() {} public setBridgeBlocked() {} + public stop() {} } export class MetricPeg { @@ -63,36 +64,44 @@ export class MetricPeg { export class PrometheusBridgeMetrics implements IBridgeMetrics { private matrixCallCounter: Counter; + private matrixFailedCallCounter: Counter; private remoteCallCounter: Counter; private storeCallCounter: Counter; private presenceGauge: Gauge; private remoteRequest: Histogram; private matrixRequest: Histogram; - private requestsInFlight: Map; - private matrixRequestStatus: Map; + private requestsInFlight = new Map(); private httpServer: http.Server; private remoteMonthlyActiveUsers: Gauge; private bridgeBlocked: Gauge; public init(as: Appservice, config: DiscordBridgeConfigMetrics) { collectDefaultMetrics(); - // TODO: Bind this for every user. + this.httpServer = http.createServer((req, res) => { if (req.method !== "GET" || req.url !== "/metrics") { res.writeHead(404, "Not found"); res.end(); return; } + // eslint-disable-next-line @typescript-eslint/naming-convention res.writeHead(200, "OK", {"Content-Type": register.contentType}); res.write(register.metrics()); res.end(); }); + this.matrixCallCounter = new Counter({ help: "Count of matrix API calls made", labelNames: ["method", "result"], name: "matrix_api_calls", }); + this.matrixFailedCallCounter = new Counter({ + help: "Count of failed matrix API calls made", + labelNames: ["method", "result"], + name: "matrix_api_calls_failed", + }); register.registerMetric(this.matrixCallCounter); + register.registerMetric(this.matrixFailedCallCounter); this.remoteCallCounter = new Counter({ help: "Count of remote API calls made", @@ -136,7 +145,6 @@ export class PrometheusBridgeMetrics implements IBridgeMetrics { } }); }, REQUEST_EXPIRE_TIME_MS); - this.httpServer.listen(config.port, config.host); this.remoteMonthlyActiveUsers = new Gauge({ help: "Current count of remote users active this month", @@ -159,6 +167,7 @@ export class PrometheusBridgeMetrics implements IBridgeMetrics { onStartMetric: this.sdkStartMetric.bind(this), }); + this.httpServer.listen(config.port, config.host); return this; } @@ -196,35 +205,40 @@ export class PrometheusBridgeMetrics implements IBridgeMetrics { this.bridgeBlocked.set(isBlocked ? 1 : 0); } - private sdkStartMetric(metricName: string, context: IMetricContext) { - // We don't use this yet. - } - private sdkEndMetric(metricName: string, context: FunctionCallContext, timeMs: number) { if (metricName !== METRIC_MATRIX_CLIENT_FUNCTION_CALL) { return; // We don't handle any other type yet. } - const successFail = this.matrixRequestStatus.get(context.uniqueId)!; - this.matrixRequestStatus.delete(context.uniqueId); + const ctx = context as FunctionCallContext; this.matrixRequest.observe({ - method: context.functionName, - result: successFail, + method: ctx.functionName, }, timeMs); } - private sdkResetMetric(metricName: string, context: IMetricContext) { - // We don't use this yet. - } - private sdkIncrementMetric(metricName: string, context: IMetricContext, amount: number) { if (metricName === METRIC_MATRIX_CLIENT_SUCCESSFUL_FUNCTION_CALL) { - this.matrixRequestStatus.set(context.uniqueId, "success"); - } else if (metricName === METRIC_MATRIX_CLIENT_FAILED_FUNCTION_CALL) { - this.matrixRequestStatus.set(context.uniqueId, "failed"); + const ctx = context as FunctionCallContext; + this.matrixCallCounter.inc({method: ctx.functionName}, amount); + } + if (metricName === METRIC_MATRIX_CLIENT_FAILED_FUNCTION_CALL) { + const ctx = context as FunctionCallContext; + this.matrixFailedCallCounter.inc({method: ctx.functionName}, amount); } } - private sdkDecrementMetric(metricName: string, context: IMetricContext, amount: number) { - // We don't use this yet. + private sdkResetMetric() { + // Bot SDK doesn't use this yet. + } + + private sdkDecrementMetric() { + // Bot SDK doesn't use this yet. + } + + private sdkStartMetric() { + // We don't use this. + } + + public stop() { + this.httpServer.close(); } } From 0b887e63465d75a23d576abcfb34d9018e2d9662 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Wed, 13 Jul 2022 10:06:07 +0100 Subject: [PATCH 3/4] Support stopping the bridge gracefully --- src/bot.ts | 4 +++- src/discordas.ts | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 73c0345a..7b4513c9 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -423,7 +423,9 @@ export class DiscordBot { } } - public async stop(): Promise { + public stop(): void { + this.presenceHandler.Stop(); + this._bot?.destroy(); this._bot = undefined; } diff --git a/src/discordas.ts b/src/discordas.ts index 3d0e4453..2c6b8e29 100644 --- a/src/discordas.ts +++ b/src/discordas.ts @@ -1,5 +1,5 @@ /* -Copyright 2017 - 2019 matrix-appservice-discord +Copyright 2017 - 2019 matrix-appservice-discord, 2022 (C) The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -148,7 +148,7 @@ async function run(): Promise { if (config.database.roomStorePath || config.database.userStorePath) { log.error("The keys 'roomStorePath' and/or 'userStorePath' is still defined in the config. " + "Please see docs/bridge-migrations.md on " + - "https://github.com/Half-Shot/matrix-appservice-discord/"); + "https://github.com/matrix-org/matrix-appservice-discord/"); throw Error("Bridge has legacy configuration options and is unable to start"); } const registration = yaml.safeLoad(fs.readFileSync(registrationPath, "utf8")) as IAppserviceRegistration; @@ -181,6 +181,16 @@ async function run(): Promise { const roomhandler = discordbot.RoomHandler; const eventProcessor = discordbot.MxEventProcessor; + + process.once("SIGINT", () => { + log.info("Got signal to stop application.."); + appservice.stop(); + MetricPeg.get.stop(); + discordbot.stop(); + process.exit(0); + }); + + // 2020-12-07: If this fails to build in TypeScript with // "Namespace 'serveStatic' has no exported member 'RequestHandlerConstructor'.", // remove @types/express-serve-static-core and @types/serve-static from yarn.lock @@ -225,7 +235,6 @@ async function run(): Promise { await appservice.begin(); log.info(`Started listening on port ${port}`); - } run().catch((err) => { From bbd3187bdfda802572f025d941426c56f03eb3e4 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Wed, 13 Jul 2022 10:13:15 +0100 Subject: [PATCH 4/4] correct yarn.lock --- yarn.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn.lock b/yarn.lock index ecef8afa..5bc0d1ae 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2845,7 +2845,7 @@ matrix-discord-parser@0.1.5: resolved "https://registry.yarnpkg.com/matrix-discord-parser/-/matrix-discord-parser-0.1.5.tgz#dd6a481a569567e8e30d70599d4dcb173261504c" integrity sha512-SilBNcNeJCrL6XEHVfbOhupNJICFdyFWfwv85Wtyh1Xxc0XPv4zK/gpEPogVeuKJD3TE0KT4iHq0nLgUJ8GBiQ== dependencies: - discord-markdown "git://github.com/Sorunome/discord-markdown.git#7958a03a952ed02cbd588b09eb04bc070b3a11f2" + discord-markdown "git+https://github.com/Sorunome/discord-markdown.git#7958a03a952ed02cbd588b09eb04bc070b3a11f2" escape-html "^1.0.3" got "^11.6.0" highlight.js "^9.18.1"