mirror of
https://github.com/misskey-dev/misskey.git
synced 2026-08-05 06:46:09 +00:00
enhance: OTel設定時、JSONログにtrace_id, span_id, trace_flagがつくように (#17748)
* enhance: OTel設定時、JSONログにtrace_id, span_id, trace_flagがつくように * fix
This commit is contained in:
@@ -23,6 +23,9 @@ type JsonLogRecord = {
|
||||
readonly processId: number;
|
||||
readonly isPrimary: boolean;
|
||||
readonly workerId: number | null;
|
||||
readonly trace_id?: string;
|
||||
readonly span_id?: string;
|
||||
readonly trace_flags?: number;
|
||||
};
|
||||
|
||||
const defaultDependencies: JsonConsoleBackendDependencies = {
|
||||
@@ -45,6 +48,10 @@ function createJsonLogRecord(record: LogRecord): JsonLogRecord {
|
||||
processId: record.processId,
|
||||
isPrimary: record.isPrimary,
|
||||
workerId: record.workerId,
|
||||
// active Spanの情報は値が存在するログだけ、標準のsnake_case名で出力します。
|
||||
...(record.traceId != null ? { trace_id: record.traceId } : {}),
|
||||
...(record.spanId != null ? { span_id: record.spanId } : {}),
|
||||
...(record.traceFlags != null ? { trace_flags: record.traceFlags } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
type LogNormalizationProfile,
|
||||
} from './LogNormalizer.js';
|
||||
import type { LogBackend } from './LogBackend.js';
|
||||
import type { LogLevel, LogLevelSetting, LogRecord, LogRecordInput } from './types.js';
|
||||
import type { LogLevel, LogLevelSetting, LogRecord, LogRecordInput, LogTraceContextProvider } from './types.js';
|
||||
|
||||
/** ログを出力したプロセスを識別するための情報です。 */
|
||||
export type LogProcessInfo = {
|
||||
@@ -113,6 +113,7 @@ export class LogManager {
|
||||
private backend: LogBackend;
|
||||
private readonly dependencies: LogManagerDependencies;
|
||||
private normalizationProfile: LogNormalizationProfile;
|
||||
private traceContextProvider: LogTraceContextProvider | undefined;
|
||||
private configuredLevel: LogLevelSetting | undefined;
|
||||
private configuredDomains: readonly (readonly [string, LogLevelSetting])[];
|
||||
private shutdownPromise: Promise<void> | undefined;
|
||||
@@ -132,6 +133,7 @@ export class LogManager {
|
||||
...dependencies,
|
||||
};
|
||||
this.normalizationProfile = options.normalizationProfile ?? 'standard';
|
||||
this.traceContextProvider = undefined;
|
||||
this.configuredLevel = undefined;
|
||||
this.configuredDomains = [];
|
||||
}
|
||||
@@ -156,6 +158,11 @@ export class LogManager {
|
||||
this.normalizationProfile = profile;
|
||||
}
|
||||
|
||||
/** ログ出力時にactiveなTrace Contextを取得する処理を登録します。 */
|
||||
public setTraceContextProvider(provider?: LogTraceContextProvider): void {
|
||||
this.traceContextProvider = provider;
|
||||
}
|
||||
|
||||
/** backendに残っているログをflushしてから終了処理を行います。 */
|
||||
public shutdown(): Promise<void> {
|
||||
if (this.shutdownPromise != null) return this.shutdownPromise;
|
||||
@@ -220,6 +227,8 @@ export class LogManager {
|
||||
const normalizedError = typeof error !== 'undefined'
|
||||
? serializeLogError(error, { profile: this.normalizationProfile })
|
||||
: undefined;
|
||||
// 実際に出力するログだけ、TelemetryからactiveなTrace Contextを取得します。
|
||||
const traceContext = this.traceContextProvider?.();
|
||||
const record = {
|
||||
...inputWithoutStructuredValues,
|
||||
context,
|
||||
@@ -228,6 +237,7 @@ export class LogManager {
|
||||
processId: processInfo.processId,
|
||||
isPrimary: processInfo.isPrimary,
|
||||
workerId: processInfo.workerId,
|
||||
...(traceContext ?? {}),
|
||||
...(normalizedAttributes ? { attributes: normalizedAttributes } : {}),
|
||||
...(normalizedError ? { error: normalizedError } : {}),
|
||||
} as LogRecord;
|
||||
|
||||
@@ -8,8 +8,8 @@ import { BootstrapConsoleBackend } from './BootstrapConsoleBackend.js';
|
||||
import { JsonConsoleBackend } from './JsonConsoleBackend.js';
|
||||
import { PrettyConsoleBackend } from './PrettyConsoleBackend.js';
|
||||
import type { LogManagerConfiguration } from './LogManager.js';
|
||||
import type { LogTraceContextProvider, LogFormat } from './types.js';
|
||||
import type { LogBackend } from './LogBackend.js';
|
||||
import type { LogFormat } from './types.js';
|
||||
|
||||
/**
|
||||
* プロセス内のすべてのLoggerが共有するLogManagerです。
|
||||
@@ -32,6 +32,11 @@ export function configureLogging(configuration?: LogManagerConfiguration & { rea
|
||||
logManager.setBackend(backend);
|
||||
}
|
||||
|
||||
/** Telemetry初期化後に、ログへTrace Contextを付加する取得処理を登録します。 */
|
||||
export function setLogTraceContextProvider(provider?: LogTraceContextProvider): void {
|
||||
logManager.setTraceContextProvider(provider);
|
||||
}
|
||||
|
||||
/** プロセス終了前に現在のログ出力処理を保留分まで書き出して閉じます。 */
|
||||
export function shutdownLogging(): Promise<void> {
|
||||
return logManager.shutdown();
|
||||
|
||||
@@ -26,6 +26,16 @@ export type LogAttributeValue =
|
||||
/** 正規化後のログ属性です。 */
|
||||
export type LogAttributes = Readonly<Record<string, LogAttributeValue>>;
|
||||
|
||||
/** activeなSpanとログを関連付けるためのTrace Contextです。 */
|
||||
export type LogTraceContext = {
|
||||
readonly traceId: string;
|
||||
readonly spanId: string;
|
||||
readonly traceFlags: number;
|
||||
};
|
||||
|
||||
/** LogManagerが出力直前に呼び出すTrace Context取得処理です。 */
|
||||
export type LogTraceContextProvider = () => LogTraceContext | undefined;
|
||||
|
||||
/** ロガーの呼び出し側が構造化ログとして指定する入力です。 */
|
||||
export type LogWriteInput = {
|
||||
readonly level: LogLevel;
|
||||
@@ -81,6 +91,9 @@ export type LogRecord = Omit<LogRecordInput, 'attributes' | 'error'> & {
|
||||
readonly processId: number;
|
||||
readonly isPrimary: boolean;
|
||||
readonly workerId: number | null;
|
||||
readonly traceId?: string;
|
||||
readonly spanId?: string;
|
||||
readonly traceFlags?: number;
|
||||
readonly attributes?: LogAttributes;
|
||||
readonly error?: SerializedError;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user