Skip to content

Commit 514337e

Browse files
feat/otel package, bacsic tel metrics exporter
1 parent 9d5b498 commit 514337e

8 files changed

Lines changed: 232 additions & 0 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"workspaces": [
1212
"packages/node",
13+
"packages/opentelemetry-node",
1314
"packages/browser",
1415
"packages/react",
1516
"packages/vue"

packages/opentelemetry-node/.eslintrc.js

Whitespace-only changes.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Traceo SDK for Node.js
2+
3+
Library for integration with the [Traceo Platform](https://github.com/traceo-dev/traceo).
4+
5+
### Installation
6+
To install this SDK add this package to your package.json like below:
7+
```
8+
yarn add @traceo-sdk/node
9+
```
10+
or
11+
```
12+
npm install @traceo-sdk/node
13+
```
14+
15+
### Usage
16+
First what you need is to initialize `TraceoClient` in your application.
17+
```ts
18+
import { TraceoClient } from "@traceo-sdk/node";
19+
20+
new TraceoClient({
21+
projectId: <your_project_id>,
22+
url: <you_traceo_instance_url>
23+
});
24+
```
25+
26+
`TraceoClient` options require two parameters. `projectId` 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`.
27+
28+
### Incidents handling
29+
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`.
30+
31+
##### Handlers
32+
The easiest way is to use `ExceptionsHandlers.catchException()` in `try-catch` clause like below:
33+
```ts
34+
import { ExceptionHandlers } from "@traceo-sdk/node";
35+
36+
try {
37+
//your code
38+
} catch (error) {
39+
ExceptionHandlers.catchException(error);
40+
}
41+
```
42+
43+
If you use [NestJS](https://nestjs.com/) framework then you can also create [Interceptor](https://docs.nestjs.com/interceptors) to catch exceptions like below:
44+
45+
traceo.interceptor.ts
46+
```ts
47+
import { ExceptionHandlers } from "@traceo-sdk/node";
48+
//other imports
49+
50+
@Injectable()
51+
export class TraceoInterceptor implements NestInterceptor {
52+
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
53+
return next.handle().pipe(
54+
tap(null, (exception) => {
55+
ExceptionHandlers.catchException(exception);
56+
}),
57+
);
58+
}
59+
}
60+
```
61+
62+
main.ts
63+
```ts
64+
app.useGlobalInterceptors(new TraceoInterceptor());
65+
```
66+
67+
##### Middleware
68+
Another approach is to use `ExceptionMiddlewares.errorMiddleware()`. If you use the [Express.js](https://expressjs.com/) framework, you can use our middleware like below:
69+
70+
Javascript:
71+
```js
72+
import { ExceptionMiddlewares } from "@traceo-sdk/node";
73+
74+
app.use(ExceptionMiddlewares.errorMiddleware());
75+
```
76+
77+
Typescript:
78+
```ts
79+
const { ExceptionMiddlewares } from "@traceo-sdk/node";
80+
81+
app.use(ExceptionMiddlewares.errorMiddleware() as express.ErrorRequestHandler);
82+
```
83+
84+
Remember that `ExceptionMiddlwares.errorMiddleware()` should be before any other error middlewares and after all routes/controllers.
85+
86+
##### Middleware options
87+
88+
89+
| Parameter | Description | Default |
90+
| ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
91+
| `allowLocalhost` | If false then middleware doesn't catch exceptions from requests coming from `localhost` | true |
92+
| `allowHttp` | If false then middleware doesn't catch exceptions received from requests where `req.protocol = http` and catch only exception received with `https` | true |
93+
94+
### Logger
95+
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:
96+
```ts
97+
import { TraceoClient } from "@traceo-sdk/node";
98+
99+
new TraceoClient({
100+
scrapLogsInterval: 120 //in seconds
101+
});
102+
```
103+
104+
Example of using logger:
105+
```ts
106+
import { Logger } from "@traceo-sdk/node";
107+
108+
const traceo = new TraceoClient({...});
109+
110+
traceo.logger.log("Traceo");
111+
```
112+
113+
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.
114+
```ts
115+
traceo.logger.log("Traceo", "Example", "Log");
116+
// [TraceoLogger][LOG] - 31.10.2022, 13:55:45 - Traceo Example Log
117+
118+
traceo.logger.debug("Traceo", {
119+
hello: "World"
120+
});
121+
// [TraceoLogger][DEBUG] - 31.10.2022, 13:58:00 - Traceo { hello: 'World' }
122+
```
123+
### Metrics
124+
To activate the collection of metrics from your application, set the parameter `collectMetrics` in your `TraceoClient` to true:
125+
126+
```ts
127+
new TraceoClient({ collectMetrics: true });
128+
```
129+
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.
130+
131+
```ts
132+
new TraceoClient({ scrapMetricsInterval: <interval_in_seconds> });
133+
```
134+
135+
Remember that provided `scrapMetricsInterval` can't be less than `15` seconds.
136+
137+
## Support
138+
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).
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "@traceo-sdk/opentelemetry-node",
3+
"version": "0.32.1",
4+
"author": "Traceo",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"license": "MIT",
8+
"files": [
9+
"dist"
10+
],
11+
"homepage": "https://github.com/traceo-dev/traceo-sdk",
12+
"repository": {
13+
"type": "git",
14+
"url": "git://github.com/traceo-dev/traceo-sdk.git"
15+
},
16+
"scripts": {
17+
"build": "tsc -p ./tsconfig.json --outDir dist",
18+
"build:tarball": "yarn build && npm pack",
19+
"prebuild": "rimraf ./dist",
20+
"lint": "run-s lint:prettier",
21+
"lint:prettier": "prettier ./src/**/*.{js,ts,tsx} --write",
22+
"prepack": "yarn lint",
23+
"test": "jest",
24+
"test:watch": "jest --watch --notify"
25+
},
26+
"dependencies": {
27+
"@opentelemetry/exporter-metrics-otlp-proto": "^0.38.0",
28+
"@opentelemetry/resources": "^1.12.0",
29+
"@opentelemetry/sdk-metrics": "^1.12.0",
30+
"@opentelemetry/sdk-node": "^0.38.0",
31+
"os": "^0.1.2",
32+
"stacktrace-parser-node": "^1.1.3",
33+
"@traceo-sdk/node": "0.32.1"
34+
},
35+
"devDependencies": {
36+
"@types/node": "10.17.0"
37+
},
38+
"engines": {
39+
"node": ">=10"
40+
}
41+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { TraceoOTLPMetricExporter } from "./otel-metrics-exporter";
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-proto";
2+
import { AggregationTemporality } from "@opentelemetry/sdk-metrics";
3+
import * as opentelemetry from "@opentelemetry/sdk-node";
4+
import { TraceoClient } from "@traceo-sdk/node";
5+
6+
type ExporterOptions = {
7+
client: TraceoClient,
8+
/**
9+
* Add docs
10+
*/
11+
temporalityPreference?: AggregationTemporality
12+
}
13+
export class TraceoOTLPMetricExporter extends OTLPMetricExporter {
14+
private client: TraceoClient;
15+
16+
constructor({
17+
client,
18+
temporalityPreference = AggregationTemporality.DELTA
19+
}: ExporterOptions) {
20+
super({
21+
temporalityPreference
22+
});
23+
24+
this.client = client;
25+
}
26+
27+
export(metrics: opentelemetry.metrics.ResourceMetrics, resultCallback: (result: opentelemetry.core.ExportResult) => void): void {
28+
29+
/**
30+
* TODO: Create common node package and put there http/request service
31+
*/
32+
// parse metrics and send to traceo
33+
34+
resultCallback({ code: opentelemetry.core.ExportResultCode.SUCCESS });
35+
}
36+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./exporters"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"declaration": true,
5+
"target": "es2017",
6+
"outDir": "./dist",
7+
"baseUrl": ".",
8+
"incremental": true,
9+
"esModuleInterop": true,
10+
"resolveJsonModule": true,
11+
"allowSyntheticDefaultImports": true
12+
},
13+
"include": ["src/**/*"]
14+
}

0 commit comments

Comments
 (0)