@echoteam/signoz-react 1.2.2 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -15370,6 +15370,16 @@ function addFetchLogging(config) {
15370
15370
  const responseData = await clonedResponse.text();
15371
15371
  const truncatedData = truncateBody(responseData, config.maxBodyLogSize);
15372
15372
  span.setAttribute('response.data', truncatedData);
15373
+ // Try to parse JSON and extract message if exists
15374
+ try {
15375
+ const jsonData = JSON.parse(responseData);
15376
+ if (jsonData.message) {
15377
+ span.setAttribute('response.message', jsonData.message);
15378
+ }
15379
+ }
15380
+ catch (e) {
15381
+ // Not JSON or no message field, ignore
15382
+ }
15373
15383
  if (config.enableConsoleLog) {
15374
15384
  console.log(`[SignOz] ${method} Response from ${url} (${response.status}) on page ${pagePath} [${Math.round(duration)}ms]:`, truncatedData);
15375
15385
  }
@@ -15452,6 +15462,16 @@ function addXHRLogging(config) {
15452
15462
  const responseData = xhr.responseText;
15453
15463
  const truncatedData = truncateBody(responseData, config.maxBodyLogSize);
15454
15464
  span.setAttribute('response.data', truncatedData);
15465
+ // Try to parse JSON and extract message if exists
15466
+ try {
15467
+ const jsonData = JSON.parse(responseData);
15468
+ if (jsonData.message) {
15469
+ span.setAttribute('response.message', jsonData.message);
15470
+ }
15471
+ }
15472
+ catch (e) {
15473
+ // Not JSON or no message field, ignore
15474
+ }
15455
15475
  if (config.enableConsoleLog) {
15456
15476
  console.log(`[SignOz] ${method} Response from ${url} (${xhr.status}) on page ${pagePath} [${Math.round(duration)}ms]:`, truncatedData);
15457
15477
  }
@@ -15605,6 +15625,172 @@ function addNavigationTracking(enableConsoleLog = false) {
15605
15625
  return result;
15606
15626
  };
15607
15627
  }
15628
+ // Fungsi untuk menambahkan WebSocket logging
15629
+ function addWebSocketLogging(config) {
15630
+ if (!config.enableWebSocketLogging)
15631
+ return;
15632
+ const OriginalWebSocket = window.WebSocket;
15633
+ window.WebSocket = function (url, protocols) {
15634
+ const ws = new OriginalWebSocket(url, protocols);
15635
+ const wsUrl = typeof url === 'string' ? url : url.toString();
15636
+ // Capture current page info
15637
+ const pageUrl = window.location.href;
15638
+ const pagePath = window.location.pathname;
15639
+ const tracer = trace.getTracer('websocket-logger');
15640
+ let connectionSpan = tracer.startSpan('WebSocket Connection');
15641
+ const connectionStartTime = performance.now();
15642
+ connectionSpan.setAttribute('websocket.url', wsUrl);
15643
+ connectionSpan.setAttribute('websocket.protocols', Array.isArray(protocols) ? protocols.join(',') : (protocols || 'none'));
15644
+ connectionSpan.setAttribute('page.url', pageUrl);
15645
+ connectionSpan.setAttribute('page.pathname', pagePath);
15646
+ // Track connection open
15647
+ const originalOnOpen = ws.onopen;
15648
+ ws.addEventListener('open', function (event) {
15649
+ const duration = performance.now() - connectionStartTime;
15650
+ connectionSpan.setAttribute('websocket.state', 'open');
15651
+ connectionSpan.setAttribute('duration_ms', Math.round(duration));
15652
+ connectionSpan.setStatus({ code: SpanStatusCode.OK });
15653
+ if (config.enableConsoleLog) {
15654
+ console.log(`[SignOz] WebSocket Connected to ${wsUrl} from page ${pagePath} [${Math.round(duration)}ms]`);
15655
+ }
15656
+ connectionSpan.end();
15657
+ if (originalOnOpen) {
15658
+ originalOnOpen.call(ws, event);
15659
+ }
15660
+ });
15661
+ // Track messages sent
15662
+ const originalSend = ws.send;
15663
+ ws.send = function (data) {
15664
+ if (config.logWebSocketMessages) {
15665
+ const messageSpan = tracer.startSpan('WebSocket Send');
15666
+ const sendStartTime = performance.now();
15667
+ messageSpan.setAttribute('websocket.url', wsUrl);
15668
+ messageSpan.setAttribute('websocket.direction', 'send');
15669
+ messageSpan.setAttribute('page.url', pageUrl);
15670
+ messageSpan.setAttribute('page.pathname', pagePath);
15671
+ // Log message data
15672
+ if (typeof data === 'string') {
15673
+ const truncatedData = truncateBody(data, config.maxBodyLogSize);
15674
+ messageSpan.setAttribute('websocket.message', truncatedData);
15675
+ // Try to parse JSON and extract message if exists
15676
+ try {
15677
+ const jsonData = JSON.parse(data);
15678
+ if (jsonData.message) {
15679
+ messageSpan.setAttribute('websocket.message.text', jsonData.message);
15680
+ }
15681
+ if (jsonData.type || jsonData.event) {
15682
+ messageSpan.setAttribute('websocket.message.type', jsonData.type || jsonData.event);
15683
+ }
15684
+ }
15685
+ catch (e) {
15686
+ // Not JSON, ignore
15687
+ }
15688
+ if (config.enableConsoleLog) {
15689
+ console.log(`[SignOz] WebSocket Send to ${wsUrl} from page ${pagePath}:`, truncatedData);
15690
+ }
15691
+ }
15692
+ else {
15693
+ messageSpan.setAttribute('websocket.message', '[Binary Data]');
15694
+ messageSpan.setAttribute('websocket.message.type', 'binary');
15695
+ if (config.enableConsoleLog) {
15696
+ console.log(`[SignOz] WebSocket Send to ${wsUrl} from page ${pagePath}: [Binary Data]`);
15697
+ }
15698
+ }
15699
+ const duration = performance.now() - sendStartTime;
15700
+ messageSpan.setAttribute('duration_ms', Math.round(duration));
15701
+ messageSpan.setStatus({ code: SpanStatusCode.OK });
15702
+ messageSpan.end();
15703
+ }
15704
+ return originalSend.call(ws, data);
15705
+ };
15706
+ // Track messages received
15707
+ const originalOnMessage = ws.onmessage;
15708
+ ws.addEventListener('message', function (event) {
15709
+ if (config.logWebSocketMessages) {
15710
+ const messageSpan = tracer.startSpan('WebSocket Receive');
15711
+ messageSpan.setAttribute('websocket.url', wsUrl);
15712
+ messageSpan.setAttribute('websocket.direction', 'receive');
15713
+ messageSpan.setAttribute('page.url', pageUrl);
15714
+ messageSpan.setAttribute('page.pathname', pagePath);
15715
+ // Log message data
15716
+ if (typeof event.data === 'string') {
15717
+ const truncatedData = truncateBody(event.data, config.maxBodyLogSize);
15718
+ messageSpan.setAttribute('websocket.message', truncatedData);
15719
+ messageSpan.setAttribute('response.data', truncatedData); // Add response.data for consistency
15720
+ // Try to parse JSON and extract message if exists
15721
+ try {
15722
+ const jsonData = JSON.parse(event.data);
15723
+ if (jsonData.message) {
15724
+ messageSpan.setAttribute('websocket.message.text', jsonData.message);
15725
+ messageSpan.setAttribute('response.message', jsonData.message); // Add response.message for consistency
15726
+ }
15727
+ if (jsonData.type || jsonData.event) {
15728
+ messageSpan.setAttribute('websocket.message.type', jsonData.type || jsonData.event);
15729
+ }
15730
+ }
15731
+ catch (e) {
15732
+ // Not JSON, ignore
15733
+ }
15734
+ if (config.enableConsoleLog) {
15735
+ console.log(`[SignOz] WebSocket Receive from ${wsUrl} on page ${pagePath}:`, truncatedData);
15736
+ }
15737
+ }
15738
+ else {
15739
+ messageSpan.setAttribute('websocket.message', '[Binary Data]');
15740
+ messageSpan.setAttribute('websocket.message.type', 'binary');
15741
+ messageSpan.setAttribute('response.data', '[Binary Data]'); // Add response.data for consistency
15742
+ if (config.enableConsoleLog) {
15743
+ console.log(`[SignOz] WebSocket Receive from ${wsUrl} on page ${pagePath}: [Binary Data]`);
15744
+ }
15745
+ }
15746
+ messageSpan.setStatus({ code: SpanStatusCode.OK });
15747
+ messageSpan.end();
15748
+ }
15749
+ if (originalOnMessage) {
15750
+ originalOnMessage.call(ws, event);
15751
+ }
15752
+ });
15753
+ // Track connection errors
15754
+ const originalOnError = ws.onerror;
15755
+ ws.addEventListener('error', function (event) {
15756
+ const errorSpan = tracer.startSpan('WebSocket Error');
15757
+ errorSpan.setAttribute('websocket.url', wsUrl);
15758
+ errorSpan.setAttribute('websocket.state', 'error');
15759
+ errorSpan.setAttribute('page.url', pageUrl);
15760
+ errorSpan.setAttribute('page.pathname', pagePath);
15761
+ errorSpan.recordException(new Error('WebSocket connection error'));
15762
+ errorSpan.setStatus({ code: SpanStatusCode.ERROR, message: 'WebSocket connection error' });
15763
+ if (config.enableConsoleLog) {
15764
+ console.error(`[SignOz] WebSocket Error for ${wsUrl} on page ${pagePath}`);
15765
+ }
15766
+ errorSpan.end();
15767
+ if (originalOnError) {
15768
+ originalOnError.call(ws, event);
15769
+ }
15770
+ });
15771
+ // Track connection close
15772
+ const originalOnClose = ws.onclose;
15773
+ ws.addEventListener('close', function (event) {
15774
+ const closeSpan = tracer.startSpan('WebSocket Close');
15775
+ closeSpan.setAttribute('websocket.url', wsUrl);
15776
+ closeSpan.setAttribute('websocket.state', 'closed');
15777
+ closeSpan.setAttribute('websocket.close.code', event.code);
15778
+ closeSpan.setAttribute('websocket.close.reason', event.reason || 'No reason provided');
15779
+ closeSpan.setAttribute('websocket.close.wasClean', event.wasClean);
15780
+ closeSpan.setAttribute('page.url', pageUrl);
15781
+ closeSpan.setAttribute('page.pathname', pagePath);
15782
+ if (config.enableConsoleLog) {
15783
+ console.log(`[SignOz] WebSocket Closed ${wsUrl} on page ${pagePath} - Code: ${event.code}, Reason: ${event.reason || 'No reason'}, Clean: ${event.wasClean}`);
15784
+ }
15785
+ closeSpan.setStatus({ code: SpanStatusCode.OK });
15786
+ closeSpan.end();
15787
+ if (originalOnClose) {
15788
+ originalOnClose.call(ws, event);
15789
+ }
15790
+ });
15791
+ return ws;
15792
+ };
15793
+ }
15608
15794
  /**
15609
15795
  * Inisialisasi SignOz tracing untuk aplikasi React
15610
15796
  * @param config - Konfigurasi SignOz (opsional, akan menggunakan environment variables jika tidak disediakan)
@@ -15636,7 +15822,9 @@ function initializeSignOzTracing(config) {
15636
15822
  enableDocumentLoad: (config === null || config === void 0 ? void 0 : config.enableDocumentLoad) !== undefined ? config.enableDocumentLoad : (getConfigValue('REACT_APP_SIGNOZ_ENABLE_DOCUMENT_LOAD') === 'true'),
15637
15823
  enableErrorTracking: (config === null || config === void 0 ? void 0 : config.enableErrorTracking) !== undefined ? config.enableErrorTracking : (getConfigValue('REACT_APP_SIGNOZ_ENABLE_ERROR_TRACKING') !== 'false'),
15638
15824
  enableNavigationTracking: (config === null || config === void 0 ? void 0 : config.enableNavigationTracking) !== undefined ? config.enableNavigationTracking : (getConfigValue('REACT_APP_SIGNOZ_ENABLE_NAVIGATION_TRACKING') === 'true'),
15639
- enableConsoleLog: (config === null || config === void 0 ? void 0 : config.enableConsoleLog) !== undefined ? config.enableConsoleLog : (getConfigValue('REACT_APP_SIGNOZ_ENABLE_CONSOLE_LOG') === 'true')
15825
+ enableConsoleLog: (config === null || config === void 0 ? void 0 : config.enableConsoleLog) !== undefined ? config.enableConsoleLog : (getConfigValue('REACT_APP_SIGNOZ_ENABLE_CONSOLE_LOG') === 'true'),
15826
+ enableWebSocketLogging: (config === null || config === void 0 ? void 0 : config.enableWebSocketLogging) !== undefined ? config.enableWebSocketLogging : (getConfigValue('REACT_APP_SIGNOZ_ENABLE_WEBSOCKET_LOGGING') === 'true'),
15827
+ logWebSocketMessages: (config === null || config === void 0 ? void 0 : config.logWebSocketMessages) !== undefined ? config.logWebSocketMessages : (getConfigValue('REACT_APP_SIGNOZ_LOG_WEBSOCKET_MESSAGES') === 'true')
15640
15828
  };
15641
15829
  // Validasi konfigurasi
15642
15830
  const { isValid, missingFields } = validateConfig(effectiveConfig);
@@ -15724,6 +15912,15 @@ function initializeSignOzTracing(config) {
15724
15912
  if (effectiveConfig.enableNavigationTracking) {
15725
15913
  addNavigationTracking(effectiveConfig.enableConsoleLog);
15726
15914
  }
15915
+ // Tambahkan WebSocket logging
15916
+ if (effectiveConfig.enableWebSocketLogging) {
15917
+ addWebSocketLogging({
15918
+ enableWebSocketLogging: effectiveConfig.enableWebSocketLogging,
15919
+ logWebSocketMessages: effectiveConfig.logWebSocketMessages,
15920
+ maxBodyLogSize: effectiveConfig.maxBodyLogSize,
15921
+ enableConsoleLog: effectiveConfig.enableConsoleLog
15922
+ });
15923
+ }
15727
15924
  console.log('SignOz: Konfigurasi tracing:', {
15728
15925
  serviceName: effectiveConfig.serviceName,
15729
15926
  environment: effectiveConfig.environment,
@@ -15735,7 +15932,9 @@ function initializeSignOzTracing(config) {
15735
15932
  maxBodyLogSize: effectiveConfig.maxBodyLogSize,
15736
15933
  enableDocumentLoad: effectiveConfig.enableDocumentLoad,
15737
15934
  enableErrorTracking: effectiveConfig.enableErrorTracking,
15738
- enableNavigationTracking: effectiveConfig.enableNavigationTracking
15935
+ enableNavigationTracking: effectiveConfig.enableNavigationTracking,
15936
+ enableWebSocketLogging: effectiveConfig.enableWebSocketLogging,
15937
+ logWebSocketMessages: effectiveConfig.logWebSocketMessages
15739
15938
  });
15740
15939
  console.log('SignOz: Tracing berhasil diinisialisasi');
15741
15940
  }