@farm.js/otel 0.1.0-beta.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +74 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Farm.js Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Attributes } from "@opentelemetry/api";
|
|
2
|
+
import { type InstrumentationConfigMap } from "@opentelemetry/auto-instrumentations-node";
|
|
3
|
+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
4
|
+
import type { Instrumentation } from "@opentelemetry/instrumentation";
|
|
5
|
+
import { type Resource } from "@opentelemetry/resources";
|
|
6
|
+
import { NodeSDK } from "@opentelemetry/sdk-node";
|
|
7
|
+
import { type Sampler, type SpanExporter, type SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
8
|
+
export interface FarmOTelOptions {
|
|
9
|
+
serviceName?: string;
|
|
10
|
+
serviceVersion?: string;
|
|
11
|
+
resource?: Resource;
|
|
12
|
+
resourceAttributes?: Attributes;
|
|
13
|
+
traceExporter?: SpanExporter;
|
|
14
|
+
exporter?: ConstructorParameters<typeof OTLPTraceExporter>[0];
|
|
15
|
+
spanProcessors?: SpanProcessor[];
|
|
16
|
+
sampler?: Sampler;
|
|
17
|
+
autoInstrumentations?: boolean;
|
|
18
|
+
instrumentationConfig?: InstrumentationConfigMap;
|
|
19
|
+
instrumentations?: Array<Instrumentation | Instrumentation[]>;
|
|
20
|
+
}
|
|
21
|
+
export interface FarmOTelController {
|
|
22
|
+
sdk: NodeSDK;
|
|
23
|
+
forceFlush(): Promise<void>;
|
|
24
|
+
shutdown(): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
export declare function registerOTel(options?: FarmOTelOptions): Promise<FarmOTelController>;
|
|
27
|
+
export declare function forceFlushOTel(): Promise<void>;
|
|
28
|
+
export declare function shutdownOTel(): Promise<void>;
|
|
29
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAEL,KAAK,wBAAwB,EAC9B,MAAM,2CAA2C,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,yCAAyC,CAA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strumentations, } from "@opentelemetry/auto-instrumentations-node";
|
|
2
|
+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
3
|
+
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
4
|
+
import { NodeSDK } from "@opentelemetry/sdk-node";
|
|
5
|
+
import { BatchSpanProcessor, } from "@opentelemetry/sdk-trace-base";
|
|
6
|
+
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from "@opentelemetry/semantic-conventions";
|
|
7
|
+
const FARM_OTEL_STATE = Symbol.for("@farm.js/otel/state");
|
|
8
|
+
function getGlobalState() {
|
|
9
|
+
const target = globalThis;
|
|
10
|
+
return (target[FARM_OTEL_STATE] ??= {});
|
|
11
|
+
}
|
|
12
|
+
export async function registerOTel(options = {}) {
|
|
13
|
+
const state = getGlobalState();
|
|
14
|
+
if (state.controller)
|
|
15
|
+
return state.controller;
|
|
16
|
+
if (state.initializing)
|
|
17
|
+
return state.initializing;
|
|
18
|
+
state.initializing = Promise.resolve()
|
|
19
|
+
.then(() => {
|
|
20
|
+
const defaultResource = resourceFromAttributes({
|
|
21
|
+
[ATTR_SERVICE_NAME]: options.serviceName ?? process.env.OTEL_SERVICE_NAME ?? "farm-app",
|
|
22
|
+
...(options.serviceVersion ? { [ATTR_SERVICE_VERSION]: options.serviceVersion } : {}),
|
|
23
|
+
...options.resourceAttributes,
|
|
24
|
+
});
|
|
25
|
+
const resource = options.resource ? defaultResource.merge(options.resource) : defaultResource;
|
|
26
|
+
const spanProcessors = options.spanProcessors ?? [
|
|
27
|
+
new BatchSpanProcessor(options.traceExporter ?? new OTLPTraceExporter(options.exporter)),
|
|
28
|
+
];
|
|
29
|
+
const instrumentations = options.instrumentations ??
|
|
30
|
+
(options.autoInstrumentations === false
|
|
31
|
+
? []
|
|
32
|
+
: [getNodeAutoInstrumentations(options.instrumentationConfig)]);
|
|
33
|
+
const sdk = new NodeSDK({
|
|
34
|
+
resource,
|
|
35
|
+
sampler: options.sampler,
|
|
36
|
+
spanProcessors,
|
|
37
|
+
instrumentations,
|
|
38
|
+
});
|
|
39
|
+
sdk.start();
|
|
40
|
+
let shutdownPromise;
|
|
41
|
+
const controller = {
|
|
42
|
+
sdk,
|
|
43
|
+
async forceFlush() {
|
|
44
|
+
await Promise.all(spanProcessors.map((processor) => processor.forceFlush()));
|
|
45
|
+
},
|
|
46
|
+
shutdown() {
|
|
47
|
+
if (!shutdownPromise) {
|
|
48
|
+
shutdownPromise = sdk.shutdown().finally(() => {
|
|
49
|
+
state.controller = undefined;
|
|
50
|
+
state.initializing = undefined;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return shutdownPromise;
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
state.controller = controller;
|
|
57
|
+
return controller;
|
|
58
|
+
})
|
|
59
|
+
.catch((error) => {
|
|
60
|
+
state.initializing = undefined;
|
|
61
|
+
throw error;
|
|
62
|
+
});
|
|
63
|
+
return state.initializing;
|
|
64
|
+
}
|
|
65
|
+
export async function forceFlushOTel() {
|
|
66
|
+
const state = getGlobalState();
|
|
67
|
+
const controller = state.controller ?? (await state.initializing);
|
|
68
|
+
await controller?.forceFlush();
|
|
69
|
+
}
|
|
70
|
+
export async function shutdownOTel() {
|
|
71
|
+
const state = getGlobalState();
|
|
72
|
+
const controller = state.controller ?? (await state.initializing);
|
|
73
|
+
await controller?.shutdown();
|
|
74
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@farm.js/otel",
|
|
3
|
+
"version": "0.1.0-beta.25",
|
|
4
|
+
"description": "OpenTelemetry SDK setup for Farm.js applications",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/farming-labs/farm.js",
|
|
9
|
+
"directory": "packages/farm-otel"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"module": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@opentelemetry/api": "1.9.1",
|
|
30
|
+
"@opentelemetry/auto-instrumentations-node": "0.79.0",
|
|
31
|
+
"@opentelemetry/exporter-trace-otlp-http": "0.221.0",
|
|
32
|
+
"@opentelemetry/instrumentation": "0.221.0",
|
|
33
|
+
"@opentelemetry/resources": "2.10.0",
|
|
34
|
+
"@opentelemetry/sdk-node": "0.221.0",
|
|
35
|
+
"@opentelemetry/sdk-trace-base": "2.10.0",
|
|
36
|
+
"@opentelemetry/semantic-conventions": "1.43.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "^20.10.5",
|
|
40
|
+
"typescript": "^5.3.3"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=20.6.0"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc",
|
|
47
|
+
"dev": "tsc --watch",
|
|
48
|
+
"type-check": "tsc --noEmit",
|
|
49
|
+
"test": "pnpm run build && node --test test/*.test.mjs"
|
|
50
|
+
}
|
|
51
|
+
}
|