forked from mtusi2026/mtusi2026lab6
36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import os from 'node:os';
|
||
|
||
export type ServiceIdentity = {
|
||
readonly serviceName: string;
|
||
readonly serviceVersion: string;
|
||
readonly instanceId: string;
|
||
};
|
||
|
||
function fromEnv(defaultServiceName: string): ServiceIdentity {
|
||
return {
|
||
serviceName: process.env.OTEL_SERVICE_NAME?.trim() || defaultServiceName,
|
||
serviceVersion: process.env.SERVICE_VERSION?.trim() || 'dev',
|
||
// В compose у реплик API нет INSTANCE_ID, но HOSTNAME — id контейнера,
|
||
// уникальный для каждой реплики: так service.instance.id различает
|
||
// api-1 и api-2 во всех трёх сигналах.
|
||
instanceId:
|
||
process.env.INSTANCE_ID?.trim() ||
|
||
process.env.HOSTNAME?.trim() ||
|
||
os.hostname(),
|
||
};
|
||
}
|
||
|
||
let current = fromEnv('messenger');
|
||
|
||
/** service.name / service.version / service.instance.id, общие для всех сигналов. */
|
||
export function serviceIdentity(): ServiceIdentity {
|
||
return current;
|
||
}
|
||
|
||
export function resolveServiceIdentity(
|
||
defaultServiceName: string,
|
||
): ServiceIdentity {
|
||
current = fromEnv(defaultServiceName);
|
||
return current;
|
||
}
|