@datadog/mobile-react-native 3.1.2 → 3.3.0

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.
Files changed (88) hide show
  1. package/DatadogSDKReactNative.podspec +7 -7
  2. package/android/build.gradle +23 -8
  3. package/android/src/main/kotlin/com/datadog/reactnative/DdRumImplementation.kt +1 -0
  4. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkImplementation.kt +1 -0
  5. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkNativeInitialization.kt +5 -0
  6. package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
  7. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +54 -0
  8. package/ios/Sources/AnyEncodable.swift +16 -83
  9. package/ios/Sources/Attributes.swift +30 -0
  10. package/ios/Sources/DdRumImplementation.swift +2 -1
  11. package/ios/Sources/DdSdkNativeInitialization.swift +8 -1
  12. package/ios/Sources/RUMMonitorInternalProtocol.swift +8 -1
  13. package/ios/Sources/SdkVersion.swift +1 -1
  14. package/lib/commonjs/DdAttributes.js +7 -1
  15. package/lib/commonjs/DdAttributes.js.map +1 -1
  16. package/lib/commonjs/index.js +12 -0
  17. package/lib/commonjs/index.js.map +1 -1
  18. package/lib/commonjs/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.js +1 -1
  19. package/lib/commonjs/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.js.map +1 -1
  20. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/headers.js +3 -1
  21. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/headers.js.map +1 -1
  22. package/lib/commonjs/rum/instrumentation/resourceTracking/graphql/graphqlHeaders.js +4 -1
  23. package/lib/commonjs/rum/instrumentation/resourceTracking/graphql/graphqlHeaders.js.map +1 -1
  24. package/lib/commonjs/rum/instrumentation/resourceTracking/graphql/graphqlUtils.js +51 -0
  25. package/lib/commonjs/rum/instrumentation/resourceTracking/graphql/graphqlUtils.js.map +1 -0
  26. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js +6 -0
  27. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js.map +1 -1
  28. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.js +36 -1
  29. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.js.map +1 -1
  30. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/xhrUtils.js +170 -0
  31. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/xhrUtils.js.map +1 -0
  32. package/lib/commonjs/trace/DdTrace.js +1 -1
  33. package/lib/commonjs/trace/DdTrace.js.map +1 -1
  34. package/lib/commonjs/version.js +1 -1
  35. package/lib/module/DdAttributes.js +7 -1
  36. package/lib/module/DdAttributes.js.map +1 -1
  37. package/lib/module/index.js +2 -2
  38. package/lib/module/index.js.map +1 -1
  39. package/lib/module/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.js +1 -1
  40. package/lib/module/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.js.map +1 -1
  41. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/headers.js +2 -0
  42. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/headers.js.map +1 -1
  43. package/lib/module/rum/instrumentation/resourceTracking/graphql/graphqlHeaders.js +3 -2
  44. package/lib/module/rum/instrumentation/resourceTracking/graphql/graphqlHeaders.js.map +1 -1
  45. package/lib/module/rum/instrumentation/resourceTracking/graphql/graphqlUtils.js +45 -0
  46. package/lib/module/rum/instrumentation/resourceTracking/graphql/graphqlUtils.js.map +1 -0
  47. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js +6 -0
  48. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js.map +1 -1
  49. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.js +38 -3
  50. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.js.map +1 -1
  51. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/xhrUtils.js +161 -0
  52. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/xhrUtils.js.map +1 -0
  53. package/lib/module/trace/DdTrace.js +1 -1
  54. package/lib/module/trace/DdTrace.js.map +1 -1
  55. package/lib/module/version.js +1 -1
  56. package/lib/typescript/DdAttributes.d.ts +6 -0
  57. package/lib/typescript/DdAttributes.d.ts.map +1 -1
  58. package/lib/typescript/index.d.ts +2 -2
  59. package/lib/typescript/index.d.ts.map +1 -1
  60. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/headers.d.ts +2 -0
  61. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/headers.d.ts.map +1 -1
  62. package/lib/typescript/rum/instrumentation/resourceTracking/graphql/graphqlHeaders.d.ts +2 -0
  63. package/lib/typescript/rum/instrumentation/resourceTracking/graphql/graphqlHeaders.d.ts.map +1 -1
  64. package/lib/typescript/rum/instrumentation/resourceTracking/graphql/graphqlUtils.d.ts +7 -0
  65. package/lib/typescript/rum/instrumentation/resourceTracking/graphql/graphqlUtils.d.ts.map +1 -0
  66. package/lib/typescript/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.d.ts.map +1 -1
  67. package/lib/typescript/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/xhrUtils.d.ts +36 -0
  68. package/lib/typescript/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/xhrUtils.d.ts.map +1 -0
  69. package/lib/typescript/rum/instrumentation/resourceTracking/requestProxy/interfaces/RumResource.d.ts +11 -0
  70. package/lib/typescript/rum/instrumentation/resourceTracking/requestProxy/interfaces/RumResource.d.ts.map +1 -1
  71. package/lib/typescript/trace/DdTrace.d.ts.map +1 -1
  72. package/lib/typescript/version.d.ts +1 -1
  73. package/package.json +2 -2
  74. package/src/DdAttributes.ts +8 -1
  75. package/src/index.tsx +5 -1
  76. package/src/rum/instrumentation/interactionTracking/DdBabelInteractionTracking.ts +1 -1
  77. package/src/rum/instrumentation/interactionTracking/__tests__/DdBabelInteractionTracking.test.ts +69 -0
  78. package/src/rum/instrumentation/resourceTracking/distributedTracing/headers.ts +2 -0
  79. package/src/rum/instrumentation/resourceTracking/graphql/__tests__/graphqlUtils.test.ts +143 -0
  80. package/src/rum/instrumentation/resourceTracking/graphql/graphqlHeaders.ts +4 -2
  81. package/src/rum/instrumentation/resourceTracking/graphql/graphqlUtils.ts +61 -0
  82. package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.ts +10 -0
  83. package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.ts +56 -6
  84. package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/__tests__/XHRProxy.test.ts +841 -1
  85. package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/xhrUtils.ts +175 -0
  86. package/src/rum/instrumentation/resourceTracking/requestProxy/interfaces/RumResource.ts +9 -0
  87. package/src/trace/DdTrace.ts +1 -4
  88. package/src/version.ts +1 -1
