@yolo-croket-dev/core 1.9.7-slowquery.0 → 1.9.7-slowquery.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
|
|
2
|
+
import { BaseRpcExceptionFilter } from '@nestjs/microservices';
|
|
3
|
+
import { Observable } from 'rxjs';
|
|
4
|
+
export declare class GrpcExceptionFilter extends BaseRpcExceptionFilter implements ExceptionFilter {
|
|
5
|
+
private readonly CROKET_EXCEPTION_NAMES;
|
|
6
|
+
catch(exception: unknown, host: ArgumentsHost): Observable<unknown>;
|
|
7
|
+
/**
|
|
8
|
+
* HTTP Status 코드를 gRPC Status 코드로 변환
|
|
9
|
+
*/
|
|
10
|
+
private convertHttpStatusToGrpcStatus;
|
|
11
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
25
|
+
if (mod && mod.__esModule) return mod;
|
|
26
|
+
var result = {};
|
|
27
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
28
|
+
__setModuleDefault(result, mod);
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
+
exports.GrpcExceptionFilter = void 0;
|
|
33
|
+
const common_1 = require("@nestjs/common");
|
|
34
|
+
const microservices_1 = require("@nestjs/microservices");
|
|
35
|
+
const grpc_js_1 = require("@grpc/grpc-js");
|
|
36
|
+
const CroketExceptions = __importStar(require("@yolo-croket-dev/core/exceptions"));
|
|
37
|
+
/**
|
|
38
|
+
* CroketExceptions에서 export된 모든 클래스의 constructor 배열 생성
|
|
39
|
+
*/
|
|
40
|
+
const getCroketExceptionClasses = () => Object.values(CroketExceptions).filter((value) => typeof value === 'function' && value.prototype && value.prototype.constructor === value);
|
|
41
|
+
const croketExceptionClasses = getCroketExceptionClasses();
|
|
42
|
+
let GrpcExceptionFilter = class GrpcExceptionFilter extends microservices_1.BaseRpcExceptionFilter {
|
|
43
|
+
constructor() {
|
|
44
|
+
super(...arguments);
|
|
45
|
+
this.CROKET_EXCEPTION_NAMES = croketExceptionClasses.map(d => d.prototype.constructor.name);
|
|
46
|
+
}
|
|
47
|
+
catch(exception, host) {
|
|
48
|
+
var _a;
|
|
49
|
+
const metadata = new grpc_js_1.Metadata();
|
|
50
|
+
let grpcStatus;
|
|
51
|
+
let errorMessage;
|
|
52
|
+
const className = ((_a = exception === null || exception === void 0 ? void 0 : exception.constructor) === null || _a === void 0 ? void 0 : _a.name) || 'Error';
|
|
53
|
+
if (this.CROKET_EXCEPTION_NAMES.includes(className)) {
|
|
54
|
+
const msaException = exception;
|
|
55
|
+
const httpStatus = exception.getStatus();
|
|
56
|
+
const errInfo = {
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
58
|
+
errType: exception.errType,
|
|
59
|
+
type: className || exception.constructor.name,
|
|
60
|
+
message: msaException.errMessage || exception.message,
|
|
61
|
+
status: httpStatus,
|
|
62
|
+
action: msaException.errAction,
|
|
63
|
+
};
|
|
64
|
+
metadata.set('error', Buffer.from(JSON.stringify(errInfo)).toString('base64'));
|
|
65
|
+
metadata.set('status', httpStatus.toString());
|
|
66
|
+
grpcStatus = this.convertHttpStatusToGrpcStatus(httpStatus);
|
|
67
|
+
errorMessage = msaException.errMessage || exception.message;
|
|
68
|
+
}
|
|
69
|
+
else if (exception instanceof microservices_1.RpcException) {
|
|
70
|
+
const rpcError = exception.getError();
|
|
71
|
+
grpcStatus = rpcError.code || grpc_js_1.status.INTERNAL;
|
|
72
|
+
errorMessage = rpcError.message || 'Internal Server Error';
|
|
73
|
+
if (rpcError.metadata) {
|
|
74
|
+
const errorMetadata = rpcError.metadata;
|
|
75
|
+
const keys = errorMetadata.getMap();
|
|
76
|
+
Object.keys(keys).forEach((key) => {
|
|
77
|
+
const value = errorMetadata.get(key);
|
|
78
|
+
if (Array.isArray(value)) {
|
|
79
|
+
value.forEach((v) => metadata.add(key, v));
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
metadata.set(key, value);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
errorMessage = (exception === null || exception === void 0 ? void 0 : exception.message) || 'Internal Server Error';
|
|
89
|
+
grpcStatus = grpc_js_1.status.INTERNAL;
|
|
90
|
+
}
|
|
91
|
+
// BaseRpcExceptionFilter의 catch 메서드를 사용하여 metadata를 포함한 RpcException 생성
|
|
92
|
+
const rpcException = new microservices_1.RpcException({
|
|
93
|
+
code: grpcStatus,
|
|
94
|
+
message: errorMessage,
|
|
95
|
+
metadata,
|
|
96
|
+
});
|
|
97
|
+
return super.catch(rpcException, host);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* HTTP Status 코드를 gRPC Status 코드로 변환
|
|
101
|
+
*/
|
|
102
|
+
convertHttpStatusToGrpcStatus(httpStatus) {
|
|
103
|
+
switch (httpStatus) {
|
|
104
|
+
case 200:
|
|
105
|
+
return grpc_js_1.status.OK;
|
|
106
|
+
case 400:
|
|
107
|
+
return grpc_js_1.status.INVALID_ARGUMENT;
|
|
108
|
+
case 401:
|
|
109
|
+
return grpc_js_1.status.UNAUTHENTICATED;
|
|
110
|
+
case 403:
|
|
111
|
+
return grpc_js_1.status.PERMISSION_DENIED;
|
|
112
|
+
case 404:
|
|
113
|
+
return grpc_js_1.status.NOT_FOUND;
|
|
114
|
+
case 409:
|
|
115
|
+
return grpc_js_1.status.ALREADY_EXISTS;
|
|
116
|
+
case 412:
|
|
117
|
+
return grpc_js_1.status.FAILED_PRECONDITION;
|
|
118
|
+
case 429:
|
|
119
|
+
return grpc_js_1.status.RESOURCE_EXHAUSTED;
|
|
120
|
+
case 500:
|
|
121
|
+
return grpc_js_1.status.INTERNAL;
|
|
122
|
+
case 501:
|
|
123
|
+
return grpc_js_1.status.UNIMPLEMENTED;
|
|
124
|
+
case 503:
|
|
125
|
+
return grpc_js_1.status.UNAVAILABLE;
|
|
126
|
+
case 504:
|
|
127
|
+
return grpc_js_1.status.DEADLINE_EXCEEDED;
|
|
128
|
+
default:
|
|
129
|
+
return grpc_js_1.status.INTERNAL;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
exports.GrpcExceptionFilter = GrpcExceptionFilter;
|
|
134
|
+
exports.GrpcExceptionFilter = GrpcExceptionFilter = __decorate([
|
|
135
|
+
(0, common_1.Catch)()
|
|
136
|
+
], GrpcExceptionFilter);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './grpc-exception.filter';
|
package/filters/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./grpc-exception.filter"), exports);
|
|
@@ -41,6 +41,12 @@ const operators_1 = require("rxjs/operators");
|
|
|
41
41
|
const dd_trace_1 = __importDefault(require("dd-trace"));
|
|
42
42
|
const { SERVER_NAME } = process.env;
|
|
43
43
|
const msaLogger = new msa_logger_1.default(msa_logger_1.ServerTypeEnum.SERVICE);
|
|
44
|
+
// interface IContent {
|
|
45
|
+
// pattern: string;
|
|
46
|
+
// data: IRequest;
|
|
47
|
+
// /** 무슨 아이디인지 모르겠음 */
|
|
48
|
+
// id: string;
|
|
49
|
+
// }
|
|
44
50
|
const MAX_CUT_LENGTH = 500;
|
|
45
51
|
dd_trace_1.default.init();
|
|
46
52
|
let LoggingServiceInterceptor = class LoggingServiceInterceptor {
|
|
@@ -77,35 +83,65 @@ let LoggingServiceInterceptor = class LoggingServiceInterceptor {
|
|
|
77
83
|
intercept(context, next) {
|
|
78
84
|
const now = Date.now();
|
|
79
85
|
const args = context.getArgs();
|
|
80
|
-
const
|
|
81
|
-
const
|
|
86
|
+
const rpcContext = context.switchToRpc();
|
|
87
|
+
const rmqContext = rpcContext.getContext();
|
|
88
|
+
const data = rpcContext.getData();
|
|
82
89
|
const metadata = args[1];
|
|
83
|
-
const
|
|
84
|
-
const isGrpc = contextType === 'rpc';
|
|
90
|
+
const isGrpc = !rmqContext.getMessage;
|
|
85
91
|
let param;
|
|
86
92
|
let pastFrom;
|
|
87
93
|
let pastFroms;
|
|
94
|
+
let originalMessage;
|
|
95
|
+
let pattern; /** @xxx: Grpc: path , Mq: 메시지 패턴명 */
|
|
96
|
+
let payload;
|
|
97
|
+
let pastFromsForMetadata;
|
|
98
|
+
let pastFromsForMessage;
|
|
88
99
|
if (isGrpc) {
|
|
89
|
-
param = metadata.get('param') || {
|
|
90
|
-
|
|
91
|
-
|
|
100
|
+
param = metadata.get('param') || {
|
|
101
|
+
param: {
|
|
102
|
+
from: '-',
|
|
103
|
+
froms: '-',
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
pastFrom = metadata.get('from') || '-';
|
|
107
|
+
pastFromsForMetadata = metadata.get('froms') || [pastFrom];
|
|
108
|
+
originalMessage = args[0];
|
|
109
|
+
const gRPCArg = args[2];
|
|
110
|
+
pattern = gRPCArg.path;
|
|
111
|
+
payload = data;
|
|
92
112
|
}
|
|
93
113
|
else {
|
|
94
114
|
param = data.param;
|
|
95
115
|
pastFrom = param.from ? JSON.parse(JSON.stringify(param.from)) : '-';
|
|
96
|
-
|
|
116
|
+
pastFromsForMessage = param.froms ? JSON.parse(JSON.stringify(param.froms)) : [pastFrom];
|
|
117
|
+
originalMessage = rmqContext.getMessage();
|
|
118
|
+
pattern = originalMessage.messagePattern;
|
|
119
|
+
// const originalcontent = originalMessage.content;
|
|
120
|
+
// const content = JSON.parse(originalcontent.toString()) as IContent;
|
|
121
|
+
payload = context.switchToHttp().getRequest().data;
|
|
122
|
+
}
|
|
123
|
+
// @TODO: 삭제
|
|
124
|
+
if (pastFromsForMetadata) {
|
|
125
|
+
pastFroms = [
|
|
126
|
+
...(pastFromsForMessage || []),
|
|
127
|
+
...(pastFromsForMetadata || []),
|
|
128
|
+
];
|
|
129
|
+
}
|
|
130
|
+
else if (pastFromsForMessage) {
|
|
131
|
+
pastFroms = [
|
|
132
|
+
...(pastFromsForMetadata || []),
|
|
133
|
+
...(pastFromsForMessage || []),
|
|
134
|
+
];
|
|
97
135
|
}
|
|
98
136
|
// const pastFrom = data.param.from ? JSON.parse(JSON.stringify(data.param.from)) : '-';
|
|
99
137
|
// const pastFroms = data.param.froms ? JSON.parse(JSON.stringify(data.param.froms)) : [pastFrom];
|
|
100
|
-
const originalMessage = rmqContext.getMessage();
|
|
101
|
-
const ctx = context.switchToHttp();
|
|
102
|
-
const request = ctx.getRequest();
|
|
103
|
-
const pastParam = JSON.parse(JSON.stringify(
|
|
104
|
-
const
|
|
105
|
-
const content = JSON.parse(originalcontent.toString());
|
|
106
|
-
const traceId = content.data.param ? content.data.param.traceId : '-';
|
|
138
|
+
// const originalMessage = rmqContext.getMessage();
|
|
139
|
+
// const ctx = context.switchToHttp();
|
|
140
|
+
// const request = ctx.getRequest() as IRequest;
|
|
141
|
+
const pastParam = JSON.parse(JSON.stringify(param));
|
|
142
|
+
const traceId = param ? param.traceId : '-';
|
|
107
143
|
/** from 변경 */
|
|
108
|
-
const tmpFrom = `${SERVER_NAME}: ${
|
|
144
|
+
const tmpFrom = `${SERVER_NAME}: ${pattern}`;
|
|
109
145
|
const tmpFroms = JSON.parse(JSON.stringify(pastFroms));
|
|
110
146
|
tmpFroms.push(tmpFrom);
|
|
111
147
|
if (isGrpc) {
|
|
@@ -119,8 +155,8 @@ let LoggingServiceInterceptor = class LoggingServiceInterceptor {
|
|
|
119
155
|
msaLogger.printInputInterceptorService({
|
|
120
156
|
traceId,
|
|
121
157
|
messageBrokerInfo: {
|
|
122
|
-
messagePatten:
|
|
123
|
-
payload: JSON.stringify(this.keyCutter(JSON.stringify(
|
|
158
|
+
messagePatten: pattern,
|
|
159
|
+
payload: JSON.stringify(this.keyCutter(JSON.stringify(payload))),
|
|
124
160
|
},
|
|
125
161
|
paramInfo: pastParam,
|
|
126
162
|
traceInfo: {
|
|
@@ -132,11 +168,11 @@ let LoggingServiceInterceptor = class LoggingServiceInterceptor {
|
|
|
132
168
|
const currentSpan = dd_trace_1.default.scope().active();
|
|
133
169
|
let loggingSpan = null;
|
|
134
170
|
if (currentSpan) {
|
|
135
|
-
loggingSpan = dd_trace_1.default.startSpan(`logging-service-interceptor-${
|
|
171
|
+
loggingSpan = dd_trace_1.default.startSpan(`logging-service-interceptor-${pattern}`, { childOf: currentSpan.context() });
|
|
136
172
|
}
|
|
137
173
|
if (loggingSpan) {
|
|
138
174
|
loggingSpan.setTag('traceId', traceId);
|
|
139
|
-
loggingSpan.setTag('messagePattern',
|
|
175
|
+
loggingSpan.setTag('messagePattern', pattern);
|
|
140
176
|
}
|
|
141
177
|
return next.handle()
|
|
142
178
|
.pipe((0, operators_1.tap)(() => {
|
|
@@ -148,8 +184,8 @@ let LoggingServiceInterceptor = class LoggingServiceInterceptor {
|
|
|
148
184
|
ms: elapsed,
|
|
149
185
|
},
|
|
150
186
|
messageBrokerInfo: {
|
|
151
|
-
messagePatten:
|
|
152
|
-
payload: JSON.stringify(this.keyCutter(JSON.stringify(
|
|
187
|
+
messagePatten: pattern,
|
|
188
|
+
payload: JSON.stringify(this.keyCutter(JSON.stringify(payload))),
|
|
153
189
|
},
|
|
154
190
|
traceInfo: {
|
|
155
191
|
from: pastFrom,
|
|
@@ -178,8 +214,8 @@ let LoggingServiceInterceptor = class LoggingServiceInterceptor {
|
|
|
178
214
|
errOption: error.errOption,
|
|
179
215
|
},
|
|
180
216
|
messageBrokerInfo: {
|
|
181
|
-
messagePatten:
|
|
182
|
-
payload: JSON.stringify(this.keyCutter(JSON.stringify(
|
|
217
|
+
messagePatten: pattern,
|
|
218
|
+
payload: JSON.stringify(this.keyCutter(JSON.stringify(payload))),
|
|
183
219
|
},
|
|
184
220
|
traceInfo: {
|
|
185
221
|
from: pastFrom,
|
|
@@ -194,8 +230,8 @@ let LoggingServiceInterceptor = class LoggingServiceInterceptor {
|
|
|
194
230
|
ms: elapsed,
|
|
195
231
|
},
|
|
196
232
|
messageBrokerInfo: {
|
|
197
|
-
messagePatten:
|
|
198
|
-
payload: JSON.stringify(this.keyCutter(JSON.stringify(
|
|
233
|
+
messagePatten: pattern,
|
|
234
|
+
payload: JSON.stringify(this.keyCutter(JSON.stringify(payload))),
|
|
199
235
|
},
|
|
200
236
|
traceInfo: {
|
|
201
237
|
from: pastFrom,
|