fix(server): otel not working due to port conflicts after combining containers (#10078)

fix: otel not working due to port conflicts after combining containers

Fixes #9759
This commit is contained in:
Zack Pollard 2024-06-10 17:01:04 +01:00 committed by GitHub
parent 20efd82461
commit 2f2aecfb47
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 44 additions and 26 deletions

View File

@ -3,10 +3,10 @@ global:
evaluation_interval: 15s
scrape_configs:
- job_name: immich_server
- job_name: immich_api
static_configs:
- targets: ['immich-server:8081']
- job_name: immich_microservices
static_configs:
- targets: ['immich-microservices:8081']
- targets: ['immich-server:8082']

View File

@ -374,7 +374,8 @@ export const immichAppConfig: ConfigModuleOptions = {
DB_SKIP_MIGRATIONS: Joi.boolean().optional().default(false),
IMMICH_PORT: Joi.number().optional(),
IMMICH_METRICS_PORT: Joi.number().optional(),
IMMICH_API_METRICS_PORT: Joi.number().optional(),
IMMICH_MICROSERVICES_METRICS_PORT: Joi.number().optional(),
IMMICH_METRICS: Joi.boolean().optional().default(false),
IMMICH_HOST_METRICS: Joi.boolean().optional().default(false),

View File

@ -17,7 +17,7 @@ import { StorageService } from 'src/services/storage.service';
import { SystemConfigService } from 'src/services/system-config.service';
import { UserService } from 'src/services/user.service';
import { VersionService } from 'src/services/version.service';
import { otelSDK } from 'src/utils/instrumentation';
import { otelShutdown } from 'src/utils/instrumentation';
@Injectable()
export class MicroservicesService {
@ -102,6 +102,6 @@ export class MicroservicesService {
async teardown() {
await this.libraryService.teardown();
await this.metadataService.teardown();
await otelSDK.shutdown();
await otelShutdown();
}
}

View File

@ -33,14 +33,18 @@ const aggregation = new metrics.ExplicitBucketHistogramAggregation(
true,
);
const metricsPort = Number.parseInt(process.env.IMMICH_METRICS_PORT ?? '8081');
let otelSingleton: NodeSDK | undefined;
export const otelSDK = new NodeSDK({
export const otelStart = (port: number) => {
if (otelSingleton) {
throw new Error('OpenTelemetry SDK already started');
}
otelSingleton = new NodeSDK({
resource: new resources.Resource({
[SemanticResourceAttributes.SERVICE_NAME]: `immich`,
[SemanticResourceAttributes.SERVICE_VERSION]: serverVersion.toString(),
}),
metricReader: new PrometheusExporter({ port: metricsPort }),
metricReader: new PrometheusExporter({ port }),
contextManager: new AsyncLocalStorageContextManager(),
instrumentations: [
new HttpInstrumentation(),
@ -49,7 +53,16 @@ export const otelSDK = new NodeSDK({
new PgInstrumentation(),
],
views: [new metrics.View({ aggregation, instrumentName: '*', instrumentUnit: 'ms' })],
});
});
otelSingleton.start();
};
export const otelShutdown = async () => {
if (otelSingleton) {
await otelSingleton.shutdown();
otelSingleton = undefined;
}
};
export const otelConfig: OpenTelemetryModuleOptions = {
metrics: {

View File

@ -9,14 +9,16 @@ import { envName, excludePaths, isDev, serverVersion, WEB_ROOT } from 'src/const
import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { WebSocketAdapter } from 'src/middleware/websocket.adapter';
import { ApiService } from 'src/services/api.service';
import { otelSDK } from 'src/utils/instrumentation';
import { otelStart } from 'src/utils/instrumentation';
import { useSwagger } from 'src/utils/misc';
const host = process.env.HOST;
async function bootstrap() {
process.title = 'immich-api';
otelSDK.start();
const otelPort = Number.parseInt(process.env.IMMICH_API_METRICS_PORT ?? '8081');
otelStart(otelPort);
const port = Number(process.env.IMMICH_PORT) || 3001;
const app = await NestFactory.create<NestExpressApplication>(ApiModule, { bufferLogs: true });

View File

@ -4,10 +4,12 @@ import { MicroservicesModule } from 'src/app.module';
import { envName, serverVersion } from 'src/constants';
import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { WebSocketAdapter } from 'src/middleware/websocket.adapter';
import { otelSDK } from 'src/utils/instrumentation';
import { otelStart } from 'src/utils/instrumentation';
export async function bootstrap() {
otelSDK.start();
const otelPort = Number.parseInt(process.env.IMMICH_MICROSERVICES_METRICS_PORT ?? '8082');
otelStart(otelPort);
const app = await NestFactory.create(MicroservicesModule, { bufferLogs: true });
const logger = await app.resolve(ILoggerRepository);