Skip to content

Commit 8d788bc

Browse files
committed
fix/missing readme update
1 parent c7b2ad1 commit 8d788bc

1 file changed

Lines changed: 139 additions & 0 deletions

File tree

README.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,74 @@ main.ts
6565
app.useGlobalInterceptors(new TraceoInterceptor());
6666
```
6767

68+
##### Middleware
69+
Another approach is to use `ExceptionMiddlewares.errorMiddleware()`. If you use the [Express.js](https://expressjs.com/) framework, you can use our middl# Traceo SDK for Node.js
70+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
71+
72+
Library for integration with the [Traceo Platform](https://github.com/traceo-dev/traceo).
73+
74+
### Installation
75+
To install this SDK add this package to your package.json like below:
76+
```
77+
yarn add @traceo-sdk/node
78+
```
79+
or
80+
```
81+
npm install @traceo-sdk/node
82+
```
83+
84+
### Usage
85+
First what you need is to initialize `TraceoClient` in your application.
86+
```ts
87+
import { TraceoClient } from "@traceo-sdk/node";
88+
89+
new TraceoClient({
90+
appId: <your_application_id>,
91+
url: <you_traceo_instance_url>
92+
});
93+
```
94+
95+
`TraceoClient` options require two parameters. `appId` is a unique identifier of an application created on the Traceo platform. Information about application ID you can get from the Traceo Platform in `Settings|Details` tab. `url` parameter specifies the address where your Traceo Platform instance is located. Address should be passed in the format `<protocol>://<domain>:<port>`, eq. `http://localhost:3000`.
96+
97+
### Incidents handling
98+
Incidents are all the exceptions and other problems that occur in your application. After each exception occurs, the Traceo SDK catches the exception and sends it to the Traceo Platform. This package provide the two main ways to catch exceptions in your application - `Handlers` and `Middlewares`.
99+
100+
##### Handlers
101+
The easiest way is to use `ExceptionsHandlers.catchException()` in `try-catch` clause like below:
102+
```ts
103+
import { ExceptionHandlers } from "@traceo-sdk/node";
104+
105+
try {
106+
//your code
107+
} catch (error) {
108+
ExceptionHandlers.catchException(error);
109+
}
110+
```
111+
112+
If you use [NestJS](https://nestjs.com/) framework then you can also create [Interceptor](https://docs.nestjs.com/interceptors) to catch exceptions like below:
113+
114+
traceo.interceptor.ts
115+
```ts
116+
import { ExceptionHandlers } from "@traceo-sdk/node";
117+
//other imports
118+
119+
@Injectable()
120+
export class TraceoInterceptor implements NestInterceptor {
121+
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
122+
return next.handle().pipe(
123+
tap(null, (exception) => {
124+
ExceptionHandlers.catchException(exception);
125+
}),
126+
);
127+
}
128+
}
129+
```
130+
131+
main.ts
132+
```ts
133+
app.useGlobalInterceptors(new TraceoInterceptor());
134+
```
135+
68136
##### Middleware
69137
Another approach is to use `ExceptionMiddlewares.errorMiddleware()`. If you use the [Express.js](https://expressjs.com/) framework, you can use our middleware like below:
70138

@@ -87,6 +155,77 @@ Remember that `ExceptionMiddlwares.errorMiddleware()` should be before any other
87155
##### Middleware options
88156

89157

158+
| Parameter | Description | Default |
159+
| ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
160+
| `allowLocalhost` | If false then middleware doesn't catch exceptions from requests coming from `localhost` | true |
161+
| `allowHttp` | If false then middleware doesn't catch exceptions received from requests where `req.protocol = http` and catch only exception received with `https` | true |
162+
163+
### Logger
164+
The Traceo SDK can be used also as a logger. Each log is saved on the Traceo Platform, thanks to which it is possible to later easily access the recorded information. Logs are sent to Traceo in every 60 seconds. To change this behavior, set a custom value (measured in seconds) in the `scrapLogsInterval` field inside traceo client properties like below:
165+
```ts
166+
import { TraceoClient } from "@traceo-sdk/node";
167+
168+
new TraceoClient({
169+
scrapLogsInterval: 120 //in seconds
170+
});
171+
```
172+
173+
Example of using logger:
174+
```ts
175+
import { Logger } from "@traceo-sdk/node";
176+
177+
const traceo = new TraceoClient({...});
178+
179+
traceo.logger.log("Traceo");
180+
```
181+
182+
The `logger` can use 5 different types of log: `log`, `info`, `debug`, `warn`, `error`. Each function responsible for logging the appropriate log type accepts a list of arguments in the parameter.
183+
```ts
184+
traceo.logger.log("Traceo", "Example", "Log");
185+
// [TraceoLogger][LOG] - 31.10.2022, 13:55:45 - Traceo Example Log
186+
187+
traceo.logger.debug("Traceo", {
188+
hello: "World"
189+
});
190+
// [TraceoLogger][DEBUG] - 31.10.2022, 13:58:00 - Traceo { hello: 'World' }
191+
```
192+
### Metrics
193+
To activate the collection of metrics from your application, set the parameter `collectMetrics` in your `TraceoClient` to true:
194+
195+
```ts
196+
new TraceoClient({ collectMetrics: true });
197+
```
198+
Metrics are collected from the application every 30 seconds. If you want to collect metrics at a different time interval then you can use the `scrapMetricsInterval` parameter.
199+
200+
```ts
201+
new TraceoClient({ scrapMetricsInterval: <interval_in_seconds> });
202+
```
203+
204+
Remember that provided `scrapMetricsInterval` can't be less than `15` seconds.
205+
206+
## Support
207+
Feel free to create Issues, Pull Request and Discussion. If you want to contact with the developer working on this package click [here](mailto:piotr.szewczyk.software@gmail.com).
208+
eware like below:
209+
210+
Javascript:
211+
```js
212+
import { ExceptionMiddlewares } from "@traceo-sdk/node";
213+
214+
app.use(ExceptionMiddlewares.errorMiddleware());
215+
```
216+
217+
Typescript:
218+
```ts
219+
const { ExceptionMiddlewares } from "@traceo-sdk/node";
220+
221+
app.use(ExceptionMiddlewares.errorMiddleware() as express.ErrorRequestHandler);
222+
```
223+
224+
Remember that `ExceptionMiddlwares.errorMiddleware()` should be before any other error middlewares and after all routes/controllers.
225+
226+
##### Middleware options
227+
228+
90229
| Parameter | Description | Default |
91230
|---|---|---|
92231
| `allowLocalhost` | If false then middleware doesn't catch exceptions from requests coming from `localhost` | true |

0 commit comments

Comments
 (0)