|
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. |
0 commit comments