Skip to content

Commit

Permalink
feat(@artusx/otl): init
Browse files Browse the repository at this point in the history
  • Loading branch information
thonatos committed May 22, 2024
1 parent d3bef0f commit 72c36d4
Show file tree
Hide file tree
Showing 12 changed files with 595 additions and 48 deletions.
485 changes: 437 additions & 48 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions packages/libs/otl/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
lib
dist
coverage
6 changes: 6 additions & 0 deletions packages/libs/otl/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": ["@artusx/eslint-config"],
"parserOptions": {
"project": "./tsconfig.json"
}
}
1 change: 1 addition & 0 deletions packages/libs/otl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
3 changes: 3 additions & 0 deletions packages/libs/otl/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
registry=https://registry.npmjs.org/
always-auth=false
strict-ssl=false
1 change: 1 addition & 0 deletions packages/libs/otl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @artusx/plugin-opentelemetry
6 changes: 6 additions & 0 deletions packages/libs/otl/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverageFrom: ['<rootDir>/src/**/*.ts'],
};
62 changes: 62 additions & 0 deletions packages/libs/otl/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "@artusx/otl",
"version": "0.0.0-dev.0",
"description": "opentelemetry for artusx",
"keywords": [
"artusx"
],
"homepage": "https://github.com/artusjs/artusx",
"repository": {
"type": "git",
"url": "https://github.com/artusjs/artusx.git"
},
"license": "MIT",
"author": "Suyi <[email protected]>",
"exports": {
".": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
}
},
"main": "lib/index.js",
"types": "lib/index.d.js",
"files": [
"lib"
],
"scripts": {
"build": "npm run tsc",
"cov": "jest --coverage --detectOpenHandles --testTimeout=15000",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"test": "jest --detectOpenHandles --testTimeout=15000",
"tsc": "rm -rf lib && tsc -p ./tsconfig.build.json"
},
"dependencies": {
"@opentelemetry/api": "~1.8.0",
"@opentelemetry/exporter-jaeger": "latest",
"@opentelemetry/exporter-zipkin": "latest",
"@opentelemetry/instrumentation": "latest",
"@opentelemetry/instrumentation-http": "latest",
"@opentelemetry/instrumentation-koa": "~0.40.0",
"@opentelemetry/resources": "latest",
"@opentelemetry/sdk-trace-base": "latest",
"@opentelemetry/sdk-trace-node": "latest",
"@opentelemetry/semantic-conventions": "latest"
},
"devDependencies": {
"@artusx/eslint-config": "workspace:*",
"@artusx/tsconfig": "workspace:*",
"@types/jest": "~29.5.11",
"@types/node": "^18.11.17",
"@typescript-eslint/eslint-plugin": "~6.19.1",
"eslint": "~8.56.0",
"eslint-plugin-import": "~2.29.1",
"jest": "~29.7.0",
"ts-jest": "~29.1.2",
"tslib": "^2.5.0",
"typescript": "^4.9.4"
},
"publishConfig": {
"access": "public"
}
}
56 changes: 56 additions & 0 deletions packages/libs/otl/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as api from '@opentelemetry/api';
import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { JaegerExporter } from '@opentelemetry/exporter-jaeger';
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin';
import { Resource } from '@opentelemetry/resources';
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions';

const EXPORTER = process.env.EXPORTER || '';

export const getTraceId = () => {
const currentSpan = api.trace.getSpan(api.context.active());
if (!currentSpan) {
return '';
}

console.log(currentSpan.spanContext());

return currentSpan.spanContext().traceId;
};

export const addEvent = (event: string, data: { key: string; value: any }) => {
const currentSpan = api.trace.getSpan(api.context.active());
currentSpan?.addEvent(event);
currentSpan?.setAttribute(data.key, data.value);
};

export const setupTracing = (serviceName: string) => {
const provider = new NodeTracerProvider({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: serviceName,
}),
});

let exporter;

if (EXPORTER === 'jaeger') {
exporter = new JaegerExporter();
} else {
exporter = new ZipkinExporter();
}

provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

registerInstrumentations({
instrumentations: [new KoaInstrumentation(), new HttpInstrumentation()],
tracerProvider: provider,
});

provider.register();

return api.trace.getTracer(serviceName);
};
4 changes: 4 additions & 0 deletions packages/libs/otl/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["src/**/*.ts", "src/**/*.json"]
}
9 changes: 9 additions & 0 deletions packages/libs/otl/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@artusx/tsconfig",
"compilerOptions": {
"baseUrl": ".",
"types": ["node", "jest", "reflect-metadata"],
"outDir": "lib"
},
"include": ["src/**/*.ts", "src/**/*.json", "test/**/*.ts"]
}
6 changes: 6 additions & 0 deletions rush.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@
"tags": ["artusx-libs"],
"versionPolicyName": "public"
},
{
"packageName": "@artusx/otl",
"projectFolder": "packages/libs/otl",
"tags": ["artusx-libs"],
"versionPolicyName": "public"
},
{
"packageName": "@artusx/plugin-redis",
"projectFolder": "packages/plugins/redis",
Expand Down

0 comments on commit 72c36d4

Please sign in to comment.