Skip to content

Commit 5aeee1f

Browse files
committed
cleanup and added readme doc
1 parent 3403b8d commit 5aeee1f

3 files changed

Lines changed: 135 additions & 27 deletions

File tree

README.md

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

lib/node/exceptions/handler.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import { TraceoError } from "../../transport/base";
55
import { Incident } from "../../transport/events";
66
import { getOsPlatform } from "../helpers";
77

8-
type Catch = {
9-
shouldBeCatched?: (error: any) => boolean;
10-
};
11-
128
/**
139
* For using in middleware and as an function in try/catch
1410
*
@@ -21,25 +17,9 @@ type Catch = {
2117
* }
2218
* ```
2319
*
24-
* Usage with `shouldBeCatched`:
25-
* @example
26-
*
27-
* ```
28-
* catchException(error, {
29-
* shouldBeCatched: () => {
30-
* return true;
31-
* }
32-
* });
33-
* ```
3420
*
3521
*/
36-
export const catchException = async (error: any, catchOptions?: Catch) => {
37-
if (catchOptions?.shouldBeCatched !== undefined) {
38-
if (!catchOptions?.shouldBeCatched(error)) {
39-
return;
40-
}
41-
}
42-
22+
export const catchException = async (error: any) => {
4323
if (isClientConnected()) {
4424
await handleException(error);
4525
}
@@ -70,7 +50,7 @@ const prepareException = async (error: TraceoError): Promise<Incident> => {
7050
type: name,
7151
message,
7252
traces,
73-
stack: String(stack),
53+
stack,
7454
platform,
7555
};
7656

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "traceo-sdk",
3-
"version": "0.3.24-beta",
3+
"version": "0.3.25",
44
"license": "MIT",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -18,7 +18,8 @@
1818
"test:watch": "jest --watch --notify"
1919
},
2020
"dependencies": {
21-
"stacktrace-parser-node": "^1.1.3"
21+
"stacktrace-parser-node": "^1.1.3",
22+
"tslib": "^2.3.1"
2223
},
2324
"devDependencies": {
2425
"@rollup/plugin-commonjs": "^21.0.1",
@@ -34,7 +35,6 @@
3435
"rimraf": "^3.0.2",
3536
"rollup": "^2.66.1",
3637
"ts-node": "^10.4.0",
37-
"tslib": "^2.3.1",
3838
"tslint": "^5.11.0",
3939
"typedoc": "^0.22.11",
4040
"typescript": "^4.5.5"

0 commit comments

Comments
 (0)