@dofe/infra-prisma 0.1.1
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.
- package/dist/db-metrics/index.d.ts +2 -0
- package/dist/db-metrics/index.d.ts.map +1 -0
- package/dist/db-metrics/index.js +18 -0
- package/dist/db-metrics/index.js.map +1 -0
- package/dist/db-metrics/src/db-metrics.module.d.ts +22 -0
- package/dist/db-metrics/src/db-metrics.module.d.ts.map +1 -0
- package/dist/db-metrics/src/db-metrics.module.js +140 -0
- package/dist/db-metrics/src/db-metrics.module.js.map +1 -0
- package/dist/db-metrics/src/db-metrics.service.d.ts +183 -0
- package/dist/db-metrics/src/db-metrics.service.d.ts.map +1 -0
- package/dist/db-metrics/src/db-metrics.service.js +334 -0
- package/dist/db-metrics/src/db-metrics.service.js.map +1 -0
- package/dist/db-metrics/src/index.d.ts +6 -0
- package/dist/db-metrics/src/index.d.ts.map +1 -0
- package/dist/db-metrics/src/index.js +24 -0
- package/dist/db-metrics/src/index.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware/soft-delete.middleware.d.ts +51 -0
- package/dist/middleware/soft-delete.middleware.d.ts.map +1 -0
- package/dist/middleware/soft-delete.middleware.js +289 -0
- package/dist/middleware/soft-delete.middleware.js.map +1 -0
- package/dist/middleware/tenant-isolation-extension.d.ts +18 -0
- package/dist/middleware/tenant-isolation-extension.d.ts.map +1 -0
- package/dist/middleware/tenant-isolation-extension.js +124 -0
- package/dist/middleware/tenant-isolation-extension.js.map +1 -0
- package/dist/prisma/index.d.ts +7 -0
- package/dist/prisma/index.d.ts.map +1 -0
- package/dist/prisma/index.js +25 -0
- package/dist/prisma/index.js.map +1 -0
- package/dist/prisma/prisma.module.d.ts +3 -0
- package/dist/prisma/prisma.module.d.ts.map +1 -0
- package/dist/prisma/prisma.module.js +26 -0
- package/dist/prisma/prisma.module.js.map +1 -0
- package/dist/prisma/prisma.service.d.ts +10 -0
- package/dist/prisma/prisma.service.d.ts.map +1 -0
- package/dist/prisma/prisma.service.js +36 -0
- package/dist/prisma/prisma.service.js.map +1 -0
- package/dist/prisma/types.d.ts +4 -0
- package/dist/prisma/types.d.ts.map +1 -0
- package/dist/prisma/types.js +22 -0
- package/dist/prisma/types.js.map +1 -0
- package/dist/prisma-read/prisma-read.module.d.ts +3 -0
- package/dist/prisma-read/prisma-read.module.d.ts.map +1 -0
- package/dist/prisma-read/prisma-read.module.js +24 -0
- package/dist/prisma-read/prisma-read.module.js.map +1 -0
- package/dist/prisma-read/prisma-read.service.d.ts +47 -0
- package/dist/prisma-read/prisma-read.service.d.ts.map +1 -0
- package/dist/prisma-read/prisma-read.service.js +222 -0
- package/dist/prisma-read/prisma-read.service.js.map +1 -0
- package/dist/prisma-write/prisma-write.module.d.ts +3 -0
- package/dist/prisma-write/prisma-write.module.d.ts.map +1 -0
- package/dist/prisma-write/prisma-write.module.js +24 -0
- package/dist/prisma-write/prisma-write.module.js.map +1 -0
- package/dist/prisma-write/prisma-write.service.d.ts +47 -0
- package/dist/prisma-write/prisma-write.service.d.ts.map +1 -0
- package/dist/prisma-write/prisma-write.service.js +222 -0
- package/dist/prisma-write/prisma-write.service.js.map +1 -0
- package/dist/prometheus/index.d.ts +5 -0
- package/dist/prometheus/index.d.ts.map +1 -0
- package/dist/prometheus/index.js +9 -0
- package/dist/prometheus/index.js.map +1 -0
- package/dist/prometheus/prometheus.module.d.ts +3 -0
- package/dist/prometheus/prometheus.module.d.ts.map +1 -0
- package/dist/prometheus/prometheus.module.js +230 -0
- package/dist/prometheus/prometheus.module.js.map +1 -0
- package/package.json +42 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/db-metrics/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
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("./src"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/db-metrics/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wCAAsB"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database Metrics Module
|
|
3
|
+
* 数据库指标模块
|
|
4
|
+
*
|
|
5
|
+
* Provides database query and transaction metrics collection
|
|
6
|
+
* with Prometheus integration and Winston logging.
|
|
7
|
+
*
|
|
8
|
+
* 提供数据库查询和事务指标收集,
|
|
9
|
+
* 集成 Prometheus 和 Winston 日志。
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // Import in AppModule
|
|
14
|
+
* @Module({
|
|
15
|
+
* imports: [DbMetricsModule],
|
|
16
|
+
* })
|
|
17
|
+
* export class AppModule {}
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class DbMetricsModule {
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=db-metrics.module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db-metrics.module.d.ts","sourceRoot":"","sources":["../../../src/db-metrics/src/db-metrics.module.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBA+Ga,eAAe;CAAG"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
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;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.DbMetricsModule = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const nestjs_prometheus_1 = require("@willsoto/nestjs-prometheus");
|
|
12
|
+
const config_1 = require("@nestjs/config");
|
|
13
|
+
const prometheus_1 = require("../../prometheus");
|
|
14
|
+
const db_metrics_service_1 = require("./db-metrics.service");
|
|
15
|
+
/**
|
|
16
|
+
* Database Metrics Module
|
|
17
|
+
* 数据库指标模块
|
|
18
|
+
*
|
|
19
|
+
* Provides database query and transaction metrics collection
|
|
20
|
+
* with Prometheus integration and Winston logging.
|
|
21
|
+
*
|
|
22
|
+
* 提供数据库查询和事务指标收集,
|
|
23
|
+
* 集成 Prometheus 和 Winston 日志。
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* // Import in AppModule
|
|
28
|
+
* @Module({
|
|
29
|
+
* imports: [DbMetricsModule],
|
|
30
|
+
* })
|
|
31
|
+
* export class AppModule {}
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
let DbMetricsModule = class DbMetricsModule {
|
|
35
|
+
};
|
|
36
|
+
exports.DbMetricsModule = DbMetricsModule;
|
|
37
|
+
exports.DbMetricsModule = DbMetricsModule = __decorate([
|
|
38
|
+
(0, common_1.Global)(),
|
|
39
|
+
(0, common_1.Module)({
|
|
40
|
+
imports: [
|
|
41
|
+
config_1.ConfigModule,
|
|
42
|
+
// 导入 PrometheusConfigModule 而不是自己注册 PrometheusModule
|
|
43
|
+
// 这样可以避免重复注册 /metrics 路由
|
|
44
|
+
// PrometheusConfigModule 已经全局注册了 Prometheus 模块和 /metrics 路由
|
|
45
|
+
prometheus_1.PrometheusConfigModule,
|
|
46
|
+
],
|
|
47
|
+
providers: [
|
|
48
|
+
db_metrics_service_1.DbMetricsService,
|
|
49
|
+
// =====================================================================
|
|
50
|
+
// Query Metrics
|
|
51
|
+
// 查询指标
|
|
52
|
+
// =====================================================================
|
|
53
|
+
/**
|
|
54
|
+
* Query duration histogram
|
|
55
|
+
* 查询时长直方图
|
|
56
|
+
*
|
|
57
|
+
* Buckets are chosen to capture typical database query latencies:
|
|
58
|
+
* - 10ms-100ms: Fast queries
|
|
59
|
+
* - 100ms-500ms: Normal queries
|
|
60
|
+
* - 500ms-1s: Slow queries
|
|
61
|
+
* - 1s+: Very slow queries requiring optimization
|
|
62
|
+
*/
|
|
63
|
+
(0, nestjs_prometheus_1.makeHistogramProvider)({
|
|
64
|
+
name: 'prisma_query_duration_seconds',
|
|
65
|
+
help: 'Duration of Prisma queries in seconds',
|
|
66
|
+
labelNames: ['model', 'action', 'db_type', 'status'],
|
|
67
|
+
buckets: [0.01, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10],
|
|
68
|
+
}),
|
|
69
|
+
/**
|
|
70
|
+
* Total query counter
|
|
71
|
+
* 查询总数计数器
|
|
72
|
+
*/
|
|
73
|
+
(0, nestjs_prometheus_1.makeCounterProvider)({
|
|
74
|
+
name: 'prisma_query_total',
|
|
75
|
+
help: 'Total number of Prisma queries',
|
|
76
|
+
labelNames: ['model', 'action', 'db_type', 'status'],
|
|
77
|
+
}),
|
|
78
|
+
/**
|
|
79
|
+
* Slow query counter by threshold level
|
|
80
|
+
* 按阈值级别的慢查询计数器
|
|
81
|
+
*/
|
|
82
|
+
(0, nestjs_prometheus_1.makeCounterProvider)({
|
|
83
|
+
name: 'prisma_slow_query_total',
|
|
84
|
+
help: 'Total number of slow queries by threshold level',
|
|
85
|
+
labelNames: ['model', 'action', 'db_type', 'threshold_level'],
|
|
86
|
+
}),
|
|
87
|
+
// =====================================================================
|
|
88
|
+
// Transaction Metrics
|
|
89
|
+
// 事务指标
|
|
90
|
+
// =====================================================================
|
|
91
|
+
/**
|
|
92
|
+
* Transaction duration histogram
|
|
93
|
+
* 事务时长直方图
|
|
94
|
+
*
|
|
95
|
+
* Buckets are chosen for typical transaction durations:
|
|
96
|
+
* - 100ms-1s: Fast transactions
|
|
97
|
+
* - 1s-10s: Normal transactions
|
|
98
|
+
* - 10s+: Long-running transactions
|
|
99
|
+
*/
|
|
100
|
+
(0, nestjs_prometheus_1.makeHistogramProvider)({
|
|
101
|
+
name: 'prisma_transaction_duration_seconds',
|
|
102
|
+
help: 'Duration of Prisma transactions in seconds',
|
|
103
|
+
labelNames: ['status', 'retry_count', 'isolation_level'],
|
|
104
|
+
buckets: [0.1, 0.5, 1, 2.5, 5, 10, 30, 60],
|
|
105
|
+
}),
|
|
106
|
+
/**
|
|
107
|
+
* Total transaction counter
|
|
108
|
+
* 事务总数计数器
|
|
109
|
+
*/
|
|
110
|
+
(0, nestjs_prometheus_1.makeCounterProvider)({
|
|
111
|
+
name: 'prisma_transaction_total',
|
|
112
|
+
help: 'Total number of transactions',
|
|
113
|
+
labelNames: ['status', 'retry_count', 'isolation_level'],
|
|
114
|
+
}),
|
|
115
|
+
/**
|
|
116
|
+
* Active transactions gauge
|
|
117
|
+
* 活跃事务数仪表
|
|
118
|
+
*/
|
|
119
|
+
(0, nestjs_prometheus_1.makeGaugeProvider)({
|
|
120
|
+
name: 'prisma_active_transactions',
|
|
121
|
+
help: 'Number of currently active transactions',
|
|
122
|
+
}),
|
|
123
|
+
// =====================================================================
|
|
124
|
+
// Connection Pool Metrics
|
|
125
|
+
// 连接池指标
|
|
126
|
+
// =====================================================================
|
|
127
|
+
/**
|
|
128
|
+
* Active connections gauge
|
|
129
|
+
* 活跃连接数仪表
|
|
130
|
+
*/
|
|
131
|
+
(0, nestjs_prometheus_1.makeGaugeProvider)({
|
|
132
|
+
name: 'prisma_pool_connections_active',
|
|
133
|
+
help: 'Number of active connections in the pool',
|
|
134
|
+
labelNames: ['db_type'],
|
|
135
|
+
}),
|
|
136
|
+
],
|
|
137
|
+
exports: [db_metrics_service_1.DbMetricsService],
|
|
138
|
+
})
|
|
139
|
+
], DbMetricsModule);
|
|
140
|
+
//# sourceMappingURL=db-metrics.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db-metrics.module.js","sourceRoot":"","sources":["../../../src/db-metrics/src/db-metrics.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAgD;AAChD,mEAIqC;AACrC,2CAA8C;AAC9C,iDAA0D;AAC1D,6DAAwD;AAExD;;;;;;;;;;;;;;;;;;GAkBG;AAgHI,IAAM,eAAe,GAArB,MAAM,eAAe;CAAG,CAAA;AAAlB,0CAAe;0BAAf,eAAe;IA/G3B,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC;QACN,OAAO,EAAE;YACP,qBAAY;YACZ,qDAAqD;YACrD,yBAAyB;YACzB,4DAA4D;YAC5D,mCAAsB;SACvB;QACD,SAAS,EAAE;YACT,qCAAgB;YAEhB,wEAAwE;YACxE,gBAAgB;YAChB,OAAO;YACP,wEAAwE;YAExE;;;;;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AAA,qCAAiB,EAAC;gBAChB,IAAI,EAAE,4BAA4B;gBAClC,IAAI,EAAE,yCAAyC;aAChD,CAAC;YAEF,wEAAwE;YACxE,0BAA0B;YAC1B,QAAQ;YACR,wEAAwE;YAExE;;;eAGG;YACH,IAAA,qCAAiB,EAAC;gBAChB,IAAI,EAAE,gCAAgC;gBACtC,IAAI,EAAE,0CAA0C;gBAChD,UAAU,EAAE,CAAC,SAAS,CAAC;aACxB,CAAC;SACH;QACD,OAAO,EAAE,CAAC,qCAAgB,CAAC;KAC5B,CAAC;GACW,eAAe,CAAG"}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { ConfigService } from '@nestjs/config';
|
|
3
|
+
import { Logger } from 'winston';
|
|
4
|
+
import { Counter, Histogram, Gauge } from 'prom-client';
|
|
5
|
+
/**
|
|
6
|
+
* Slow query threshold levels
|
|
7
|
+
* 慢查询阈值级别
|
|
8
|
+
*/
|
|
9
|
+
export interface SlowQueryThresholds {
|
|
10
|
+
/**
|
|
11
|
+
* Queries >= info threshold will be logged at INFO level (ms)
|
|
12
|
+
* 达到此阈值的查询将以 INFO 级别记录
|
|
13
|
+
*/
|
|
14
|
+
info: number;
|
|
15
|
+
/**
|
|
16
|
+
* Queries >= warn threshold will be logged at WARN level (ms)
|
|
17
|
+
* 达到此阈值的查询将以 WARN 级别记录
|
|
18
|
+
*/
|
|
19
|
+
warn: number;
|
|
20
|
+
/**
|
|
21
|
+
* Queries >= error threshold will be logged at ERROR level (ms)
|
|
22
|
+
* 达到此阈值的查询将以 ERROR 级别记录
|
|
23
|
+
*/
|
|
24
|
+
error: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Database metrics configuration
|
|
28
|
+
* 数据库指标配置
|
|
29
|
+
*/
|
|
30
|
+
export interface DbMetricsConfig {
|
|
31
|
+
/**
|
|
32
|
+
* Enable or disable metrics collection
|
|
33
|
+
* 启用或禁用指标收集
|
|
34
|
+
*/
|
|
35
|
+
enabled: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Slow query thresholds
|
|
38
|
+
* 慢查询阈值
|
|
39
|
+
*/
|
|
40
|
+
slowQueryThresholds: SlowQueryThresholds;
|
|
41
|
+
/**
|
|
42
|
+
* Whether to log query parameters
|
|
43
|
+
* 是否记录查询参数
|
|
44
|
+
*/
|
|
45
|
+
logQueryParams: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Whether to log query results
|
|
48
|
+
* 是否记录查询结果
|
|
49
|
+
*/
|
|
50
|
+
logQueryResult: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Maximum length of parameters to log
|
|
53
|
+
* 记录参数的最大长度
|
|
54
|
+
*/
|
|
55
|
+
maxParamLogLength: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Query context for tracking
|
|
59
|
+
* 查询跟踪上下文
|
|
60
|
+
*/
|
|
61
|
+
export interface QueryContext {
|
|
62
|
+
startTime: number;
|
|
63
|
+
traceId: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Query parameters for metrics
|
|
67
|
+
* 查询指标参数
|
|
68
|
+
*/
|
|
69
|
+
export interface QueryParams {
|
|
70
|
+
model: string;
|
|
71
|
+
action: string;
|
|
72
|
+
dbType: 'read' | 'write';
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Transaction context for tracking
|
|
76
|
+
* 事务跟踪上下文
|
|
77
|
+
*/
|
|
78
|
+
export interface TransactionContext {
|
|
79
|
+
startTime: number;
|
|
80
|
+
traceId: string;
|
|
81
|
+
sqlCount: number;
|
|
82
|
+
tablesInvolved: Set<string>;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Transaction metadata for logging
|
|
86
|
+
* 事务日志元数据
|
|
87
|
+
*/
|
|
88
|
+
export interface TransactionMetadata {
|
|
89
|
+
retryCount?: number;
|
|
90
|
+
isolationLevel?: string;
|
|
91
|
+
sqlCount?: number;
|
|
92
|
+
tablesInvolved?: string[];
|
|
93
|
+
lockWaitTime?: number;
|
|
94
|
+
errorType?: string;
|
|
95
|
+
errorMessage?: string;
|
|
96
|
+
methodName?: string;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Database Metrics Service
|
|
100
|
+
* 数据库指标服务
|
|
101
|
+
*
|
|
102
|
+
* Provides unified metrics collection and logging for database operations.
|
|
103
|
+
* 为数据库操作提供统一的指标收集和日志记录。
|
|
104
|
+
*/
|
|
105
|
+
export declare class DbMetricsService implements OnModuleInit {
|
|
106
|
+
private readonly configService?;
|
|
107
|
+
private readonly logger?;
|
|
108
|
+
private readonly queryDuration?;
|
|
109
|
+
private readonly queryTotal?;
|
|
110
|
+
private readonly slowQueryTotal?;
|
|
111
|
+
private readonly txDuration?;
|
|
112
|
+
private readonly txTotal?;
|
|
113
|
+
private readonly activeTx?;
|
|
114
|
+
private config;
|
|
115
|
+
constructor(configService?: ConfigService, logger?: Logger, queryDuration?: Histogram<string>, queryTotal?: Counter<string>, slowQueryTotal?: Counter<string>, txDuration?: Histogram<string>, txTotal?: Counter<string>, activeTx?: Gauge<string>);
|
|
116
|
+
onModuleInit(): void;
|
|
117
|
+
/**
|
|
118
|
+
* Get current configuration
|
|
119
|
+
* 获取当前配置
|
|
120
|
+
*/
|
|
121
|
+
getConfig(): DbMetricsConfig;
|
|
122
|
+
/**
|
|
123
|
+
* Check if metrics collection is enabled
|
|
124
|
+
* 检查指标收集是否启用
|
|
125
|
+
*/
|
|
126
|
+
isEnabled(): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Start tracking a query
|
|
129
|
+
* 开始跟踪查询
|
|
130
|
+
*/
|
|
131
|
+
recordQueryStart(): QueryContext;
|
|
132
|
+
/**
|
|
133
|
+
* Record query completion
|
|
134
|
+
* 记录查询完成
|
|
135
|
+
*/
|
|
136
|
+
recordQueryEnd(ctx: QueryContext, params: QueryParams, status: 'success' | 'error', args?: unknown, error?: Error): void;
|
|
137
|
+
/**
|
|
138
|
+
* Determine the threshold level based on query duration
|
|
139
|
+
* 根据查询时长确定阈值级别
|
|
140
|
+
*/
|
|
141
|
+
private getThresholdLevel;
|
|
142
|
+
/**
|
|
143
|
+
* Log query information with appropriate level
|
|
144
|
+
* 以适当的级别记录查询信息
|
|
145
|
+
*/
|
|
146
|
+
private logQuery;
|
|
147
|
+
/**
|
|
148
|
+
* Start tracking a transaction
|
|
149
|
+
* 开始跟踪事务
|
|
150
|
+
*/
|
|
151
|
+
recordTransactionStart(): TransactionContext;
|
|
152
|
+
/**
|
|
153
|
+
* Increment SQL count in transaction context
|
|
154
|
+
* 增加事务上下文中的 SQL 计数
|
|
155
|
+
*/
|
|
156
|
+
incrementSqlCount(ctx: TransactionContext, model?: string): void;
|
|
157
|
+
/**
|
|
158
|
+
* Record transaction completion
|
|
159
|
+
* 记录事务完成
|
|
160
|
+
*/
|
|
161
|
+
recordTransactionEnd(ctx: TransactionContext, status: 'success' | 'error' | 'rollback', metadata?: TransactionMetadata): void;
|
|
162
|
+
/**
|
|
163
|
+
* Log transaction information
|
|
164
|
+
* 记录事务信息
|
|
165
|
+
*/
|
|
166
|
+
private logTransaction;
|
|
167
|
+
/**
|
|
168
|
+
* Get current trace ID from CLS namespace
|
|
169
|
+
* 从 CLS 命名空间获取当前 traceId
|
|
170
|
+
*/
|
|
171
|
+
private getTraceId;
|
|
172
|
+
/**
|
|
173
|
+
* Create a fallback context when metrics service is not available
|
|
174
|
+
* 当指标服务不可用时创建回退上下文
|
|
175
|
+
*/
|
|
176
|
+
static createFallbackQueryContext(): QueryContext;
|
|
177
|
+
/**
|
|
178
|
+
* Create a fallback transaction context
|
|
179
|
+
* 创建回退事务上下文
|
|
180
|
+
*/
|
|
181
|
+
static createFallbackTransactionContext(): TransactionContext;
|
|
182
|
+
}
|
|
183
|
+
//# sourceMappingURL=db-metrics.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db-metrics.service.d.ts","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