Skip to content

Commit 8eb9cab

Browse files
feat/node-core package, exporter for otel metrics
1 parent 514337e commit 8eb9cab

29 files changed

Lines changed: 189 additions & 622 deletions

packages/node/global.d.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/node/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@traceo-sdk/node",
3-
"version": "0.32.1",
3+
"version": "0.32.2",
44
"author": "Traceo",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -25,6 +25,7 @@
2525
},
2626
"dependencies": {
2727
"stacktrace-parser-node": "^1.1.3",
28+
"@traceo-sdk/node-core": "0.32.2",
2829
"os": "^0.1.2"
2930
},
3031
"devDependencies": {

packages/node/src/client.ts

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,31 @@
1+
import { INodeClient, CoreClient, TraceoOptions, Dictionary } from "@traceo-sdk/node-core";
12
import { Logger } from "./logger";
2-
import { Metrics } from "./metrics";
3+
import { MetricsRunner } from "./metrics";
34
import { Scrapper } from "./scrapper";
4-
import { ClientOptions, TraceoOptions } from "./types";
5-
import { TRACEO_SDK_VERSION } from "./version";
65

7-
export class Client {
8-
public headers: { [key: string]: any };
9-
10-
private _metrics: Metrics;
11-
private scrappedData: Scrapper;
6+
export class Client extends CoreClient implements INodeClient {
7+
public headers: Dictionary<any>;
128

139
public readonly logger: Logger;
1410
public options: TraceoOptions;
1511

16-
constructor(apiKey: string, options: ClientOptions) {
17-
this.configGlobalClient();
18-
19-
this.options = {
20-
...options,
21-
apiKey
22-
};
23-
24-
this.headers = {
25-
"x-sdk-name": "node",
26-
"x-sdk-version": TRACEO_SDK_VERSION,
27-
"x-sdk-key": apiKey
28-
};
12+
constructor(apiKey: string, options: Omit<TraceoOptions, "apiKey">) {
13+
super(apiKey, options);
2914

3015
this.logger = new Logger();
31-
this.scrappedData = new Scrapper();
32-
33-
this._metrics = new Metrics();
34-
35-
if (!this.isOffline) {
36-
this.initSDK();
37-
}
38-
}
39-
40-
public static get client(): Client {
41-
return global["__TRACEO__"];
4216
}
4317

4418
public static get logger(): Logger {
45-
return this.client.logger;
46-
}
47-
48-
public static get config(): TraceoOptions {
49-
return this.client.options;
19+
return this.logger;
5020
}
5121

52-
private get isOffline(): boolean {
53-
return this.options.offline;
54-
}
55-
56-
private initSDK(): void {
57-
this.scrappedData.collect();
22+
public initSDK(): void {
23+
const scrapper = new Scrapper();
24+
scrapper.collect();
5825

5926
if (this.options.collectMetrics) {
60-
this._metrics.register();
27+
const metrics = new MetricsRunner();
28+
metrics.register();
6129
}
6230
}
63-
64-
private configGlobalClient(): void {
65-
global["__TRACEO__"] = this;
66-
}
6731
}

packages/node/src/core/global.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/node/src/core/http.ts

Lines changed: 0 additions & 84 deletions
This file was deleted.

packages/node/src/core/is.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/node/src/exceptions/handler.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { stacktrace } from "stacktrace-parser-node";
2-
import { HttpModule } from "../core/http";
3-
import { isClientConnected } from "../core/is";
4-
import { TraceoError, NodeIncidentType, CAPTURE_ENDPOINT } from "../types";
5-
import { getOsDetails } from "../helpers";
2+
import { transport, TraceoError, NodeIncidentType, CAPTURE_ENDPOINT, utils } from "@traceo-sdk/node-core";
63

74
/**
85
* For using in middleware and as an function in try/catch
@@ -19,7 +16,7 @@ import { getOsDetails } from "../helpers";
1916
*
2017
*/
2118
export const catchException = async (error: Error) => {
22-
if (isClientConnected()) {
19+
if (utils.isClientConnected()) {
2320
await handleException(error);
2421
}
2522

@@ -28,8 +25,8 @@ export const catchException = async (error: Error) => {
2825

2926
const handleException = async (error: TraceoError) => {
3027
const event: NodeIncidentType = await prepareException(error);
31-
const httpModule = HttpModule.getInstance();
32-
httpModule.request({
28+
29+
transport.request({
3330
url: CAPTURE_ENDPOINT.INCIDENT,
3431
body: event,
3532
onError: (error: Error) => {
@@ -43,7 +40,7 @@ const handleException = async (error: TraceoError) => {
4340

4441
const prepareException = async (error: TraceoError): Promise<NodeIncidentType> => {
4542
const { stack } = error;
46-
const platform = getOsDetails();
43+
const platform = utils.getOsDetails();
4744

4845
const { message, name, traces } = await stacktrace.parse(error);
4946
const event: NodeIncidentType = {

packages/node/src/exceptions/middleware.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import {
22
TraceoError,
33
TraceoIncomingMessage,
44
TraceoServerResponse,
5-
ErrorMiddlewareOptions
6-
} from "../types";
7-
import { isClientConnected, isLocalhost } from "../core/is";
8-
import { getProtocol, getIp } from "../helpers";
5+
ErrorMiddlewareOptions,
6+
utils,
7+
} from "@traceo-sdk/node-core";
98
import { catchException } from "./handler";
109
/**
1110
* Base middleware to catch and intercept error across the express app.
@@ -37,7 +36,7 @@ export const errorMiddleware = (options: ErrorMiddlewareOptions = {}) => {
3736
_res: TraceoServerResponse,
3837
next: (error: TraceoError) => void
3938
): Promise<void> {
40-
if (!isClientConnected()) {
39+
if (!utils.isClientConnected()) {
4140
next(error);
4241
return;
4342
}
@@ -52,14 +51,15 @@ export const errorMiddleware = (options: ErrorMiddlewareOptions = {}) => {
5251

5352
const isToCatch = (req: TraceoIncomingMessage, options: ErrorMiddlewareOptions = {}): boolean => {
5453
if (options.allowHttp !== undefined && !options.allowHttp) {
55-
const isSecure = getProtocol(req) === "https" ? true : false;
54+
const isSecure = utils.getProtocol(req) === "https" ? true : false;
5655
if (!isSecure) {
5756
return false;
5857
}
5958
}
6059

6160
if (options.allowLocalhost !== undefined && !options.allowLocalhost) {
62-
if (isLocalhost(String(getIp(req)))) {
61+
const ip = utils.getIp(req) as string;
62+
if (utils.isLocalhost(ip)) {
6363
return false;
6464
}
6565
}

packages/node/src/helpers.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

packages/node/src/logger.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import { format } from "util";
2-
import { HttpModule } from "./core/http";
3-
import { CAPTURE_ENDPOINT, LogLevel } from "./types";
4-
import { Client } from "./client";
2+
import { CAPTURE_ENDPOINT, LogLevel, transport } from "@traceo-sdk/node-core";
3+
import { getGlobalTraceo } from "@traceo-sdk/node-core/dist/utils";
54

65
export class Logger {
7-
private readonly http: HttpModule;
86
private logsQueue = [];
97
private INTERVAL = 60;
108

119
constructor() {
12-
this.http = HttpModule.getInstance();
1310
this.logsQueue = [];
1411

15-
const scrapLogsInterval = Client.config?.scrapLogsInterval;
12+
const client = getGlobalTraceo();
13+
14+
const scrapLogsInterval = client.options.scrapLogsInterval;
1615
if (scrapLogsInterval && scrapLogsInterval >= 15) {
1716
this.INTERVAL = scrapLogsInterval;
1817
}
@@ -67,7 +66,7 @@ export class Logger {
6766

6867
private async sendLogs() {
6968
if (this.logsQueue.length > 0) {
70-
this.http.request({
69+
transport.request({
7170
url: CAPTURE_ENDPOINT.LOG,
7271
body: this.logsQueue,
7372
onError: (error: Error) => {

0 commit comments

Comments
 (0)