@ogcio/o11y-sdk-node 0.1.0-beta.1
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/CHANGELOG.md +8 -0
- package/coverage/cobertura-coverage.xml +199 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +131 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sdk-node/index.html +116 -0
- package/coverage/lcov-report/sdk-node/index.ts.html +106 -0
- package/coverage/lcov-report/sdk-node/lib/grpc.ts.html +178 -0
- package/coverage/lcov-report/sdk-node/lib/http.ts.html +190 -0
- package/coverage/lcov-report/sdk-node/lib/index.html +191 -0
- package/coverage/lcov-report/sdk-node/lib/index.ts.html +265 -0
- package/coverage/lcov-report/sdk-node/lib/instrumentation.node.ts.html +310 -0
- package/coverage/lcov-report/sdk-node/lib/options.ts.html +109 -0
- package/coverage/lcov-report/sdk-node/lib/utils.ts.html +115 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov.info +206 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +2 -0
- package/dist/lib/grpc.d.ts +3 -0
- package/dist/lib/grpc.js +26 -0
- package/dist/lib/http.d.ts +3 -0
- package/dist/lib/http.js +29 -0
- package/dist/lib/index.d.ts +46 -0
- package/dist/lib/index.js +1 -0
- package/dist/lib/instrumentation.node.d.ts +3 -0
- package/dist/lib/instrumentation.node.js +53 -0
- package/dist/lib/options.d.ts +7 -0
- package/dist/lib/options.js +1 -0
- package/dist/lib/utils.d.ts +3 -0
- package/dist/lib/utils.js +5 -0
- package/dist/vitest.config.d.ts +2 -0
- package/dist/vitest.config.js +25 -0
- package/index.ts +7 -0
- package/lib/grpc.ts +31 -0
- package/lib/http.ts +35 -0
- package/lib/index.ts +57 -0
- package/lib/instrumentation.node.ts +75 -0
- package/lib/options.ts +8 -0
- package/lib/utils.ts +10 -0
- package/package.json +49 -0
- package/test/index.test.ts +29 -0
- package/test/node-config.test.ts +113 -0
- package/test/validation.test.ts +66 -0
- package/test-report.xml +39 -0
- package/tsconfig.json +14 -0
- package/vitest.config.ts +26 -0
package/lib/http.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
2
|
+
import type { NodeSDKConfig } from "./index.js";
|
|
3
|
+
import type { Exporters } from "./options.js";
|
|
4
|
+
import { LogRecordProcessorMap } from "./utils.js";
|
|
5
|
+
import { CompressionAlgorithm } from "@opentelemetry/otlp-exporter-base";
|
|
6
|
+
import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";
|
|
7
|
+
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
|
|
8
|
+
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
|
|
9
|
+
|
|
10
|
+
export default function buildHttpExporters(config: NodeSDKConfig): Exporters {
|
|
11
|
+
if (config.collectorUrl.endsWith("/")) {
|
|
12
|
+
config.collectorUrl = config.collectorUrl.slice(0, -1);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
traces: new OTLPTraceExporter({
|
|
17
|
+
url: `${config.collectorUrl}/v1/traces`,
|
|
18
|
+
compression: CompressionAlgorithm.GZIP,
|
|
19
|
+
}),
|
|
20
|
+
metrics: new PeriodicExportingMetricReader({
|
|
21
|
+
exporter: new OTLPMetricExporter({
|
|
22
|
+
url: `${config.collectorUrl}/v1/metrics`,
|
|
23
|
+
compression: CompressionAlgorithm.GZIP,
|
|
24
|
+
}),
|
|
25
|
+
}),
|
|
26
|
+
logs: [
|
|
27
|
+
new LogRecordProcessorMap[config.collectorMode ?? "batch"](
|
|
28
|
+
new OTLPLogExporter({
|
|
29
|
+
url: `${config.collectorUrl}/v1/logs`,
|
|
30
|
+
compression: CompressionAlgorithm.GZIP,
|
|
31
|
+
}),
|
|
32
|
+
),
|
|
33
|
+
],
|
|
34
|
+
};
|
|
35
|
+
}
|
package/lib/index.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
interface SDKConfig {
|
|
2
|
+
/**
|
|
3
|
+
* The opentelemetry collector entrypoint GRPC url.
|
|
4
|
+
* If the collectoUrl is null or undefined, the instrumentation will not be activated.
|
|
5
|
+
* @example http://alloy:4317
|
|
6
|
+
*/
|
|
7
|
+
collectorUrl: string;
|
|
8
|
+
/**
|
|
9
|
+
* Name of your application used for the collector to group logs
|
|
10
|
+
*/
|
|
11
|
+
serviceName?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Diagnostic log level for the internal runtime instrumentation
|
|
14
|
+
*
|
|
15
|
+
* @type string
|
|
16
|
+
* @default INFO
|
|
17
|
+
*/
|
|
18
|
+
diagLogLevel?: SDKLogLevel;
|
|
19
|
+
/**
|
|
20
|
+
* Collector signals processing mode.
|
|
21
|
+
* signle: makes an http/grpc request for each signal and it is immediately processed inside grafana
|
|
22
|
+
* batch: sends multiple signals within a time window, optimized to reduce http/grpc calls in production
|
|
23
|
+
*
|
|
24
|
+
* @type string
|
|
25
|
+
* @default batch
|
|
26
|
+
*/
|
|
27
|
+
collectorMode?: SDKCollectorMode;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface NodeSDKConfig extends SDKConfig {
|
|
31
|
+
/**
|
|
32
|
+
* Flag to enable or disable the tracing for node:fs module
|
|
33
|
+
*
|
|
34
|
+
* @default false disabling `instrumentation-fs` because it bloating the traces
|
|
35
|
+
*/
|
|
36
|
+
enableFS?: boolean;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* http based connection protocol used to send signals.
|
|
40
|
+
*
|
|
41
|
+
* @default grpc
|
|
42
|
+
*/
|
|
43
|
+
protocol?: SDKProtocol;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type SDKCollectorMode = "single" | "batch";
|
|
47
|
+
|
|
48
|
+
export type SDKProtocol = "grpc" | "http";
|
|
49
|
+
|
|
50
|
+
export type SDKLogLevel =
|
|
51
|
+
| "NONE"
|
|
52
|
+
| "ERROR"
|
|
53
|
+
| "WARN"
|
|
54
|
+
| "INFO"
|
|
55
|
+
| "DEBUG"
|
|
56
|
+
| "VERBOSE"
|
|
57
|
+
| "ALL";
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { NodeSDK } from "@opentelemetry/sdk-node";
|
|
2
|
+
import type { NodeSDKConfig } from "./index.js";
|
|
3
|
+
import type { Exporters } from "./options.js";
|
|
4
|
+
import isUrl from "is-url";
|
|
5
|
+
import buildHttpExporters from "./http.js";
|
|
6
|
+
import buildGrpcExporters from "./grpc.js";
|
|
7
|
+
import { diag, DiagConsoleLogger, DiagLogLevel } from "@opentelemetry/api";
|
|
8
|
+
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
|
|
9
|
+
import { W3CTraceContextPropagator } from "@opentelemetry/core";
|
|
10
|
+
|
|
11
|
+
export default function buildNodeInstrumentation(
|
|
12
|
+
config?: NodeSDKConfig,
|
|
13
|
+
): NodeSDK | undefined {
|
|
14
|
+
if (!config) {
|
|
15
|
+
console.warn(
|
|
16
|
+
"observability config not set. Skipping NodeJS OpenTelemetry instrumentation.",
|
|
17
|
+
);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!config.collectorUrl) {
|
|
22
|
+
console.warn(
|
|
23
|
+
"collectorUrl not set. Skipping NodeJS OpenTelemetry instrumentation.",
|
|
24
|
+
);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!isUrl(config.collectorUrl)) {
|
|
29
|
+
console.error(
|
|
30
|
+
"collectorUrl does not use a valid format. Skipping NodeJS OpenTelemetry instrumentation.",
|
|
31
|
+
);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let exporter: Exporters;
|
|
36
|
+
|
|
37
|
+
if (config.protocol === "http") {
|
|
38
|
+
exporter = buildHttpExporters(config);
|
|
39
|
+
} else {
|
|
40
|
+
exporter = buildGrpcExporters(config);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
diag.setLogger(
|
|
45
|
+
new DiagConsoleLogger(),
|
|
46
|
+
config.diagLogLevel
|
|
47
|
+
? DiagLogLevel[config.diagLogLevel]
|
|
48
|
+
: DiagLogLevel.INFO,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const sdk = new NodeSDK({
|
|
52
|
+
serviceName: config.serviceName,
|
|
53
|
+
traceExporter: exporter.traces,
|
|
54
|
+
metricReader: exporter.metrics,
|
|
55
|
+
logRecordProcessors: exporter.logs,
|
|
56
|
+
textMapPropagator: new W3CTraceContextPropagator(),
|
|
57
|
+
instrumentations: [
|
|
58
|
+
getNodeAutoInstrumentations({
|
|
59
|
+
"@opentelemetry/instrumentation-fs": {
|
|
60
|
+
enabled: config.enableFS ?? false,
|
|
61
|
+
},
|
|
62
|
+
}),
|
|
63
|
+
],
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
sdk.start();
|
|
67
|
+
console.log("NodeJS OpenTelemetry instrumentation started successfully.");
|
|
68
|
+
return sdk;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.error(
|
|
71
|
+
"Error starting NodeJS OpenTelemetry instrumentation:",
|
|
72
|
+
error,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
}
|
package/lib/options.ts
ADDED
package/lib/utils.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { SDKCollectorMode } from "./index.js";
|
|
2
|
+
import { logs } from "@opentelemetry/sdk-node";
|
|
3
|
+
|
|
4
|
+
export const LogRecordProcessorMap: Record<
|
|
5
|
+
SDKCollectorMode,
|
|
6
|
+
typeof logs.SimpleLogRecordProcessor | typeof logs.BatchLogRecordProcessor
|
|
7
|
+
> = {
|
|
8
|
+
single: logs.SimpleLogRecordProcessor,
|
|
9
|
+
batch: logs.BatchLogRecordProcessor,
|
|
10
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ogcio/o11y-sdk-node",
|
|
3
|
+
"version": "0.1.0-beta.1",
|
|
4
|
+
"description": "Opentelemetry standard instrumentation SDK for NodeJS based project",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "rm -rf dist && tsc -p tsconfig.json",
|
|
9
|
+
"test": "vitest"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": "./dist/index.js",
|
|
13
|
+
"./*": "./dist/*.js"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"observability",
|
|
17
|
+
"o11y",
|
|
18
|
+
"opentelemetry",
|
|
19
|
+
"node",
|
|
20
|
+
"nodejs",
|
|
21
|
+
"ogcio"
|
|
22
|
+
],
|
|
23
|
+
"author": "team:ogcio/observability",
|
|
24
|
+
"license": "ISC",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@opentelemetry/api": "^1.9.0",
|
|
27
|
+
"@opentelemetry/auto-instrumentations-node": "^0.53.0",
|
|
28
|
+
"@opentelemetry/core": "1.28.0",
|
|
29
|
+
"@opentelemetry/exporter-logs-otlp-grpc": "^0.55.0",
|
|
30
|
+
"@opentelemetry/exporter-logs-otlp-http": "^0.55.0",
|
|
31
|
+
"@opentelemetry/exporter-metrics-otlp-grpc": "^0.55.0",
|
|
32
|
+
"@opentelemetry/exporter-metrics-otlp-http": "^0.55.0",
|
|
33
|
+
"@opentelemetry/exporter-trace-otlp-grpc": "^0.55.0",
|
|
34
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.55.0",
|
|
35
|
+
"@opentelemetry/instrumentation": "^0.55.0",
|
|
36
|
+
"@opentelemetry/otlp-exporter-base": "^0.55.0",
|
|
37
|
+
"@opentelemetry/sdk-metrics": "^1.28.0",
|
|
38
|
+
"@opentelemetry/sdk-node": "^0.55.0",
|
|
39
|
+
"is-url": "^1.2.4"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/is-url": "^1.2.32",
|
|
43
|
+
"@types/node": "^22.10.0",
|
|
44
|
+
"@vitest/coverage-v8": "^2.1.6",
|
|
45
|
+
"tsx": "^4.19.2",
|
|
46
|
+
"typescript": "^5.7.2",
|
|
47
|
+
"vitest": "^2.1.6"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, test, expect, vi } from "vitest";
|
|
2
|
+
import { NodeSDKConfig } from "../index";
|
|
3
|
+
import { instrumentNode } from "../index";
|
|
4
|
+
|
|
5
|
+
vi.mock("../lib/instrumentation.node", () => ({
|
|
6
|
+
default: vi.fn(),
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
describe("instrumentNode", () => {
|
|
10
|
+
test("should call buildNodeInstrumentation with the provided config", async () => {
|
|
11
|
+
const config: NodeSDKConfig = {
|
|
12
|
+
serviceName: "custom-service",
|
|
13
|
+
collectorUrl: "http://custom-collector.com",
|
|
14
|
+
protocol: "grpc",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const buildNodeInstrumentation = await import(
|
|
18
|
+
"../lib/instrumentation.node"
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
instrumentNode(config);
|
|
22
|
+
|
|
23
|
+
expect(buildNodeInstrumentation.default).toHaveBeenCalledWith(config);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("should not throw when called without arguments", () => {
|
|
27
|
+
expect(() => instrumentNode()).not.toThrow();
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { test, describe, assert, expect } from "vitest";
|
|
2
|
+
import buildNodeInstrumentation from "../lib/instrumentation.node.js";
|
|
3
|
+
import { NodeSDK, logs } from "@opentelemetry/sdk-node";
|
|
4
|
+
import { OTLPTraceExporter as GRPC_OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-grpc";
|
|
5
|
+
import { OTLPMetricExporter as GRPC_OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-grpc";
|
|
6
|
+
import { OTLPLogExporter as GRPC_OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-grpc";
|
|
7
|
+
|
|
8
|
+
import { OTLPTraceExporter as HTTP_OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
9
|
+
import { OTLPMetricExporter as HTTP_OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
|
|
10
|
+
import { OTLPLogExporter as HTTP_OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
|
|
11
|
+
import { NodeSDKConfig } from "../lib/index.js";
|
|
12
|
+
import buildHttpExporters from "../lib/http.js";
|
|
13
|
+
|
|
14
|
+
describe("verify config settings", () => {
|
|
15
|
+
const commonConfig = {
|
|
16
|
+
collectorUrl: "https://testurl.com",
|
|
17
|
+
serviceName: "test",
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
test("grpc config", () => {
|
|
21
|
+
const config: NodeSDKConfig = {
|
|
22
|
+
...commonConfig,
|
|
23
|
+
protocol: "grpc",
|
|
24
|
+
diagLogLevel: "NONE",
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const sdk: NodeSDK | undefined = buildNodeInstrumentation(config);
|
|
28
|
+
|
|
29
|
+
assert.ok(sdk);
|
|
30
|
+
|
|
31
|
+
const _configuration = sdk["_configuration"];
|
|
32
|
+
assert.equal(_configuration.serviceName, commonConfig.serviceName);
|
|
33
|
+
|
|
34
|
+
const traceExporter = _configuration.traceExporter;
|
|
35
|
+
assert.equal(
|
|
36
|
+
traceExporter["_transport"]["_parameters"]["compression"],
|
|
37
|
+
"gzip",
|
|
38
|
+
);
|
|
39
|
+
assert.ok(traceExporter instanceof GRPC_OTLPTraceExporter);
|
|
40
|
+
|
|
41
|
+
const logRecordProcessors = _configuration.logRecordProcessors;
|
|
42
|
+
assert.equal(logRecordProcessors.length, 1);
|
|
43
|
+
// assert default signals sending mode
|
|
44
|
+
assert.ok(logRecordProcessors[0] instanceof logs.BatchLogRecordProcessor);
|
|
45
|
+
assert.ok(
|
|
46
|
+
logRecordProcessors[0]["_exporter"] instanceof GRPC_OTLPLogExporter,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const metricReader = _configuration.metricReader;
|
|
50
|
+
assert.ok(metricReader._exporter instanceof GRPC_OTLPMetricExporter);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("http config", () => {
|
|
54
|
+
const config: NodeSDKConfig = {
|
|
55
|
+
...commonConfig,
|
|
56
|
+
protocol: "http",
|
|
57
|
+
diagLogLevel: "NONE",
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const sdk: NodeSDK | undefined = buildNodeInstrumentation(config);
|
|
61
|
+
assert.ok(sdk);
|
|
62
|
+
|
|
63
|
+
const _configuration = sdk["_configuration"];
|
|
64
|
+
assert.equal(_configuration.serviceName, commonConfig.serviceName);
|
|
65
|
+
|
|
66
|
+
const traceExporter = _configuration.traceExporter;
|
|
67
|
+
assert.equal(
|
|
68
|
+
traceExporter._transport._transport._parameters.compression,
|
|
69
|
+
"gzip",
|
|
70
|
+
);
|
|
71
|
+
assert.ok(traceExporter instanceof HTTP_OTLPTraceExporter);
|
|
72
|
+
|
|
73
|
+
const logRecordProcessors = _configuration.logRecordProcessors;
|
|
74
|
+
assert.equal(logRecordProcessors.length, 1);
|
|
75
|
+
// assert default signals sending mode
|
|
76
|
+
assert.ok(logRecordProcessors[0] instanceof logs.BatchLogRecordProcessor);
|
|
77
|
+
assert.ok(
|
|
78
|
+
logRecordProcessors[0]["_exporter"] instanceof HTTP_OTLPLogExporter,
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const metricReader = _configuration.metricReader;
|
|
82
|
+
assert.ok(metricReader._exporter instanceof HTTP_OTLPMetricExporter);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("single log sending config", () => {
|
|
86
|
+
const config: NodeSDKConfig = {
|
|
87
|
+
...commonConfig,
|
|
88
|
+
protocol: "grpc",
|
|
89
|
+
diagLogLevel: "NONE",
|
|
90
|
+
collectorMode: "single",
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const sdk: NodeSDK | undefined = buildNodeInstrumentation(config);
|
|
94
|
+
|
|
95
|
+
assert.ok(sdk);
|
|
96
|
+
|
|
97
|
+
const _configuration = sdk["_configuration"];
|
|
98
|
+
|
|
99
|
+
const logRecordProcessors = _configuration.logRecordProcessors;
|
|
100
|
+
assert.equal(logRecordProcessors.length, 1);
|
|
101
|
+
assert.ok(logRecordProcessors[0] instanceof logs.SimpleLogRecordProcessor);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("check if clear base endpoint final slash", () => {
|
|
105
|
+
const config: NodeSDKConfig = {
|
|
106
|
+
collectorUrl: "http://example.com/",
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
buildHttpExporters(config);
|
|
110
|
+
|
|
111
|
+
expect(config.collectorUrl).toBe("http://example.com");
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { NodeSDK } from "@opentelemetry/sdk-node";
|
|
2
|
+
import { test, describe, assert, vi, expect } from "vitest";
|
|
3
|
+
|
|
4
|
+
import buildNodeInstrumentation from "../lib/instrumentation.node.js";
|
|
5
|
+
import { instrumentNode } from "../index.js";
|
|
6
|
+
|
|
7
|
+
describe("validation config: should return without breaking the execution", () => {
|
|
8
|
+
test("should call buildNodeInstrumentation without config and skip instrumentation", async () => {
|
|
9
|
+
const consoleWarnSpy = vi
|
|
10
|
+
.spyOn(console, "warn")
|
|
11
|
+
.mockImplementation(vi.fn());
|
|
12
|
+
|
|
13
|
+
instrumentNode();
|
|
14
|
+
|
|
15
|
+
expect(consoleWarnSpy).toHaveBeenCalled();
|
|
16
|
+
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|
17
|
+
"observability config not set. Skipping NodeJS OpenTelemetry instrumentation.",
|
|
18
|
+
);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("node instrumentation: url undefined", async () => {
|
|
22
|
+
let sdk: NodeSDK | undefined = undefined;
|
|
23
|
+
|
|
24
|
+
const consoleWarnSpy = vi
|
|
25
|
+
.spyOn(console, "warn")
|
|
26
|
+
.mockImplementation(vi.fn());
|
|
27
|
+
const consoleLogSpy = vi.spyOn(console, "log").mockImplementation(vi.fn());
|
|
28
|
+
|
|
29
|
+
assert.doesNotThrow(() => {
|
|
30
|
+
sdk = buildNodeInstrumentation({
|
|
31
|
+
collectorUrl: undefined!,
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
assert.equal(sdk, undefined);
|
|
36
|
+
|
|
37
|
+
expect(consoleWarnSpy).toHaveBeenCalled();
|
|
38
|
+
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|
39
|
+
"collectorUrl not set. Skipping NodeJS OpenTelemetry instrumentation.",
|
|
40
|
+
);
|
|
41
|
+
expect(consoleLogSpy).not.toHaveBeenCalled();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("node instrumentation: invalid url", () => {
|
|
45
|
+
let sdk: NodeSDK | undefined = undefined;
|
|
46
|
+
|
|
47
|
+
const consoleErrorSpy = vi
|
|
48
|
+
.spyOn(console, "error")
|
|
49
|
+
.mockImplementation(vi.fn());
|
|
50
|
+
const consoleLogSpy = vi.spyOn(console, "log").mockImplementation(vi.fn());
|
|
51
|
+
|
|
52
|
+
assert.doesNotThrow(() => {
|
|
53
|
+
sdk = buildNodeInstrumentation({
|
|
54
|
+
collectorUrl: "notavalidURL",
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
assert.equal(sdk, undefined);
|
|
59
|
+
|
|
60
|
+
expect(consoleErrorSpy).toHaveBeenCalled();
|
|
61
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith(
|
|
62
|
+
"collectorUrl does not use a valid format. Skipping NodeJS OpenTelemetry instrumentation.",
|
|
63
|
+
);
|
|
64
|
+
expect(consoleLogSpy).not.toHaveBeenCalled();
|
|
65
|
+
});
|
|
66
|
+
});
|
package/test-report.xml
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8" ?>
|
|
2
|
+
<testsuites name="vitest tests" tests="9" failures="0" errors="0" time="0.781">
|
|
3
|
+
<testsuite name="test/index.test.ts" timestamp="2024-11-26T09:25:25.586Z" hostname="ABozzelli-ITMAC24" tests="2" failures="0" errors="0" skipped="0" time="0.002388">
|
|
4
|
+
<testcase classname="test/index.test.ts" name="instrumentNode > should call buildNodeInstrumentation with the provided config" time="0.001363792">
|
|
5
|
+
</testcase>
|
|
6
|
+
<testcase classname="test/index.test.ts" name="instrumentNode > should not throw when called without arguments" time="0.000454542">
|
|
7
|
+
</testcase>
|
|
8
|
+
</testsuite>
|
|
9
|
+
<testsuite name="test/node-config.test.ts" timestamp="2024-11-26T09:25:25.587Z" hostname="ABozzelli-ITMAC24" tests="4" failures="0" errors="0" skipped="0" time="0.042719333">
|
|
10
|
+
<testcase classname="test/node-config.test.ts" name="verify config settings > grpc config" time="0.017177333">
|
|
11
|
+
<system-out>
|
|
12
|
+
NodeJS OpenTelemetry instrumentation started successfully.
|
|
13
|
+
|
|
14
|
+
</system-out>
|
|
15
|
+
</testcase>
|
|
16
|
+
<testcase classname="test/node-config.test.ts" name="verify config settings > http config" time="0.016206917">
|
|
17
|
+
<system-out>
|
|
18
|
+
NodeJS OpenTelemetry instrumentation started successfully.
|
|
19
|
+
|
|
20
|
+
</system-out>
|
|
21
|
+
</testcase>
|
|
22
|
+
<testcase classname="test/node-config.test.ts" name="verify config settings > single log sending config" time="0.008346875">
|
|
23
|
+
<system-out>
|
|
24
|
+
NodeJS OpenTelemetry instrumentation started successfully.
|
|
25
|
+
|
|
26
|
+
</system-out>
|
|
27
|
+
</testcase>
|
|
28
|
+
<testcase classname="test/node-config.test.ts" name="verify config settings > check if clear base endpoint final slash" time="0.000374">
|
|
29
|
+
</testcase>
|
|
30
|
+
</testsuite>
|
|
31
|
+
<testsuite name="test/validation.test.ts" timestamp="2024-11-26T09:25:25.587Z" hostname="ABozzelli-ITMAC24" tests="3" failures="0" errors="0" skipped="0" time="0.003302375">
|
|
32
|
+
<testcase classname="test/validation.test.ts" name="validation config: should return without breaking the execution > should call buildNodeInstrumentation without config and skip instrumentation" time="0.00188275">
|
|
33
|
+
</testcase>
|
|
34
|
+
<testcase classname="test/validation.test.ts" name="validation config: should return without breaking the execution > node instrumentation: url undefined" time="0.000551875">
|
|
35
|
+
</testcase>
|
|
36
|
+
<testcase classname="test/validation.test.ts" name="validation config: should return without breaking the execution > node instrumentation: invalid url" time="0.000253958">
|
|
37
|
+
</testcase>
|
|
38
|
+
</testsuite>
|
|
39
|
+
</testsuites>
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
|
4
|
+
"module": "NodeNext" /* Specify what module code is generated. */,
|
|
5
|
+
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
|
|
6
|
+
"moduleResolution": "NodeNext",
|
|
7
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
|
|
8
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
9
|
+
"strict": true /* Enable all strict type-checking options. */,
|
|
10
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */,
|
|
11
|
+
"declaration": true
|
|
12
|
+
},
|
|
13
|
+
"exclude": ["**/test/**"]
|
|
14
|
+
}
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { defineConfig } from "vitest/config";
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
test: {
|
|
5
|
+
globals: true,
|
|
6
|
+
watch: false,
|
|
7
|
+
include: ["**/test/*.test.ts"],
|
|
8
|
+
exclude: ["**/fixtures/**", "**/dist/**"],
|
|
9
|
+
poolOptions: {
|
|
10
|
+
threads: {
|
|
11
|
+
maxThreads: 8,
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
clearMocks: true,
|
|
15
|
+
testTimeout: 30_000,
|
|
16
|
+
coverage: {
|
|
17
|
+
enabled: true,
|
|
18
|
+
provider: "v8",
|
|
19
|
+
reportsDirectory: "coverage",
|
|
20
|
+
reporter: ["lcov", "cobertura"],
|
|
21
|
+
clean: true,
|
|
22
|
+
},
|
|
23
|
+
reporters: ["default", ["junit", { outputFile: "test-report.xml" }]],
|
|
24
|
+
environment: "node",
|
|
25
|
+
},
|
|
26
|
+
});
|