Skip to content

Commit b624a8b

Browse files
Merge pull request #6 from traceo-dev/feat/otel
Feat/otel
2 parents 9d5b498 + 5f3a637 commit b624a8b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1424
-677
lines changed

CHANGELOG.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1 @@
11
# CHANGELOG
2-
3-
## 0.0.3-alpha - 2022-03-23
4-
- Split transport file into few small files
5-
- removing try/catch from various functions
6-
- added options in parameters to `catchException` function
7-
- fixes for middleware and process of handling exceptions
8-
- fix for build script
9-
## 0.0.2-alpha - 2022-03-23
10-
- Migration from monorepo in lerna to single monolith
11-
- Bugfixes
12-
- Using rollup.js
13-
- First working version
14-
-
15-
16-
## 0.0.1-alpha - 2022-03-22
17-
- Initial release with base client library

package.json

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,37 @@
99
"lint:prettier": "lerna run lint:prettier"
1010
},
1111
"workspaces": [
12+
"packages/node-core",
13+
"packages/opentelemetry-node",
1214
"packages/node",
1315
"packages/browser",
1416
"packages/react",
1517
"packages/vue"
1618
],
1719
"devDependencies": {
18-
"@types/node": "10.17.0",
19-
"@types/rimraf": "^3.0.2",
20-
"jsdom": "^19.0.0",
21-
"lerna": "6.5.0-alpha.2",
22-
"tslib": "^2.3.1",
23-
"ts-loader": "^9.4.2",
24-
"typescript": "^4.5.5",
2520
"@types/jest": "^29.2.0",
26-
"eslint": "^8.8.0",
27-
"jest": "^26.5.5",
28-
"os": "^0.1.2",
29-
"prettier-check": "^2.0.0",
30-
"rollup": "^2.66.1",
31-
"ts-node": "^10.4.0",
32-
"tslint": "^5.11.0",
21+
"@types/node": "20.3.1",
3322
"@types/react": "^16.9.49",
3423
"@types/react-dom": "^16.9.8",
24+
"@types/rimraf": "^3.0.2",
25+
"eslint": "^8.8.0",
26+
"jest": "^26.5.5",
27+
"jsdom": "^19.0.0",
28+
"lerna": "6.5.0-alpha.2",
3529
"lint-staged": "^10.5.3",
3630
"npm-run-all": "^4.1.5",
31+
"os": "^0.1.2",
3732
"prettier": "2.1.2",
33+
"prettier-check": "^2.0.0",
3834
"react": "^18.1.0",
3935
"react-dom": "^18.1.0",
4036
"rimraf": "^3.0.2",
37+
"rollup": "^2.66.1",
38+
"ts-loader": "^9.4.2",
39+
"ts-node": "^10.4.0",
40+
"tslib": "^2.3.1",
41+
"tslint": "^5.11.0",
42+
"typescript": "^4.7.4",
4143
"webpack": "^5.11.0"
4244
},
4345
"jest": {

packages/node-core/.eslintrc.js

Whitespace-only changes.

packages/node-core/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Traceo SDK for Node.js - Core

packages/node-core/global.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { INodeClient } from "./src/types"
2+
3+
declare global {
4+
var __TRACEO__: INodeClient
5+
}
6+
7+
export { };

packages/node-core/package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "@traceo-sdk/node-core",
3+
"version": "0.32.2",
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+
"devDependencies": {
27+
"@types/node": "10.17.0"
28+
},
29+
"engines": {
30+
"node": ">=10"
31+
}
32+
}

packages/node-core/src/client.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { INodeClient, TraceoOptions } from "./types";
2+
import { TRACEO_SDK_VERSION } from "./version";
3+
4+
export abstract class CoreClient implements INodeClient {
5+
public options: TraceoOptions;
6+
public headers: { [key: string]: any; };
7+
8+
constructor(apiKey: string, { collectMetrics = true, offline = false, ...opts }: Omit<TraceoOptions, "apiKey">) {
9+
this.configGlobalClient();
10+
11+
this.options = {
12+
...opts,
13+
collectMetrics,
14+
offline,
15+
apiKey
16+
};
17+
18+
this.headers = {
19+
"x-sdk-name": "node",
20+
"x-sdk-version": TRACEO_SDK_VERSION,
21+
"x-sdk-key": apiKey
22+
};
23+
24+
if (!this.isOffline) {
25+
this.initSDK();
26+
} else {
27+
console.warn('Traceo does not collect any data because it is offline mode.')
28+
}
29+
}
30+
31+
private configGlobalClient(): void {
32+
global["__TRACEO__"] = this;
33+
}
34+
35+
private get isOffline(): boolean {
36+
return this.options.offline;
37+
}
38+
39+
public abstract initSDK(): void;
40+
}

packages/node-core/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export { CoreClient } from "./client";
2+
3+
export * from "./transport";
4+
export * from "./types";
5+
6+
export * as utils from "./utils";
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import * as http from "http";
2+
import * as https from "https";
3+
import { RequestType, RequestOptionsType } from "./types";
4+
import { getGlobalTraceo } from "./utils";
5+
6+
const requestWriteBody = (method: RequestType, request: http.ClientRequest, body: {}) => {
7+
if (method === "POST") {
8+
request.write(JSON.stringify(body));
9+
}
10+
}
11+
12+
const requestModule = (): typeof http | typeof https => {
13+
const protocol = clientURL().protocol;
14+
return protocol == "http:" ? http : https;
15+
}
16+
17+
const clientURL = (): URL => {
18+
return new URL(getGlobalTraceo().options.host);
19+
}
20+
21+
const requestOptions = (url: string, method: RequestType): http.RequestOptions => {
22+
const host = getGlobalTraceo().options.host;
23+
const reqUrl = new URL(host);
24+
const path = new URL(url, host);
25+
26+
return {
27+
protocol: reqUrl.protocol,
28+
port: reqUrl.port,
29+
host: reqUrl.hostname,
30+
method,
31+
path: path.pathname
32+
};
33+
}
34+
35+
const requestHeaders = (method: RequestType) => {
36+
const headers = getGlobalTraceo().headers;
37+
38+
if (method !== "POST") return headers;
39+
return {
40+
headers: {
41+
"Content-Type": "application/json",
42+
...headers
43+
}
44+
};
45+
}
46+
47+
/**
48+
* Make http/s request to Traceo platoform.
49+
*
50+
* Default request method is POST, in this case in hedaers is passed "Content-Type": "application/json".
51+
* URL is concatenation of passed host to client and pathanme to this method.
52+
* Use callback/onError callbacks to handle action after operation.
53+
*/
54+
const request = ({ url, method = "POST", body, onError, callback }: RequestOptionsType) => {
55+
const options = {
56+
...requestHeaders(method),
57+
...requestOptions(url, method)
58+
};
59+
60+
const httpModule = requestModule();
61+
62+
const request = httpModule.request(options, callback);
63+
request.on("error", () => onError);
64+
65+
requestWriteBody(method, request, body);
66+
67+
request.end();
68+
}
69+
70+
export const transport = {
71+
request
72+
}
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
import { Trace } from "./trace";
2-
import { Environment } from "./index";
3-
4-
export interface EventResponse {
5-
statusCode: number;
6-
statusMessage?: string;
7-
body?: string;
8-
}
92

103
export type NodeIncidentType = {
114
name: string;

0 commit comments

Comments
 (0)