-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9205f2e
commit 743a562
Showing
10 changed files
with
199 additions
and
8 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 |
---|---|---|
|
@@ -105,3 +105,5 @@ dist | |
|
||
# reports of jest tests | ||
reports | ||
|
||
src/version.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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,10 @@ | ||
import { writeFileSync } from 'fs'; | ||
import { readPackageJsonSync } from '@map-colonies/read-pkg'; | ||
|
||
const packageJson = readPackageJsonSync(); | ||
const version = packageJson.version; | ||
const versionFile = 'src/version.ts'; | ||
|
||
const content = `/* prettier-ignore */\n/* eslint-disable*/\nexport const PACKAGE_VERSION = '${version}';\n`; | ||
|
||
writeFileSync(versionFile, content); |
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,59 @@ | ||
import type { Registry } from 'prom-client'; | ||
import { getOptions } from './options'; | ||
import { PACKAGE_VERSION } from './version'; | ||
import { LOCAL_SCHEMAS_PACKAGE_VERSION } from './constants'; | ||
import { createDebug } from './utils/debug'; | ||
import { createConfigError } from './errors'; | ||
|
||
const debug = createDebug('metrics'); | ||
const MILLISECONDS_PER_SECOND = 1000; | ||
|
||
let promClient: typeof import('prom-client') | undefined; | ||
|
||
function loadPromClient(): void { | ||
if (promClient === undefined) { | ||
debug('loading prom-client'); | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
promClient = require('prom-client'); | ||
} catch (error) { | ||
console.log('error', error); | ||
|
||
throw createConfigError('promClientNotInstalledError', 'prom-client is not installed and metrics was initialized', error as Error); | ||
} | ||
} | ||
} | ||
|
||
export function initializeMetrics(registry: Registry, schemaId: string, actualVersion: number | undefined): void { | ||
debug('initializing metrics'); | ||
loadPromClient(); | ||
|
||
if (promClient === undefined) { | ||
return; | ||
} | ||
|
||
const { offlineMode, configName, version } = getOptions(); | ||
const gauge = new promClient.Gauge({ | ||
name: 'config_time_of_last_fetch_unix_timestamp', | ||
help: 'The time of the last fetch of the configuration in unix timestamp', | ||
labelNames: ['name', 'request_version', 'actual_version', 'offline_mode', 'schemas_package_version', 'package_version', 'schema_id'], | ||
}); | ||
|
||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
gauge.set( | ||
{ | ||
name: configName, | ||
request_version: version, | ||
actual_version: actualVersion, | ||
offline_mode: String(offlineMode ?? false), | ||
schemas_package_version: LOCAL_SCHEMAS_PACKAGE_VERSION, | ||
package_version: PACKAGE_VERSION, | ||
schema_id: schemaId, | ||
}, | ||
|
||
Date.now() / MILLISECONDS_PER_SECOND | ||
); | ||
|
||
/* eslint-enable @typescript-eslint/naming-convention */ | ||
registry.registerMetric(gauge); | ||
} |
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,40 @@ | ||
import { Registry } from 'prom-client'; | ||
import { getOptions } from '../src/options'; | ||
import { BaseOptions } from '../src/types'; | ||
import { initializeMetrics } from '../src/metrics'; | ||
import { PACKAGE_VERSION } from '../src/version'; | ||
import { LOCAL_SCHEMAS_PACKAGE_VERSION } from '../src/constants'; | ||
|
||
jest.mock('../src/options'); | ||
|
||
const mockedGetOptions = getOptions as jest.MockedFunction<typeof getOptions>; | ||
|
||
mockedGetOptions.mockReturnValue({ | ||
offlineMode: false, | ||
configName: 'avi', | ||
version: 1, | ||
} as BaseOptions); | ||
|
||
describe('httpClient', () => { | ||
beforeEach(() => {}); | ||
|
||
describe('#initializeMetrics', () => { | ||
it('should initialize the metrics in the registry with the correct labels', async () => { | ||
const registry = new Registry(); | ||
initializeMetrics(registry, 'schema', 1); | ||
|
||
const metric = (await registry.getMetricsAsJSON())[0]; | ||
expect(metric).toHaveProperty('name', 'config_time_of_last_fetch_unix_timestamp'); | ||
expect(metric).toHaveProperty('values[0].labels', { | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
actual_version: 1, | ||
name: 'avi', | ||
offline_mode: 'false', | ||
package_version: PACKAGE_VERSION, | ||
request_version: 1, | ||
schema_id: 'schema', | ||
schemas_package_version: LOCAL_SCHEMAS_PACKAGE_VERSION, | ||
}); | ||
}); | ||
}); | ||
}); |