Skip to content

Commit

Permalink
Merge pull request #786 from DataDog/carlosnogueira/RUM-1206/add-tele…
Browse files Browse the repository at this point in the history
…metry-to-apollo-client

[RUM-1206] Add telemetry to apollo client
  • Loading branch information
marco-saia-datadog authored Feb 5, 2025
2 parents 9097cf7 + 2feafb9 commit 9564964
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
BatchSize
} from './DdSdkReactNativeConfiguration';
import { DdSdkReactNative } from './DdSdkReactNative';
import { DdSdk } from './sdk/DdSdk';
import { InternalLog } from './InternalLog';
import { ProxyConfiguration, ProxyType } from './ProxyConfiguration';
import { SdkVerbosity } from './SdkVerbosity';
Expand Down Expand Up @@ -48,6 +49,7 @@ export {
ErrorSource,
DdSdkReactNativeConfiguration,
DdSdkReactNative,
DdSdk,
InternalLog,
ProxyConfiguration,
ProxyType,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-apollo-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"peerDependencies": {
"@apollo/client": ">=3.0",
"@datadog/mobile-react-native": "^2.0.1",
"@datadog/mobile-react-native": "^2.6.1",
"react": ">=16.13.1",
"react-native": ">=0.63.4 <1.0"
},
Expand Down
34 changes: 32 additions & 2 deletions packages/react-native-apollo-client/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,28 @@
* NOTE: Do not import from '@apollo/client/utilities' as the package does not exist in Apollo Client < v3.
*/

import { version } from '@apollo/client/package.json';
import type { Operation } from '@apollo/client';
import { DdSdk } from '@datadog/mobile-react-native';
import type { DefinitionNode, OperationDefinitionNode } from 'graphql';

import { ErrorCode, errorMessages } from './types';

const apolloVersion = `[Apollo v${version}]`;

export const getVariables = (operation: Operation): string | null => {
if (operation.variables) {
try {
return JSON.stringify(operation.variables);
} catch (e) {
// TODO RUM-1206: telemetry
DdSdk?.telemetryError(
_getErrorMessage(
ErrorCode.GQL_VARIABLE_RETRIEVAL_ERROR,
apolloVersion
),
_getErrorStack(e),
ErrorCode.GQL_VARIABLE_RETRIEVAL_ERROR
);
return null;
}
}
Expand Down Expand Up @@ -48,7 +61,24 @@ export const getOperationType = (
})[0] || null
);
} catch (e) {
// TODO RUM-1206: telemetry
DdSdk?.telemetryError(
_getErrorMessage(ErrorCode.GQL_OPERATION_TYPE_ERROR, apolloVersion),
_getErrorStack(e),
ErrorCode.GQL_OPERATION_TYPE_ERROR
);
return null;
}
};

const _getErrorMessage = (code: ErrorCode, details: string) =>
`${errorMessages[code]} - ${details}`;

const _getErrorStack = (error: unknown): string => {
if (!error) {
return '';
}

return error instanceof Error
? error.stack ?? 'No stack trace available'
: `Non-Error thrown: ${error}`;
};
11 changes: 11 additions & 0 deletions packages/react-native-apollo-client/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const enum ErrorCode {
GQL_VARIABLE_RETRIEVAL_ERROR = 'GQL_VARIABLE_RETRIEVAL_ERROR',
GQL_OPERATION_TYPE_ERROR = 'GQL_OPERATION_TYPE_ERROR'
}

export const errorMessages: Record<ErrorCode, string> = {
[ErrorCode.GQL_VARIABLE_RETRIEVAL_ERROR]:
'Failed to retrieve GraphQL variables from operation',
[ErrorCode.GQL_OPERATION_TYPE_ERROR]:
'Failed to determine GraphQL operation type'
};
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2909,7 +2909,7 @@ __metadata:
react-native-builder-bob: 0.26.0
peerDependencies:
"@apollo/client": ">=3.0"
"@datadog/mobile-react-native": ^2.0.1
"@datadog/mobile-react-native": ^2.6.1
react: ">=16.13.1"
react-native: ">=0.63.4 <1.0"
languageName: unknown
Expand Down

0 comments on commit 9564964

Please sign in to comment.