@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
120 lines (116 loc) • 4.88 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const currentScopes = require('../../currentScopes.js');
const attributes$1 = require('@sentry/conventions/attributes');
const op = require('@sentry/conventions/op');
const semanticAttributes = require('../../semanticAttributes.js');
const hasSpanStreamingEnabled = require('../../tracing/spans/hasSpanStreamingEnabled.js');
const spanNames = require('../../tracing/spans/spanNames.js');
const trace = require('../../tracing/trace.js');
const attributeExtraction = require('./attributeExtraction.js');
const attributes = require('./attributes.js');
const methodConfig = require('./methodConfig.js');
const piiFiltering = require('./piiFiltering.js');
const sessionExtraction = require('./sessionExtraction.js');
function createSpanName(method, target) {
return target ? `${method} ${target}` : method;
}
function buildSentryAttributes(type) {
let op$1;
let origin;
switch (type) {
case "request":
op$1 = op.MCP_SERVER;
origin = attributes.MCP_FUNCTION_ORIGIN_VALUE;
break;
case "notification-incoming":
op$1 = op.MCP_NOTIFICATION_CLIENT_TO_SERVER;
origin = attributes.MCP_NOTIFICATION_ORIGIN_VALUE;
break;
case "notification-outgoing":
op$1 = op.MCP_NOTIFICATION_SERVER_TO_CLIENT;
origin = attributes.MCP_NOTIFICATION_ORIGIN_VALUE;
break;
}
return {
[attributes$1.SENTRY_OP]: op$1,
[semanticAttributes.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: origin,
[attributes$1.SENTRY_SEGMENT_NAME_SOURCE]: attributes.MCP_ROUTE_SOURCE_VALUE
};
}
function createMcpSpan(config) {
const { type, message, transport, extra, callback, options } = config;
const { method } = message;
const params = message.params;
const client = currentScopes.getClient();
const spanStreamingEnabled = !!client && hasSpanStreamingEnabled.hasSpanStreamingEnabled(client);
let spanName;
if (type === "request") {
const targetInfo = methodConfig.extractTargetInfo(method, params || {});
const target = spanStreamingEnabled && !targetInfo.targetIsLowCardinality ? void 0 : targetInfo.target;
spanName = method ? createSpanName(method, target) : spanNames.MCP_SERVER_SPAN_NAME_FALLBACK;
} else {
spanName = method || (spanStreamingEnabled ? spanNames.MCP_NOTIFICATION_SPAN_NAME_FALLBACK : method);
}
const rawAttributes = {
...sessionExtraction.buildTransportAttributes(transport, extra, message),
[attributes.MCP_METHOD_NAME_ATTRIBUTE]: method,
...attributeExtraction.buildTypeSpecificAttributes(type, message, params, options?.recordInputs),
...buildSentryAttributes(type)
};
const userInfo = Boolean(client?.getDataCollectionOptions().userInfo);
const attributes$1 = piiFiltering.filterMcpPiiFromSpanData(rawAttributes, userInfo);
return trace.startSpan(
{
name: spanName,
// oxlint-disable-next-line typescript/no-deprecated
forceTransaction: true,
attributes: attributes$1
},
callback
);
}
function createMcpNotificationSpan(jsonRpcMessage, transport, extra, options, callback) {
return createMcpSpan({
type: "notification-incoming",
message: jsonRpcMessage,
transport,
extra,
callback,
options
});
}
function createMcpOutgoingNotificationSpan(jsonRpcMessage, transport, options, callback) {
return createMcpSpan({
type: "notification-outgoing",
message: jsonRpcMessage,
transport,
options,
callback
});
}
function buildMcpServerSpanConfig(jsonRpcMessage, transport, extra, options) {
const { method } = jsonRpcMessage;
const params = jsonRpcMessage.params;
const client = currentScopes.getClient();
const spanStreamingEnabled = !!client && hasSpanStreamingEnabled.hasSpanStreamingEnabled(client);
const targetInfo = methodConfig.extractTargetInfo(method, params || {});
const target = spanStreamingEnabled && !targetInfo.targetIsLowCardinality ? void 0 : targetInfo.target;
const spanName = method ? createSpanName(method, target) : spanNames.MCP_SERVER_SPAN_NAME_FALLBACK;
const rawAttributes = {
...sessionExtraction.buildTransportAttributes(transport, extra, jsonRpcMessage),
[attributes.MCP_METHOD_NAME_ATTRIBUTE]: method,
...attributeExtraction.buildTypeSpecificAttributes("request", jsonRpcMessage, params, options?.recordInputs),
...buildSentryAttributes("request")
};
const userInfo = Boolean(client?.getDataCollectionOptions().userInfo);
const attributes$1 = piiFiltering.filterMcpPiiFromSpanData(rawAttributes, userInfo);
return {
name: spanName,
forceTransaction: true,
attributes: attributes$1
};
}
exports.buildMcpServerSpanConfig = buildMcpServerSpanConfig;
exports.createMcpNotificationSpan = createMcpNotificationSpan;
exports.createMcpOutgoingNotificationSpan = createMcpOutgoingNotificationSpan;
//# sourceMappingURL=spans.js.map