@ryanzeng/nest-observe 0.1.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.
- package/LICENSE +21 -0
- package/README.md +177 -0
- package/dist/index.d.mts +197 -0
- package/dist/index.d.ts +197 -0
- package/dist/index.js +1121 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1073 -0
- package/dist/index.mjs.map +1 -0
- package/dist/register.d.mts +2 -0
- package/dist/register.d.ts +2 -0
- package/dist/register.js +891 -0
- package/dist/register.js.map +1 -0
- package/dist/register.mjs +892 -0
- package/dist/register.mjs.map +1 -0
- package/package.json +96 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ryan Zeng
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# @ryanzeng/nest-observe
|
|
2
|
+
|
|
3
|
+
面向 NestJS 的零配置、Vendor Neutral Observability SDK。基于 OpenTelemetry,通过标准 OTLP 同时发送 Traces、Metrics 和 Logs,可连接 OpenObserve、Grafana、Jaeger、Datadog、Elastic 等兼容后端。
|
|
4
|
+
|
|
5
|
+
## 快速开始
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @ryanzeng/nest-observe
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
把注册入口放在应用入口的第一条 import(必须早于 `@nestjs/core` 和业务模块):
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import '@ryanzeng/nest-observe/register';
|
|
15
|
+
import { NestFactory } from '@nestjs/core';
|
|
16
|
+
import { AppModule } from './app.module';
|
|
17
|
+
|
|
18
|
+
async function bootstrap() {
|
|
19
|
+
const app = await NestFactory.create(AppModule);
|
|
20
|
+
await app.listen(3000);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
void bootstrap();
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
设置标准 OpenTelemetry 环境变量:
|
|
27
|
+
|
|
28
|
+
```env
|
|
29
|
+
OTEL_SERVICE_NAME=mall-app
|
|
30
|
+
OTEL_SERVICE_VERSION=1.0.0
|
|
31
|
+
OTEL_EXPORTER_OTLP_ENDPOINT=https://observe.example.com
|
|
32
|
+
OTEL_EXPORTER_OTLP_HEADERS=Authorization=Basic%20xxx
|
|
33
|
+
OTEL_RESOURCE_ATTRIBUTES=git.commit.sha=abc123
|
|
34
|
+
OBSERVE_ENVIRONMENT=production
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
通用 endpoint 会自动派生 `/v1/traces`、`/v1/metrics` 和 `/v1/logs`。也可使用标准的 `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`、`OTEL_EXPORTER_OTLP_METRICS_ENDPOINT`、`OTEL_EXPORTER_OTLP_LOGS_ENDPOINT` 分别覆盖。
|
|
38
|
+
|
|
39
|
+
初始化或 exporter 配置失败时,SDK 会降级为 no-op,不会阻止业务应用启动。
|
|
40
|
+
|
|
41
|
+
## Nest Module
|
|
42
|
+
|
|
43
|
+
`register` 已能在加载阶段自动挂载 HTTP、Nest Controller、Provider、Prisma 和 Logger instrumentation。也可以导入全局 Module;它为较晚初始化或测试场景提供 Discovery fallback,并在 Nest 关闭时 flush/shutdown:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { Module } from '@nestjs/common';
|
|
47
|
+
import { ObserveModule } from '@ryanzeng/nest-observe';
|
|
48
|
+
|
|
49
|
+
@Module({
|
|
50
|
+
imports: [ObserveModule.forRoot()],
|
|
51
|
+
})
|
|
52
|
+
export class AppModule {}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## 装饰器
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { Injectable } from '@nestjs/common';
|
|
59
|
+
import { IgnoreTrace, Trace } from '@ryanzeng/nest-observe';
|
|
60
|
+
|
|
61
|
+
@Injectable()
|
|
62
|
+
export class OrderService {
|
|
63
|
+
@Trace('order.create')
|
|
64
|
+
async createOrder() {
|
|
65
|
+
return this.reserveInventory();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@Trace()
|
|
69
|
+
private async reserveInventory() {
|
|
70
|
+
// 自动成为 order.create 的 child span
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
@IgnoreTrace()
|
|
74
|
+
healthCheck() {
|
|
75
|
+
return 'ok';
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
`@Trace()` 支持同步函数、Promise 和 RxJS Observable,自动记录耗时、异常、ERROR status,并继承当前 context。`@Trace()` 也可应用在 class 上。
|
|
81
|
+
|
|
82
|
+
## 自动采集内容
|
|
83
|
+
|
|
84
|
+
Traces:
|
|
85
|
+
|
|
86
|
+
- Node HTTP client/server、Nest Controller/handler、Provider method
|
|
87
|
+
- Prisma instrumentation
|
|
88
|
+
- `@Trace()` 自定义 span 和自然 parent/child context
|
|
89
|
+
- Controller/Provider 异常事件、错误状态及 stack trace
|
|
90
|
+
|
|
91
|
+
Logs:
|
|
92
|
+
|
|
93
|
+
- 默认 Nest `Logger` 的 `verbose/debug/log/warn/error/fatal`
|
|
94
|
+
- OTLP severity、`nestjs.context`、`trace_id`、`span_id`
|
|
95
|
+
- `service.name`、`service.version`、`deployment.environment.name`
|
|
96
|
+
- `StructuredLogEmitter` 抽象可用于后续 Pino/Winston adapter
|
|
97
|
+
|
|
98
|
+
Metrics:
|
|
99
|
+
|
|
100
|
+
- `system.cpu.utilization`、`process.cpu.time`
|
|
101
|
+
- `process.memory.rss`、`nodejs.memory.heap.used`、`nodejs.memory.heap.total`
|
|
102
|
+
- `nodejs.eventloop.delay`、`nodejs.eventloop.utilization`
|
|
103
|
+
- `nodejs.gc.duration`、`process.uptime`
|
|
104
|
+
- `http.server.request.count`、`http.server.request.duration`、`http.server.error.count`
|
|
105
|
+
- `nestjs.method.calls`、`nestjs.method.duration`、`nestjs.method.errors`
|
|
106
|
+
|
|
107
|
+
HTTP 与 Nest method 指标只使用 route template、method、status、controller/provider/method 等有限维度,避免把 URL id 或请求数据变成高基数标签。
|
|
108
|
+
|
|
109
|
+
## 程序化配置
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
import { observe } from '@ryanzeng/nest-observe';
|
|
113
|
+
|
|
114
|
+
const telemetry = observe({
|
|
115
|
+
serviceName: 'mall-app',
|
|
116
|
+
serviceVersion: '1.0.0',
|
|
117
|
+
environment: 'production',
|
|
118
|
+
traces: true,
|
|
119
|
+
logs: true,
|
|
120
|
+
metrics: true,
|
|
121
|
+
providerTracing: true,
|
|
122
|
+
controllerTracing: true,
|
|
123
|
+
sampling: 0.1,
|
|
124
|
+
allowedHeaders: ['x-correlation-id'],
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
await telemetry.forceFlush();
|
|
128
|
+
await telemetry.shutdown();
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
环境变量方式仍是推荐方式。有合理默认值的配置无需填写。
|
|
132
|
+
|
|
133
|
+
### Sampling
|
|
134
|
+
|
|
135
|
+
支持显式 `sampling`(0–1)以及标准环境变量:
|
|
136
|
+
|
|
137
|
+
```env
|
|
138
|
+
OTEL_TRACES_SAMPLER=parentbased_traceidratio
|
|
139
|
+
OTEL_TRACES_SAMPLER_ARG=0.1
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
也支持 `always_on`、`always_off`、`parentbased_always_on` 和 `parentbased_always_off`。HTTP/runtime/method metrics 不依赖 trace sampling。
|
|
143
|
+
|
|
144
|
+
## 安全与隐私
|
|
145
|
+
|
|
146
|
+
- 不采集 request body、完整 Redis value 或数据库参数。
|
|
147
|
+
- HTTP header 只按白名单采集;默认仅包含 `accept`、`content-type`、`user-agent`、`x-request-id`、`traceparent`。
|
|
148
|
+
- `Authorization`、Cookie、password、secret、token、API key 和手机号字段始终脱敏,即使误加入 header allowlist。
|
|
149
|
+
- 常见签名、token、password 等 URL query 参数由 HTTP instrumentation 脱敏。
|
|
150
|
+
- Batch processor 异步发送,带有有限队列、batch size、export timeout 和 metric cardinality limit。
|
|
151
|
+
|
|
152
|
+
## 资源属性
|
|
153
|
+
|
|
154
|
+
三类信号统一带有:
|
|
155
|
+
|
|
156
|
+
```text
|
|
157
|
+
service.name
|
|
158
|
+
service.version
|
|
159
|
+
deployment.environment.name
|
|
160
|
+
service.instance.id
|
|
161
|
+
telemetry.sdk.name
|
|
162
|
+
telemetry.sdk.version
|
|
163
|
+
host.name
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
`OTEL_RESOURCE_ATTRIBUTES` 可补充 `git.commit.sha`、`container.id` 等自定义资源信息。
|
|
167
|
+
|
|
168
|
+
## 开发
|
|
169
|
+
|
|
170
|
+
项目使用 TDD。完整检查:
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
pnpm install
|
|
174
|
+
pnpm check
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
最低运行环境为 Node.js 20,支持 NestJS 10、11 和 12。
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { LogRecordExporter, LoggerProvider } from '@opentelemetry/sdk-logs';
|
|
2
|
+
import { MetricReader, MeterProvider } from '@opentelemetry/sdk-metrics';
|
|
3
|
+
import { SpanExporter, SpanProcessor, ReadableSpan } from '@opentelemetry/sdk-trace-base';
|
|
4
|
+
import { Attributes, Meter, Tracer, Span, Context } from '@opentelemetry/api';
|
|
5
|
+
import { Logger } from '@opentelemetry/api-logs';
|
|
6
|
+
import { InstrumentationConfig, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation';
|
|
7
|
+
import { NestInstrumentation } from '@opentelemetry/instrumentation-nestjs-core';
|
|
8
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
9
|
+
import { DynamicModule } from '@nestjs/common';
|
|
10
|
+
import { Resource } from '@opentelemetry/resources';
|
|
11
|
+
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
|
|
12
|
+
|
|
13
|
+
interface ObserveExporters {
|
|
14
|
+
/** Primarily useful for custom backends and tests. OTLP is used by default. */
|
|
15
|
+
span?: SpanExporter;
|
|
16
|
+
log?: LogRecordExporter;
|
|
17
|
+
metricReader?: MetricReader;
|
|
18
|
+
}
|
|
19
|
+
interface ObserveOptions {
|
|
20
|
+
enabled?: boolean;
|
|
21
|
+
serviceName?: string;
|
|
22
|
+
serviceVersion?: string;
|
|
23
|
+
environment?: string;
|
|
24
|
+
instanceId?: string;
|
|
25
|
+
endpoint?: string;
|
|
26
|
+
headers?: Record<string, string>;
|
|
27
|
+
traces?: boolean;
|
|
28
|
+
logs?: boolean;
|
|
29
|
+
metrics?: boolean;
|
|
30
|
+
providerTracing?: boolean;
|
|
31
|
+
controllerTracing?: boolean;
|
|
32
|
+
sampling?: number;
|
|
33
|
+
allowedHeaders?: string[];
|
|
34
|
+
exportTimeoutMillis?: number;
|
|
35
|
+
metricExportIntervalMillis?: number;
|
|
36
|
+
resourceAttributes?: Record<string, string | number | boolean>;
|
|
37
|
+
exporters?: ObserveExporters;
|
|
38
|
+
}
|
|
39
|
+
interface ResolvedObserveConfig {
|
|
40
|
+
enabled: boolean;
|
|
41
|
+
serviceName: string;
|
|
42
|
+
serviceVersion: string;
|
|
43
|
+
environment: string;
|
|
44
|
+
instanceId: string;
|
|
45
|
+
endpoints: {
|
|
46
|
+
traces: string | undefined;
|
|
47
|
+
metrics: string | undefined;
|
|
48
|
+
logs: string | undefined;
|
|
49
|
+
};
|
|
50
|
+
headers: Record<string, string>;
|
|
51
|
+
traces: boolean;
|
|
52
|
+
logs: boolean;
|
|
53
|
+
metrics: boolean;
|
|
54
|
+
providerTracing: boolean;
|
|
55
|
+
controllerTracing: boolean;
|
|
56
|
+
sampling: number;
|
|
57
|
+
allowedHeaders: string[];
|
|
58
|
+
exportTimeoutMillis: number;
|
|
59
|
+
metricExportIntervalMillis: number;
|
|
60
|
+
resourceAttributes: Record<string, string | number | boolean>;
|
|
61
|
+
exporters?: ObserveExporters;
|
|
62
|
+
}
|
|
63
|
+
interface ObserveHandle {
|
|
64
|
+
readonly started: boolean;
|
|
65
|
+
readonly config: Readonly<ResolvedObserveConfig>;
|
|
66
|
+
forceFlush(): Promise<void>;
|
|
67
|
+
shutdown(): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
type Environment = Record<string, string | undefined>;
|
|
71
|
+
declare function parseKeyValueList(value?: string): Record<string, string>;
|
|
72
|
+
declare function resolveObserveConfig(options?: ObserveOptions, env?: Environment): ResolvedObserveConfig;
|
|
73
|
+
|
|
74
|
+
type IgnoreDecorator = MethodDecorator & ClassDecorator;
|
|
75
|
+
declare function IgnoreTrace(): IgnoreDecorator;
|
|
76
|
+
|
|
77
|
+
declare function markTraceIgnored(target: object): void;
|
|
78
|
+
declare function markTraceDecorated(target: object): void;
|
|
79
|
+
declare function isTraceIgnored(method?: object, type?: object): boolean;
|
|
80
|
+
declare function isTraceDecorated(method?: object): boolean;
|
|
81
|
+
|
|
82
|
+
interface TraceOptions {
|
|
83
|
+
name?: string;
|
|
84
|
+
attributes?: Record<string, string>;
|
|
85
|
+
}
|
|
86
|
+
type TraceDecorator = MethodDecorator & ClassDecorator;
|
|
87
|
+
declare function Trace(nameOrOptions?: string | TraceOptions): TraceDecorator;
|
|
88
|
+
|
|
89
|
+
interface StructuredLogRecord {
|
|
90
|
+
body: unknown;
|
|
91
|
+
severityText: 'TRACE' | 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'FATAL';
|
|
92
|
+
attributes: Attributes;
|
|
93
|
+
timestamp?: number;
|
|
94
|
+
}
|
|
95
|
+
interface StructuredLogEmitter {
|
|
96
|
+
emit(record: StructuredLogRecord): void;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
declare class NestLoggerInstrumentation {
|
|
100
|
+
private readonly emitter;
|
|
101
|
+
private readonly resourceAttributes;
|
|
102
|
+
private originals;
|
|
103
|
+
private enabled;
|
|
104
|
+
constructor(emitter: StructuredLogEmitter, resourceAttributes?: Record<string, string>);
|
|
105
|
+
enable(): void;
|
|
106
|
+
disable(): void;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
declare class OpenTelemetryLogEmitter implements StructuredLogEmitter {
|
|
110
|
+
private readonly logger;
|
|
111
|
+
constructor(name?: string, version?: string, logger?: Logger);
|
|
112
|
+
emit(record: StructuredLogRecord): void;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
interface CompatibleNestInstrumentationConfig extends InstrumentationConfig {
|
|
116
|
+
providerTracing?: boolean;
|
|
117
|
+
controllerTracing?: boolean;
|
|
118
|
+
}
|
|
119
|
+
/** Extends upstream Nest support to v12 and instruments DI-created providers. */
|
|
120
|
+
declare class CompatibleNestInstrumentation extends NestInstrumentation {
|
|
121
|
+
private readonly observeConfig;
|
|
122
|
+
private methodInstrumenter?;
|
|
123
|
+
constructor(observeConfig?: CompatibleNestInstrumentationConfig);
|
|
124
|
+
init(): InstrumentationNodeModuleDefinition;
|
|
125
|
+
private getMethodInstrumenter;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
declare class HttpRequestMetrics {
|
|
129
|
+
private readonly serviceName;
|
|
130
|
+
private readonly requestCount;
|
|
131
|
+
private readonly requestDuration;
|
|
132
|
+
private readonly errorCount;
|
|
133
|
+
private readonly startedAt;
|
|
134
|
+
constructor(meter: Meter, serviceName: string);
|
|
135
|
+
start(request: IncomingMessage): void;
|
|
136
|
+
record(request: IncomingMessage, response: ServerResponse): void;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
declare class RuntimeMetrics {
|
|
140
|
+
private readonly meter;
|
|
141
|
+
private readonly callbacks;
|
|
142
|
+
private eventLoopDelay;
|
|
143
|
+
private gcObserver;
|
|
144
|
+
private started;
|
|
145
|
+
private previousElu;
|
|
146
|
+
constructor(meter: Meter);
|
|
147
|
+
private observe;
|
|
148
|
+
start(): void;
|
|
149
|
+
stop(): void;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
type NestComponentKind = 'provider' | 'controller';
|
|
153
|
+
declare class NestMethodInstrumenter {
|
|
154
|
+
private readonly tracer;
|
|
155
|
+
private readonly calls;
|
|
156
|
+
private readonly duration;
|
|
157
|
+
private readonly errors;
|
|
158
|
+
private readonly instrumented;
|
|
159
|
+
constructor(tracer: Tracer, meter: Meter);
|
|
160
|
+
instrumentInstance(instance: object, kind: NestComponentKind, componentName?: string): void;
|
|
161
|
+
instrumentPrototype(type: Function, kind: NestComponentKind, componentName?: string): void;
|
|
162
|
+
private instrumentTarget;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
declare const OBSERVE_OPTIONS: unique symbol;
|
|
166
|
+
declare const OBSERVE_HANDLE: unique symbol;
|
|
167
|
+
declare class ObserveModule {
|
|
168
|
+
static forRoot(options?: ObserveOptions): DynamicModule;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
declare const SDK_NAME = "@ryanzen9/nest-observe";
|
|
172
|
+
declare const SDK_VERSION = "0.1.0";
|
|
173
|
+
declare function createObserveResource(config: ResolvedObserveConfig): Resource;
|
|
174
|
+
|
|
175
|
+
declare const REDACTED = "[REDACTED]";
|
|
176
|
+
declare function isSensitiveKey(name: string): boolean;
|
|
177
|
+
declare function redactText(value: string): string;
|
|
178
|
+
declare function redact<T>(value: T): T;
|
|
179
|
+
declare function sanitizeHeaders(headers: Record<string, string | string[] | undefined>, allowedHeaders: readonly string[]): Record<string, string>;
|
|
180
|
+
|
|
181
|
+
/** Last-line protection for spans emitted by third-party instrumentation. */
|
|
182
|
+
declare class SpanRedactionProcessor implements SpanProcessor {
|
|
183
|
+
onStart(_span: Span, _parentContext: Context): void;
|
|
184
|
+
onEnd(span: ReadableSpan): void;
|
|
185
|
+
forceFlush(): Promise<void>;
|
|
186
|
+
shutdown(): Promise<void>;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
interface ObserveRuntime extends ObserveHandle {
|
|
190
|
+
readonly tracerProvider: NodeTracerProvider | undefined;
|
|
191
|
+
readonly meterProvider: MeterProvider | undefined;
|
|
192
|
+
readonly loggerProvider: LoggerProvider | undefined;
|
|
193
|
+
}
|
|
194
|
+
declare function observe(options?: ObserveOptions): ObserveRuntime;
|
|
195
|
+
declare function getObserveRuntime(): ObserveRuntime | undefined;
|
|
196
|
+
|
|
197
|
+
export { CompatibleNestInstrumentation, type CompatibleNestInstrumentationConfig, HttpRequestMetrics, IgnoreTrace, type NestComponentKind, NestLoggerInstrumentation, NestMethodInstrumenter, OBSERVE_HANDLE, OBSERVE_OPTIONS, type ObserveExporters, type ObserveHandle, ObserveModule, type ObserveOptions, type ObserveRuntime, OpenTelemetryLogEmitter, REDACTED, type ResolvedObserveConfig, RuntimeMetrics, SDK_NAME, SDK_VERSION, SpanRedactionProcessor, type StructuredLogEmitter, type StructuredLogRecord, Trace, type TraceOptions, createObserveResource, getObserveRuntime, isSensitiveKey, isTraceDecorated, isTraceIgnored, markTraceDecorated, markTraceIgnored, observe, parseKeyValueList, redact, redactText, resolveObserveConfig, sanitizeHeaders };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { LogRecordExporter, LoggerProvider } from '@opentelemetry/sdk-logs';
|
|
2
|
+
import { MetricReader, MeterProvider } from '@opentelemetry/sdk-metrics';
|
|
3
|
+
import { SpanExporter, SpanProcessor, ReadableSpan } from '@opentelemetry/sdk-trace-base';
|
|
4
|
+
import { Attributes, Meter, Tracer, Span, Context } from '@opentelemetry/api';
|
|
5
|
+
import { Logger } from '@opentelemetry/api-logs';
|
|
6
|
+
import { InstrumentationConfig, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation';
|
|
7
|
+
import { NestInstrumentation } from '@opentelemetry/instrumentation-nestjs-core';
|
|
8
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
9
|
+
import { DynamicModule } from '@nestjs/common';
|
|
10
|
+
import { Resource } from '@opentelemetry/resources';
|
|
11
|
+
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
|
|
12
|
+
|
|
13
|
+
interface ObserveExporters {
|
|
14
|
+
/** Primarily useful for custom backends and tests. OTLP is used by default. */
|
|
15
|
+
span?: SpanExporter;
|
|
16
|
+
log?: LogRecordExporter;
|
|
17
|
+
metricReader?: MetricReader;
|
|
18
|
+
}
|
|
19
|
+
interface ObserveOptions {
|
|
20
|
+
enabled?: boolean;
|
|
21
|
+
serviceName?: string;
|
|
22
|
+
serviceVersion?: string;
|
|
23
|
+
environment?: string;
|
|
24
|
+
instanceId?: string;
|
|
25
|
+
endpoint?: string;
|
|
26
|
+
headers?: Record<string, string>;
|
|
27
|
+
traces?: boolean;
|
|
28
|
+
logs?: boolean;
|
|
29
|
+
metrics?: boolean;
|
|
30
|
+
providerTracing?: boolean;
|
|
31
|
+
controllerTracing?: boolean;
|
|
32
|
+
sampling?: number;
|
|
33
|
+
allowedHeaders?: string[];
|
|
34
|
+
exportTimeoutMillis?: number;
|
|
35
|
+
metricExportIntervalMillis?: number;
|
|
36
|
+
resourceAttributes?: Record<string, string | number | boolean>;
|
|
37
|
+
exporters?: ObserveExporters;
|
|
38
|
+
}
|
|
39
|
+
interface ResolvedObserveConfig {
|
|
40
|
+
enabled: boolean;
|
|
41
|
+
serviceName: string;
|
|
42
|
+
serviceVersion: string;
|
|
43
|
+
environment: string;
|
|
44
|
+
instanceId: string;
|
|
45
|
+
endpoints: {
|
|
46
|
+
traces: string | undefined;
|
|
47
|
+
metrics: string | undefined;
|
|
48
|
+
logs: string | undefined;
|
|
49
|
+
};
|
|
50
|
+
headers: Record<string, string>;
|
|
51
|
+
traces: boolean;
|
|
52
|
+
logs: boolean;
|
|
53
|
+
metrics: boolean;
|
|
54
|
+
providerTracing: boolean;
|
|
55
|
+
controllerTracing: boolean;
|
|
56
|
+
sampling: number;
|
|
57
|
+
allowedHeaders: string[];
|
|
58
|
+
exportTimeoutMillis: number;
|
|
59
|
+
metricExportIntervalMillis: number;
|
|
60
|
+
resourceAttributes: Record<string, string | number | boolean>;
|
|
61
|
+
exporters?: ObserveExporters;
|
|
62
|
+
}
|
|
63
|
+
interface ObserveHandle {
|
|
64
|
+
readonly started: boolean;
|
|
65
|
+
readonly config: Readonly<ResolvedObserveConfig>;
|
|
66
|
+
forceFlush(): Promise<void>;
|
|
67
|
+
shutdown(): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
type Environment = Record<string, string | undefined>;
|
|
71
|
+
declare function parseKeyValueList(value?: string): Record<string, string>;
|
|
72
|
+
declare function resolveObserveConfig(options?: ObserveOptions, env?: Environment): ResolvedObserveConfig;
|
|
73
|
+
|
|
74
|
+
type IgnoreDecorator = MethodDecorator & ClassDecorator;
|
|
75
|
+
declare function IgnoreTrace(): IgnoreDecorator;
|
|
76
|
+
|
|
77
|
+
declare function markTraceIgnored(target: object): void;
|
|
78
|
+
declare function markTraceDecorated(target: object): void;
|
|
79
|
+
declare function isTraceIgnored(method?: object, type?: object): boolean;
|
|
80
|
+
declare function isTraceDecorated(method?: object): boolean;
|
|
81
|
+
|
|
82
|
+
interface TraceOptions {
|
|
83
|
+
name?: string;
|
|
84
|
+
attributes?: Record<string, string>;
|
|
85
|
+
}
|
|
86
|
+
type TraceDecorator = MethodDecorator & ClassDecorator;
|
|
87
|
+
declare function Trace(nameOrOptions?: string | TraceOptions): TraceDecorator;
|
|
88
|
+
|
|
89
|
+
interface StructuredLogRecord {
|
|
90
|
+
body: unknown;
|
|
91
|
+
severityText: 'TRACE' | 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'FATAL';
|
|
92
|
+
attributes: Attributes;
|
|
93
|
+
timestamp?: number;
|
|
94
|
+
}
|
|
95
|
+
interface StructuredLogEmitter {
|
|
96
|
+
emit(record: StructuredLogRecord): void;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
declare class NestLoggerInstrumentation {
|
|
100
|
+
private readonly emitter;
|
|
101
|
+
private readonly resourceAttributes;
|
|
102
|
+
private originals;
|
|
103
|
+
private enabled;
|
|
104
|
+
constructor(emitter: StructuredLogEmitter, resourceAttributes?: Record<string, string>);
|
|
105
|
+
enable(): void;
|
|
106
|
+
disable(): void;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
declare class OpenTelemetryLogEmitter implements StructuredLogEmitter {
|
|
110
|
+
private readonly logger;
|
|
111
|
+
constructor(name?: string, version?: string, logger?: Logger);
|
|
112
|
+
emit(record: StructuredLogRecord): void;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
interface CompatibleNestInstrumentationConfig extends InstrumentationConfig {
|
|
116
|
+
providerTracing?: boolean;
|
|
117
|
+
controllerTracing?: boolean;
|
|
118
|
+
}
|
|
119
|
+
/** Extends upstream Nest support to v12 and instruments DI-created providers. */
|
|
120
|
+
declare class CompatibleNestInstrumentation extends NestInstrumentation {
|
|
121
|
+
private readonly observeConfig;
|
|
122
|
+
private methodInstrumenter?;
|
|
123
|
+
constructor(observeConfig?: CompatibleNestInstrumentationConfig);
|
|
124
|
+
init(): InstrumentationNodeModuleDefinition;
|
|
125
|
+
private getMethodInstrumenter;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
declare class HttpRequestMetrics {
|
|
129
|
+
private readonly serviceName;
|
|
130
|
+
private readonly requestCount;
|
|
131
|
+
private readonly requestDuration;
|
|
132
|
+
private readonly errorCount;
|
|
133
|
+
private readonly startedAt;
|
|
134
|
+
constructor(meter: Meter, serviceName: string);
|
|
135
|
+
start(request: IncomingMessage): void;
|
|
136
|
+
record(request: IncomingMessage, response: ServerResponse): void;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
declare class RuntimeMetrics {
|
|
140
|
+
private readonly meter;
|
|
141
|
+
private readonly callbacks;
|
|
142
|
+
private eventLoopDelay;
|
|
143
|
+
private gcObserver;
|
|
144
|
+
private started;
|
|
145
|
+
private previousElu;
|
|
146
|
+
constructor(meter: Meter);
|
|
147
|
+
private observe;
|
|
148
|
+
start(): void;
|
|
149
|
+
stop(): void;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
type NestComponentKind = 'provider' | 'controller';
|
|
153
|
+
declare class NestMethodInstrumenter {
|
|
154
|
+
private readonly tracer;
|
|
155
|
+
private readonly calls;
|
|
156
|
+
private readonly duration;
|
|
157
|
+
private readonly errors;
|
|
158
|
+
private readonly instrumented;
|
|
159
|
+
constructor(tracer: Tracer, meter: Meter);
|
|
160
|
+
instrumentInstance(instance: object, kind: NestComponentKind, componentName?: string): void;
|
|
161
|
+
instrumentPrototype(type: Function, kind: NestComponentKind, componentName?: string): void;
|
|
162
|
+
private instrumentTarget;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
declare const OBSERVE_OPTIONS: unique symbol;
|
|
166
|
+
declare const OBSERVE_HANDLE: unique symbol;
|
|
167
|
+
declare class ObserveModule {
|
|
168
|
+
static forRoot(options?: ObserveOptions): DynamicModule;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
declare const SDK_NAME = "@ryanzen9/nest-observe";
|
|
172
|
+
declare const SDK_VERSION = "0.1.0";
|
|
173
|
+
declare function createObserveResource(config: ResolvedObserveConfig): Resource;
|
|
174
|
+
|
|
175
|
+
declare const REDACTED = "[REDACTED]";
|
|
176
|
+
declare function isSensitiveKey(name: string): boolean;
|
|
177
|
+
declare function redactText(value: string): string;
|
|
178
|
+
declare function redact<T>(value: T): T;
|
|
179
|
+
declare function sanitizeHeaders(headers: Record<string, string | string[] | undefined>, allowedHeaders: readonly string[]): Record<string, string>;
|
|
180
|
+
|
|
181
|
+
/** Last-line protection for spans emitted by third-party instrumentation. */
|
|
182
|
+
declare class SpanRedactionProcessor implements SpanProcessor {
|
|
183
|
+
onStart(_span: Span, _parentContext: Context): void;
|
|
184
|
+
onEnd(span: ReadableSpan): void;
|
|
185
|
+
forceFlush(): Promise<void>;
|
|
186
|
+
shutdown(): Promise<void>;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
interface ObserveRuntime extends ObserveHandle {
|
|
190
|
+
readonly tracerProvider: NodeTracerProvider | undefined;
|
|
191
|
+
readonly meterProvider: MeterProvider | undefined;
|
|
192
|
+
readonly loggerProvider: LoggerProvider | undefined;
|
|
193
|
+
}
|
|
194
|
+
declare function observe(options?: ObserveOptions): ObserveRuntime;
|
|
195
|
+
declare function getObserveRuntime(): ObserveRuntime | undefined;
|
|
196
|
+
|
|
197
|
+
export { CompatibleNestInstrumentation, type CompatibleNestInstrumentationConfig, HttpRequestMetrics, IgnoreTrace, type NestComponentKind, NestLoggerInstrumentation, NestMethodInstrumenter, OBSERVE_HANDLE, OBSERVE_OPTIONS, type ObserveExporters, type ObserveHandle, ObserveModule, type ObserveOptions, type ObserveRuntime, OpenTelemetryLogEmitter, REDACTED, type ResolvedObserveConfig, RuntimeMetrics, SDK_NAME, SDK_VERSION, SpanRedactionProcessor, type StructuredLogEmitter, type StructuredLogRecord, Trace, type TraceOptions, createObserveResource, getObserveRuntime, isSensitiveKey, isTraceDecorated, isTraceIgnored, markTraceDecorated, markTraceIgnored, observe, parseKeyValueList, redact, redactText, resolveObserveConfig, sanitizeHeaders };
|