Skip to content

Commit

Permalink
fix: span ubdefined
Browse files Browse the repository at this point in the history
  • Loading branch information
richenlin committed Jul 27, 2023
1 parent e5cfd7a commit 3802e46
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
16 changes: 11 additions & 5 deletions src/handler/grpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Usage:
* @Author: richen
* @Date: 2021-11-19 00:23:06
* @LastEditTime: 2023-07-27 22:59:15
* @LastEditTime: 2023-07-27 23:32:21
*/
import * as Helper from "koatty_lib";
import { KoattyContext } from "koatty_core";
Expand All @@ -30,8 +30,11 @@ export async function gRPCHandler(ctx: KoattyContext, next: Function, ext?: any)
ctx.rpc.call.sendMetadata(ctx.rpc.call.metadata);

const span = <Span>ext.span;
span?.setTag(Tags.HTTP_URL, ctx.originalUrl);
span?.setTag(Tags.HTTP_METHOD, ctx.method);
if (span) {
span.setTag(Tags.HTTP_URL, ctx.originalUrl);
span.setTag(Tags.HTTP_METHOD, ctx.method);
}


// event callback
const finish = () => {
Expand All @@ -41,8 +44,11 @@ export async function gRPCHandler(ctx: KoattyContext, next: Function, ext?: any)
const status = StatusCodeConvert(ctx.status);
const msg = `{"action":"${ctx.protocol}","code":"${status}","startTime":"${startTime}","duration":"${(now - Helper.toInt(startTime)) || 0}","requestId":"${ext.requestId}","endTime":"${now}","path":"${originalPath}"}`;
Logger[(status > 0 ? 'Error' : 'Info')](msg);
span?.log({ "request": msg });
span?.finish();
if (span) {
span.log({ "request": msg });
span.finish();
}

// ctx = null;
};
ctx.res.once("finish", finish);
Expand Down
15 changes: 10 additions & 5 deletions src/handler/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Usage:
* @Author: richen
* @Date: 2021-11-19 00:14:59
* @LastEditTime: 2023-07-27 23:08:45
* @LastEditTime: 2023-07-27 23:30:42
*/
import { Helper } from "koatty_lib";
import { catcher } from "../catcher";
Expand Down Expand Up @@ -35,16 +35,21 @@ export async function httpHandler(ctx: KoattyContext, next: Function, ext?: any)
ctx.set('X-XSS-Protection', '1;mode=block');

const span = <Span>ext.span;
span?.setTag(Tags.HTTP_URL, ctx.originalUrl);
span?.setTag(Tags.HTTP_METHOD, ctx.method);
if (span) {
span.setTag(Tags.HTTP_URL, ctx.originalUrl);
span.setTag(Tags.HTTP_METHOD, ctx.method);
}


// response finish
ctx.res.once('finish', () => {
const now = Date.now();
const msg = `{"action":"${ctx.method}","code":"${ctx.status}","startTime":"${ctx.startTime}","duration":"${(now - ctx.startTime) || 0}","requestId":"${ext.requestId}","endTime":"${now}","path":"${ctx.originalPath || '/'}"}`;
Logger[(ctx.status >= 400 ? 'Error' : 'Info')](msg);
span?.log({ "request": msg });
span?.finish();
if (span) {
span.log({ "request": msg });
span.finish();
}
// ctx = null;
});

Expand Down
15 changes: 10 additions & 5 deletions src/handler/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Usage:
* @Author: richen
* @Date: 2021-11-19 00:24:43
* @LastEditTime: 2023-07-27 23:08:51
* @LastEditTime: 2023-07-27 23:32:51
*/
import { inspect } from "util";
import * as Helper from "koatty_lib";
Expand Down Expand Up @@ -37,16 +37,21 @@ export async function wsHandler(ctx: KoattyContext, next: Function, ext?: any):
ctx.set('X-XSS-Protection', '1;mode=block');

const span = <Span>ext.span;
span?.setTag(Tags.HTTP_URL, ctx.originalUrl);
span?.setTag(Tags.HTTP_METHOD, ctx.method);
if (span) {
span.setTag(Tags.HTTP_URL, ctx.originalUrl);
span.setTag(Tags.HTTP_METHOD, ctx.method);
}


// after send message event
const finish = () => {
const now = Date.now();
const msg = `{"action":"${ctx.protocol}","code":"${ctx.status}","startTime":"${ctx.startTime}","duration":"${(now - ctx.startTime) || 0}","requestId":"${ext.requestId}","endTime":"${now}","path":"${ctx.originalPath || '/'}"}`;
Logger[(ctx.status >= 400 ? 'Error' : 'Info')](msg);
span?.log({ "request": msg });
span?.finish();
if (span) {
span.log({ "request": msg });
span.finish();
}
// ctx = null;
}
ctx.res.once("finish", finish);
Expand Down

0 comments on commit 3802e46

Please sign in to comment.