Skip to content

Commit 5ba5528

Browse files
fix(auth-server): safely access metrics methods in redis to prevent crash if disabled
1 parent fdc7cd0 commit 5ba5528

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

packages/fxa-auth-server/lib/redis.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class FxaRedis extends RedisShared {
4141
this.log.error('redis', new Error('invalid ttl on access token'));
4242
return;
4343
}
44-
this.metrics?.increment('redis.setAccessToken');
44+
this.metrics?.increment?.('redis.setAccessToken');
4545
const span = tracer.startSpan('redis.setAccessToken');
4646
const value = JSON.stringify(token);
4747
const result = await this.redis.setAccessToken(
@@ -62,7 +62,7 @@ class FxaRedis extends RedisShared {
6262
* @returns {Promise<boolean>} done
6363
*/
6464
async removeAccessToken(id) {
65-
this.metrics?.increment('redis.removeAccessToken');
65+
this.metrics?.increment?.('redis.removeAccessToken');
6666
const span = tracer.startSpan('redis.removeAccessToken');
6767
// This does not remove the id from the user's index
6868
// because getAccessTokens cleans up expired/missing tokens
@@ -76,7 +76,7 @@ class FxaRedis extends RedisShared {
7676
* @param {Buffer | string} uid
7777
*/
7878
async removeAccessTokensForPublicClients(uid) {
79-
this.metrics?.increment('redis.removeAccessTokensForPublicClients');
79+
this.metrics?.increment?.('redis.removeAccessTokensForPublicClients');
8080
const span = tracer.startSpan('redis.removeAccessTokensForPublicClients');
8181
const result = await this.redis.removeAccessTokensForPublicClients(
8282
hex(uid)
@@ -91,7 +91,7 @@ class FxaRedis extends RedisShared {
9191
* @param {Buffer | string} clientId
9292
*/
9393
async removeAccessTokensForUserAndClient(uid, clientId) {
94-
this.metrics?.increment('redis.removeAccessTokensForUserAndClient');
94+
this.metrics?.increment?.('redis.removeAccessTokensForUserAndClient');
9595
const span = tracer.startSpan('redis.removeAccessTokensForUserAndClient');
9696
const result = await this.redis.removeAccessTokensForUserAndClient(
9797
hex(uid),
@@ -106,7 +106,7 @@ class FxaRedis extends RedisShared {
106106
* @param {Buffer | string} uid
107107
*/
108108
async removeAccessTokensForUser(uid) {
109-
this.metrics?.increment('redis.removeAccessTokensForUser');
109+
this.metrics?.increment?.('redis.removeAccessTokensForUser');
110110
const span = tracer.startSpan('redis.removeAccessTokensForUser');
111111
const result = await this.redis.removeAccessTokensForUser(hex(uid));
112112
span.end();
@@ -119,7 +119,7 @@ class FxaRedis extends RedisShared {
119119
* @param {RefreshTokenMetadata} token
120120
*/
121121
async setRefreshToken(uid, tokenId, token) {
122-
this.metrics?.increment('redis.setRefreshToken');
122+
this.metrics?.increment?.('redis.setRefreshToken');
123123
const span = tracer.startSpan('redis.setRefreshToken');
124124
const p1 = this.redis.setRefreshToken(
125125
hex(uid),
@@ -140,7 +140,7 @@ class FxaRedis extends RedisShared {
140140
* @param {Buffer | string} tokenId
141141
*/
142142
async removeRefreshToken(uid, tokenId) {
143-
this.metrics?.increment('redis.removeRefreshToken');
143+
this.metrics?.increment?.('redis.removeRefreshToken');
144144
const span = await tracer.startSpan('redis.removeRefreshToken');
145145
const p1 = this.redis.hdel(hex(uid), hex(tokenId));
146146
const p2 = this.resolveInMs(p1, this.timeoutMs);
@@ -154,7 +154,7 @@ class FxaRedis extends RedisShared {
154154
* @param {Buffer | string} uid
155155
*/
156156
async removeRefreshTokensForUser(uid) {
157-
this.metrics?.increment('redis.removeRefreshTokensForUser');
157+
this.metrics?.increment?.('redis.removeRefreshTokensForUser');
158158
const span = tracer.startSpan('redis.removeRefreshTokensForUser');
159159
const p1 = this.redis.unlink(hex(uid));
160160
const p2 = this.resolveInMs(p1, this.timeoutMs);
@@ -164,13 +164,13 @@ class FxaRedis extends RedisShared {
164164
}
165165

166166
async get(key) {
167-
this.metrics?.increment('redis.get');
167+
this.metrics?.increment?.('redis.get');
168168
const span = tracer.startSpan('redis.get');
169169
const result = await this.redis.get(key);
170170

171171
if (result?.length > 0) {
172172
span.setAttribute('redis.get.size', result.length);
173-
this.metrics?.histogram('redis.get.size', result.length);
173+
this.metrics?.histogram?.('redis.get.size', result.length);
174174
}
175175
span.end();
176176
return result;

0 commit comments

Comments
 (0)