@@ -33,9 +33,12 @@ import {
33
33
  ORIGIN_HEADER_KEY,
34
34
  TRACESTATE_HEADER_KEY,
35
35
  TAGS_HEADER_KEY,
36
- BAGGAGE_HEADER_KEY
36
+ BAGGAGE_HEADER_KEY,
37
+ TRACKED_BY_HEADER_KEY,
38
+ TRACKED_BY_HEADER_VALUE
37
39
  } from '../../../distributedTracing/headers';
38
40
  import {
41
+ DATADOG_GRAPH_QL_ERROR_HEADER,
39
42
  DATADOG_GRAPH_QL_OPERATION_NAME_HEADER,
40
43
  DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
41
44
  DATADOG_GRAPH_QL_VARIABLES_HEADER
@@ -452,6 +455,57 @@ describe('XHRProxy', () => {
452
455
  );
453
456
  });
454
457
 
458
+ it('adds the x-datadog-tracked-by header for first party host requests', async () => {
459
+ // GIVEN
460
+ const method = 'GET';
461
+ const url = 'https://api.example.com/v2/user';
462
+ xhrProxy.onTrackingStart({
463
+ tracingSamplingRate: 100,
464
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([
465
+ {
466
+ match: 'api.example.com',
467
+ propagatorTypes: [PropagatorType.DATADOG]
468
+ }
469
+ ])
470
+ });
471
+
472
+ // WHEN
473
+ const xhr = new XMLHttpRequestMock();
474
+ xhr.open(method, url);
475
+ xhr.send();
476
+ xhr.notifyResponseArrived();
477
+ xhr.complete(200, 'ok');
478
+ await flushPromises();
479
+
480
+ // THEN
481
+ expect(xhr.requestHeaders.get(TRACKED_BY_HEADER_KEY)).toBe(
482
+ TRACKED_BY_HEADER_VALUE
483
+ );
484
+ });
485
+
486
+ it('adds the x-datadog-tracked-by header for non-first party host requests', async () => {
487
+ // GIVEN
488
+ const method = 'GET';
489
+ const url = 'https://api.example.com/v2/user';
490
+ xhrProxy.onTrackingStart({
491
+ tracingSamplingRate: 100,
492
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
493
+ });
494
+
495
+ // WHEN
496
+ const xhr = new XMLHttpRequestMock();
497
+ xhr.open(method, url);
498
+ xhr.send();
499
+ xhr.notifyResponseArrived();
500
+ xhr.complete(200, 'ok');
501
+ await flushPromises();
502
+
503
+ // THEN
504
+ expect(xhr.requestHeaders.get(TRACKED_BY_HEADER_KEY)).toBe(
505
+ TRACKED_BY_HEADER_VALUE
506
+ );
507
+ });
508
+
455
509
  it('adds tracecontext request headers when the host is instrumented with tracecontext and request is sampled', async () => {
456
510
  // GIVEN
457
511
  const method = 'GET';
@@ -1639,4 +1693,790 @@ describe('XHRProxy', () => {
1639
1693
  );
1640
1694
  });
1641
1695
  });
