-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create instrumentation package with minimal tests
- Loading branch information
Showing
14 changed files
with
808 additions
and
81 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
'@ogma/instrumentation': minor | ||
--- | ||
|
||
Initial release of @ogma/instrumentation |
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# compiled output | ||
lib/ | ||
dist/ | ||
tmp/ | ||
node_modules/ | ||
|
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
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
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 @@ | ||
{ | ||
"src": "./packages/instrumentation/src", | ||
"include": ["packages/instrumentation/src/**/*.ts"], | ||
"reporter": ["text", "lcov"], | ||
"reportDir": "./packages/instrumentation/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,7 @@ | ||
# instrumentation | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Building | ||
|
||
Run `nx build instrumentation` to build the library. |
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,19 @@ | ||
{ | ||
"name": "@ogma/instrumentation", | ||
"version": "0.0.1", | ||
"dependencies": { | ||
"@opentelemetry/context-async-hooks": "^1.25.0", | ||
"@opentelemetry/instrumentation": "^0.44.0", | ||
"@opentelemetry/sdk-trace-base": "^1.25.0", | ||
"@opentelemetry/sdk-trace-node": "^1.25.0", | ||
"tslib": "^2.3.0" | ||
}, | ||
"peerDependencies": { | ||
"@ogma/logger": "^3.2.0", | ||
"@opentelemetry/api": "^1.3.0" | ||
}, | ||
"type": "commonjs", | ||
"main": "./src/index.js", | ||
"typings": "./src/index.d.ts", | ||
"private": true | ||
} |
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,28 @@ | ||
{ | ||
"name": "instrumentation", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "packages/instrumentation/src", | ||
"projectType": "library", | ||
"tags": [], | ||
"targets": { | ||
"build": { | ||
"executor": "@nx/js:tsc", | ||
"outputs": ["{options.outputPath}"], | ||
"options": { | ||
"outputPath": "dist/packages/instrumentation", | ||
"main": "packages/instrumentation/src/index.ts", | ||
"tsConfig": "packages/instrumentation/tsconfig.build.json", | ||
"assets": ["packages/instrumentation/*.md"] | ||
} | ||
}, | ||
"test": { | ||
"executor": "nx-uvu:uvu", | ||
"options": { | ||
"rootDir": "./packages/instrumentation/test", | ||
"coverage": true, | ||
"coverageConfig": "./packages/instrumentation/.c8rc", | ||
"useSwc": true | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
export * from './lib/instrumentation'; |
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,93 @@ | ||
/* eslint-disable @typescript-eslint/no-this-alias */ | ||
import { context, diag, isSpanContextValid, Span, trace } from '@opentelemetry/api'; | ||
import { | ||
InstrumentationBase, | ||
InstrumentationNodeModuleDefinition, | ||
safeExecuteInTheMiddle, | ||
} from '@opentelemetry/instrumentation'; | ||
|
||
import { OgmaInstrumentationConfig } from './types'; | ||
|
||
const ogmaVersions = ['>=3.2.0']; | ||
|
||
export class OgmaInstrumentation extends InstrumentationBase { | ||
constructor(config: OgmaInstrumentationConfig = {}) { | ||
super('@opentelemetry/instrumentation-ogma', '0.0.1', config); | ||
} | ||
|
||
protected init() { | ||
return [ | ||
new InstrumentationNodeModuleDefinition<any>( | ||
'@ogma/logger', | ||
ogmaVersions, | ||
(ogmaModule, moduleVersion) => { | ||
diag.debug(`Applying patch for @ogma/logger@${moduleVersion}`); | ||
const instrumentation = this; | ||
Object.assign(ogmaModule.OgmaDefaults, { | ||
...ogmaModule.OgmaDefaults, | ||
mixin: instrumentation._getMixinFunction(), | ||
}); | ||
|
||
return ogmaModule; | ||
}, | ||
), | ||
]; | ||
} | ||
|
||
override getConfig(): OgmaInstrumentationConfig { | ||
return this._config; | ||
} | ||
|
||
override setConfig(config: OgmaInstrumentationConfig) { | ||
this._config = config; | ||
} | ||
|
||
private _callHook(span: Span, record: Record<string, string>, level: string) { | ||
const hook = this.getConfig().logHook; | ||
|
||
if (!hook) { | ||
return; | ||
} | ||
|
||
safeExecuteInTheMiddle( | ||
() => hook(span, record, level), | ||
(err) => { | ||
if (err) { | ||
diag.error('@ogma/logger instrumentation: error calling logHook', err); | ||
} | ||
}, | ||
true, | ||
); | ||
} | ||
|
||
private _getMixinFunction() { | ||
const instrumentation = this; | ||
return function otelMixin(level: string) { | ||
if (!instrumentation.isEnabled()) { | ||
return {}; | ||
} | ||
|
||
const span = trace.getSpan(context.active()); | ||
|
||
if (!span) { | ||
return {}; | ||
} | ||
|
||
const spanContext = span.spanContext(); | ||
|
||
if (!isSpanContextValid(spanContext)) { | ||
return {}; | ||
} | ||
|
||
const record = { | ||
trace_id: spanContext.traceId, | ||
span_id: spanContext.spanId, | ||
trace_flags: `0${spanContext.traceFlags.toString(16)}`, | ||
}; | ||
|
||
instrumentation._callHook(span, record, level); | ||
|
||
return record; | ||
}; | ||
} | ||
} |
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 @@ | ||
import { Span } from '@opentelemetry/api'; | ||
import { InstrumentationConfig } from '@opentelemetry/instrumentation'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type LogHookFunction = (span: Span, record: Record<string, any>, level?: string) => void; | ||
|
||
export interface OgmaInstrumentationConfig extends InstrumentationConfig { | ||
logHook?: LogHookFunction; | ||
} |
Oops, something went wrong.