@gravity-ui/gateway 4.11.1 → 4.12.0

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.
@@ -1,10 +1,11 @@
1
- import { ApiActionConfig, ApiByScope, ApiServiceMixedActionConfig, GatewayConfig, GatewayRequest, GatewayResponse, SchemasByScope } from '../models/common';
1
+ import { ApiActionConfig, ApiByScope, ApiServiceMixedActionConfig, GatewayConfig, GatewayRequest, GatewayResponse, SchemasByScope, SendStats } from '../models/common';
2
2
  import { GatewayContext } from '../models/context';
3
3
  import { AppErrorConstructor } from '../models/error';
4
4
  import type { GrpcContext } from './grpc';
5
5
  export declare function createMixedAction<TSchema extends SchemasByScope, Context extends GatewayContext, Req extends GatewayRequest<Context>, Res extends GatewayResponse>(config: ApiServiceMixedActionConfig<Context, Req, Res, any, any, any>, api: ApiByScope<TSchema, Context, Req, Res>, serviceName: string, actionName: string, extra: {
6
6
  config: GatewayConfig<Context, Req, Res>;
7
7
  grpcContext: GrpcContext;
8
+ sendStats?: SendStats<Context>;
8
9
  }, ErrorConstructor: AppErrorConstructor): (actionConfig: ApiActionConfig<Context, any>) => Promise<{
9
10
  responseData: any;
10
11
  debugHeaders: {};
@@ -16,9 +16,23 @@ const constants_1 = require("../constants");
16
16
  const common_1 = require("../utils/common");
17
17
  const create_context_api_1 = require("../utils/create-context-api");
18
18
  const parse_error_1 = require("../utils/parse-error");
19
+ const redact_sensitive_headers_1 = require("../utils/redact-sensitive-headers");
20
+ function getMixedResponseSize(data, ctx, ErrorConstructor) {
21
+ var _a;
22
+ let responseSize = 0;
23
+ try {
24
+ responseSize = constants_1.ECMA_STRING_SIZE * ((_a = JSON.stringify(data)) === null || _a === void 0 ? void 0 : _a.length);
25
+ }
26
+ catch (error) {
27
+ (0, common_1.handleError)(ErrorConstructor, error, ctx, 'Calculate response size failed');
28
+ }
29
+ return responseSize;
30
+ }
19
31
  function createMixedAction(config, api, serviceName, actionName, extra, ErrorConstructor) {
20
32
  return async (actionConfig) => {
33
+ var _a, _b, _c;
21
34
  const { args } = actionConfig, context = __rest(actionConfig, ["args"]);
35
+ const { requestId, headers: requestHeaders, ctx: parentCtx, userId } = actionConfig;
22
36
  const ctx = actionConfig.ctx.create(`Gateway ${serviceName} ${actionName} [mixed]`, {
23
37
  tags: {
24
38
  action: actionName,
@@ -27,15 +41,30 @@ function createMixedAction(config, api, serviceName, actionName, extra, ErrorCon
27
41
  },
28
42
  });
29
43
  const contextApi = (0, create_context_api_1.generateContextApi)(api, Object.assign(Object.assign({}, context), { ctx }));
44
+ const startRequestTime = Date.now();
45
+ const requestData = {
46
+ timestamp: startRequestTime,
47
+ service: serviceName,
48
+ action: actionName,
49
+ requestId: requestId || '',
50
+ requestMethod: '',
51
+ requestUrl: '',
52
+ traceId: ((_a = ctx.getTraceId) === null || _a === void 0 ? void 0 : _a.call(ctx)) || '',
53
+ userId: userId || '',
54
+ };
30
55
  try {
31
56
  const responseData = await config(contextApi, args, Object.assign(Object.assign({ headers: actionConfig.headers, lang: actionConfig.headers[constants_1.DEFAULT_LANG_HEADER] || constants_1.Lang.Ru, ctx }, extra), { abortSignal: actionConfig.abortSignal }));
32
57
  ctx.log('Request completed');
58
+ (_b = extra.sendStats) === null || _b === void 0 ? void 0 : _b.call(extra, Object.assign(Object.assign({}, requestData), { requestTime: Date.now() - startRequestTime, responseSize: getMixedResponseSize(responseData, ctx, ErrorConstructor), restStatus: 200 }), (0, redact_sensitive_headers_1.redactSensitiveHeaders)(parentCtx, requestHeaders), parentCtx, { debugHeaders: {} });
33
59
  return {
34
60
  responseData,
35
61
  debugHeaders: {},
36
62
  };
37
63
  }
38
64
  catch (e) {
65
+ const errorData = e instanceof Object && 'error' in e ? e.error : e;
66
+ const restStatus = (errorData === null || errorData === void 0 ? void 0 : errorData.status) || 500;
67
+ (_c = extra.sendStats) === null || _c === void 0 ? void 0 : _c.call(extra, Object.assign(Object.assign({}, requestData), { requestTime: Date.now() - startRequestTime, responseSize: getMixedResponseSize(errorData, ctx, ErrorConstructor), restStatus }), (0, redact_sensitive_headers_1.redactSensitiveHeaders)(parentCtx, requestHeaders), parentCtx, { debugHeaders: {} });
39
68
  if (e instanceof Object && 'error' in e) {
40
69
  (0, common_1.handleError)(ErrorConstructor, e, ctx, 'Request failed', {
41
70
  actionName,
package/build/index.js CHANGED
@@ -69,7 +69,7 @@ function createApiAction(schema, config, serviceKey, actionName, api, grpcContex
69
69
  const env = config.env || '';
70
70
  if (isMixedActionConfig(action)) {
71
71
  const resultServiceName = serviceName || serviceKey;
72
- return (0, mixed_1.createMixedAction)(action, api, resultServiceName, actionName, { config, grpcContext }, config.ErrorConstructor);
72
+ return (0, mixed_1.createMixedAction)(action, api, resultServiceName, actionName, { config, grpcContext, sendStats: config.sendStats }, config.ErrorConstructor);
73
73
  }
74
74
  const endpointsConfig = lodash_1.default.get(serviceSchema.endpoints, [installation, env]);
75
75
  if (isRestActionConfig(action)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/gateway",
3
- "version": "4.11.1",
3
+ "version": "4.12.0",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "main": "build/index.js",