1696
+
1697
+ describe('GraphQL error filtering', () => {
1698
+ it('extracts and filters GraphQL errors with all fields from response', async () => {
1699
+ // GIVEN
1700
+ const method = 'POST';
1701
+ const url = 'https://api.example.com/graphql';
1702
+ const graphqlResponse = {
1703
+ data: { user: null },
1704
+ errors: [
1705
+ {
1706
+ message: 'User not found',
1707
+ locations: [{ line: 2, column: 3 }],
1708
+ path: ['user', 0, 'id'],
1709
+ extensions: {
1710
+ code: 'NOT_FOUND',
1711
+ stacktrace: ['at UserResolver.getUser...', '...'],
1712
+ timestamp: '2024-01-01',
1713
+ customField: 'should be filtered'
1714
+ }
1715
+ }
1716
+ ]
1717
+ };
1718
+
1719
+ xhrProxy.onTrackingStart({
1720
+ tracingSamplingRate: 100,
1721
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
1722
+ });
1723
+
1724
+ // WHEN
1725
+ const xhr = new XMLHttpRequestMock();
1726
+ xhr.open(method, url);
1727
+ xhr.setRequestHeader(
1728
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
1729
+ 'query'
1730
+ );
1731
+ xhr.setRequestHeader(
1732
+ DATADOG_GRAPH_QL_OPERATION_NAME_HEADER,
1733
+ 'GetUser'
1734
+ );
1735
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'true');
1736
+ xhr.send();
1737
+ xhr.notifyResponseArrived();
1738
+ xhr.responseText = JSON.stringify(graphqlResponse);
1739
+ xhr.complete(200, JSON.stringify(graphqlResponse));
1740
+ await flushPromises();
1741
+
1742
+ // THEN
1743
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
1744
+ expect(attributes['_dd.graphql.operation_type']).toEqual('query');
1745
+ expect(attributes['_dd.graphql.operation_name']).toEqual('GetUser');
1746
+ expect(attributes['_dd.graphql.errors']).toBeDefined();
1747
+
1748
+ const errors = JSON.parse(attributes['_dd.graphql.errors']);
1749
+ expect(errors).toHaveLength(1);
1750
+
1751
+ expect(errors[0]).toEqual({
1752
+ message: 'User not found',
1753
+ code: 'NOT_FOUND',
1754
+ locations: [{ line: 2, column: 3 }],
1755
+ path: ['user', 0, 'id']
1756
+ });
1757
+ // Verify stacktrace and other extensions are filtered out
1758
+ expect(errors[0].extensions).toBeUndefined();
1759
+ expect(errors[0].stacktrace).toBeUndefined();
1760
+ expect(errors[0].timestamp).toBeUndefined();
1761
+ });
1762
+
1763
+ it('extracts code from legacy top-level code field when extensions.code is missing', async () => {
1764
+ // GIVEN
1765
+ const method = 'POST';
1766
+ const url = 'https://api.example.com/graphql';
1767
+ const graphqlResponse = {
1768
+ data: null,
1769
+ errors: [
1770
+ {
1771
+ message: 'Invalid query',
1772
+ code: 'GRAPHQL_VALIDATION_FAILED' // Legacy top-level code
1773
+ }
1774
+ ]
1775
+ };
1776
+
1777
+ xhrProxy.onTrackingStart({
1778
+ tracingSamplingRate: 100,
1779
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
1780
+ });
1781
+
1782
+ // WHEN
1783
+ const xhr = new XMLHttpRequestMock();
1784
+ xhr.open(method, url);
1785
+ xhr.setRequestHeader(
1786
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
1787
+ 'query'
1788
+ );
1789
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'true');
1790
+ xhr.send();
1791
+ xhr.notifyResponseArrived();
1792
+ xhr.responseText = JSON.stringify(graphqlResponse);
1793
+ xhr.complete(200, JSON.stringify(graphqlResponse));
1794
+ await flushPromises();
1795
+
1796
+ // THEN
1797
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
1798
+
1799
+ const errors = JSON.parse(attributes['_dd.graphql.errors']);
1800
+ expect(errors[0]).toEqual({
1801
+ message: 'Invalid query',
1802
+ code: 'GRAPHQL_VALIDATION_FAILED'
1803
+ });
1804
+ });
1805
+
1806
+ it('handles multiple GraphQL errors', async () => {
1807
+ // GIVEN
1808
+ const method = 'POST';
1809
+ const url = 'https://api.example.com/graphql';
1810
+ const graphqlResponse = {
1811
+ data: { user: { id: '1', posts: null, comments: null } },
1812
+ errors: [
1813
+ {
1814
+ message: 'Insufficient permissions',
1815
+ path: ['user', 'posts'],
1816
+ extensions: { code: 'FORBIDDEN' }
1817
+ },
1818
+ {
1819
+ message: 'Database timeout',
1820
+ path: ['user', 'comments'],
1821
+ extensions: { code: 'TIMEOUT' }
1822
+ }
1823
+ ]
1824
+ };
1825
+
1826
+ xhrProxy.onTrackingStart({
1827
+ tracingSamplingRate: 100,
1828
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
1829
+ });
1830
+
1831
+ // WHEN
1832
+ const xhr = new XMLHttpRequestMock();
1833
+ xhr.open(method, url);
1834
+ xhr.setRequestHeader(
1835
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
1836
+ 'query'
1837
+ );
1838
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'true');
1839
+ xhr.send();
1840
+ xhr.notifyResponseArrived();
1841
+ xhr.responseText = JSON.stringify(graphqlResponse);
1842
+ xhr.complete(200, JSON.stringify(graphqlResponse));
1843
+ await flushPromises();
1844
+
1845
+ // THEN
1846
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
1847
+
1848
+ const errors = JSON.parse(attributes['_dd.graphql.errors']);
1849
+ expect(errors).toHaveLength(2);
1850
+ expect(errors[0]).toEqual({
1851
+ message: 'Insufficient permissions',
1852
+ code: 'FORBIDDEN',
1853
+ path: ['user', 'posts']
1854
+ });
1855
+ expect(errors[1]).toEqual({
1856
+ message: 'Database timeout',
1857
+ code: 'TIMEOUT',
1858
+ path: ['user', 'comments']
1859
+ });
1860
+ });
1861
+
1862
+ it('handles GraphQL errors with only required message field', async () => {
1863
+ // GIVEN
1864
+ const method = 'POST';
1865
+ const url = 'https://api.example.com/graphql';
1866
+ const graphqlResponse = {
1867
+ data: null,
1868
+ errors: [
1869
+ {
1870
+ message: 'Internal server error'
1871
+ }
1872
+ ]
1873
+ };
1874
+
1875
+ xhrProxy.onTrackingStart({
1876
+ tracingSamplingRate: 100,
1877
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
1878
+ });
1879
+
1880
+ // WHEN
1881
+ const xhr = new XMLHttpRequestMock();
1882
+ xhr.open(method, url);
1883
+ xhr.setRequestHeader(
1884
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
1885
+ 'mutation'
1886
+ );
1887
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'true');
1888
+ xhr.send();
1889
+ xhr.notifyResponseArrived();
1890
+ xhr.responseText = JSON.stringify(graphqlResponse);
1891
+ xhr.complete(200, JSON.stringify(graphqlResponse));
1892
+ await flushPromises();
1893
+
1894
+ // THEN
1895
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
1896
+
1897
+ const errors = JSON.parse(attributes['_dd.graphql.errors']);
1898
+ expect(errors[0]).toEqual({
1899
+ message: 'Internal server error'
1900
+ });
1901
+ });
1902
+
1903
+ it('does not extract errors for non-GraphQL requests', async () => {
1904
+ // GIVEN
1905
+ const method = 'GET';
1906
+ const url = 'https://api.example.com/users';
1907
+ const response = {
1908
+ errors: [{ message: 'Some error' }]
1909
+ };
1910
+
1911
+ xhrProxy.onTrackingStart({
1912
+ tracingSamplingRate: 100,
1913
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
1914
+ });
1915
+
1916
+ // WHEN
1917
+ const xhr = new XMLHttpRequestMock();
1918
+ xhr.open(method, url);
1919
+ // Note: No GraphQL operation type header
1920
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'true');
1921
+ xhr.send();
1922
+ xhr.notifyResponseArrived();
1923
+ xhr.responseText = JSON.stringify(response);
1924
+ xhr.complete(200, JSON.stringify(response));
1925
+ await flushPromises();
1926
+
1927
+ // THEN
1928
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
1929
+ expect(attributes['_dd.graphql.errors']).toBeUndefined();
1930
+ });
1931
+
1932
+ it('does not extract errors when trackErrors is false', async () => {
1933
+ // GIVEN
1934
+ const method = 'POST';
1935
+ const url = 'https://api.example.com/graphql';
1936
+ const graphqlResponse = {
1937
+ data: { user: null },
1938
+ errors: [
1939
+ {
1940
+ message: 'User not found',
1941
+ extensions: { code: 'NOT_FOUND' }
1942
+ }
1943
+ ]
1944
+ };
1945
+
1946
+ xhrProxy.onTrackingStart({
1947
+ tracingSamplingRate: 100,
1948
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
1949
+ });
1950
+
1951
+ // WHEN
1952
+ const xhr = new XMLHttpRequestMock();
1953
+ xhr.open(method, url);
1954
+ xhr.setRequestHeader(
1955
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
1956
+ 'query'
1957
+ );
1958
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'false'); // Explicitly disabled
1959
+ xhr.send();
1960
+ xhr.notifyResponseArrived();
1961
+ xhr.responseText = JSON.stringify(graphqlResponse);
1962
+ xhr.complete(200, JSON.stringify(graphqlResponse));
1963
+ await flushPromises();
1964
+
1965
+ // THEN - errors should NOT be extracted
1966
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
1967
+ expect(attributes['_dd.graphql.errors']).toBeUndefined();
1968
+ // But other GraphQL attributes should still be set
1969
+ expect(attributes['_dd.graphql.operation_type']).toEqual('query');
1970
+ });
1971
+
1972
+ it('does not extract errors when trackErrors header is missing', async () => {
1973
+ // GIVEN
1974
+ const method = 'POST';
1975
+ const url = 'https://api.example.com/graphql';
1976
+ const graphqlResponse = {
1977
+ data: { user: null },
1978
+ errors: [
1979
+ {
1980
+ message: 'User not found',
1981
+ extensions: { code: 'NOT_FOUND' }
1982
+ }
1983
+ ]
1984
+ };
1985
+
1986
+ xhrProxy.onTrackingStart({
1987
+ tracingSamplingRate: 100,
1988
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
1989
+ });
1990
+
1991
+ // WHEN
1992
+ const xhr = new XMLHttpRequestMock();
1993
+ xhr.open(method, url);
1994
+ xhr.setRequestHeader(
1995
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
1996
+ 'query'
1997
+ );
1998
+ // Note: No DATADOG_GRAPH_QL_ERROR_HEADER set
1999
+ xhr.send();
2000
+ xhr.notifyResponseArrived();
2001
+ xhr.responseText = JSON.stringify(graphqlResponse);
2002
+ xhr.complete(200, JSON.stringify(graphqlResponse));
2003
+ await flushPromises();
2004
+
2005
+ // THEN - errors should NOT be extracted
2006
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
2007
+ expect(attributes['_dd.graphql.errors']).toBeUndefined();
2008
+ // But other GraphQL attributes should still be set
2009
+ expect(attributes['_dd.graphql.operation_type']).toEqual('query');
2010
+ });
2011
+
2012
+ it('handles invalid JSON response gracefully', async () => {
2013
+ // GIVEN
2014
+ const method = 'POST';
2015
+ const url = 'https://api.example.com/graphql';
2016
+ const invalidJson = '{ invalid json }';
2017
+
2018
+ xhrProxy.onTrackingStart({
2019
+ tracingSamplingRate: 100,
2020
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
2021
+ });
2022
+
2023
+ // WHEN
2024
+ const xhr = new XMLHttpRequestMock();
2025
+ xhr.open(method, url);
2026
+ xhr.setRequestHeader(
2027
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
2028
+ 'query'
2029
+ );
2030
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'true');
2031
+ xhr.send();
2032
+ xhr.notifyResponseArrived();
2033
+ xhr.responseText = invalidJson;
2034
+ xhr.complete(200, invalidJson);
2035
+ await flushPromises();
2036
+
2037
+ // THEN - should not crash and should not set errors attribute
2038
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
2039
+ expect(attributes['_dd.graphql.errors']).toBeUndefined();
2040
+ });
2041
+
2042
+ it('handles GraphQL response with empty errors array', async () => {
2043
+ // GIVEN
2044
+ const method = 'POST';
2045
+ const url = 'https://api.example.com/graphql';
2046
+ const graphqlResponse = {
2047
+ data: { user: { id: '1' } },
2048
+ errors: []
2049
+ };
2050
+
2051
+ xhrProxy.onTrackingStart({
2052
+ tracingSamplingRate: 100,
2053
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
2054
+ });
2055
+
2056
+ // WHEN
2057
+ const xhr = new XMLHttpRequestMock();
2058
+ xhr.open(method, url);
2059
+ xhr.setRequestHeader(
2060
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
2061
+ 'query'
2062
+ );
2063
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'true');
2064
+ xhr.send();
2065
+ xhr.notifyResponseArrived();
2066
+ xhr.responseText = JSON.stringify(graphqlResponse);
2067
+ xhr.complete(200, JSON.stringify(graphqlResponse));
2068
+ await flushPromises();
2069
+
2070
+ // THEN - empty array should not set errors attribute
2071
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
2072
+ expect(attributes['_dd.graphql.errors']).toBeUndefined();
2073
+ });
2074
+
2075
+ it('handles GraphQL response without errors field', async () => {
2076
+ // GIVEN
2077
+ const method = 'POST';
2078
+ const url = 'https://api.example.com/graphql';
2079
+ const graphqlResponse = {
2080
+ data: { user: { id: '1' } }
2081
+ };
2082
+
2083
+ xhrProxy.onTrackingStart({
2084
+ tracingSamplingRate: 100,
2085
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
2086
+ });
2087
+
2088
+ // WHEN
2089
+ const xhr = new XMLHttpRequestMock();
2090
+ xhr.open(method, url);
2091
+ xhr.setRequestHeader(
2092
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
2093
+ 'query'
2094
+ );
2095
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'true');
2096
+ xhr.send();
2097
+ xhr.notifyResponseArrived();
2098
+ xhr.responseText = JSON.stringify(graphqlResponse);
2099
+ xhr.complete(200, JSON.stringify(graphqlResponse));
2100
+ await flushPromises();
2101
+
2102
+ // THEN
2103
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
2104
+ expect(attributes['_dd.graphql.errors']).toBeUndefined();
2105
+ });
2106
+
2107
+ it('prefers extensions.code over legacy top-level code', async () => {
2108
+ // GIVEN
2109
+ const method = 'POST';
2110
+ const url = 'https://api.example.com/graphql';
2111
+ const graphqlResponse = {
2112
+ data: null,
2113
+ errors: [
2114
+ {
2115
+ message: 'Error',
2116
+ code: 'LEGACY_CODE',
2117
+ extensions: {
2118
+ code: 'EXTENSIONS_CODE' // Should take precedence
2119
+ }
2120
+ }
2121
+ ]
2122
+ };
2123
+
2124
+ xhrProxy.onTrackingStart({
2125
+ tracingSamplingRate: 100,
2126
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
2127
+ });
2128
+
2129
+ // WHEN
2130
+ const xhr = new XMLHttpRequestMock();
2131
+ xhr.open(method, url);
2132
+ xhr.setRequestHeader(
2133
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
2134
+ 'query'
2135
+ );
2136
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'true');
2137
+ xhr.send();
2138
+ xhr.notifyResponseArrived();
2139
+ xhr.responseText = JSON.stringify(graphqlResponse);
2140
+ xhr.complete(200, JSON.stringify(graphqlResponse));
2141
+ await flushPromises();
2142
+
2143
+ // THEN
2144
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
2145
+
2146
+ const errors = JSON.parse(attributes['_dd.graphql.errors']);
2147
+ expect(errors[0].code).toEqual('EXTENSIONS_CODE');
2148
+ });
2149
+
2150
+ it('handles mixed path types (strings and integers)', async () => {
2151
+ // GIVEN
2152
+ const method = 'POST';
2153
+ const url = 'https://api.example.com/graphql';
2154
+ const graphqlResponse = {
2155
+ data: null,
2156
+ errors: [
2157
+ {
2158
+ message: 'Item not found',
2159
+ path: ['items', 0, 'name', 'first'] // Mixed string and int
2160
+ }
2161
+ ]
2162
+ };
2163
+
2164
+ xhrProxy.onTrackingStart({
2165
+ tracingSamplingRate: 100,
2166
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
2167
+ });
2168
+
2169
+ // WHEN
2170
+ const xhr = new XMLHttpRequestMock();
2171
+ xhr.open(method, url);
2172
+ xhr.setRequestHeader(
2173
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
2174
+ 'query'
2175
+ );
2176
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'true');
2177
+ xhr.send();
2178
+ xhr.notifyResponseArrived();
2179
+ xhr.responseText = JSON.stringify(graphqlResponse);
2180
+ xhr.complete(200, JSON.stringify(graphqlResponse));
2181
+ await flushPromises();
2182
+
2183
+ // THEN
2184
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
2185
+ const errors = JSON.parse(attributes['_dd.graphql.errors']);
2186
+ expect(errors[0].path).toEqual(['items', 0, 'name', 'first']);
2187
+ });
2188
+
2189
+ describe('response integrity during error extraction', () => {
2190
+ it('allows application to read responseText while extracting errors (text responseType)', async () => {
2191
+ // GIVEN
2192
+ const method = 'POST';
2193
+ const url = 'https://api.example.com/graphql';
2194
+ const graphqlResponse = {
2195
+ data: { user: null },
2196
+ errors: [
2197
+ {
2198
+ message: 'User not found',
2199
+ extensions: { code: 'NOT_FOUND' }
2200
+ }
2201
+ ]
2202
+ };
2203
+ const responseBody = JSON.stringify(graphqlResponse);
2204
+
2205
+ xhrProxy.onTrackingStart({
2206
+ tracingSamplingRate: 100,
2207
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
2208
+ });
2209
+
2210
+ // WHEN
2211
+ const xhr = new XMLHttpRequestMock();
2212
+ let applicationReadResponse: string | null = null;
2213
+
2214
+ xhr.open(method, url);
2215
+ xhr.setRequestHeader(
2216
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
2217
+ 'query'
2218
+ );
2219
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'true');
2220
+
2221
+ // Simulate application reading response in onreadystatechange
2222
+ const originalOnReadyStateChange = xhr.onreadystatechange;
2223
+ xhr.onreadystatechange = () => {
2224
+ if (xhr.readyState === XMLHttpRequestMock.DONE) {
2225
+ // Application reads responseText immediately when request completes
2226
+ applicationReadResponse = xhr.responseText;
2227
+ }
2228
+ return originalOnReadyStateChange.call(xhr);
2229
+ };
2230
+
2231
+ xhr.send();
2232
+ xhr.notifyResponseArrived();
2233
+ xhr.responseText = responseBody;
2234
+ xhr.complete(200, responseBody, 'text');
2235
+ await flushPromises();
2236
+
2237
+ // THEN - Application successfully read the response
2238
+ expect(applicationReadResponse).toBe(responseBody);
2239
+ const parsedByApp = JSON.parse(applicationReadResponse!);
2240
+ expect(parsedByApp.errors).toHaveLength(1);
2241
+ expect(parsedByApp.errors[0].message).toBe('User not found');
2242
+
2243
+ // AND - Datadog successfully extracted filtered errors
2244
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
2245
+ const errors = JSON.parse(attributes['_dd.graphql.errors']);
2246
+ expect(errors).toHaveLength(1);
2247
+ expect(errors[0]).toEqual({
2248
+ message: 'User not found',
2249
+ code: 'NOT_FOUND'
2250
+ });
2251
+ });
2252
+
2253
+ it('allows application to read response object while extracting errors (json responseType)', async () => {
2254
+ // GIVEN
2255
+ const method = 'POST';
2256
+ const url = 'https://api.example.com/graphql';
2257
+ const graphqlResponse = {
2258
+ data: { user: null },
2259
+ errors: [
2260
+ {
2261
+ message: 'Access denied',
2262
+ path: ['user'],
2263
+ extensions: { code: 'FORBIDDEN' }
2264
+ }
2265
+ ]
2266
+ };
2267
+
2268
+ xhrProxy.onTrackingStart({
2269
+ tracingSamplingRate: 100,
2270
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
2271
+ });
2272
+
2273
+ // WHEN
2274
+ const xhr = new XMLHttpRequestMock();
2275
+ let applicationReadResponse: any = null;
2276
+
2277
+ xhr.open(method, url);
2278
+ xhr.setRequestHeader(
2279
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
2280
+ 'mutation'
2281
+ );
2282
+
2283
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'true');
2284
+
2285
+ // Simulate application reading response in onreadystatechange
2286
+ const originalOnReadyStateChange = xhr.onreadystatechange;
2287
+ xhr.onreadystatechange = () => {
2288
+ if (xhr.readyState === XMLHttpRequestMock.DONE) {
2289
+ // Application reads parsed response object (json responseType)
2290
+ applicationReadResponse = xhr.response;
2291
+ }
2292
+ return originalOnReadyStateChange.call(xhr);
2293
+ };
2294
+
2295
+ xhr.send();
2296
+ xhr.notifyResponseArrived();
2297
+ xhr.complete(200, graphqlResponse, 'json');
2298
+ await flushPromises();
2299
+
2300
+ // THEN - Application successfully read the parsed response
2301
+ expect(applicationReadResponse).toEqual(graphqlResponse);
2302
+ expect(applicationReadResponse.errors).toHaveLength(1);
2303
+ expect(applicationReadResponse.errors[0].message).toBe(
2304
+ 'Access denied'
2305
+ );
2306
+ expect(applicationReadResponse.errors[0].extensions.code).toBe(
2307
+ 'FORBIDDEN'
2308
+ );
2309
+
2310
+ // AND - Datadog successfully extracted filtered errors (extensions filtered out)
2311
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
2312
+ const errors = JSON.parse(attributes['_dd.graphql.errors']);
2313
+ expect(errors).toHaveLength(1);
2314
+ expect(errors[0]).toEqual({
2315
+ message: 'Access denied',
2316
+ code: 'FORBIDDEN',
2317
+ path: ['user']
2318
+ });
2319
+ expect(errors[0].extensions).toBeUndefined();
2320
+ });
2321
+
2322
+ it('allows application to read blob response while extracting errors (blob responseType)', async () => {
2323
+ // GIVEN
2324
+ const method = 'POST';
2325
+ const url = 'https://api.example.com/graphql';
2326
+ const graphqlResponse = {
2327
+ data: null,
2328
+ errors: [
2329
+ {
2330
+ message: 'Server error',
2331
+ extensions: { code: 'INTERNAL_ERROR' }
2332
+ }
2333
+ ]
2334
+ };
2335
+ const responseBody = JSON.stringify(graphqlResponse);
2336
+
2337
+ // Create a mock blob with _data property (RN-style blob)
2338
+ const mockBlob = {
2339
+ _data: responseBody,
2340
+ size: responseBody.length,
2341
+ type: 'application/json'
2342
+ };
2343
+
2344
+ xhrProxy.onTrackingStart({
2345
+ tracingSamplingRate: 100,
2346
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
2347
+ });
2348
+
2349
+ // WHEN
2350
+ const xhr = new XMLHttpRequestMock();
2351
+ let applicationReadBlob: any = null;
2352
+
2353
+ xhr.open(method, url);
2354
+ xhr.setRequestHeader(
2355
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
2356
+ 'query'
2357
+ );
2358
+
2359
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'true');
2360
+
2361
+ // Simulate application reading response in onreadystatechange
2362
+ const originalOnReadyStateChange = xhr.onreadystatechange;
2363
+ xhr.onreadystatechange = () => {
2364
+ if (xhr.readyState === XMLHttpRequestMock.DONE) {
2365
+ // Application reads blob response
2366
+ applicationReadBlob = xhr.response;
2367
+ }
2368
+ return originalOnReadyStateChange.call(xhr);
2369
+ };
2370
+
2371
+ xhr.send();
2372
+ xhr.notifyResponseArrived();
2373
+ xhr.complete(200, mockBlob, 'blob');
2374
+ await flushPromises();
2375
+
2376
+ // THEN - Application successfully read the blob
2377
+ expect(applicationReadBlob).toBe(mockBlob);
2378
+ expect(applicationReadBlob._data).toBe(responseBody);
2379
+ expect(applicationReadBlob.size).toBe(responseBody.length);
2380
+
2381
+ // AND - Datadog successfully extracted filtered errors
2382
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
2383
+ const errors = JSON.parse(attributes['_dd.graphql.errors']);
2384
+ expect(errors).toHaveLength(1);
2385
+ expect(errors[0]).toEqual({
2386
+ message: 'Server error',
2387
+ code: 'INTERNAL_ERROR'
2388
+ });
2389
+ });
2390
+
2391
+ it('handles multiple errors without affecting application response access', async () => {
2392
+ // GIVEN
2393
+ const method = 'POST';
2394
+ const url = 'https://api.example.com/graphql';
2395
+ const graphqlResponse = {
2396
+ data: {
2397
+ user: { id: '1', name: 'Alice' },
2398
+ posts: null,
2399
+ comments: null
2400
+ },
2401
+ errors: [
2402
+ {
2403
+ message: 'Posts not found',
2404
+ path: ['posts'],
2405
+ extensions: {
2406
+ code: 'NOT_FOUND',
2407
+ details: 'filtered'
2408
+ }
2409
+ },
2410
+ {
2411
+ message: 'Comments timeout',
2412
+ path: ['comments'],
2413
+ extensions: { code: 'TIMEOUT', stacktrace: ['...'] }
2414
+ }
2415
+ ]
2416
+ };
2417
+ const responseBody = JSON.stringify(graphqlResponse);
2418
+
2419
+ xhrProxy.onTrackingStart({
2420
+ tracingSamplingRate: 100,
2421
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder([])
2422
+ });
2423
+
2424
+ // WHEN
2425
+ const xhr = new XMLHttpRequestMock();
2426
+ let applicationReadResponse: string | null = null;
2427
+
2428
+ xhr.open(method, url);
2429
+ xhr.setRequestHeader(
2430
+ DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
2431
+ 'query'
2432
+ );
2433
+
2434
+ xhr.setRequestHeader(DATADOG_GRAPH_QL_ERROR_HEADER, 'true');
2435
+
2436
+ // Simulate application reading response
2437
+ const originalOnReadyStateChange = xhr.onreadystatechange;
2438
+ xhr.onreadystatechange = () => {
2439
+ if (xhr.readyState === XMLHttpRequestMock.DONE) {
2440
+ applicationReadResponse = xhr.responseText;
2441
+ }
2442
+ return originalOnReadyStateChange.call(xhr);
2443
+ };
2444
+
2445
+ xhr.send();
2446
+ xhr.notifyResponseArrived();
2447
+ xhr.responseText = responseBody;
2448
+ xhr.complete(200, responseBody, 'text');
2449
+ await flushPromises();
2450
+
2451
+ // THEN - Application got complete response with all error details
2452
+ expect(applicationReadResponse).toBe(responseBody);
2453
+ const parsedByApp = JSON.parse(applicationReadResponse!);
2454
+ expect(parsedByApp.errors).toHaveLength(2);
2455
+ expect(parsedByApp.errors[0].extensions.details).toBe(
2456
+ 'filtered'
2457
+ );
2458
+ expect(parsedByApp.errors[1].extensions.stacktrace).toEqual([
2459
+ '...'
2460
+ ]);
2461
+
2462
+ // AND - Datadog got filtered errors (no extensions details)
2463
+ const attributes = DdNativeRum.stopResource.mock.calls[0][4];
2464
+ const errors = JSON.parse(attributes['_dd.graphql.errors']);
2465
+ expect(errors).toHaveLength(2);
2466
+ expect(errors[0]).toEqual({
2467
+ message: 'Posts not found',
2468
+ code: 'NOT_FOUND',
2469
+ path: ['posts']
2470
+ });
2471
+ expect(errors[0].extensions).toBeUndefined();
2472
+ expect(errors[0].details).toBeUndefined();
2473
+ expect(errors[1]).toEqual({
2474
+ message: 'Comments timeout',
2475
+ code: 'TIMEOUT',
2476
+ path: ['comments']
2477
+ });
2478
+ expect(errors[1].stacktrace).toBeUndefined();
2479
+ });
2480
+ });
2481
+ });
1642
2482
  });