@tracewayapp/nestjs 0.2.0 → 0.3.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.
Files changed (2) hide show
  1. package/README.md +192 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,192 @@
1
+ # @tracewayapp/nestjs
2
+
3
+ NestJS integration for Traceway. Provides a module, middleware, exception filter, service, and decorators for request tracing and error capture.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @tracewayapp/nestjs
9
+ ```
10
+
11
+ ## Setup
12
+
13
+ ### 1. Import TracewayModule
14
+
15
+ Add `TracewayModule.forRoot()` to your app module:
16
+
17
+ ```typescript
18
+ import { Module } from "@nestjs/common";
19
+ import { TracewayModule } from "@tracewayapp/nestjs";
20
+
21
+ @Module({
22
+ imports: [
23
+ TracewayModule.forRoot({
24
+ connectionString: "your-token@https://traceway.example.com/api/report",
25
+ }),
26
+ ],
27
+ })
28
+ export class AppModule {}
29
+ ```
30
+
31
+ ### 2. Add Middleware for Request Tracing
32
+
33
+ Apply `TracewayMiddleware` to trace all HTTP requests:
34
+
35
+ ```typescript
36
+ import { MiddlewareConsumer, Module, NestModule } from "@nestjs/common";
37
+ import { TracewayModule, TracewayMiddleware } from "@tracewayapp/nestjs";
38
+
39
+ @Module({
40
+ imports: [
41
+ TracewayModule.forRoot({
42
+ connectionString: "your-token@https://traceway.example.com/api/report",
43
+ }),
44
+ ],
45
+ })
46
+ export class AppModule implements NestModule {
47
+ configure(consumer: MiddlewareConsumer) {
48
+ consumer.apply(TracewayMiddleware).forRoutes("*");
49
+ }
50
+ }
51
+ ```
52
+
53
+ ### 3. Add Exception Filter
54
+
55
+ Register `TracewayExceptionFilter` globally to capture unhandled exceptions:
56
+
57
+ ```typescript
58
+ import { MiddlewareConsumer, Module, NestModule } from "@nestjs/common";
59
+ import { APP_FILTER } from "@nestjs/core";
60
+ import {
61
+ TracewayModule,
62
+ TracewayMiddleware,
63
+ TracewayExceptionFilter,
64
+ } from "@tracewayapp/nestjs";
65
+
66
+ @Module({
67
+ imports: [
68
+ TracewayModule.forRoot({
69
+ connectionString: "your-token@https://traceway.example.com/api/report",
70
+ debug: true,
71
+ onErrorRecording: ["url", "query", "body", "headers"],
72
+ }),
73
+ ],
74
+ providers: [
75
+ {
76
+ provide: APP_FILTER,
77
+ useClass: TracewayExceptionFilter,
78
+ },
79
+ ],
80
+ })
81
+ export class AppModule implements NestModule {
82
+ configure(consumer: MiddlewareConsumer) {
83
+ consumer.apply(TracewayMiddleware).forRoutes("*");
84
+ }
85
+ }
86
+ ```
87
+
88
+ ## TracewayService
89
+
90
+ Inject `TracewayService` to capture errors and create spans manually:
91
+
92
+ ```typescript
93
+ import { Injectable } from "@nestjs/common";
94
+ import { TracewayService, Span } from "@tracewayapp/nestjs";
95
+
96
+ @Injectable()
97
+ export class UsersService {
98
+ constructor(private readonly traceway: TracewayService) {}
99
+
100
+ @Span("db.users.findAll")
101
+ async findAll() {
102
+ // Span automatically created and ended
103
+ return this.userRepository.find();
104
+ }
105
+
106
+ async createUser(data: CreateUserDto) {
107
+ const span = this.traceway.startSpan("db.users.create");
108
+ try {
109
+ const user = await this.userRepository.save(data);
110
+ return user;
111
+ } finally {
112
+ this.traceway.endSpan(span);
113
+ }
114
+ }
115
+
116
+ async riskyOperation() {
117
+ try {
118
+ await this.externalApi.call();
119
+ } catch (error) {
120
+ this.traceway.captureException(error);
121
+ throw error;
122
+ }
123
+ }
124
+ }
125
+ ```
126
+
127
+ ## @Span Decorator
128
+
129
+ Automatically creates and ends a span for a method:
130
+
131
+ ```typescript
132
+ import { Span } from "@tracewayapp/nestjs";
133
+
134
+ @Span("operation-name")
135
+ async myMethod() {
136
+ // span is created on entry and ended on return
137
+ }
138
+ ```
139
+
140
+ ## Async Configuration
141
+
142
+ Use `forRootAsync` for dynamic configuration (e.g., from `ConfigService`):
143
+
144
+ ```typescript
145
+ import { Module } from "@nestjs/common";
146
+ import { ConfigModule, ConfigService } from "@nestjs/config";
147
+ import { TracewayModule } from "@tracewayapp/nestjs";
148
+
149
+ @Module({
150
+ imports: [
151
+ ConfigModule.forRoot(),
152
+ TracewayModule.forRootAsync({
153
+ imports: [ConfigModule],
154
+ useFactory: (config: ConfigService) => ({
155
+ connectionString: config.get("TRACEWAY_CONNECTION_STRING"),
156
+ debug: config.get("NODE_ENV") !== "production",
157
+ }),
158
+ inject: [ConfigService],
159
+ }),
160
+ ],
161
+ })
162
+ export class AppModule {}
163
+ ```
164
+
165
+ ## What Gets Captured
166
+
167
+ | Data | Description |
168
+ |------|-------------|
169
+ | Endpoint | HTTP method and route (e.g., `GET /users/:id`) |
170
+ | Duration | Request processing time |
171
+ | Status Code | HTTP response status |
172
+ | Client IP | Client IP address from headers or socket |
173
+ | Exceptions | Unhandled errors with full stack traces |
174
+ | Spans | Custom spans created within the request |
175
+
176
+ ## Configuration Options
177
+
178
+ | Option | Type | Default | Description |
179
+ |--------|------|---------|-------------|
180
+ | `connectionString` | `string` | — | Traceway connection string (`token@url`) |
181
+ | `debug` | `boolean` | `false` | Enable debug logging |
182
+ | `version` | `string` | `""` | Application version |
183
+ | `serverName` | `string` | hostname | Server identifier |
184
+ | `sampleRate` | `number` | `1.0` | Normal trace sampling rate (0.0-1.0) |
185
+ | `errorSampleRate` | `number` | `1.0` | Error trace sampling rate (0.0-1.0) |
186
+ | `ignoredRoutes` | `string[]` | `[]` | Routes to exclude from tracing |
187
+ | `onErrorRecording` | `ErrorRecordingField[]` | `[]` | Request data to include on errors (`"url"`, `"query"`, `"body"`, `"headers"`) |
188
+
189
+ ## Requirements
190
+
191
+ - NestJS >= 10
192
+ - `@tracewayapp/backend` (installed automatically as dependency)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tracewayapp/nestjs",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Traceway NestJS integration with module, middleware, and decorators",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -23,7 +23,7 @@
23
23
  "dev": "tsup --watch"
24
24
  },
25
25
  "dependencies": {
26
- "@tracewayapp/backend": "0.2.0"
26
+ "@tracewayapp/backend": "0.3.0"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "@nestjs/common": "^10.0.0",