-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
595 additions
and
48 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
lib | ||
dist | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": ["@artusx/eslint-config"], | ||
"parserOptions": { | ||
"project": "./tsconfig.json" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @artusx/plugin-opentelemetry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": ["src/**/*.ts", "src/**/*.json"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters