Skip to content

Commit

Permalink
refactor!: options change (#69)
Browse files Browse the repository at this point in the history
* refactor!: get options programmatically be the user

* chore: more work on integration

* chore: changed version to original

* fix: removed unneeded call for config

* refactor: changed version to serviceVersion

* fix: removed commented line
  • Loading branch information
CptSchnitz authored Jul 30, 2024
1 parent d1b779c commit 8599539
Show file tree
Hide file tree
Showing 12 changed files with 323 additions and 114 deletions.
86 changes: 63 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"./conventions": {
"default": "./dist/semanticConventions/index.js",
"types": "./dist/semanticConventions/index.d.ts"
},
"./prom-metrics": {
"default": "./dist/metrics/middleware/metrics.js",
"types": "./dist/metrics/middleware/metrics.d.ts"
}
},
"scripts": {
Expand Down Expand Up @@ -50,6 +54,7 @@
],
"homepage": "https://github.com/MapColonies/telemetry#readme",
"dependencies": {
"@apideck/better-ajv-errors": "^0.3.6",
"@map-colonies/read-pkg": "^0.0.1",
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/auto-instrumentations-node": "^0.40.2",
Expand All @@ -59,7 +64,8 @@
"@opentelemetry/sdk-node": "^0.46.0",
"@opentelemetry/sdk-trace-base": "^1.19.0",
"@opentelemetry/sdk-trace-node": "^1.19.0",
"env-var": "^7.1.1",
"ajv": "^8.16.0",
"ajv-formats": "^3.0.1",
"express-prom-bundle": "^6.6.0",
"prom-client": "^15.1.0"
},
Expand All @@ -72,7 +78,6 @@
"@types/express": "^4.17.12",
"@types/faker": "^5.5.3",
"@types/node": "^14.14.12",
"ajv": "^8.12.0",
"change-case-all": "^2.1.0",
"comment-json": "^4.2.3",
"commitlint": "^11.0.0",
Expand All @@ -87,7 +92,7 @@
"standard-version": "^9.0.0",
"ts-node": "^9.1.1",
"tsimp": "2.0.11",
"typescript": "^5.3.3",
"typedoc": "^0.25.13"
"typedoc": "^0.25.13",
"typescript": "^5.3.3"
}
}
92 changes: 81 additions & 11 deletions src/common/config.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,97 @@
import { hostname } from 'os';
import * as env from 'env-var';
import Ajv, { type JSONSchemaType } from 'ajv';
import addFormats from 'ajv-formats';
import { betterAjvErrors } from '@apideck/better-ajv-errors';
import { loadPackageInfo } from '../common/packageInfoLoader';

/**
* Represents the configuration for common settings.
*/
interface CommonConfig {
/**
* The name of the service to put as attribute.
* By default will be read from the package.json file.
*/
serviceName: string;
/**
* The value of the hostname attribute to use, will override the hostname.
*/
hostname: string;
version: string;
/**
* The version of the service to put as attribute.
* By default will be read from the package.json file.
*/
serviceVersion: string;
}

const ajv = addFormats(
new Ajv({
useDefaults: true,
coerceTypes: true,
allErrors: true,
verbose: true,
})
);

const JSON_INDENTATION = 2;

function mergeAndValidateConfig<T extends Record<keyof T, string | boolean | undefined | null | number>>(
inputConfig: Partial<T>,
envConfig: Partial<Record<keyof T, string>>,
schema: JSONSchemaType<T>
): T {
// clean up undefined values so that they don't override the defaults
for (const key in envConfig) {
if (envConfig[key] === undefined) {
delete envConfig[key];
}
}

const mergedConfig = { ...inputConfig, ...envConfig };

const isValid = ajv.validate(schema, mergedConfig);

if (!isValid) {
const betterErrors = betterAjvErrors({
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
schema: schema as Parameters<typeof betterAjvErrors>[0]['schema'],
data: mergedConfig,
errors: ajv.errors,
});
throw new Error(JSON.stringify(betterErrors, null, JSON_INDENTATION));
}

return mergedConfig;
}

const packageJsonInfo = loadPackageInfo();

const commonConfigSchema: JSONSchemaType<CommonConfig> = {
type: 'object',
properties: {
serviceName: { type: 'string', default: packageJsonInfo.name },
hostname: { type: 'string', default: hostname() },
serviceVersion: { type: 'string', default: packageJsonInfo.version },
},
required: ['serviceName', 'hostname', 'serviceVersion'],
};

const envCommonConfig: Partial<CommonConfig> = {
serviceName: process.env.TELEMETRY_SERVICE_NAME,
hostname: process.env.TELEMETRY_HOST_NAME,
serviceVersion: process.env.TELEMETRY_SERVICE_VERSION,
};

let commonConfig: CommonConfig | undefined;

const getCommonConfig = (): CommonConfig => {
function getCommonConfig(options: Partial<CommonConfig>): CommonConfig {
if (commonConfig) {
return commonConfig;
}

const packageConfig = loadPackageInfo();
commonConfig = {
serviceName: env.get('TELEMETRY_SERVICE_NAME').asString() ?? packageConfig.name,
hostname: env.get('TELEMETRY_HOST_NAME').asString() ?? hostname(),
version: env.get('TELEMETRY_SERVICE_VERSION').asString() ?? packageConfig.version,
};
commonConfig = mergeAndValidateConfig<CommonConfig>(options, envCommonConfig, commonConfigSchema);

return commonConfig;
};
}

export { CommonConfig, getCommonConfig };
export { CommonConfig, getCommonConfig, mergeAndValidateConfig };
4 changes: 4 additions & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type Prettify<T> = {
[K in keyof T]: T[K];
// eslint-disable-next-line @typescript-eslint/ban-types
} & {};
Loading

0 comments on commit 8599539

Please sign in to comment.