Skip to content

Commit c7b2ad1

Browse files
committed
feat/readme for node package
1 parent 8c2b3f5 commit c7b2ad1

5 files changed

Lines changed: 155 additions & 15 deletions

File tree

packages/node/README.md

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,139 @@
1-
<!-- Traceo NODE JS SDK -->
1+
# Traceo SDK for Node.js
2+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
3+
4+
Library for integration with the [Traceo Platform](https://github.com/traceo-dev/traceo).
5+
6+
### Installation
7+
To install this SDK add this package to your package.json like below:
8+
```
9+
yarn add @traceo-sdk/node
10+
```
11+
or
12+
```
13+
npm install @traceo-sdk/node
14+
```
15+
16+
### Usage
17+
First what you need is to initialize `TraceoClient` in your application.
18+
```ts
19+
import { TraceoClient } from "@traceo-sdk/node";
20+
21+
new TraceoClient({
22+
appId: <your_application_id>,
23+
url: <you_traceo_instance_url>
24+
});
25+
```
26+
27+
`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`.
28+
29+
### Incidents handling
30+
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`.
31+
32+
##### Handlers
33+
The easiest way is to use `ExceptionsHandlers.catchException()` in `try-catch` clause like below:
34+
```ts
35+
import { ExceptionHandlers } from "@traceo-sdk/node";
36+
37+
try {
38+
//your code
39+
} catch (error) {
40+
ExceptionHandlers.catchException(error);
41+
}
42+
```
43+
44+
If you use [NestJS](https://nestjs.com/) framework then you can also create [Interceptor](https://docs.nestjs.com/interceptors) to catch exceptions like below:
45+
46+
traceo.interceptor.ts
47+
```ts
48+
import { ExceptionHandlers } from "@traceo-sdk/node";
49+
//other imports
50+
51+
@Injectable()
52+
export class TraceoInterceptor implements NestInterceptor {
53+
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
54+
return next.handle().pipe(
55+
tap(null, (exception) => {
56+
ExceptionHandlers.catchException(exception);
57+
}),
58+
);
59+
}
60+
}
61+
```
62+
63+
main.ts
64+
```ts
65+
app.useGlobalInterceptors(new TraceoInterceptor());
66+
```
67+
68+
##### Middleware
69+
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+
71+
Javascript:
72+
```js
73+
import { ExceptionMiddlewares } from "@traceo-sdk/node";
74+
75+
app.use(ExceptionMiddlewares.errorMiddleware());
76+
```
77+
78+
Typescript:
79+
```ts
80+
const { ExceptionMiddlewares } from "@traceo-sdk/node";
81+
82+
app.use(ExceptionMiddlewares.errorMiddleware() as express.ErrorRequestHandler);
83+
```
84+
85+
Remember that `ExceptionMiddlwares.errorMiddleware()` should be before any other error middlewares and after all routes/controllers.
86+
87+
##### Middleware options
88+
89+
90+
| Parameter | Description | Default |
91+
| ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
92+
| `allowLocalhost` | If false then middleware doesn't catch exceptions from requests coming from `localhost` | true |
93+
| `allowHttp` | If false then middleware doesn't catch exceptions received from requests where `req.protocol = http` and catch only exception received with `https` | true |
94+
95+
### Logger
96+
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:
97+
```ts
98+
import { TraceoClient } from "@traceo-sdk/node";
99+
100+
new TraceoClient({
101+
scrapLogsInterval: 120 //in seconds
102+
});
103+
```
104+
105+
Example of using logger:
106+
```ts
107+
import { Logger } from "@traceo-sdk/node";
108+
109+
const traceo = new TraceoClient({...});
110+
111+
traceo.logger.log("Traceo");
112+
```
113+
114+
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.
115+
```ts
116+
traceo.logger.log("Traceo", "Example", "Log");
117+
// [TraceoLogger][LOG] - 31.10.2022, 13:55:45 - Traceo Example Log
118+
119+
traceo.logger.debug("Traceo", {
120+
hello: "World"
121+
});
122+
// [TraceoLogger][DEBUG] - 31.10.2022, 13:58:00 - Traceo { hello: 'World' }
123+
```
124+
### Metrics
125+
To activate the collection of metrics from your application, set the parameter `collectMetrics` in your `TraceoClient` to true:
126+
127+
```ts
128+
new TraceoClient({ collectMetrics: true });
129+
```
130+
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.
131+
132+
```ts
133+
new TraceoClient({ scrapMetricsInterval: <interval_in_seconds> });
134+
```
135+
136+
Remember that provided `scrapMetricsInterval` can't be less than `15` seconds.
137+
138+
## Support
139+
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).

packages/node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@traceo-sdk/node",
3-
"version": "0.31.6",
3+
"version": "0.31.7",
44
"author": "Traceo",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

packages/node/src/client.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Logger } from "./logger";
22
import { Metrics } from "./metrics";
33
import { Scrapper } from "./scrapper";
44
import { TraceoOptions } from "./types";
5-
import { IClientMetrics } from "./types/interfaces/IMetrics";
65
import { TRACEO_SDK_VERSION } from "./version";
76

87
export class Client {
@@ -24,7 +23,7 @@ export class Client {
2423
"x-sdk-key": this.options.apiKey,
2524
};
2625

27-
this.logger = new Logger(options?.scrapLogsInterval);
26+
this.logger = new Logger();
2827
this.scrappedData = new Scrapper();
2928

3029
this._metrics = new Metrics();
@@ -58,9 +57,9 @@ export class Client {
5857
}
5958
}
6059

61-
public metrics(): IClientMetrics {
62-
return this._metrics;
63-
}
60+
// private metrics(): IClientMetrics {
61+
// return this._metrics;
62+
// }
6463

6564
private configGlobalClient(): void {
6665
global["__TRACEO__"] = this;

packages/node/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export { Client as TraceoClient } from "./client";
2-
export { Logger } from "./logger";
32

43
export * as ExceptionHandlers from "./exceptions/handler";
54
export * as ExceptionMiddlewares from "./exceptions/middleware";

packages/node/src/logger.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
import { format } from "util";
22
import { HttpModule } from "./core/http";
33
import { LogLevel } from "./types";
4-
import * as http from "http";
4+
import { Client } from "./client";
55

66
export class Logger {
77
private readonly http: HttpModule;
8-
private DEFAULT_INTERVAL = 60; //60s
98
private logsQueue = [];
9+
private INTERVAL = 60;
1010

11-
constructor(scrapInterval?: number) {
11+
constructor() {
1212
this.http = HttpModule.getInstance();
1313
this.logsQueue = [];
1414

15-
let interval = this.DEFAULT_INTERVAL;
16-
if (scrapInterval && scrapInterval >= 15) {
17-
interval = scrapInterval;
15+
const scrapLogsInterval = Client.config?.scrapLogsInterval;
16+
if (scrapLogsInterval && scrapLogsInterval >= 15) {
17+
this.INTERVAL = scrapLogsInterval;
1818
}
1919

20-
setInterval(() => this.sendLogs(), interval * 1000);
20+
this.register();
21+
}
22+
23+
private register() {
24+
setInterval(() => this.sendLogs(), this.INTERVAL * 1000);
2125
}
2226

2327
public log(...args: any[]): void {

0 commit comments

Comments
 (0)