-
Notifications
You must be signed in to change notification settings - Fork 532
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add api counter for riddler fetchTenantKey metric (#16423)
Currently, fetchTenantKeyMetric generates many logs in our service. We decided to reduce the number of logs in riddler by using apiCounter to count these metrics and log the values after a given period of time.
- Loading branch information
Showing
10 changed files
with
138 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
server/routerlicious/packages/services-utils/src/apiCounters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
export interface IApiCounters { | ||
initializeCounter(apiName: string): void; | ||
initializeCounters(apiNames: string[]): void; | ||
incrementCounter(apiName: string, incrementBy?: number): void; | ||
decrementCounter(apiName: string, decrementBy?: number): void; | ||
resetAllCounters(): void; | ||
getCounter(apiName: string): number | undefined; | ||
getCounters(): Record<string, number>; | ||
countersAreActive: boolean; | ||
} | ||
|
||
export class InMemoryApiCounters implements IApiCounters { | ||
private readonly apiCounters = new Map<string, number>(); | ||
constructor(apiNames?: string[]) { | ||
if (apiNames && apiNames.length > 0) { | ||
this.initializeCounters(apiNames); | ||
} | ||
} | ||
|
||
public initializeCounter(apiName: string): void { | ||
this.apiCounters.set(apiName, 0); | ||
} | ||
|
||
public initializeCounters(apiNames: string[]): void { | ||
apiNames.forEach((apiName) => this.apiCounters.set(apiName, 0)); | ||
} | ||
|
||
public incrementCounter(apiName: string, incrementBy = 1): void { | ||
if (incrementBy < 1) { | ||
return; | ||
} | ||
const currentValue = this.apiCounters.get(apiName) ?? 0; | ||
this.apiCounters.set(apiName, currentValue + incrementBy); | ||
} | ||
|
||
public decrementCounter(apiName: string, decrementBy = 1): void { | ||
if (decrementBy < 1) { | ||
return; | ||
} | ||
const currentValue = this.apiCounters.get(apiName) ?? 0; | ||
const tentativeUpdate = currentValue - decrementBy; | ||
// If the decrement would result in a negative number, reset it to 0. | ||
const updatedValue = tentativeUpdate > 0 ? tentativeUpdate : 0; | ||
this.apiCounters.set(apiName, updatedValue); | ||
} | ||
|
||
public resetAllCounters(): void { | ||
this.apiCounters.forEach((_: number, key) => this.apiCounters.set(key, 0)); | ||
} | ||
|
||
public getCounter(apiName: string): number | undefined { | ||
return this.apiCounters.get(apiName); | ||
} | ||
|
||
public getCounters(): Record<string, number> { | ||
return Object.fromEntries(this.apiCounters); | ||
} | ||
|
||
get countersAreActive(): boolean { | ||
for (const v of this.apiCounters.values()) { | ||
if (v > 0) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters