/** * @license * Copyright 2025 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { Config } from '../config/config.js'; import { initializeTelemetry, shutdownTelemetry } from './sdk.js'; import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc'; import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-grpc'; import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc'; import { OTLPTraceExporter as OTLPTraceExporterHttp } from '@opentelemetry/exporter-trace-otlp-http'; import { OTLPLogExporter as OTLPLogExporterHttp } from '@opentelemetry/exporter-logs-otlp-http'; import { OTLPMetricExporter as OTLPMetricExporterHttp } from '@opentelemetry/exporter-metrics-otlp-http'; import { NodeSDK } from '@opentelemetry/sdk-node'; vi.mock('@opentelemetry/exporter-trace-otlp-grpc'); vi.mock('@opentelemetry/exporter-logs-otlp-grpc'); vi.mock('@opentelemetry/exporter-metrics-otlp-grpc'); vi.mock('@opentelemetry/exporter-trace-otlp-http'); vi.mock('@opentelemetry/exporter-logs-otlp-http'); vi.mock('@opentelemetry/exporter-metrics-otlp-http'); vi.mock('@opentelemetry/sdk-node'); describe('Telemetry SDK', () => { let mockConfig: Config; beforeEach(() => { vi.clearAllMocks(); mockConfig = { getTelemetryEnabled: () => true, getTelemetryOtlpEndpoint: () => 'http://localhost:4317', getTelemetryOtlpProtocol: () => 'grpc', getTelemetryOutfile: () => undefined, getDebugMode: () => false, getSessionId: () => 'test-session', } as unknown as Config; }); afterEach(async () => { await shutdownTelemetry(mockConfig); }); it('should use gRPC exporters when protocol is grpc', () => { initializeTelemetry(mockConfig); expect(OTLPTraceExporter).toHaveBeenCalledWith({ url: 'http://localhost:4317', compression: 'gzip', }); expect(OTLPLogExporter).toHaveBeenCalledWith({ url: 'http://localhost:4317', compression: 'gzip', }); expect(OTLPMetricExporter).toHaveBeenCalledWith({ url: 'http://localhost:4317', compression: 'gzip', }); expect(NodeSDK.prototype.start).toHaveBeenCalled(); }); it('should use HTTP exporters when protocol is http', () => { vi.spyOn(mockConfig, 'getTelemetryEnabled').mockReturnValue(true); vi.spyOn(mockConfig, 'getTelemetryOtlpProtocol').mockReturnValue('http'); vi.spyOn(mockConfig, 'getTelemetryOtlpEndpoint').mockReturnValue( 'http://localhost:4318', ); initializeTelemetry(mockConfig); expect(OTLPTraceExporterHttp).toHaveBeenCalledWith({ url: 'http://localhost:4318/', }); expect(OTLPLogExporterHttp).toHaveBeenCalledWith({ url: 'http://localhost:4318/', }); expect(OTLPMetricExporterHttp).toHaveBeenCalledWith({ url: 'http://localhost:4318/', }); expect(NodeSDK.prototype.start).toHaveBeenCalled(); }); it('should parse gRPC endpoint correctly', () => { vi.spyOn(mockConfig, 'getTelemetryOtlpEndpoint').mockReturnValue( 'https://my-collector.com', ); initializeTelemetry(mockConfig); expect(OTLPTraceExporter).toHaveBeenCalledWith( expect.objectContaining({ url: 'https://my-collector.com' }), ); }); it('should parse HTTP endpoint correctly', () => { vi.spyOn(mockConfig, 'getTelemetryOtlpProtocol').mockReturnValue('http'); vi.spyOn(mockConfig, 'getTelemetryOtlpEndpoint').mockReturnValue( 'https://my-collector.com', ); initializeTelemetry(mockConfig); expect(OTLPTraceExporterHttp).toHaveBeenCalledWith( expect.objectContaining({ url: 'https://my-collector.com/' }), ); }); });