refactor progress
This commit is contained in:
62
packages/initialization/src/CreateServiceInstrumentation.tsx
Normal file
62
packages/initialization/src/CreateServiceInstrumentation.tsx
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {startServiceInitialization} from '@fluxer/initialization/src/Init';
|
||||
import type {ShutdownFn} from '@fluxer/initialization/src/ServiceInitializationTypes';
|
||||
import type {InstrumentationConfig} from '@fluxer/telemetry/src/Telemetry';
|
||||
|
||||
interface CreateServiceInstrumentationOptions {
|
||||
serviceName: string;
|
||||
config: {
|
||||
env: string;
|
||||
telemetry: {
|
||||
enabled: boolean;
|
||||
otlp_endpoint: string;
|
||||
api_key: string;
|
||||
trace_sampling_ratio: number;
|
||||
};
|
||||
sentry: {
|
||||
enabled: boolean;
|
||||
dsn: string;
|
||||
};
|
||||
};
|
||||
ignoreIncomingPaths?: Array<string>;
|
||||
instrumentations?: InstrumentationConfig;
|
||||
}
|
||||
|
||||
export function createServiceInstrumentation(options: CreateServiceInstrumentationOptions): ShutdownFn {
|
||||
const {serviceName, config, ignoreIncomingPaths = ['/_health'], instrumentations = {fetch: true}} = options;
|
||||
|
||||
return startServiceInitialization({
|
||||
serviceName,
|
||||
environment: config.env,
|
||||
telemetry: {
|
||||
enabled: config.telemetry.enabled,
|
||||
otlpEndpoint: config.telemetry.otlp_endpoint,
|
||||
apiKey: config.telemetry.api_key,
|
||||
traceSamplingRatio: config.telemetry.trace_sampling_ratio,
|
||||
ignoreIncomingPaths,
|
||||
instrumentations,
|
||||
},
|
||||
sentry: {
|
||||
enabled: config.sentry.enabled,
|
||||
dsn: config.sentry.dsn,
|
||||
},
|
||||
});
|
||||
}
|
||||
100
packages/initialization/src/Init.tsx
Normal file
100
packages/initialization/src/Init.tsx
Normal file
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {getSentryBuildContext, getServiceVersionLabel} from '@fluxer/config/src/BuildMetadata';
|
||||
import type {ServiceInitConfig, ShutdownFn} from '@fluxer/initialization/src/ServiceInitializationTypes';
|
||||
import {flushSentry, initSentry} from '@fluxer/sentry/src/Sentry';
|
||||
import type {SentryConfig} from '@fluxer/sentry/src/SentryContracts';
|
||||
import {
|
||||
initializeTelemetry,
|
||||
shouldInitializeTelemetry,
|
||||
shutdownTelemetry,
|
||||
type TelemetryConfig,
|
||||
} from '@fluxer/telemetry/src/Telemetry';
|
||||
|
||||
const SENTRY_FLUSH_TIMEOUT_MS = 2000;
|
||||
|
||||
let initialized = false;
|
||||
|
||||
export async function initializeService(config: ServiceInitConfig): Promise<void> {
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
const {serviceName, environment = 'production', telemetry, sentry} = config;
|
||||
const serviceVersion = config.serviceVersion ?? getServiceVersionLabel();
|
||||
|
||||
if (telemetry?.enabled ?? shouldInitializeTelemetry()) {
|
||||
const telemetryConfig: Partial<TelemetryConfig> = {
|
||||
serviceName,
|
||||
serviceVersion,
|
||||
environment,
|
||||
otlpEndpoint: telemetry?.otlpEndpoint,
|
||||
otlpApiKey: telemetry?.apiKey,
|
||||
traceSamplingRatio: telemetry?.traceSamplingRatio,
|
||||
metricExportIntervalMs: telemetry?.metricExportIntervalMs,
|
||||
ignoreIncomingPaths: telemetry?.ignoreIncomingPaths,
|
||||
instrumentations: telemetry?.instrumentations,
|
||||
};
|
||||
|
||||
await initializeTelemetry(telemetryConfig);
|
||||
}
|
||||
|
||||
if (sentry?.enabled && sentry.dsn) {
|
||||
const buildContext = getSentryBuildContext();
|
||||
const sentryConfig: SentryConfig = {
|
||||
dsn: sentry.dsn,
|
||||
serviceName,
|
||||
environment,
|
||||
release: serviceVersion !== undefined ? `${serviceName}@${serviceVersion}` : undefined,
|
||||
sampleRate: sentry.sampleRate,
|
||||
buildSha: sentry.buildSha ?? buildContext.buildSha,
|
||||
buildNumber: sentry.buildNumber ?? buildContext.buildNumber,
|
||||
buildTimestamp: sentry.buildTimestamp ?? buildContext.buildTimestamp,
|
||||
releaseChannel: sentry.releaseChannel ?? buildContext.releaseChannel,
|
||||
};
|
||||
|
||||
initSentry(sentryConfig);
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
export async function shutdownService(): Promise<void> {
|
||||
if (!initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
await flushSentry(SENTRY_FLUSH_TIMEOUT_MS);
|
||||
await shutdownTelemetry();
|
||||
|
||||
initialized = false;
|
||||
}
|
||||
|
||||
export function isServiceInitialized(): boolean {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
export function startServiceInitialization(config: ServiceInitConfig): ShutdownFn {
|
||||
initializeService(config).catch((err: unknown) => {
|
||||
process.stderr.write(`[instrument] Failed to initialize instrumentation: ${err}\n`);
|
||||
});
|
||||
|
||||
return shutdownService;
|
||||
}
|
||||
50
packages/initialization/src/ServiceInitializationTypes.tsx
Normal file
50
packages/initialization/src/ServiceInitializationTypes.tsx
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import type {InstrumentationConfig} from '@fluxer/telemetry/src/Telemetry';
|
||||
|
||||
export interface ServiceInitConfig {
|
||||
serviceName: string;
|
||||
serviceVersion?: string;
|
||||
environment?: string;
|
||||
telemetry?: TelemetryInitConfig;
|
||||
sentry?: SentryInitConfig;
|
||||
}
|
||||
|
||||
export interface TelemetryInitConfig {
|
||||
enabled?: boolean;
|
||||
otlpEndpoint?: string;
|
||||
apiKey?: string;
|
||||
traceSamplingRatio?: number;
|
||||
metricExportIntervalMs?: number;
|
||||
ignoreIncomingPaths?: Array<string>;
|
||||
instrumentations?: InstrumentationConfig;
|
||||
}
|
||||
|
||||
export interface SentryInitConfig {
|
||||
enabled?: boolean;
|
||||
dsn?: string;
|
||||
sampleRate?: number;
|
||||
buildSha?: string;
|
||||
buildNumber?: string;
|
||||
buildTimestamp?: string;
|
||||
releaseChannel?: string;
|
||||
}
|
||||
|
||||
export type ShutdownFn = () => Promise<void>;
|
||||
Reference in New Issue
Block a user