Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docker/prometheus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
3 changes: 2 additions & 1 deletion server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions server/src/services/microservices.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -102,6 +102,6 @@ export class MicroservicesService {
async teardown() {
await this.libraryService.teardown();
await this.metadataService.teardown();
await otelSDK.shutdown();
await otelShutdown();
}
}
47 changes: 30 additions & 17 deletions server/src/utils/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,36 @@ const aggregation = new metrics.ExplicitBucketHistogramAggregation(
true,
);

const metricsPort = Number.parseInt(process.env.IMMICH_METRICS_PORT ?? '8081');

export const otelSDK = new NodeSDK({
resource: new resources.Resource({
[SemanticResourceAttributes.SERVICE_NAME]: `immich`,
[SemanticResourceAttributes.SERVICE_VERSION]: serverVersion.toString(),
}),
metricReader: new PrometheusExporter({ port: metricsPort }),
contextManager: new AsyncLocalStorageContextManager(),
instrumentations: [
new HttpInstrumentation(),
new IORedisInstrumentation(),
new NestInstrumentation(),
new PgInstrumentation(),
],
views: [new metrics.View({ aggregation, instrumentName: '*', instrumentUnit: 'ms' })],
});
let otelSingleton: NodeSDK | undefined;

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 }),
contextManager: new AsyncLocalStorageContextManager(),
instrumentations: [
new HttpInstrumentation(),
new IORedisInstrumentation(),
new NestInstrumentation(),
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: {
Expand Down
6 changes: 4 additions & 2 deletions server/src/workers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
6 changes: 4 additions & 2 deletions server/src/workers/microservices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading