Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
feat(ts-sdk): add GPU metrics collection (parity with Python gpu instrumentor) (#1343)
zeroshotmindf793c8a21646
diff --git a/sdk/typescript/README.md b/sdk/typescript/README.mdindex 786bd013..a427ebde 100644--- a/sdk/typescript/README.md+++ b/sdk/typescript/README.md@@ -208,6 +208,7 @@ Below is a detailed overview of the configuration options available, allowing yo| `disabledInstrumentations` | List of instrumentations to disable. | `undefined` | No || `instrumentations` | Object of instrumentation modules for manual patching | `undefined` | No || `pricingJson` | URL or file path of the pricing JSON file. | `https://github.com/openlit/openlit/blob/main/assets/pricing.json` | No |+| `collectGpuStats` | Flag to enable or disable GPU metrics collection (NVIDIA via `nvidia-smi`, AMD via `rocm-smi`). | `false` | No |### OpenLIT Prompt Hub - `Openlit.getPrompt()`diff --git a/sdk/typescript/src/index.ts b/sdk/typescript/src/index.tsindex a4cbd070..7006a7b0 100644--- a/sdk/typescript/src/index.ts+++ b/sdk/typescript/src/index.ts@@ -20,6 +20,7 @@ import { runEval, runEvalBatch, fetchEvalTypes } from './evals';import { logScore } from './score';import { TracedSpan, startTrace, trace as traceManual } from './manual-trace';import Metrics from './otel/metrics';+import GpuMetricsCollector from './instrumentation/gpu';import SemanticConvention from './semantic-convention';import { parseBoolEnv } from './otel/utils';import { setupAutoGuards } from './guard/integration';@@ -98,6 +99,12 @@ function resolveOptions(options?: OpenlitOptions): ResolvedOptions {disableEvents = envDisableEvents ?? false;}+ let collectGpuStats = o.collectGpuStats ?? undefined;+ const envCollectGpuStats = parseBoolEnv('OPENLIT_COLLECT_GPU_STATS');+ if (collectGpuStats === undefined) {+ collectGpuStats = envCollectGpuStats ?? false;+ }+const openlitApiKey = o.openlitApiKey ?? process.env.OPENLIT_API_KEY ?? undefined;const openlitUrl = o.openlitUrl ?? process.env.OPENLIT_URL ?? undefined;@@ -120,6 +127,7 @@ function resolveOptions(options?: OpenlitOptions): ResolvedOptions {openlitUrl,guards: o.guards,guardFailOpen: o.guardFailOpen ?? true,+ collectGpuStats,};}@@ -199,10 +207,24 @@ class Openlit extends BaseOpenlit {const exportIntervalMillis =Number(process.env.OTEL_EXPORTER_OTLP_METRICS_EXPORT_INTERVAL ?? 60000) || 60000;- Metrics.setup({+ const meter = Metrics.setup({...setupBase,exportIntervalMillis,});++ // GPU metrics: only register gauges when a supported GPU is detected+ // (mirrors Python openlit.init collect_gpu_stats path).+ if (resolved.collectGpuStats && meter) {+ new GpuMetricsCollector()+ .setup({+ meter,+ environment: resolved.environment,+ applicationName: resolved.applicationName,+ })+ .catch((e) => {+ diag.error(`OpenLIT GPU metrics setup failed: ${e}`);+ });+ }}OpenlitConfig.openlitApiKey = resolved.openlitApiKey;diff --git a/sdk/typescript/src/instrumentation/__tests__/gpu-collector.test.ts b/sdk/typescript/src/instrumentation/__tests__/gpu-collector.test.tsnew file mode 100644index 00000000..c2d76e05--- /dev/null+++ b/sdk/typescript/src/instrumentation/__tests__/gpu-collector.test.ts@@ -0,0 +1,276 @@+import { GpuMetricsCollector, CommandRunner, GpuStats } from '../gpu';+import SemanticConvention from '../../semantic-convention';++// Columns match NVIDIA_SMI_FIELDS order:+// index, uuid, name, util.gpu, util.encoder, util.decoder, temp, fan,+// mem.total, mem.used, mem.free, power.draw, enforced.power.limit, power.limit+const NVIDIA_CSV =+ '0, GPU-11111111-2222-3333-4444-555555555555, NVIDIA A100-SXM4-40GB, 87, 15, 5, 65, 30, 40960, 30000, 10960, 250.50, 400.00, 400.00\n' ++ '1, GPU-aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee, NVIDIA A100-SXM4-40GB, 12, 0, 0, 40, 25, 40960, 1024, 39936, 60.25, 0, 400.00\n';++const ROCM_JSON = JSON.stringify({+ card0: {+ 'GPU ID': '0x740f',+ 'Unique ID': '0x123456789abcdef',+ 'Card series': 'AMD Instinct MI210',+ 'GPU use (%)': '42',+ 'Temperature (Sensor edge) (C)': '55.0',+ 'Fan speed (%)': '35',+ 'VRAM Total Memory (B)': String(64 * 1024 * 1024 * 1024),+ 'VRAM Total Used Memory (B)': String(16 * 1024 * 1024 * 1024),+ 'Average Graphics Package Power (W)': '180.0',+ 'Max Graphics Package Power (W)': '300.0',+ },+ system: { 'Driver version': '6.2' },+});++function nvidiaRunner(): CommandRunner {+ return async (cmd, args) => {+ if (cmd !== 'nvidia-smi') throw new Error(`${cmd}: command not found`);+ if (args[0] === '-L') return 'GPU 0: NVIDIA A100\nGPU 1: NVIDIA A100\n';+ return NVIDIA_CSV;+ };+}++function amdRunner(): CommandRunner {+ return async (cmd) => {+ if (cmd !== 'rocm-smi') throw new Error(`${cmd}: command not found`);+ return ROCM_JSON;+ };+}++const noGpuRunner: CommandRunner = async (cmd) => {+ throw new Error(`${cmd}: command not found`);+};++interface FakeObservation {+ gauge: unknown;+ value: number;+ attributes: Record<string, unknown>;+}++function makeFakeMeter() {+ const gauges: Array<{ name: string; token: object }> = [];+ const observations: FakeObservation[] = [];+ let callback:+ | ((result: {+ observe: (g: unknown, v: number, a: Record<string, unknown>) => void;+ }) => Promise<void>)+ | null = null;++ const meter = {+ createObservableGauge: (name: string) => {+ const token = { name };+ gauges.push({ name, token });+ return token;+ },+ addBatchObservableCallback: (cb: typeof callback) => {+ callback = cb;+ },+ };++ const runCallback = async () => {+ if (!callback) throw new Error('no callback registered');+ await callback({+ observe: (gauge, value, attributes) => observations.push({ gauge, value, attributes }),+ });+ };++ return { meter: meter as any, gauges, observations, runCallback };+}++describe('GpuMetricsCollector', () => {+ it('detects nvidia GPUs', async () => {+ expect(await new GpuMetricsCollector(nvidiaRunner()).detectGpuType()).toBe('nvidia');+ });++ it('detects amd GPUs when nvidia-smi is missing', async () => {+ expect(await new GpuMetricsCollector(amdRunner()).detectGpuType()).toBe('amd');+ });++ it('returns null when no supported GPU tooling exists', async () => {+ expect(await new GpuMetricsCollector(noGpuRunner).detectGpuType()).toBeNull();+ });++ it('setup registers all 11 gauges with Python-matching metric names', async () => {+ const { meter, gauges } = makeFakeMeter();+ const collector = new GpuMetricsCollector(nvidiaRunner());+ const gpuType = await collector.setup({+ meter,+ environment: 'staging',+ applicationName: 'my-app',+ });++ expect(gpuType).toBe('nvidia');+ expect(gauges.map((g) => g.name)).toEqual([+ 'gpu.utilization',+ 'gpu.enc.utilization',+ 'gpu.dec.utilization',+ 'gpu.temperature',+ 'gpu.fan_speed',+ 'gpu.memory.available',+ 'gpu.memory.total',+ 'gpu.memory.used',+ 'gpu.memory.free',+ 'gpu.power.draw',+ 'gpu.power.limit',+ ]);+ });++ it('setup returns null and registers nothing without a GPU', async () => {+ const { meter, gauges } = makeFakeMeter();+ const gpuType = await new GpuMetricsCollector(noGpuRunner).setup({+ meter,+ environment: 'staging',+ applicationName: 'my-app',+ });+ expect(gpuType).toBeNull();+ expect(gauges).toHaveLength(0);+ });++ it('observes nvidia stats per GPU with Python-matching attributes', async () => {+ const { meter, observations, runCallback } = makeFakeMeter();+ const collector = new GpuMetricsCollector(nvidiaRunner());+ await collector.setup({ meter, environment: 'staging', applicationName: 'my-app' });+ await runCallback();++ // 2 GPUs x 11 metrics+ expect(observations).toHaveLength(22);++ const gpu0 = observations.filter((o) => o.attributes[SemanticConvention.GPU_INDEX] === '0');+ expect(gpu0).toHaveLength(11);+ expect(gpu0[0].attributes).toEqual({+ 'telemetry.sdk.name': 'openlit',+ 'service.name': 'my-app',+ [SemanticConvention.ATTR_DEPLOYMENT_ENVIRONMENT]: 'staging',+ [SemanticConvention.GPU_INDEX]: '0',+ [SemanticConvention.GPU_UUID]: 'GPU-11111111-2222-3333-4444-555555555555',+ [SemanticConvention.GPU_NAME]: 'NVIDIA A100-SXM4-40GB',+ });++ const valueOf = (metric: string, index: string) =>+ observations.find(+ (o) =>+ (o.gauge as { name: string }).name === metric &&+ o.attributes[SemanticConvention.GPU_INDEX] === index+ )?.value;++ expect(valueOf('gpu.utilization', '0')).toBe(87);+ expect(valueOf('gpu.enc.utilization', '0')).toBe(15);+ expect(valueOf('gpu.dec.utilization', '0')).toBe(5);+ expect(valueOf('gpu.temperature', '0')).toBe(65);+ expect(valueOf('gpu.fan_speed', '0')).toBe(30);+ expect(valueOf('gpu.memory.total', '0')).toBe(40960);+ expect(valueOf('gpu.memory.used', '0')).toBe(30000);+ expect(valueOf('gpu.memory.free', '0')).toBe(10960);+ expect(valueOf('gpu.memory.available', '0')).toBe(10960);+ expect(valueOf('gpu.power.draw', '0')).toBe(250.5);+ expect(valueOf('gpu.power.limit', '0')).toBe(400);+ expect(valueOf('gpu.utilization', '1')).toBe(12);+ // GPU 1: enforced.power.limit is 0 / N/A → fall back to power.limit+ expect(valueOf('gpu.power.limit', '1')).toBe(400);+ expect(valueOf('gpu.enc.utilization', '1')).toBe(0);+ expect(valueOf('gpu.dec.utilization', '1')).toBe(0);+ });++ it('observes amd stats from rocm-smi JSON', async () => {+ const { meter, observations, runCallback } = makeFakeMeter();+ const collector = new GpuMetricsCollector(amdRunner());+ await collector.setup({ meter, environment: 'default', applicationName: 'default' });+ await runCallback();++ expect(observations).toHaveLength(11);+ const attrs = observations[0].attributes;+ expect(attrs[SemanticConvention.GPU_INDEX]).toBe('0');+ expect(attrs[SemanticConvention.GPU_NAME]).toBe('AMD Instinct MI210');+ expect(attrs[SemanticConvention.GPU_UUID]).toBe('0x123456789abcdef');++ const valueOf = (metric: string) =>+ observations.find((o) => (o.gauge as { name: string }).name === metric)?.value;+ expect(valueOf('gpu.utilization')).toBe(42);+ expect(valueOf('gpu.enc.utilization')).toBe(0);+ expect(valueOf('gpu.dec.utilization')).toBe(0);+ expect(valueOf('gpu.temperature')).toBe(55);+ expect(valueOf('gpu.fan_speed')).toBe(35);+ expect(valueOf('gpu.memory.total')).toBe(64 * 1024);+ expect(valueOf('gpu.memory.used')).toBe(16 * 1024);+ expect(valueOf('gpu.memory.free')).toBe(48 * 1024);+ // Python AMD memory_available == total (not free)+ expect(valueOf('gpu.memory.available')).toBe(64 * 1024);+ expect(valueOf('gpu.power.draw')).toBe(180);+ expect(valueOf('gpu.power.limit')).toBe(300);+ });++ it('swallows collection errors without throwing from the callback', async () => {+ let detect = true;+ const flakyRunner: CommandRunner = async (cmd, args) => {+ if (cmd !== 'nvidia-smi') throw new Error('not found');+ if (args[0] === '-L' && detect) return 'GPU 0: NVIDIA A100';+ throw new Error('nvidia-smi crashed');+ };+ const { meter, observations, runCallback } = makeFakeMeter();+ const collector = new GpuMetricsCollector(flakyRunner);+ await collector.setup({ meter, environment: 'default', applicationName: 'default' });+ detect = false;+ await expect(runCallback()).resolves.toBeUndefined();+ expect(observations).toHaveLength(0);+ });++ it('swallows invalid AMD JSON without throwing from the callback', async () => {+ const badJsonRunner: CommandRunner = async (cmd, args) => {+ if (cmd === 'nvidia-smi') throw new Error('not found');+ if (cmd !== 'rocm-smi') throw new Error('not found');+ if (args.includes('--showid') && args.length === 2) return '{}';+ return 'not-json{';+ };+ const { meter, observations, runCallback } = makeFakeMeter();+ const collector = new GpuMetricsCollector(badJsonRunner);+ await collector.setup({ meter, environment: 'default', applicationName: 'default' });+ await expect(runCallback()).resolves.toBeUndefined();+ expect(observations).toHaveLength(0);+ });++ it('parses malformed numeric fields as 0', async () => {+ const badCsvRunner: CommandRunner = async (cmd, args) => {+ if (cmd !== 'nvidia-smi') throw new Error('not found');+ if (args[0] === '-L') return 'GPU 0: NVIDIA A100';+ return '0, GPU-uuid, NVIDIA A100, [N/A], [N/A], [N/A], 65, [N/A], 40960, 30000, 10960, [N/A], [N/A], 400.00\n';+ };+ const collector = new GpuMetricsCollector(badCsvRunner);+ const { meter, runCallback, observations } = makeFakeMeter();+ await collector.setup({ meter, environment: 'default', applicationName: 'default' });+ await runCallback();++ const valueOf = (metric: string) =>+ observations.find((o) => (o.gauge as { name: string }).name === metric)?.value;++ expect(valueOf('gpu.utilization')).toBe(0);+ expect(valueOf('gpu.enc.utilization')).toBe(0);+ expect(valueOf('gpu.dec.utilization')).toBe(0);+ expect(valueOf('gpu.power.draw')).toBe(0);+ // enforced N/A → fall back to power.limit+ expect(valueOf('gpu.power.limit')).toBe(400);+ });+});++describe('GpuStats typing', () => {+ it('exposes all metric fields', () => {+ const stats: GpuStats = {+ index: '0',+ uuid: 'u',+ name: 'n',+ utilization: 0,+ utilization_enc: 0,+ utilization_dec: 0,+ temperature: 0,+ fan_speed: 0,+ memory_available: 0,+ memory_total: 0,+ memory_used: 0,+ memory_free: 0,+ power_draw: 0,+ power_limit: 0,+ };+ expect(Object.keys(stats)).toHaveLength(14);+ });+});diff --git a/sdk/typescript/src/instrumentation/gpu/index.ts b/sdk/typescript/src/instrumentation/gpu/index.tsnew file mode 100644index 00000000..3f3be4f0--- /dev/null+++ b/sdk/typescript/src/instrumentation/gpu/index.ts@@ -0,0 +1,272 @@+/**+ * GPU metrics collection for Node.js services, mirroring the Python SDK's+ * `gpu` instrumentor (sdk/python/src/openlit/instrumentation/gpu/).+ *+ * Instead of NVML/AMD-SMI native bindings, this collector shells out to the+ * vendor CLI tools that ship with the drivers:+ * - NVIDIA: `nvidia-smi --query-gpu=... --format=csv,noheader,nounits`+ * - AMD: `rocm-smi --json` with the relevant --show* flags+ *+ * Metric names and attributes match the Python SDK exactly so dashboards+ * stay shared across languages.+ */+import { execFile } from 'child_process';+import { diag } from '@opentelemetry/api';+import type { BatchObservableResult, ObservableGauge } from '@opentelemetry/api';+import SemanticConvention from '../../semantic-convention';+import { SDK_NAME } from '../../constant';+import type { MeterType } from '../../types';++export type GpuType = 'nvidia' | 'amd';++export interface GpuStats {+ index: string;+ uuid: string;+ name: string;+ utilization: number;+ utilization_enc: number;+ utilization_dec: number;+ temperature: number;+ fan_speed: number;+ memory_available: number;+ memory_total: number;+ memory_used: number;+ memory_free: number;+ power_draw: number;+ power_limit: number;+}++type GpuMetricKey = keyof Omit<GpuStats, 'index' | 'uuid' | 'name'>;++const GPU_METRICS: Array<{ semconv: string; key: GpuMetricKey; description: string }> = [+ { semconv: SemanticConvention.GPU_UTILIZATION, key: 'utilization', description: 'GPU Utilization' },+ { semconv: SemanticConvention.GPU_UTILIZATION_ENC, key: 'utilization_enc', description: 'GPU Utilization Enc' },+ { semconv: SemanticConvention.GPU_UTILIZATION_DEC, key: 'utilization_dec', description: 'GPU Utilization Dec' },+ { semconv: SemanticConvention.GPU_TEMPERATURE, key: 'temperature', description: 'GPU Temperature' },+ { semconv: SemanticConvention.GPU_FAN_SPEED, key: 'fan_speed', description: 'GPU Fan Speed' },+ { semconv: SemanticConvention.GPU_MEMORY_AVAILABLE, key: 'memory_available', description: 'GPU Memory Available' },+ { semconv: SemanticConvention.GPU_MEMORY_TOTAL, key: 'memory_total', description: 'GPU Memory Total' },+ { semconv: SemanticConvention.GPU_MEMORY_USED, key: 'memory_used', description: 'GPU Memory Used' },+ { semconv: SemanticConvention.GPU_MEMORY_FREE, key: 'memory_free', description: 'GPU Memory Free' },+ { semconv: SemanticConvention.GPU_POWER_DRAW, key: 'power_draw', description: 'GPU Power Draw' },+ { semconv: SemanticConvention.GPU_POWER_LIMIT, key: 'power_limit', description: 'GPU Power Limit' },+];++/**+ * Field order must stay aligned with the CSV column indices in collectNvidiaStats.+ * `enforced.power.limit` matches Python's nvmlDeviceGetEnforcedPowerLimit;+ * `power.limit` is a fallback when enforced is N/A.+ */+const NVIDIA_SMI_FIELDS = [+ 'index',+ 'uuid',+ 'name',+ 'utilization.gpu',+ 'utilization.encoder',+ 'utilization.decoder',+ 'temperature.gpu',+ 'fan.speed',+ 'memory.total',+ 'memory.used',+ 'memory.free',+ 'power.draw',+ 'enforced.power.limit',+ 'power.limit',+] as const;++export type CommandRunner = (cmd: string, args: string[]) => Promise<string>;++const defaultRunner: CommandRunner = (cmd, args) =>+ new Promise((resolve, reject) => {+ execFile(cmd, args, { timeout: 10000, windowsHide: true }, (error, stdout) => {+ if (error) reject(error);+ else resolve(stdout);+ });+ });++function toNumber(value: unknown): number {+ const n = parseFloat(String(value));+ return Number.isFinite(n) ? n : 0;+}++export class GpuMetricsCollector {+ private runner: CommandRunner;+ private gpuType: GpuType | null = null;++ constructor(runner: CommandRunner = defaultRunner) {+ this.runner = runner;+ }++ async detectGpuType(): Promise<GpuType | null> {+ try {+ await this.runner('nvidia-smi', ['-L']);+ return 'nvidia';+ } catch {+ /* not nvidia */+ }+ try {+ await this.runner('rocm-smi', ['--showid', '--json']);+ return 'amd';+ } catch {+ /* not amd */+ }+ return null;+ }++ async collectStats(): Promise<GpuStats[]> {+ if (this.gpuType === 'nvidia') return this.collectNvidiaStats();+ if (this.gpuType === 'amd') return this.collectAmdStats();+ return [];+ }++ private async collectNvidiaStats(): Promise<GpuStats[]> {+ const stdout = await this.runner('nvidia-smi', [+ `--query-gpu=${NVIDIA_SMI_FIELDS.join(',')}`,+ '--format=csv,noheader,nounits',+ ]);++ return stdout+ .split('\n')+ .map((line) => line.trim())+ .filter((line) => line.length > 0)+ .map((line) => {+ const parts = line.split(',').map((p) => p.trim());+ const memoryFree = toNumber(parts[10]);+ // Prefer enforced power limit (Python NVML parity); fall back to power.limit.+ const powerLimit = toNumber(parts[12]) || toNumber(parts[13]);+ return {+ index: parts[0] ?? '0',+ uuid: parts[1] ?? '',+ name: parts[2] ?? '',+ utilization: toNumber(parts[3]),+ utilization_enc: toNumber(parts[4]),+ utilization_dec: toNumber(parts[5]),+ temperature: toNumber(parts[6]),+ // Python NVML path always reports 0 for NVIDIA fan_speed (many+ // datacenter cards have no fan sensor). nvidia-smi exposes the value+ // when present; [N/A] becomes 0 via toNumber, matching Python.+ fan_speed: toNumber(parts[7]),+ memory_total: toNumber(parts[8]),+ memory_used: toNumber(parts[9]),+ memory_free: memoryFree,+ // Python NVIDIA: memory_available == memory_free+ memory_available: memoryFree,+ power_draw: toNumber(parts[11]),+ power_limit: powerLimit,+ };+ });+ }++ private async collectAmdStats(): Promise<GpuStats[]> {+ const stdout = await this.runner('rocm-smi', [+ '--showid',+ '--showuniqueid',+ '--showproductname',+ '--showuse',+ '--showmeminfo',+ 'vram',+ '--showtemp',+ '--showfan',+ '--showpower',+ '--json',+ ]);++ let parsed: Record<string, Record<string, unknown>>;+ try {+ parsed = JSON.parse(stdout) as Record<string, Record<string, unknown>>;+ } catch (e) {+ throw new Error(`rocm-smi returned invalid JSON: ${e}`);+ }++ const megaBytes = 1024 * 1024;++ return Object.entries(parsed)+ .filter(([card]) => card.toLowerCase().startsWith('card'))+ .map(([card, data], i) => {+ const memoryTotal = toNumber(data['VRAM Total Memory (B)']) / megaBytes;+ const memoryUsed = toNumber(data['VRAM Total Used Memory (B)']) / megaBytes;+ const memoryFree = memoryTotal - memoryUsed;+ // Prefer ordinal from "card0" / "card1" (Python uses xgmi index 0..N).+ const cardIndex = card.replace(/^card/i, '');+ return {+ index: String(cardIndex !== '' ? cardIndex : i),+ uuid: String(data['Unique ID'] ?? ''),+ name: String(data['Card series'] ?? data['Card SKU'] ?? ''),+ utilization: toNumber(data['GPU use (%)']),+ // Python AMD stubs encoder/decoder utilization to 0+ utilization_enc: 0,+ utilization_dec: 0,+ temperature: toNumber(data['Temperature (Sensor edge) (C)']),+ fan_speed: toNumber(data['Fan speed (%)']),+ memory_total: memoryTotal,+ memory_used: memoryUsed,+ memory_free: memoryFree,+ // Python AMD sets memory_available to total (not free) — keep parity+ memory_available: memoryTotal,+ power_draw: toNumber(+ data['Average Graphics Package Power (W)'] ??+ data['Current Socket Graphics Package Power (W)']+ ),+ power_limit: toNumber(data['Max Graphics Package Power (W)']),+ };+ });+ }++ /**+ * Detect a supported GPU and register observable gauges on the meter.+ * Returns the detected GPU type, or null if no supported GPU was found.+ */+ async setup(options: {+ meter: MeterType;+ environment: string;+ applicationName: string;+ }): Promise<GpuType | null> {+ this.gpuType = await this.detectGpuType();+ if (!this.gpuType) {+ // Match Python openlit.init(): skip quietly-ish when no GPU is present.+ // Use warn because OpenLIT sets DiagLogLevel.WARN (info is filtered).+ diag.warn(+ 'OpenLIT: No supported GPUs found; skipping GPU metrics collection. ' ++ 'If this is a non-GPU host, set `collectGpuStats: false` to disable GPU stats.'+ );+ return null;+ }++ const gauges: ObservableGauge[] = [];+ const gaugeByKey = new Map<ObservableGauge, GpuMetricKey>();+ for (const { semconv, key, description } of GPU_METRICS) {+ const gauge = options.meter.createObservableGauge(semconv, { description });+ gauges.push(gauge);+ gaugeByKey.set(gauge, key);+ }++ options.meter.addBatchObservableCallback(async (result: BatchObservableResult) => {+ let stats: GpuStats[];+ try {+ stats = await this.collectStats();+ } catch (e) {+ diag.error(`Error in GPU metrics collection: ${e}`);+ return;+ }++ for (const gpu of stats) {+ const attributes = {+ 'telemetry.sdk.name': SDK_NAME,+ 'service.name': options.applicationName,+ [SemanticConvention.ATTR_DEPLOYMENT_ENVIRONMENT]: options.environment,+ [SemanticConvention.GPU_INDEX]: gpu.index,+ [SemanticConvention.GPU_UUID]: gpu.uuid,+ [SemanticConvention.GPU_NAME]: gpu.name,+ };+ for (const gauge of gauges) {+ const key = gaugeByKey.get(gauge) as GpuMetricKey;+ result.observe(gauge, gpu[key], attributes);+ }+ }+ }, gauges);++ return this.gpuType;+ }+}++export default GpuMetricsCollector;diff --git a/sdk/typescript/src/semantic-convention.ts b/sdk/typescript/src/semantic-convention.tsindex ac2abbce..30f71984 100644--- a/sdk/typescript/src/semantic-convention.ts+++ b/sdk/typescript/src/semantic-convention.ts@@ -596,4 +596,20 @@ export default class SemanticConvention {static GEN_AI_EVALUATION_SCORE_LABEL = 'gen_ai.evaluation.score.label';static GEN_AI_EVALUATION_EXPLANATION = 'gen_ai.evaluation.explanation';static OPENLIT_SCORE_IDEMPOTENCY_KEY = 'openlit.score.idempotency_key';++ // ----- GPU (matches Python semcov) -----+ static GPU_INDEX = 'gpu.index';+ static GPU_UUID = 'gpu.uuid';+ static GPU_NAME = 'gpu.name';+ static GPU_UTILIZATION = 'gpu.utilization';+ static GPU_UTILIZATION_ENC = 'gpu.enc.utilization';+ static GPU_UTILIZATION_DEC = 'gpu.dec.utilization';+ static GPU_TEMPERATURE = 'gpu.temperature';+ static GPU_FAN_SPEED = 'gpu.fan_speed';+ static GPU_MEMORY_AVAILABLE = 'gpu.memory.available';+ static GPU_MEMORY_TOTAL = 'gpu.memory.total';+ static GPU_MEMORY_USED = 'gpu.memory.used';+ static GPU_MEMORY_FREE = 'gpu.memory.free';+ static GPU_POWER_DRAW = 'gpu.power.draw';+ static GPU_POWER_LIMIT = 'gpu.power.limit';}diff --git a/sdk/typescript/src/types.ts b/sdk/typescript/src/types.tsindex 6d2f6efb..ddd04f46 100644--- a/sdk/typescript/src/types.ts+++ b/sdk/typescript/src/types.ts@@ -45,6 +45,7 @@ export interface OpenlitConfigInterface {* Python: pricing_json → JS: pricingJson* Python: max_content_length → JS: maxContentLength* Python: custom_span_attributes → JS: customSpanAttributes+ * Python: collect_gpu_stats → JS: collectGpuStats*/export type OpenlitOptions = {environment?: string;@@ -65,6 +66,7 @@ export type OpenlitOptions = {openlitUrl?: string;guards?: Guard[];guardFailOpen?: boolean;+ collectGpuStats?: boolean;};/**@@ -89,6 +91,7 @@ export interface ResolvedOptions {openlitUrl?: string;guards?: Guard[];guardFailOpen: boolean;+ collectGpuStats: boolean;}export type SetupTracerOptions = ResolvedOptions & {