-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #367 from GSA/lc/1139-third-party-service-url
Third Party Services URLs
- Loading branch information
Showing
9 changed files
with
195 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import pino from 'pino'; | ||
import { logCount, logTimer } from './metric-utils'; | ||
|
||
|
||
const mockLogger = pino(); | ||
|
||
describe('Metric Utilities', () => { | ||
|
||
describe('logCount()', () => { | ||
afterAll(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
it('should call mockLogger.info with default metadata when no metadata is provided', async () => { | ||
const metricId = 'test-id'; | ||
const logMessage = 'test message'; | ||
logCount(mockLogger, null, metricId, logMessage); | ||
|
||
const expectedMetadata = { | ||
metricValue: 1, | ||
metricUnit: 'count', | ||
metricId, | ||
}; | ||
expect(mockLogger.info).toHaveBeenCalledWith(expectedMetadata, logMessage); | ||
}); | ||
|
||
it('should call mockLogger.info with provided metadata merged with defaults', () => { | ||
const metricId = 'metric2'; | ||
const logMessage = 'Another log message'; | ||
const metadata = { | ||
metricValue: 5, | ||
additionalInfo: 'Extra data' | ||
}; | ||
logCount(mockLogger, metadata, metricId, logMessage); | ||
|
||
const expectedMetadata = { | ||
metricValue: 5, | ||
metricUnit: 'count', | ||
metricId, | ||
additionalInfo: 'Extra data' | ||
}; | ||
expect(mockLogger.info).toHaveBeenCalledWith(expectedMetadata, logMessage); | ||
}); | ||
}); | ||
|
||
describe('logTimer()', () => { | ||
let timer; | ||
beforeEach(() => { | ||
timer = logTimer(mockLogger); | ||
}); | ||
|
||
it('should create a timer with a start time', () => { | ||
expect(timer.start).toBeDefined(); | ||
expect(timer.start).toBeGreaterThan(0); | ||
}); | ||
|
||
it('should call mockLogger.info with correct metadata and message after log is called', () => { | ||
const metricId = 'timer1'; | ||
const logMessage = 'Duration: Any ms'; | ||
const metadata = { additionalInfo: 'test' }; | ||
|
||
timer.log(metadata, metricId, logMessage); | ||
|
||
// Calculate the expected duration | ||
const expectedDuration = expect.any(Number); | ||
const expectedMetadata = { | ||
...metadata, | ||
metricValue: expectedDuration, | ||
metricUnit: 'ms', | ||
metricId, | ||
}; | ||
|
||
// The log message should replace {metricValue} with the duration | ||
const expectedMessage = `Duration: ${expectedDuration} ms`; | ||
|
||
expect(mockLogger.info).toHaveBeenCalledWith(expectedMetadata, expectedMessage); | ||
}); | ||
|
||
}); | ||
|
||
}); |
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,41 @@ | ||
import { Logger } from 'pino'; | ||
|
||
export function logCount( logger: Logger, metadata: any, metricId: string, logMessage: string ) { | ||
if( !metadata ) { | ||
metadata = {}; | ||
} | ||
const metaDefaults = { | ||
metricValue: 1, | ||
metricUnit: "count", | ||
}; | ||
const finalMetadata = { | ||
...metaDefaults, | ||
...metadata, | ||
metricId, | ||
}; | ||
logger.info( finalMetadata, logMessage ); | ||
}; | ||
|
||
export function logTimer( logger: Logger ) { | ||
const timer = { | ||
start: Date.now(), | ||
log: (metadata: any, metricId: string, logMessage: string, decimalPrecision = 0 ) => { | ||
const duration = Date.now() - timer.start; | ||
if( !metadata ) { | ||
metadata = {}; | ||
} | ||
const finalMetadata = { | ||
...metadata, | ||
metricUnit: "ms", | ||
metricValue: duration, | ||
metricId, | ||
}; | ||
|
||
const finalMessage = logMessage | ||
.replace(/\{metricValue\}/g, duration.toString()); | ||
|
||
logger.info( finalMetadata, finalMessage ); | ||
} | ||
}; | ||
return timer; | ||
}; |
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