Skip to content

Commit

Permalink
implement initServiceInstanceId
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki-takei committed Jan 8, 2025
1 parent 4863fed commit 027a3d4
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion apps/app/src/features/opentelemetry/server/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './start';
export * from './node-sdk';
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import type { NodeSDKConfiguration } from '@opentelemetry/sdk-node';
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION, SEMRESATTRS_SERVICE_INSTANCE_ID } from '@opentelemetry/semantic-conventions';

import { getGrowiVersion } from '~/utils/growi-version';


export const generateNodeSDKConfiguration = (serviceInstanceId?: string): Partial<NodeSDKConfiguration> => {
const version = getGrowiVersion();

export const generateNodeSDKConfiguration = (instanceId: string, version: string): Partial<NodeSDKConfiguration> => {
return {
resource: new Resource({
[ATTR_SERVICE_NAME]: 'growi',
[ATTR_SERVICE_VERSION]: version,
[SEMRESATTRS_SERVICE_INSTANCE_ID]: instanceId,
[SEMRESATTRS_SERVICE_INSTANCE_ID]: serviceInstanceId,
}),
traceExporter: new OTLPTraceExporter(),
metricReader: new PeriodicExportingMetricReader({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { NodeSDK } from '@opentelemetry/sdk-node';
import { configManager } from '~/server/service/config-manager';
import loggerFactory from '~/utils/logger';


const logger = loggerFactory('growi:opentelemetry:server');


Expand Down Expand Up @@ -37,7 +36,7 @@ function overwriteSdkDisabled(): void {

}

export const startInstrumentation = async(version: string): Promise<void> => {
export const startInstrumentation = async(): Promise<void> => {
if (sdkInstance != null) {
logger.warn('OpenTelemetry instrumentation already started');
return;
Expand Down Expand Up @@ -69,14 +68,36 @@ For more information, see https://docs.growi.org/en/admin-guide/telemetry.html.
const { NodeSDK } = await import('@opentelemetry/sdk-node');
const { generateNodeSDKConfiguration } = await import('./node-sdk-configuration');

const serviceInstanceId = configManager.getConfig('otel:serviceInstanceId', ConfigSource.env)
?? 'generated-appSiteUrlHashed'; // TODO: generated appSiteUrlHashed

sdkInstance = new NodeSDK(generateNodeSDKConfiguration(serviceInstanceId, version));
sdkInstance = new NodeSDK(generateNodeSDKConfiguration());
sdkInstance.start();
}
};

export const initServiceInstanceId = async(): Promise<void> => {
if (sdkInstance != null) {
logger.warn('OpenTelemetry instrumentation already started');
return;
}

const instrumentationEnabled = configManager.getConfig('otel:enabled', ConfigSource.env);
if (instrumentationEnabled) {
const { generateNodeSDKConfiguration } = await import('./node-sdk-configuration');
const { getInstance: getGrowiInfoService } = await import('~/server/service/growi-info');

// get GrowiInfo with additional info
const growiInfo = await getGrowiInfoService().getGrowiInfo();

const serviceInstanceId = configManager.getConfig('otel:serviceInstanceId')
?? growiInfo.appSiteUrlHashed;

const updatedResource = generateNodeSDKConfiguration(serviceInstanceId);

// overwrite resource
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(sdkInstance as any).resource = updatedResource;
}
};

// public async shutdownInstrumentation(): Promise<void> {
// await this.sdkInstance.shutdown();

Expand Down
4 changes: 1 addition & 3 deletions apps/app/src/server/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type Logger from 'bunyan';

import pkg from '^/package.json';

import { startInstrumentation } from '~/features/opentelemetry/server';
import loggerFactory from '~/utils/logger';
import { hasProcessFlag } from '~/utils/process-utils';
Expand All @@ -23,7 +21,7 @@ process.on('unhandledRejection', (reason, p) => {
async function main() {
try {
// start OpenTelemetry
await startInstrumentation(pkg.version);
await startInstrumentation();

const Crowi = (await import('./crowi')).default;
const growi = new Crowi();
Expand Down
7 changes: 7 additions & 0 deletions apps/app/src/server/crowi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import pkg from '^/package.json';
import { KeycloakUserGroupSyncService } from '~/features/external-user-group/server/service/keycloak-user-group-sync';
import { LdapUserGroupSyncService } from '~/features/external-user-group/server/service/ldap-user-group-sync';
import { startCronIfEnabled as startOpenaiCronIfEnabled } from '~/features/openai/server/services/cron';
import { initServiceInstanceId } from '~/features/opentelemetry/server';
import QuestionnaireService from '~/features/questionnaire/server/service/questionnaire';
import QuestionnaireCronService from '~/features/questionnaire/server/service/questionnaire-cron';
import loggerFactory from '~/utils/logger';
Expand Down Expand Up @@ -212,6 +213,8 @@ Crowi.prototype.init = async function() {
// depends on passport service
this.setupExternalAccountService(),
this.setupExternalUserGroupSyncService(),
// depends on aclService
this.setupOpentelemetry2ndPhase(),
]);

await normalizeData();
Expand Down Expand Up @@ -345,6 +348,10 @@ Crowi.prototype.setupSocketIoService = async function() {
this.socketIoService = new SocketIoService(this);
};

Crowi.prototype.setupOpentelemetry2ndPhase = async function() {
initServiceInstanceId();
};

Crowi.prototype.setupCron = function() {
this.questionnaireCronService = new QuestionnaireCronService(this);
this.questionnaireCronService.startCron();
Expand Down
3 changes: 2 additions & 1 deletion apps/app/src/server/service/growi-info/growi-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type Crowi from '~/server/crowi';
import { Config } from '~/server/models/config';
import { aclService } from '~/server/service/acl';
import { configManager } from '~/server/service/config-manager';
import { getGrowiVersion } from '~/utils/growi-version';

import type { IGrowiAppAdditionalInfo } from '../../../features/questionnaire/interfaces/growi-app-info';

Expand Down Expand Up @@ -46,7 +47,7 @@ export class GrowiInfoService {
const wikiType = isGuestAllowedToRead ? GrowiWikiType.open : GrowiWikiType.closed;

const baseInfo = {
version: this.crowi.version,
version: getGrowiVersion(),
osInfo: {
type: os.type(),
platform: os.platform(),
Expand Down
5 changes: 5 additions & 0 deletions apps/app/src/utils/growi-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pkg from '^/package.json';

export const getGrowiVersion = (): string => {
return pkg.version;
};

0 comments on commit 027a3d4

Please sign in to comment.