All files / src/middlewares DefaultTelemetryMiddleware.ts

96.55% Statements 28/29
92.85% Branches 13/14
100% Functions 5/5
96.55% Lines 28/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                                    10x       10x                 5x 5x             6x 6x 5x 5x     5x   5x                         5x 5x     6x             2x 1x                   2x                   2x 1x                               5x 5x     5x 1x 1x 1x             5x 1x 1x     5x      
import { IMiddleware,
  IMiddlewareContext,
  ITelemetryEvent,
  MiddlewarePriority, } from "../types.js";
 
interface PerformanceWithMemory extends Performance {
  memory?: {
    usedJSHeapSize: number;
    totalJSHeapSize: number;
    jsHeapSizeLimit: number;
  };
  measureUserAgentSpecificMemory?: () => Promise<{ bytes: number }>;
}
 
/**
 * エンジンのパフォーマンスを自動計測する標準テレメトリ・ミドルウェア。
 */
export class DefaultTelemetryMiddleware implements IMiddleware {
  readonly priority = MiddlewarePriority.LOW; // 他のミドルウェアの邪魔をしないよう低優先度
 
  // 2026 Best Practice: WeakMap による自動クリーンアップ
  // コンテキストが破棄されると、対応する開始時間も自動的にメモリから解放されます。
  private startTimes = new WeakMap<IMiddlewareContext, number>();
 
  /**
   * 探索コマンド送信時のフック。開始時間を記録します。
   */
  async onCommand(
    command: string | string[] | Uint8Array | Record<string, unknown>,
    context: IMiddlewareContext,
  ): Promise<string | string[] | Uint8Array | Record<string, unknown>> {
    this.startTimes.set(context, performance.now());
    return command;
  }
 
  /**
   * 探索結果受信時のフック。所要時間を計測しテレメトリを発行します。
   */
  async onResult<T>(result: T, context: IMiddlewareContext): Promise<T> {
    const startTime = this.startTimes.get(context);
    if (startTime !== undefined) {
      const duration = performance.now() - startTime;
      this.startTimes.delete(context);
 
      // 2026 Best Practice: メモリ使用量の計測 (Zenith Tier)
      const memory = await this.captureMemoryUsage();
 
      const event: ITelemetryEvent = {
        type: "performance",
        timestamp: Date.now(),
        duration,
        metadata: {
          action: "search",
          engineId: context.engineId,
          telemetryId: context.telemetryId,
          memory,
        },
      };
 
      // コンテキスト経由でテレメトリを発行
      Eif (context.emitTelemetry) {
        context.emitTelemetry(event);
      }
    }
    return result;
  }
 
  /**
   * 中間思考情報の受信時にテレメトリを発行します。
   */
  async onInfo<T>(info: T, context: IMiddlewareContext): Promise<T> {
    if (context.emitTelemetry) {
      context.emitTelemetry({
        type: "search",
        timestamp: Date.now(),
        metadata: {
          action: "info",
          engineId: context.engineId,
          telemetryId: context.telemetryId,
        },
      });
    }
    return info;
  }
 
  /**
   * ロード進捗の発生時にテレメトリを発行します。
   */
  async onProgress(
    progress: import("../types.js").ILoadProgress,
    context: IMiddlewareContext,
  ): Promise<void> {
    if (context.emitTelemetry) {
      context.emitTelemetry({
        type: "lifecycle",
        timestamp: Date.now(),
        metadata: {
          action: "progress",
          engineId: context.engineId,
          status: progress.status,
          loadedBytes: progress.loadedBytes,
        },
      });
    }
  }
 
  private async captureMemoryUsage(): Promise<
    Record<string, number> | undefined
  > {
    const mem: Record<string, number> = {};
    const p = performance as PerformanceWithMemory;
 
    // 1. Modern API (Zenith Tier 2026)
    if (typeof p.measureUserAgentSpecificMemory === "function") {
      try {
        const result = await p.measureUserAgentSpecificMemory();
        mem.bytes = result.bytes;
      } catch (err) {
        console.debug("[Telemetry] Failed to capture specific memory:", err);
      }
    }
 
    // 2. Legacy API (Chromium)
    if (p.memory) {
      mem.usedJSHeapSize = p.memory.usedJSHeapSize;
      mem.totalJSHeapSize = p.memory.totalJSHeapSize;
    }
 
    return Object.keys(mem).length > 0 ? mem : undefined;
  }
}