Skip to content

Commit

Permalink
feat: create instrumentation package with minimal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcdo29 committed Jul 23, 2024
1 parent 8f7ba40 commit 03a37fe
Show file tree
Hide file tree
Showing 14 changed files with 808 additions and 81 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-pianos-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ogma/instrumentation': minor
---

Initial release of @ogma/instrumentation
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# compiled output
lib/
dist/
tmp/
node_modules/
Expand Down
5 changes: 5 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
},
"docker-ready": {
"cache": true
},
"@nx/js:tsc": {
"cache": true,
"dependsOn": ["^build"],
"inputs": ["production", "^production"]
}
},
"nxCloudAccessToken": "OTZlM2FmYjYtMGQ2NS00OWMzLWJkOTQtYTBmYzE0N2Y0OTIyfHJlYWQtd3JpdGU=",
Expand Down
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,16 @@
"@nrwl/nest": "19.2.0",
"@nrwl/node": "19.2.0",
"@nrwl/workspace": "19.2.0",
"@nx/js": "19.2.0",
"@opentelemetry/api": "^1.3.0",
"@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",
"@swc-node/register": "~1.9.1",
"@swc/cli": "0.3.12",
"@swc/core": "1.5.7",
"@swc/helpers": "~0.5.11",
"@swc/register": "^0.1.10",
"@types/benchmark": "^2.1.4",
"@types/bunyan": "^1.8.10",
Expand Down Expand Up @@ -145,6 +153,7 @@
"remark-directive": "^3.0.0",
"rimraf": "^5.0.5",
"rxjs": "^7.8.1",
"semver": "^7.6.2",
"socket.io": "4.7.2",
"socket.io-client": "4.7.2",
"sonic-boom": "^3.7.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/instrumentation/.c8rc
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"
}
7 changes: 7 additions & 0 deletions packages/instrumentation/README.md
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.
19 changes: 19 additions & 0 deletions packages/instrumentation/package.json
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
}
28 changes: 28 additions & 0 deletions packages/instrumentation/project.json
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
}
}
}
}
1 change: 1 addition & 0 deletions packages/instrumentation/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/instrumentation';
93 changes: 93 additions & 0 deletions packages/instrumentation/src/lib/instrumentation.ts
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;
};
}
}
9 changes: 9 additions & 0 deletions packages/instrumentation/src/lib/types.ts
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;
}
Loading

0 comments on commit 03a37fe

Please sign in to comment.