Skip to content

Commit

Permalink
feat(unstable): replace SpanExporter with TracerProvider
Browse files Browse the repository at this point in the history
This is needed to make no-config @opentelemetry/api integration work.
  • Loading branch information
lucacasonato committed Dec 26, 2024
1 parent f4e3213 commit 294d845
Show file tree
Hide file tree
Showing 5 changed files with 996 additions and 1,019 deletions.
14 changes: 11 additions & 3 deletions cli/tsc/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1300,12 +1300,12 @@ declare namespace Deno {
*/
export namespace telemetry {
/**
* A SpanExporter compatible with OpenTelemetry.js
* https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_sdk_trace_base.SpanExporter.html
* A TracerProvider compatible with OpenTelemetry.js
* https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_api.TracerProvider.html
* @category Telemetry
* @experimental
*/
export class SpanExporter {}
export class TraceProvider {}

/**
* A ContextManager compatible with OpenTelemetry.js
Expand All @@ -1315,6 +1315,14 @@ declare namespace Deno {
*/
export class ContextManager {}

/**
* A MeterProvider compatible with OpenTelemetry.js
* https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_api.MeterProvider.html
* @category Telemetry
* @experimental
*/
export class MeterProvider {}

export {}; // only export exports
}

Expand Down
73 changes: 34 additions & 39 deletions ext/fetch/26_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ import {
} from "ext:deno_fetch/23_response.js";
import * as abortSignal from "ext:deno_web/03_abort_signal.js";
import {
endSpan,
builtinTracer,
enterSpan,
exitSpan,
Span,
restoreContext,
TRACING_ENABLED,
} from "ext:deno_telemetry/telemetry.ts";
import {
Expand Down Expand Up @@ -176,7 +175,7 @@ async function mainFetch(req, recursive, terminator) {
req.clientRid,
reqBody !== null || reqRid !== null,
reqBody,
reqRid,
reqRid
);

function onAbort() {
Expand Down Expand Up @@ -223,7 +222,7 @@ async function mainFetch(req, recursive, terminator) {
case "error":
core.close(resp.responseRid);
return networkError(
"Encountered redirect while redirect mode is set to 'error'",
"Encountered redirect while redirect mode is set to 'error'"
);
case "follow":
core.close(resp.responseRid);
Expand All @@ -241,7 +240,7 @@ async function mainFetch(req, recursive, terminator) {
core.close(resp.responseRid);
} else {
response.body = new InnerBody(
createResponseBodyStream(resp.responseRid, terminator),
createResponseBodyStream(resp.responseRid, terminator)
);
}
}
Expand All @@ -265,7 +264,7 @@ async function mainFetch(req, recursive, terminator) {
function httpRedirectFetch(request, response, terminator) {
const locationHeaders = ArrayPrototypeFilter(
response.headerList,
(entry) => byteLowerCase(entry[0]) === "location",
(entry) => byteLowerCase(entry[0]) === "location"
);
if (locationHeaders.length === 0) {
return response;
Expand All @@ -274,7 +273,7 @@ function httpRedirectFetch(request, response, terminator) {
const currentURL = new URL(request.currentUrl());
const locationURL = new URL(
locationHeaders[0][1],
response.url() ?? undefined,
response.url() ?? undefined
);
if (locationURL.hash === "") {
locationURL.hash = currentURL.hash;
Expand All @@ -292,7 +291,7 @@ function httpRedirectFetch(request, response, terminator) {
request.body.source === null
) {
return networkError(
"Can not redeliver a streaming request body after a redirect",
"Can not redeliver a streaming request body after a redirect"
);
}
if (
Expand All @@ -308,7 +307,7 @@ function httpRedirectFetch(request, response, terminator) {
if (
ArrayPrototypeIncludes(
REQUEST_BODY_HEADER_NAMES,
byteLowerCase(request.headerList[i][0]),
byteLowerCase(request.headerList[i][0])
)
) {
ArrayPrototypeSplice(request.headerList, i, 1);
Expand All @@ -320,16 +319,16 @@ function httpRedirectFetch(request, response, terminator) {
// Drop confidential headers when redirecting to a less secure protocol
// or to a different domain that is not a superdomain
if (
locationURL.protocol !== currentURL.protocol &&
locationURL.protocol !== "https:" ||
locationURL.host !== currentURL.host &&
!isSubdomain(locationURL.host, currentURL.host)
(locationURL.protocol !== currentURL.protocol &&
locationURL.protocol !== "https:") ||
(locationURL.host !== currentURL.host &&
!isSubdomain(locationURL.host, currentURL.host))
) {
for (let i = 0; i < request.headerList.length; i++) {
if (
ArrayPrototypeIncludes(
REDIRECT_SENSITIVE_HEADER_NAMES,
byteLowerCase(request.headerList[i][0]),
byteLowerCase(request.headerList[i][0])
)
) {
ArrayPrototypeSplice(request.headerList, i, 1);
Expand All @@ -352,10 +351,11 @@ function httpRedirectFetch(request, response, terminator) {
*/
function fetch(input, init = { __proto__: null }) {
let span;
let context;
try {
if (TRACING_ENABLED) {
span = new Span("fetch", { kind: 2 });
enterSpan(span);
span = builtinTracer.startSpan("fetch", { kind: 2 });
context = enterSpan(span);
}

// There is an async dispatch later that causes a stack trace disconnect.
Expand Down Expand Up @@ -389,7 +389,7 @@ function fetch(input, init = { __proto__: null }) {
function onabort() {
locallyAborted = true;
reject(
abortFetch(request, responseObject, requestObject.signal.reason),
abortFetch(request, responseObject, requestObject.signal.reason)
);
}
requestObject.signal[abortSignal.add](onabort);
Expand All @@ -412,19 +412,15 @@ function fetch(input, init = { __proto__: null }) {
// 12.2.
if (response.aborted) {
reject(
abortFetch(
request,
responseObject,
requestObject.signal.reason,
),
abortFetch(request, responseObject, requestObject.signal.reason)
);
requestObject.signal[abortSignal.remove](onabort);
return;
}
// 12.3.
if (response.type === "error") {
const err = new TypeError(
"Fetch failed: " + (response.error ?? "unknown error"),
"Fetch failed: " + (response.error ?? "unknown error")
);
reject(err);
requestObject.signal[abortSignal.remove](onabort);
Expand All @@ -438,12 +434,12 @@ function fetch(input, init = { __proto__: null }) {

resolve(responseObject);
requestObject.signal[abortSignal.remove](onabort);
},
}
),
(err) => {
reject(err);
requestObject.signal[abortSignal.remove](onabort);
},
}
);
});

Expand All @@ -454,9 +450,7 @@ function fetch(input, init = { __proto__: null }) {
await opPromise;
return result;
} finally {
if (span) {
endSpan(span);
}
span?.end();
}
})();
}
Expand All @@ -469,19 +463,17 @@ function fetch(input, init = { __proto__: null }) {
// XXX: This should always be true, otherwise `opPromise` would be present.
if (op_fetch_promise_is_settled(result)) {
// It's already settled.
endSpan(span);
span?.end();
} else {
// Not settled yet, we can return a new wrapper promise.
return SafePromisePrototypeFinally(result, () => {
endSpan(span);
span?.end();
});
}
}
return result;
} finally {
if (span) {
exitSpan(span);
}
if (context) restoreContext(context);
}
}

Expand All @@ -508,8 +500,11 @@ function abortFetch(request, responseObject, error) {
*/
function isSubdomain(subdomain, domain) {
const dot = subdomain.length - domain.length - 1;
return dot > 0 && subdomain[dot] === "." &&
StringPrototypeEndsWith(subdomain, domain);
return (
dot > 0 &&
subdomain[dot] === "." &&
StringPrototypeEndsWith(subdomain, domain)
);
}

/**
Expand All @@ -529,7 +524,7 @@ function handleWasmStreaming(source, rid) {
const res = webidl.converters["Response"](
source,
"Failed to execute 'WebAssembly.compileStreaming'",
"Argument 1",
"Argument 1"
);

// 2.3.
Expand All @@ -550,7 +545,7 @@ function handleWasmStreaming(source, rid) {
// 2.5.
if (!res.ok) {
throw new TypeError(
`Failed to receive WebAssembly content: HTTP status code ${res.status}`,
`Failed to receive WebAssembly content: HTTP status code ${res.status}`
);
}

Expand All @@ -573,7 +568,7 @@ function handleWasmStreaming(source, rid) {
// 2.7
() => core.close(rid),
// 2.8
(err) => core.abortWasmStreaming(rid, err),
(err) => core.abortWasmStreaming(rid, err)
);
} else {
// 2.7
Expand Down
Loading

0 comments on commit 294d845

Please sign in to comment.