Skip to content

Commit

Permalink
fix: TraceOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
linyyyang committed Dec 14, 2023
1 parent a187d2b commit 0a55cb0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/catcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Usage:
* @Author: richen
* @Date: 2022-02-21 11:32:03
* @LastEditTime: 2023-11-10 22:16:15
* @LastEditTime: 2023-12-14 21:49:11
*/

import { IOCContainer } from "koatty_container";
Expand Down
24 changes: 15 additions & 9 deletions src/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: richen
* @Date: 2020-11-20 17:37:32
* @LastEditors: Please set LastEditors
* @LastEditTime: 2023-08-21 16:13:02
* @LastEditTime: 2023-12-14 22:28:02
* @License: BSD (3-Clause)
* @Copyright (c) - <richenlin(at)gmail.com>
*/
Expand Down Expand Up @@ -36,6 +36,10 @@ export interface TraceOptions {
RequestIdHeaderName: string;
RequestIdName: string;
IdFactory: any;
Timeout: number;
Encoding: string;
OpenTrace: boolean;
AsyncHooks: boolean;
}

/**
Expand All @@ -45,6 +49,10 @@ const defaultOptions = {
RequestIdHeaderName: 'X-Request-Id',
RequestIdName: "requestId",
IdFactory: uuidv4,
Timeout: 10000,
Encoding: 'utf-8',
OpenTrace: false,
AsyncHooks: false,
};

/**
Expand All @@ -56,14 +64,10 @@ const defaultOptions = {
*/
export function Trace(options: TraceOptions, app: Koatty) {
options = { ...defaultOptions, ...options };
const timeout = (app.config('http_timeout') || 10) * 1000;
const encoding = app.config('encoding') || 'utf-8';
const openTrace = app.config("open_trace") || false;
const asyncHooks = app.config("async_hooks") || false;

//
let tracer: Tracer;
if (openTrace) {
if (options.OpenTrace) {
tracer = app.getMetaData("tracer")[0];
if (!tracer) {
tracer = new Tracer();
Expand All @@ -83,7 +87,9 @@ export function Trace(options: TraceOptions, app: Koatty) {
const respWapper = async (requestId: string, span?: Span) => {
// metadata
ctx.setMetaData(options.RequestIdName, requestId);

const timeout = options.Timeout;
const encoding = options.Encoding;
//
if (ctx.protocol === "grpc") {
// allow bypassing koa
ctx.respond = false;
Expand Down Expand Up @@ -112,7 +118,7 @@ export function Trace(options: TraceOptions, app: Koatty) {
}
requestId = requestId || GetTraceId(options);
let span: Span;
if (openTrace) {
if (options.OpenTrace) {
const serviceName = app.name || "unknownKoattyProject";
const wireCtx = tracer.extract(FORMAT_HTTP_HEADERS, ctx.req.headers);
if (wireCtx != null) {
Expand All @@ -123,7 +129,7 @@ export function Trace(options: TraceOptions, app: Koatty) {
span.addTags({ requestId });
ctx.setMetaData("tracer_span", span);
}
if (asyncHooks) {
if (options.AsyncHooks) {
return asyncLocalStorage.run(requestId, () => {
const asyncResource = createAsyncResource();
wrapEmitter(ctx.req, asyncResource);
Expand Down

0 comments on commit 0a55cb0

Please sign in to comment.