@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
66 lines (65 loc) • 3.22 kB
JavaScript
import { getClient } from "../../currentScopes.js";
import { hasSpanStreamingEnabled } from "../../tracing/spans/hasSpanStreamingEnabled.js";
import { HTTP_SPAN_NAME_FALLBACK } from "../../tracing/spans/spanNames.js";
import { filterCollectedUrl } from "../../utils/data-collection/filterCollectedUrl.js";
import { getContentLengthFromHeaders } from "../../utils/request.js";
import { getHttpSpanDetailsFromUrlObject, isURLObjectRelative, parseStringToURLObject } from "../../utils/url.js";
import { getRequestUrlFromClientRequest } from "./get-request-url.js";
import { HTTP_RESPONSE_BODY_SIZE, HTTP_RESPONSE_STATUS_CODE, HTTP_RESPONSE_STATUS_TEXT, NETWORK_LOCAL_ADDRESS, NETWORK_LOCAL_PORT, NETWORK_PEER_ADDRESS, NETWORK_PEER_PORT, NETWORK_PROTOCOL_NAME, NETWORK_PROTOCOL_VERSION, NETWORK_TRANSPORT, SENTRY_KIND, SENTRY_OP, SERVER_ADDRESS, SERVER_PORT, URL_FULL, USER_AGENT_ORIGINAL } from "@sentry/conventions/attributes";
import { HTTP_CLIENT } from "@sentry/conventions/op";
// src/integrations/http/get-outgoing-span-data.ts
/**
* Build the initial span name and attributes for an outgoing HTTP request.
* This is called before the span is created, to get the initial details.
*/
function getOutgoingRequestSpanData(request) {
const url = getRequestUrlFromClientRequest(request);
const urlObject = parseStringToURLObject(url);
const [name, attributes] = getHttpSpanDetailsFromUrlObject(urlObject, "client", "auto.http.client", request);
const userAgent = request.getHeader("user-agent");
const client = getClient();
const method = request.method?.toUpperCase();
const domain = urlObject && !isURLObjectRelative(urlObject) ? urlObject.hostname : void 0;
const streamedName = method ? domain ? `${method} ${domain}` : method : HTTP_SPAN_NAME_FALLBACK;
return {
name: !!client && hasSpanStreamingEnabled(client) ? streamedName : name,
attributes: {
[]: HTTP_CLIENT,
[]: "client",
[]: filterCollectedUrl(url),
[]: request.host,
[]: typeof request.port === "number" && !isNaN(request.port) ? request.port : void 0,
[]: userAgent || void 0,
...attributes
},
onlyIfParent: true
};
}
/**
* Add span attributes once the response is received.
*/
function setIncomingResponseSpanData(response, span) {
const { statusCode, statusMessage, httpVersion, socket } = response;
const transport = httpVersion?.toUpperCase() !== "QUIC" ? "tcp" : "udp";
span.setAttributes({
[]: statusCode,
[]: "http",
[]: httpVersion,
[]: transport,
[]: statusMessage?.toUpperCase(),
[]: getContentLengthFromHeaders(response.headers),
...getSocketAttrs(socket)
});
}
function getSocketAttrs(socket) {
if (!socket) return {};
const { localAddress, localPort, remoteAddress, remotePort } = socket;
return {
[]: localAddress,
[]: localPort,
[]: remoteAddress,
[]: remotePort
};
}
export { getOutgoingRequestSpanData, setIncomingResponseSpanData };
//# sourceMappingURL=get-outgoing-span-data.js.map