You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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
+
newTraceoClient({
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:
If you use [NestJS](https://nestjs.com/) framework then you can also create [Interceptor](https://docs.nestjs.com/interceptors) to catch exceptions like below:
Another approach is to use `ExceptionMiddlewares.errorMiddleware()`. If you use the [Express.js](https://expressjs.com/) framework, you can use our middleware like below:
70
138
@@ -87,6 +155,77 @@ Remember that `ExceptionMiddlwares.errorMiddleware()` should be before any other
|`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
+
newTraceoClient({
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 =newTraceoClient({...});
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
To activate the collection of metrics from your application, set the parameter `collectMetrics` in your `TraceoClient` to true:
194
+
195
+
```ts
196
+
newTraceoClient({ 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.
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).
0 commit comments