-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instrument metrics on server-side (#162)
* Server side implementation on metrics Signed-off-by: Louis Chu <[email protected]> * Refactor metrics to use sliding window Signed-off-by: Louis Chu <[email protected]> * Update unit test Signed-off-by: Louis Chu <[email protected]> --------- Signed-off-by: Louis Chu <[email protected]>
- Loading branch information
Showing
13 changed files
with
526 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { schema, TypeOf } from '@osd/config-schema'; | ||
|
||
import { METRIC_INTERVAL, DEFAULT_WINDOW_SIZE } from './server/metrics'; | ||
|
||
export const configSchema = schema.object({ | ||
metrics: schema.object({ | ||
metricInterval: schema.number({ defaultValue: METRIC_INTERVAL.ONE_MINUTE }), | ||
windowSize: schema.number({ min: 2, max: 10, defaultValue: DEFAULT_WINDOW_SIZE }), | ||
}), | ||
}); | ||
|
||
export type SearchRelevancePluginConfigType = TypeOf<typeof configSchema>; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export { MetricsServiceSetup, MetricsService } from './metrics_service'; | ||
|
||
export enum METRIC_INTERVAL { | ||
ONE_SECOND = 1000, | ||
ONE_MINUTE = 60000, | ||
} | ||
|
||
export const DEFAULT_WINDOW_SIZE = 3; | ||
|
||
export enum METRIC_NAME { | ||
RELEVANT_SEARCH = 'search_relevance', | ||
} | ||
|
||
export enum METRIC_ACTION { | ||
COMPARE_SEARCH = 'compare_search', | ||
SINGLE_SEARCH = 'single_search', | ||
FETCH_INDEX = 'fetch_index', | ||
} |
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,75 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { MetricsService, MetricsServiceSetup } from './'; | ||
|
||
describe('MetricsService', () => { | ||
let metricsService: MetricsService; | ||
let setup: MetricsServiceSetup; | ||
|
||
beforeEach(() => { | ||
metricsService = new MetricsService(); | ||
setup = metricsService.setup(); | ||
}); | ||
|
||
afterEach(() => { | ||
metricsService.stop(); | ||
}); | ||
|
||
describe('test addMetric and getStats', () => { | ||
it('should add metrics to the correct interval and ignore metrics in the future', () => { | ||
jest.useFakeTimers('modern'); | ||
jest.setSystemTime(0); | ||
setup.addMetric('component1', 'action1', 200, 100); | ||
jest.advanceTimersByTime(30000); | ||
setup.addMetric('component1', 'action1', 200, 200); | ||
setup.addMetric('component1', 'action1', 200, 300); | ||
jest.advanceTimersByTime(30000); | ||
setup.addMetric('component1', 'action1', 200, 400); | ||
|
||
const stats = setup.getStats(); | ||
expect(stats.data['component1']['action1'][200]).toEqual({ | ||
sum: 600, | ||
count: 3, | ||
}); | ||
expect(stats.overall.response_time_avg).toEqual(200); | ||
expect(stats.overall.requests_per_second).toEqual(0.05); | ||
expect(stats.component_counts['component1']).toEqual(3); | ||
expect(stats.status_code_counts[200]).toEqual(3); | ||
}); | ||
}); | ||
|
||
describe('resetMetrics', () => { | ||
it('should clear all metrics data', () => { | ||
setup.addMetric('component1', 'action1', 200, 100); | ||
metricsService.resetMetrics(); | ||
const stats = setup.getStats(); | ||
expect(stats.data).toEqual({}); | ||
expect(stats.overall).toEqual({ response_time_avg: 0, requests_per_second: 0 }); | ||
expect(stats.component_counts).toEqual({}); | ||
expect(stats.status_code_counts).toEqual({}); | ||
}); | ||
}); | ||
|
||
describe('trim', () => { | ||
it('should remove old metrics data', () => { | ||
jest.useFakeTimers('modern'); | ||
jest.setSystemTime(0); | ||
setup.addMetric('component1', 'action1', 200, 100); | ||
jest.advanceTimersByTime(60000); | ||
setup.addMetric('component1', 'action1', 200, 200); | ||
jest.advanceTimersByTime(60000); | ||
setup.addMetric('component1', 'action1', 200, 300); | ||
jest.advanceTimersByTime(60000); | ||
metricsService.trim(); | ||
jest.setSystemTime(0); | ||
const stats = setup.getStats(); | ||
expect(stats.data).toEqual({}); | ||
expect(stats.overall).toEqual({ response_time_avg: 0, requests_per_second: 0 }); | ||
expect(stats.component_counts).toEqual({}); | ||
expect(stats.status_code_counts).toEqual({}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.