Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(context): req记录context,防止close和finish事件清理后无法上报 #557

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions lib/core/runtime/create-server.hack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import * as http from "http";
import * as https from "https";
import * as domain from "domain";
import currentContext, { RequestLog } from "../context";
import currentContext, { Context, RequestLog } from "../context";
import { address } from "ip";
import { AddressInfo, isIP } from "net";
import { captureOutgoing } from "./capture/outgoing";
Expand Down Expand Up @@ -56,7 +56,7 @@ export const hack = <T extends typeof http.createServer>(

// Creating a domain and wrapping the execution.
const d = domain.create();

const context = new Context();
d.add(req);
d.add(res);

Expand Down Expand Up @@ -86,8 +86,6 @@ export const hack = <T extends typeof http.createServer>(
): ReturnType<typeof res.writeHead> => {
timestamps.onResponse = new Date().getTime();

const context = currentContext();

eventBus.emit(EVENT_LIST.RESPONSE_START, {
req, res, context
});
Expand All @@ -98,8 +96,6 @@ export const hack = <T extends typeof http.createServer>(
})(res.writeHead);

res.once("finish", () => {
const context = currentContext();

context.currentRequest = {
SN: context.SN,

Expand Down Expand Up @@ -159,9 +155,6 @@ export const hack = <T extends typeof http.createServer>(

res.once("close", () => {
timestamps.responseClose = new Date().getTime();

const context = currentContext();

clearDomain();

eventBus.emit(EVENT_LIST.RESPONSE_CLOSE, {
Expand All @@ -170,7 +163,8 @@ export const hack = <T extends typeof http.createServer>(
});

d.run(() => {
const context = currentContext();
process.domain.currentContext = context;

eventBus.emit(EVENT_LIST.REQUEST_START, {
req, context
});
Expand Down