@sentry/browser
Version:
Official Sentry SDK for browsers
178 lines (177 loc) • 7.47 kB
JavaScript
let _sentry_core = require("@sentry/core");
let _sentry_browser_utils = require("@sentry/browser-utils");
let _sentry_conventions_attributes = require("@sentry/conventions/attributes");
// src/integrations/graphqlClient.ts
const INTEGRATION_NAME = "GraphQLClient";
const _graphqlClientIntegration = ((options) => {
return {
name: INTEGRATION_NAME,
setup(client) {
_updateSpanWithGraphQLData(client, options);
_updateBreadcrumbWithGraphQLData(client, options);
}
};
});
function _updateSpanWithGraphQLData(client, options) {
client.on("beforeOutgoingRequestSpan", (span, hint) => {
const spanAttributes = (0, _sentry_core.spanToJSON)(span).attributes;
if (!(spanAttributes[_sentry_conventions_attributes.SENTRY_OP] === "http.client")) return;
const httpUrl = spanAttributes[_sentry_conventions_attributes.URL_FULL];
const httpMethod = spanAttributes[_sentry_conventions_attributes.HTTP_REQUEST_METHOD] || spanAttributes[_sentry_conventions_attributes.HTTP_METHOD];
if (!(0, _sentry_core.isString)(httpUrl) || !(0, _sentry_core.isString)(httpMethod)) return;
const { endpoints } = options;
const isTracedGraphqlEndpoint = (0, _sentry_core.stringMatchesSomePattern)(httpUrl, endpoints);
const payload = getRequestPayloadXhrOrFetch(hint);
if (isTracedGraphqlEndpoint && payload) {
const graphqlBody = getGraphQLRequestPayload(payload);
if (graphqlBody) {
if (!(0, _sentry_core.hasSpanStreamingEnabled)(client)) span.updateName(`${httpMethod} ${httpUrl} (${_getGraphQLOperation(graphqlBody)})`);
const { operationName, operationType } = _getGraphQLOperationDetails(graphqlBody);
span.setAttribute(_sentry_conventions_attributes.GRAPHQL_OPERATION_NAME, operationName);
span.setAttribute(_sentry_conventions_attributes.GRAPHQL_OPERATION_TYPE, operationType);
if (isStandardRequest(graphqlBody) && client.getDataCollectionOptions().graphQL.document === true) span.setAttribute(_sentry_conventions_attributes.GRAPHQL_DOCUMENT, graphqlBody.query);
if (isPersistedRequest(graphqlBody)) {
span.setAttribute("graphql.persisted_query.hash.sha256", graphqlBody.extensions.persistedQuery.sha256Hash);
span.setAttribute("graphql.persisted_query.version", graphqlBody.extensions.persistedQuery.version);
}
}
}
});
}
function _updateBreadcrumbWithGraphQLData(client, options) {
client.on("beforeOutgoingRequestBreadcrumb", (breadcrumb, handlerData) => {
const { category, type, data } = breadcrumb;
if (type === "http" && (category === "fetch" || category === "xhr")) {
const httpUrl = data?.url;
const { endpoints } = options;
const isTracedGraphqlEndpoint = (0, _sentry_core.stringMatchesSomePattern)(httpUrl, endpoints);
const payload = getRequestPayloadXhrOrFetch(handlerData);
if (isTracedGraphqlEndpoint && data && payload) {
const graphqlBody = getGraphQLRequestPayload(payload);
if (!data.graphql && graphqlBody) {
data["graphql.operation"] = _getGraphQLOperation(graphqlBody);
if (isStandardRequest(graphqlBody) && client.getDataCollectionOptions().graphQL.document === true) data[_sentry_conventions_attributes.GRAPHQL_DOCUMENT] = graphqlBody.query;
if (isPersistedRequest(graphqlBody)) {
data["graphql.persisted_query.hash.sha256"] = graphqlBody.extensions.persistedQuery.sha256Hash;
data["graphql.persisted_query.version"] = graphqlBody.extensions.persistedQuery.version;
}
}
}
}
});
}
/**
* The operation name and type of a GraphQL request. Persisted operations carry no query document, so
* their type is unknown.
*/
function _getGraphQLOperationDetails(requestBody) {
if (isPersistedRequest(requestBody)) return {
operationName: requestBody.operationName,
operationType: void 0
};
if (isStandardRequest(requestBody)) {
const { query: graphqlQuery, operationName: graphqlOperationName } = requestBody;
const { operationName = graphqlOperationName, operationType } = parseGraphQLQuery(graphqlQuery);
return {
operationName,
operationType
};
}
return {
operationName: void 0,
operationType: void 0
};
}
/**
* @param requestBody - GraphQL request
* @returns A formatted version of the request: 'TYPE NAME' or 'TYPE' or 'persisted NAME'
*/
function _getGraphQLOperation(requestBody) {
if (isPersistedRequest(requestBody)) return `persisted ${requestBody.operationName}`;
if (isStandardRequest(requestBody)) {
const { operationName, operationType } = _getGraphQLOperationDetails(requestBody);
return operationName ? `${operationType} ${operationName}` : `${operationType}`;
}
return "unknown";
}
/**
* Get the request body/payload based on the shape of the hint.
*
* Exported for tests only.
*/
function getRequestPayloadXhrOrFetch(hint) {
const isXhr = "xhr" in hint;
let body;
if (isXhr) {
const sentryXhrData = hint.xhr[_sentry_browser_utils.SENTRY_XHR_DATA_KEY];
body = sentryXhrData && (0, _sentry_browser_utils.getBodyString)(sentryXhrData.body)[0];
} else {
const sentryFetchData = (0, _sentry_browser_utils.getFetchRequestArgBody)(hint.input);
body = (0, _sentry_browser_utils.getBodyString)(sentryFetchData)[0];
}
return body;
}
/**
* Extract the name and type of the operation from the GraphQL query.
*
* Exported for tests only.
*/
function parseGraphQLQuery(query) {
const namedQueryRe = /^(?:\s*)(query|mutation|subscription)(?:\s*)(\w+)(?:\s*)[{(]/;
const unnamedQueryRe = /^(?:\s*)(query|mutation|subscription)(?:\s*)[{(]/;
const namedMatch = query.match(namedQueryRe);
if (namedMatch) return {
operationType: namedMatch[1],
operationName: namedMatch[2]
};
const unnamedMatch = query.match(unnamedQueryRe);
if (unnamedMatch) return {
operationType: unnamedMatch[1],
operationName: void 0
};
return {
operationType: void 0,
operationName: void 0
};
}
/**
* Helper to safely check if a value is a non-null object
*/
/**
* Type guard to check if a request is a standard GraphQL request
*/
function isStandardRequest(payload) {
return (0, _sentry_core.isObjectLike)(payload) && typeof payload.query === "string";
}
/**
* Type guard to check if a request is a persisted operation request
*/
function isPersistedRequest(payload) {
return (0, _sentry_core.isObjectLike)(payload) && typeof payload.operationName === "string" && (0, _sentry_core.isObjectLike)(payload.extensions) && (0, _sentry_core.isObjectLike)(payload.extensions.persistedQuery) && typeof payload.extensions.persistedQuery.sha256Hash === "string" && typeof payload.extensions.persistedQuery.version === "number";
}
/**
* Extract the payload of a request if it's GraphQL.
* Exported for tests only.
* @param payload - A valid JSON string
* @returns A POJO or undefined
*/
function getGraphQLRequestPayload(payload) {
try {
const requestBody = JSON.parse(payload);
if (isStandardRequest(requestBody) || isPersistedRequest(requestBody)) return requestBody;
return;
} catch {
return;
}
}
/**
* This integration ensures that GraphQL requests made in the browser
* have their GraphQL-specific data captured and attached to spans and breadcrumbs.
*/
const graphqlClientIntegration = (0, _sentry_core.defineIntegration)(_graphqlClientIntegration);
exports._getGraphQLOperation = _getGraphQLOperation;
exports.getGraphQLRequestPayload = getGraphQLRequestPayload;
exports.getRequestPayloadXhrOrFetch = getRequestPayloadXhrOrFetch;
exports.graphqlClientIntegration = graphqlClientIntegration;
exports.parseGraphQLQuery = parseGraphQLQuery;
//# sourceMappingURL=graphqlClient.js.map