Skip to content

Commit 6fb756d

Browse files
authored
feat(db): support MONGO_APP_NAME for per-worker MongoDB attribution (#557)
1 parent 8f68626 commit 6fb756d

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

.env.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ MONGO_ACCOUNTS_DATABASE_URI=mongodb://localhost:27017/hawk
1616
# MongoDB URL for connecting to events database
1717
MONGO_EVENTS_DATABASE_URI=mongodb://localhost:27017/hawk_events
1818

19+
# MongoDB appName for per-worker load attribution (e.g. hawk-worker-limiter)
20+
MONGO_APP_NAME=
21+
1922
# JWT secret key for projects tokens
2023
JWT_SECRET=qwerty
2124

lib/db/controller.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export class DatabaseController {
2020
*/
2121
private readonly connectionUri: string;
2222

23+
/**
24+
* Sent to MongoDB on handshake; overrides any `appName` query param in the URI
25+
*/
26+
private readonly appName?: string;
27+
2328
/**
2429
* GridFSBucket object
2530
* Used to store files in GridFS
@@ -30,12 +35,14 @@ export class DatabaseController {
3035
* Creates controller instance
3136
*
3237
* @param connectionUri - mongo URI for connection
38+
* @param appName - MongoDB appName, defaults to `process.env.MONGO_APP_NAME`
3339
*/
34-
constructor(connectionUri: string) {
40+
constructor(connectionUri: string, appName?: string) {
3541
if (!connectionUri) {
3642
throw new DatabaseConnectionError('Connection URI is not specified. Check .env');
3743
}
3844
this.connectionUri = connectionUri;
45+
this.appName = appName ?? process.env.MONGO_APP_NAME;
3946
}
4047

4148
/**
@@ -53,6 +60,7 @@ export class DatabaseController {
5360
this.connection = await connect(this.connectionUri, {
5461
useNewUrlParser: true,
5562
useUnifiedTopology: true,
63+
...(this.appName ? { appName: this.appName } : {}),
5664
});
5765
this.db = await this.connection.db();
5866

0 commit comments

Comments
 (0)