This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): add SSR React rendering summary metric (#996)
- Loading branch information
Showing
7 changed files
with
176 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"editorconfig": true, | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
} |
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,82 @@ | ||
/* | ||
* Copyright 2023 American Express Travel Related Services Company, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* 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. | ||
*/ | ||
|
||
describe('summaries', () => { | ||
let Summary; | ||
|
||
function load() { | ||
jest.resetModules(); | ||
|
||
jest.mock('prom-client'); | ||
({ Summary } = require('prom-client')); | ||
|
||
return require('../../../src/server/metrics/summaries'); | ||
} | ||
|
||
describe('createSummary', () => { | ||
it('is a function', () => { | ||
const { createSummary } = load(); | ||
expect(createSummary).toBeInstanceOf(Function); | ||
}); | ||
|
||
it('creates a summary with the options', () => { | ||
const { createSummary } = load(); | ||
createSummary({ name: 'yup' }); | ||
expect(Summary).toHaveBeenCalledTimes(1); | ||
const summary = Summary.mock.instances[0]; | ||
expect(summary).toBeInstanceOf(Summary); | ||
}); | ||
|
||
it('does not create a new summary if one with the same name already exists', () => { | ||
const { createSummary } = load(); | ||
createSummary({ name: 'yup' }); | ||
expect(Summary).toHaveBeenCalledTimes(1); | ||
createSummary({ name: 'yup' }); | ||
expect(Summary).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('startSummaryTimer', () => { | ||
it('is a function', () => { | ||
const { startSummaryTimer } = load(); | ||
expect(startSummaryTimer).toBeInstanceOf(Function); | ||
}); | ||
|
||
it('throws an error if the summary does not exist', () => { | ||
const { startSummaryTimer } = load(); | ||
expect(() => startSummaryTimer('nope')).toThrowErrorMatchingInlineSnapshot( | ||
'"unable to find summary nope, please create it first"' | ||
); | ||
}); | ||
|
||
it('calls the startTimer method of the summary', () => { | ||
const { createSummary, startSummaryTimer } = load(); | ||
createSummary({ name: 'yup' }); | ||
const summary = Summary.mock.instances[0]; | ||
startSummaryTimer('yup'); | ||
expect(summary.startTimer).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('calls the startTimer method of the summary with the arguments', () => { | ||
const { createSummary, startSummaryTimer } = load(); | ||
createSummary({ name: 'yup' }); | ||
const summary = Summary.mock.instances[0]; | ||
startSummaryTimer('yup', 1, 'two', [null, null, null]); | ||
expect(summary.startTimer).toHaveBeenCalledTimes(1); | ||
expect(summary.startTimer).toHaveBeenCalledWith(1, 'two', [null, null, null]); | ||
}); | ||
}); | ||
}); |
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,29 @@ | ||
/* | ||
* Copyright 2023 American Express Travel Related Services Company, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* 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 { createSummary } from './summaries'; | ||
|
||
import createMetricNamespace from './create-metric-namespace'; | ||
|
||
const ssrNamespace = createMetricNamespace('ssr'); | ||
|
||
createSummary({ | ||
name: ssrNamespace('react_rendering', 'seconds'), | ||
help: 'time taken by React to render HTML', | ||
labelNames: ['renderMethodName'], | ||
}); | ||
|
||
export default ssrNamespace.getMetricNames(); |
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,39 @@ | ||
/* | ||
* Copyright 2023 American Express Travel Related Services Company, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* 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 { Summary } from 'prom-client'; | ||
|
||
const summaries = {}; | ||
|
||
function createSummary(opts) { | ||
const { name } = opts; | ||
if (summaries[name]) { | ||
return; | ||
} | ||
summaries[name] = new Summary(opts); | ||
} | ||
|
||
function startSummaryTimer(name, ...args) { | ||
if (!summaries[name]) { | ||
throw new Error(`unable to find summary ${name}, please create it first`); | ||
} | ||
return summaries[name].startTimer(...args); | ||
} | ||
|
||
export { | ||
createSummary, | ||
startSummaryTimer, | ||
}; |
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