node-sdk.spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { Resource } from '@opentelemetry/resources';
  2. import { NodeSDK } from '@opentelemetry/sdk-node';
  3. import {
  4. beforeEach, describe, expect, it, vi,
  5. } from 'vitest';
  6. import { configManager } from '~/server/service/config-manager';
  7. import { detectServiceInstanceId, initInstrumentation } from './node-sdk';
  8. import { getSdkInstance, resetSdkInstance } from './node-sdk.testing';
  9. // Only mock configManager as it's external to what we're testing
  10. vi.mock('~/server/service/config-manager', () => ({
  11. configManager: {
  12. getConfig: vi.fn(),
  13. loadConfigs: vi.fn(),
  14. },
  15. }));
  16. describe('node-sdk', () => {
  17. beforeEach(() => {
  18. vi.clearAllMocks();
  19. vi.resetModules();
  20. resetSdkInstance();
  21. // Reset configManager mock implementation
  22. (configManager.getConfig as any).mockImplementation((key: string) => {
  23. if (key === 'otel:enabled') return true;
  24. if (key === 'otel:serviceInstanceId') return undefined;
  25. if (key === 'app:serviceInstanceId') return 'test-instance-id';
  26. return undefined;
  27. });
  28. });
  29. describe('detectServiceInstanceId', () => {
  30. it('should update service.instance.id when app:serviceInstanceId is available', async() => {
  31. // Initialize SDK first
  32. await initInstrumentation();
  33. // Get instance for testing
  34. const sdkInstance = getSdkInstance();
  35. expect(sdkInstance).toBeDefined();
  36. expect(sdkInstance).toBeInstanceOf(NodeSDK);
  37. // Verify initial state (service.instance.id should not be set)
  38. const resource = (sdkInstance as any)._resource;
  39. expect(resource).toBeInstanceOf(Resource);
  40. expect(resource.attributes['service.instance.id']).toBeUndefined();
  41. // Call detectServiceInstanceId
  42. await detectServiceInstanceId();
  43. // Verify that resource was updated with app:serviceInstanceId
  44. const updatedResource = (sdkInstance as any)._resource;
  45. expect(updatedResource.attributes['service.instance.id']).toBe('test-instance-id');
  46. });
  47. it('should update service.instance.id with otel:serviceInstanceId if available', async() => {
  48. // Mock otel:serviceInstanceId is available
  49. (configManager.getConfig as any).mockImplementation((key: string) => {
  50. if (key === 'otel:enabled') return true;
  51. if (key === 'otel:serviceInstanceId') return 'otel-instance-id';
  52. if (key === 'app:serviceInstanceId') return 'test-instance-id';
  53. return undefined;
  54. });
  55. // Initialize SDK
  56. await initInstrumentation();
  57. // Verify initial state
  58. const sdkInstance = getSdkInstance();
  59. const resource = (sdkInstance as any)._resource;
  60. expect(resource.attributes['service.instance.id']).toBeUndefined();
  61. // Call detectServiceInstanceId
  62. await detectServiceInstanceId();
  63. // Verify that otel:serviceInstanceId was used
  64. const updatedResource = (sdkInstance as any)._resource;
  65. expect(updatedResource.attributes['service.instance.id']).toBe('otel-instance-id');
  66. });
  67. it('should not create SDK instance if instrumentation is disabled', async() => {
  68. // Mock instrumentation as disabled
  69. (configManager.getConfig as any).mockImplementation((key: string) => {
  70. if (key === 'otel:enabled') return false;
  71. return undefined;
  72. });
  73. // Initialize SDK
  74. await initInstrumentation();
  75. // Verify that no SDK instance was created
  76. const sdkInstance = getSdkInstance();
  77. expect(sdkInstance).toBeUndefined();
  78. // Call detectServiceInstanceId
  79. await detectServiceInstanceId();
  80. // Verify that still no SDK instance exists
  81. const updatedSdkInstance = getSdkInstance();
  82. expect(updatedSdkInstance).toBeUndefined();
  83. });
  84. });
  85. });