@ogcio/o11y-sdk-node 0.1.0-beta.1 → 0.1.0-beta.11
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 +81 -0
- package/README.md +222 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -0
- package/dist/lib/exporter/console.d.ts +3 -0
- package/dist/lib/exporter/console.js +20 -0
- package/dist/lib/exporter/grpc.d.ts +3 -0
- package/dist/lib/exporter/grpc.js +41 -0
- package/dist/lib/exporter/http.d.ts +3 -0
- package/dist/lib/{http.js → exporter/http.js} +15 -9
- package/dist/lib/exporter/index.d.ts +8 -0
- package/dist/lib/index.d.ts +36 -6
- package/dist/lib/instrumentation.node.d.ts +1 -1
- package/dist/lib/instrumentation.node.js +47 -23
- package/dist/lib/metrics.d.ts +18 -0
- package/dist/lib/metrics.js +24 -0
- package/dist/lib/processor/enrich-logger-processor.d.ts +10 -0
- package/dist/lib/processor/enrich-logger-processor.js +19 -0
- package/dist/lib/processor/enrich-span-processor.d.ts +11 -0
- package/dist/lib/processor/enrich-span-processor.js +22 -0
- package/dist/lib/resource.d.ts +7 -0
- package/dist/lib/resource.js +18 -0
- package/dist/lib/traces.d.ts +1 -0
- package/dist/lib/traces.js +4 -0
- package/dist/lib/url-sampler.d.ts +10 -0
- package/dist/lib/url-sampler.js +25 -0
- package/dist/lib/utils.d.ts +4 -2
- package/dist/lib/utils.js +8 -3
- package/dist/package.json +58 -0
- package/dist/vitest.config.js +15 -1
- package/index.ts +4 -2
- package/lib/exporter/console.ts +31 -0
- package/lib/exporter/grpc.ts +52 -0
- package/lib/{http.ts → exporter/http.ts} +19 -11
- package/lib/exporter/index.ts +9 -0
- package/lib/index.ts +46 -5
- package/lib/instrumentation.node.ts +61 -28
- package/lib/metrics.ts +75 -0
- package/lib/processor/enrich-logger-processor.ts +34 -0
- package/lib/processor/enrich-span-processor.ts +39 -0
- package/lib/resource.ts +30 -0
- package/lib/traces.ts +5 -0
- package/lib/url-sampler.ts +52 -0
- package/lib/utils.ts +16 -4
- package/package.json +33 -25
- package/test/index.test.ts +22 -12
- package/test/integration/README.md +26 -0
- package/test/integration/integration.test.ts +58 -0
- package/test/integration/run.sh +88 -0
- package/test/metrics.test.ts +142 -0
- package/test/node-config.test.ts +76 -44
- package/test/processor/enrich-logger-processor.test.ts +58 -0
- package/test/processor/enrich-span-processor.test.ts +104 -0
- package/test/resource.test.ts +33 -0
- package/test/url-sampler.test.ts +215 -0
- package/test/utils/alloy-log-parser.ts +46 -0
- package/test/validation.test.ts +48 -11
- package/tsconfig.json +2 -1
- package/vitest.config.ts +15 -1
- package/coverage/cobertura-coverage.xml +0 -199
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -87
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +0 -131
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sdk-node/index.html +0 -116
- package/coverage/lcov-report/sdk-node/index.ts.html +0 -106
- package/coverage/lcov-report/sdk-node/lib/grpc.ts.html +0 -178
- package/coverage/lcov-report/sdk-node/lib/http.ts.html +0 -190
- package/coverage/lcov-report/sdk-node/lib/index.html +0 -191
- package/coverage/lcov-report/sdk-node/lib/index.ts.html +0 -265
- package/coverage/lcov-report/sdk-node/lib/instrumentation.node.ts.html +0 -310
- package/coverage/lcov-report/sdk-node/lib/options.ts.html +0 -109
- package/coverage/lcov-report/sdk-node/lib/utils.ts.html +0 -115
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -196
- package/coverage/lcov.info +0 -206
- package/dist/lib/grpc.d.ts +0 -3
- package/dist/lib/grpc.js +0 -26
- package/dist/lib/http.d.ts +0 -3
- package/dist/lib/options.d.ts +0 -7
- package/lib/grpc.ts +0 -31
- package/lib/options.ts +0 -8
- package/test-report.xml +0 -39
- /package/dist/lib/{options.js → exporter/index.js} +0 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { LogRecord, LogRecordProcessor } from "@opentelemetry/sdk-logs";
|
|
2
|
+
import { Context } from "@opentelemetry/api";
|
|
3
|
+
import { SignalAttributeValue } from "../index.js";
|
|
4
|
+
export declare class EnrichLogProcessor implements LogRecordProcessor {
|
|
5
|
+
private _spanAttributes?;
|
|
6
|
+
constructor(spanAttributes?: Record<string, SignalAttributeValue | (() => SignalAttributeValue)>);
|
|
7
|
+
forceFlush(): Promise<void>;
|
|
8
|
+
onEmit(logRecord: LogRecord, _context?: Context): void;
|
|
9
|
+
shutdown(): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export class EnrichLogProcessor {
|
|
2
|
+
_spanAttributes = undefined;
|
|
3
|
+
constructor(spanAttributes) {
|
|
4
|
+
this._spanAttributes = spanAttributes;
|
|
5
|
+
}
|
|
6
|
+
forceFlush() {
|
|
7
|
+
return Promise.resolve();
|
|
8
|
+
}
|
|
9
|
+
onEmit(logRecord, _context) {
|
|
10
|
+
if (this._spanAttributes) {
|
|
11
|
+
for (const [key, value] of Object.entries(this._spanAttributes)) {
|
|
12
|
+
logRecord.setAttribute(key, typeof value === "function" ? value() : value);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
shutdown() {
|
|
17
|
+
return Promise.resolve();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Context } from "@opentelemetry/api";
|
|
2
|
+
import { ReadableSpan, Span, SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
3
|
+
import { SignalAttributeValue } from "../index.js";
|
|
4
|
+
export declare class EnrichSpanProcessor implements SpanProcessor {
|
|
5
|
+
private _spanAttributes;
|
|
6
|
+
constructor(spanAttributes?: Record<string, SignalAttributeValue | (() => SignalAttributeValue)>);
|
|
7
|
+
forceFlush(): Promise<void>;
|
|
8
|
+
onStart(span: Span, _context: Context): void;
|
|
9
|
+
onEnd(_span: ReadableSpan): void;
|
|
10
|
+
shutdown(): Promise<void>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export class EnrichSpanProcessor {
|
|
2
|
+
_spanAttributes;
|
|
3
|
+
constructor(spanAttributes) {
|
|
4
|
+
this._spanAttributes = spanAttributes;
|
|
5
|
+
}
|
|
6
|
+
forceFlush() {
|
|
7
|
+
return Promise.resolve();
|
|
8
|
+
}
|
|
9
|
+
onStart(span, _context) {
|
|
10
|
+
if (this._spanAttributes != undefined) {
|
|
11
|
+
for (const [key, value] of Object.entries(this._spanAttributes)) {
|
|
12
|
+
span.setAttribute(key, typeof value === "function" ? value() : value);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
onEnd(_span) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
shutdown() {
|
|
20
|
+
return Promise.resolve();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ResourceDetector, DetectedResource } from "@opentelemetry/resources";
|
|
2
|
+
import { SignalAttributeValue } from "./index.js";
|
|
3
|
+
export declare class ObservabilityResourceDetector implements ResourceDetector {
|
|
4
|
+
private _resourceAttributes;
|
|
5
|
+
constructor(resourceAttributes?: Record<string, SignalAttributeValue>);
|
|
6
|
+
detect(): DetectedResource;
|
|
7
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import packageJson from "../package.json" with { type: "json" };
|
|
2
|
+
export class ObservabilityResourceDetector {
|
|
3
|
+
_resourceAttributes;
|
|
4
|
+
constructor(resourceAttributes) {
|
|
5
|
+
this._resourceAttributes = resourceAttributes;
|
|
6
|
+
}
|
|
7
|
+
detect() {
|
|
8
|
+
let attributes = {};
|
|
9
|
+
if (this._resourceAttributes) {
|
|
10
|
+
attributes = {
|
|
11
|
+
...this._resourceAttributes,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
attributes["o11y.sdk.name"] = packageJson.name;
|
|
15
|
+
attributes["o11y.sdk.version"] = packageJson.version;
|
|
16
|
+
return { attributes };
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getActiveSpan(): import("@opentelemetry/api").Span | undefined;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Attributes, Context, Link, SpanKind } from "@opentelemetry/api";
|
|
2
|
+
import { Sampler, SamplingResult } from "@opentelemetry/sdk-trace-base";
|
|
3
|
+
import { SamplerCondition } from "./index.js";
|
|
4
|
+
export declare class UrlSampler implements Sampler {
|
|
5
|
+
private _samplerCondition;
|
|
6
|
+
private _nextSampler;
|
|
7
|
+
constructor(samplerCondition: SamplerCondition[] | undefined, nextSampler: Sampler);
|
|
8
|
+
shouldSample(_context: Context, traceId: string, _spanName: string, _spanKind: SpanKind, attributes: Attributes, _links: Link[]): SamplingResult;
|
|
9
|
+
toString(): string;
|
|
10
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { SamplingDecision, } from "@opentelemetry/sdk-trace-base";
|
|
2
|
+
export class UrlSampler {
|
|
3
|
+
_samplerCondition;
|
|
4
|
+
_nextSampler;
|
|
5
|
+
constructor(samplerCondition = [], nextSampler) {
|
|
6
|
+
this._samplerCondition = samplerCondition;
|
|
7
|
+
this._nextSampler = nextSampler;
|
|
8
|
+
}
|
|
9
|
+
shouldSample(_context, traceId, _spanName, _spanKind, attributes, _links) {
|
|
10
|
+
const url = attributes["http.target"]?.toString();
|
|
11
|
+
if (url) {
|
|
12
|
+
for (const condition of this._samplerCondition) {
|
|
13
|
+
if ((condition.type === "equals" && url === condition.url) ||
|
|
14
|
+
(condition.type === "endsWith" && url.endsWith(condition.url)) ||
|
|
15
|
+
(condition.type === "includes" && url.includes(condition.url))) {
|
|
16
|
+
return { decision: SamplingDecision.NOT_RECORD };
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return this._nextSampler.shouldSample(_context, traceId, _spanName, _spanKind, attributes, _links);
|
|
21
|
+
}
|
|
22
|
+
toString() {
|
|
23
|
+
return "UrlSampler";
|
|
24
|
+
}
|
|
25
|
+
}
|
package/dist/lib/utils.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { BatchLogRecordProcessor, SimpleLogRecordProcessor } from "@opentelemetry/sdk-logs";
|
|
1
2
|
import { SDKCollectorMode } from "./index.js";
|
|
2
|
-
import {
|
|
3
|
-
export declare const LogRecordProcessorMap: Record<SDKCollectorMode, typeof
|
|
3
|
+
import { tracing } from "@opentelemetry/sdk-node";
|
|
4
|
+
export declare const LogRecordProcessorMap: Record<SDKCollectorMode, typeof SimpleLogRecordProcessor | typeof BatchLogRecordProcessor>;
|
|
5
|
+
export declare const SpanProcessorMap: Record<SDKCollectorMode, typeof tracing.SimpleSpanProcessor | typeof tracing.BatchSpanProcessor>;
|
package/dist/lib/utils.js
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BatchLogRecordProcessor, SimpleLogRecordProcessor, } from "@opentelemetry/sdk-logs";
|
|
2
|
+
import { tracing } from "@opentelemetry/sdk-node";
|
|
2
3
|
export const LogRecordProcessorMap = {
|
|
3
|
-
single:
|
|
4
|
-
batch:
|
|
4
|
+
single: SimpleLogRecordProcessor,
|
|
5
|
+
batch: BatchLogRecordProcessor,
|
|
6
|
+
};
|
|
7
|
+
export const SpanProcessorMap = {
|
|
8
|
+
single: tracing.SimpleSpanProcessor,
|
|
9
|
+
batch: tracing.BatchSpanProcessor,
|
|
5
10
|
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ogcio/o11y-sdk-node",
|
|
3
|
+
"version": "0.1.0-beta.11",
|
|
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
|
+
"prepublishOnly": "pnpm i && pnpm build",
|
|
11
|
+
"test:unit": "vitest --project unit",
|
|
12
|
+
"test:integration": "pnpm --filter @ogcio/o11y run prepare:integration && vitest --project integration",
|
|
13
|
+
"test:integration:dryrun": "vitest --project integration"
|
|
14
|
+
},
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./dist/index.js",
|
|
17
|
+
"./*": "./dist/*.js"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"observability",
|
|
21
|
+
"o11y",
|
|
22
|
+
"opentelemetry",
|
|
23
|
+
"node",
|
|
24
|
+
"nodejs",
|
|
25
|
+
"ogcio"
|
|
26
|
+
],
|
|
27
|
+
"author": "team:ogcio/observability",
|
|
28
|
+
"license": "ISC",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@grpc/grpc-js": "^1.13.4",
|
|
31
|
+
"@opentelemetry/api": "^1.9.0",
|
|
32
|
+
"@opentelemetry/auto-instrumentations-node": "^0.60.0",
|
|
33
|
+
"@opentelemetry/core": "^2.0.1",
|
|
34
|
+
"@opentelemetry/exporter-logs-otlp-grpc": "^0.202.0",
|
|
35
|
+
"@opentelemetry/exporter-logs-otlp-http": "^0.202.0",
|
|
36
|
+
"@opentelemetry/exporter-metrics-otlp-grpc": "^0.202.0",
|
|
37
|
+
"@opentelemetry/exporter-metrics-otlp-http": "^0.202.0",
|
|
38
|
+
"@opentelemetry/exporter-trace-otlp-grpc": "^0.202.0",
|
|
39
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.202.0",
|
|
40
|
+
"@opentelemetry/instrumentation": "^0.202.0",
|
|
41
|
+
"@opentelemetry/otlp-exporter-base": "^0.202.0",
|
|
42
|
+
"@opentelemetry/resources": "^2.0.1",
|
|
43
|
+
"@opentelemetry/sdk-logs": "^0.202.0",
|
|
44
|
+
"@opentelemetry/sdk-metrics": "^2.0.1",
|
|
45
|
+
"@opentelemetry/sdk-node": "^0.202.0",
|
|
46
|
+
"@opentelemetry/sdk-trace-base": "^2.0.1"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/node": "^22.15.29",
|
|
50
|
+
"@vitest/coverage-v8": "^3.2.1",
|
|
51
|
+
"tsx": "^4.19.4",
|
|
52
|
+
"typescript": "^5.8.3",
|
|
53
|
+
"vitest": "^3.2.1"
|
|
54
|
+
},
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=20.6.0"
|
|
57
|
+
}
|
|
58
|
+
}
|
package/dist/vitest.config.js
CHANGED
|
@@ -3,7 +3,6 @@ export default defineConfig({
|
|
|
3
3
|
test: {
|
|
4
4
|
globals: true,
|
|
5
5
|
watch: false,
|
|
6
|
-
include: ["**/test/*.test.ts"],
|
|
7
6
|
exclude: ["**/fixtures/**", "**/dist/**"],
|
|
8
7
|
poolOptions: {
|
|
9
8
|
threads: {
|
|
@@ -21,5 +20,20 @@ export default defineConfig({
|
|
|
21
20
|
},
|
|
22
21
|
reporters: ["default", ["junit", { outputFile: "test-report.xml" }]],
|
|
23
22
|
environment: "node",
|
|
23
|
+
pool: "threads",
|
|
24
|
+
workspace: [
|
|
25
|
+
{
|
|
26
|
+
test: {
|
|
27
|
+
include: ["**/test/*.test.ts", "**/test/processor/*.test.ts"],
|
|
28
|
+
name: "unit",
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
test: {
|
|
33
|
+
include: ["**/test/integration/*.test.ts"],
|
|
34
|
+
name: "integration",
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
],
|
|
24
38
|
},
|
|
25
39
|
});
|
package/index.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { NodeSDK } from "@opentelemetry/sdk-node";
|
|
2
|
-
import type { NodeSDKConfig } from "./lib/index.js";
|
|
3
2
|
import buildNodeInstrumentation from "./lib/instrumentation.node.js";
|
|
4
3
|
|
|
5
4
|
export type * from "./lib/index.js";
|
|
6
|
-
export type {
|
|
5
|
+
export type { NodeSDK };
|
|
7
6
|
export { buildNodeInstrumentation as instrumentNode };
|
|
7
|
+
|
|
8
|
+
export * from "./lib/metrics.js";
|
|
9
|
+
export * from "./lib/traces.js";
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConsoleLogRecordExporter,
|
|
3
|
+
SimpleLogRecordProcessor,
|
|
4
|
+
} from "@opentelemetry/sdk-logs";
|
|
5
|
+
import { metrics } from "@opentelemetry/sdk-node";
|
|
6
|
+
import {
|
|
7
|
+
ConsoleSpanExporter,
|
|
8
|
+
SimpleSpanProcessor,
|
|
9
|
+
} from "@opentelemetry/sdk-trace-base";
|
|
10
|
+
import { NodeSDKConfig } from "../index.js";
|
|
11
|
+
import { Exporters } from "./index.js";
|
|
12
|
+
import { EnrichSpanProcessor } from "../processor/enrich-span-processor.js";
|
|
13
|
+
import { EnrichLogProcessor } from "../processor/enrich-logger-processor.js";
|
|
14
|
+
|
|
15
|
+
export default function buildConsoleExporters(
|
|
16
|
+
config: NodeSDKConfig,
|
|
17
|
+
): Exporters {
|
|
18
|
+
return {
|
|
19
|
+
spans: [
|
|
20
|
+
new SimpleSpanProcessor(new ConsoleSpanExporter()),
|
|
21
|
+
new EnrichSpanProcessor(config.spanAttributes),
|
|
22
|
+
],
|
|
23
|
+
metrics: new metrics.PeriodicExportingMetricReader({
|
|
24
|
+
exporter: new metrics.ConsoleMetricExporter(),
|
|
25
|
+
}),
|
|
26
|
+
logs: [
|
|
27
|
+
new EnrichLogProcessor(config.spanAttributes),
|
|
28
|
+
new SimpleLogRecordProcessor(new ConsoleLogRecordExporter()),
|
|
29
|
+
],
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { metrics } from "@opentelemetry/sdk-node";
|
|
2
|
+
import { CompressionAlgorithm } from "@opentelemetry/otlp-exporter-base";
|
|
3
|
+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-grpc";
|
|
4
|
+
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-grpc";
|
|
5
|
+
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-grpc";
|
|
6
|
+
import { NodeSDKConfig } from "../index.js";
|
|
7
|
+
import { Exporters } from "./index.js";
|
|
8
|
+
import { LogRecordProcessorMap, SpanProcessorMap } from "../utils.js";
|
|
9
|
+
import { EnrichSpanProcessor } from "../processor/enrich-span-processor.js";
|
|
10
|
+
import { EnrichLogProcessor } from "../processor/enrich-logger-processor.js";
|
|
11
|
+
|
|
12
|
+
async function defaultMetadata() {
|
|
13
|
+
const { Metadata } = await import("@grpc/grpc-js");
|
|
14
|
+
|
|
15
|
+
return new Metadata({
|
|
16
|
+
waitForReady: true,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default async function buildGrpcExporters(
|
|
21
|
+
config: NodeSDKConfig,
|
|
22
|
+
): Promise<Exporters> {
|
|
23
|
+
return {
|
|
24
|
+
spans: [
|
|
25
|
+
new SpanProcessorMap[config.collectorMode ?? "batch"](
|
|
26
|
+
new OTLPTraceExporter({
|
|
27
|
+
url: `${config.collectorUrl}`,
|
|
28
|
+
compression: CompressionAlgorithm.GZIP,
|
|
29
|
+
metadata: config.grpcMetadata ?? (await defaultMetadata()),
|
|
30
|
+
}),
|
|
31
|
+
),
|
|
32
|
+
new EnrichSpanProcessor(config.spanAttributes),
|
|
33
|
+
],
|
|
34
|
+
metrics: new metrics.PeriodicExportingMetricReader({
|
|
35
|
+
exporter: new OTLPMetricExporter({
|
|
36
|
+
url: `${config.collectorUrl}`,
|
|
37
|
+
compression: CompressionAlgorithm.GZIP,
|
|
38
|
+
metadata: config.grpcMetadata ?? (await defaultMetadata()),
|
|
39
|
+
}),
|
|
40
|
+
}),
|
|
41
|
+
logs: [
|
|
42
|
+
new EnrichLogProcessor(config.spanAttributes),
|
|
43
|
+
new LogRecordProcessorMap[config.collectorMode ?? "batch"](
|
|
44
|
+
new OTLPLogExporter({
|
|
45
|
+
url: `${config.collectorUrl}`,
|
|
46
|
+
compression: CompressionAlgorithm.GZIP,
|
|
47
|
+
metadata: config.grpcMetadata ?? (await defaultMetadata()),
|
|
48
|
+
}),
|
|
49
|
+
),
|
|
50
|
+
],
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { NodeSDKConfig } from "./index.js";
|
|
3
|
-
import type { Exporters } from "./options.js";
|
|
4
|
-
import { LogRecordProcessorMap } from "./utils.js";
|
|
1
|
+
import { metrics } from "@opentelemetry/sdk-node";
|
|
5
2
|
import { CompressionAlgorithm } from "@opentelemetry/otlp-exporter-base";
|
|
6
|
-
import {
|
|
7
|
-
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
|
|
3
|
+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
8
4
|
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
|
|
5
|
+
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-http";
|
|
6
|
+
import { LogRecordProcessorMap, SpanProcessorMap } from "../utils.js";
|
|
7
|
+
import { EnrichSpanProcessor } from "../processor/enrich-span-processor.js";
|
|
8
|
+
import { Exporters } from "./index.js";
|
|
9
|
+
import { NodeSDKConfig } from "../index.js";
|
|
10
|
+
import { EnrichLogProcessor } from "../processor/enrich-logger-processor.js";
|
|
9
11
|
|
|
10
12
|
export default function buildHttpExporters(config: NodeSDKConfig): Exporters {
|
|
11
13
|
if (config.collectorUrl.endsWith("/")) {
|
|
@@ -13,17 +15,23 @@ export default function buildHttpExporters(config: NodeSDKConfig): Exporters {
|
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
return {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
spans: [
|
|
19
|
+
new SpanProcessorMap[config.collectorMode ?? "batch"](
|
|
20
|
+
new OTLPTraceExporter({
|
|
21
|
+
url: `${config.collectorUrl}/v1/traces`,
|
|
22
|
+
compression: CompressionAlgorithm.GZIP,
|
|
23
|
+
}),
|
|
24
|
+
),
|
|
25
|
+
new EnrichSpanProcessor(config.spanAttributes),
|
|
26
|
+
],
|
|
27
|
+
metrics: new metrics.PeriodicExportingMetricReader({
|
|
21
28
|
exporter: new OTLPMetricExporter({
|
|
22
29
|
url: `${config.collectorUrl}/v1/metrics`,
|
|
23
30
|
compression: CompressionAlgorithm.GZIP,
|
|
24
31
|
}),
|
|
25
32
|
}),
|
|
26
33
|
logs: [
|
|
34
|
+
new EnrichLogProcessor(config.spanAttributes),
|
|
27
35
|
new LogRecordProcessorMap[config.collectorMode ?? "batch"](
|
|
28
36
|
new OTLPLogExporter({
|
|
29
37
|
url: `${config.collectorUrl}/v1/logs`,
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { LogRecordProcessor } from "@opentelemetry/sdk-logs";
|
|
2
|
+
import type { metrics } from "@opentelemetry/sdk-node";
|
|
3
|
+
import { SpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
4
|
+
|
|
5
|
+
export type Exporters = {
|
|
6
|
+
spans: SpanProcessor[];
|
|
7
|
+
metrics: metrics.MetricReader;
|
|
8
|
+
logs: LogRecordProcessor[];
|
|
9
|
+
};
|
package/lib/index.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import type { Metadata } from "@grpc/grpc-js";
|
|
2
|
+
|
|
3
|
+
export interface NodeSDKConfig {
|
|
2
4
|
/**
|
|
3
5
|
* The opentelemetry collector entrypoint GRPC url.
|
|
4
6
|
* If the collectoUrl is null or undefined, the instrumentation will not be activated.
|
|
@@ -25,9 +27,34 @@ interface SDKConfig {
|
|
|
25
27
|
* @default batch
|
|
26
28
|
*/
|
|
27
29
|
collectorMode?: SDKCollectorMode;
|
|
28
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Array of not traced urls.
|
|
32
|
+
*
|
|
33
|
+
* @type {SamplerCondition}
|
|
34
|
+
* @default []
|
|
35
|
+
*/
|
|
36
|
+
ignoreUrls?: SamplerCondition[];
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Object containing static properties or functions used to evaluate custom attributes for every logs and traces.
|
|
40
|
+
*/
|
|
41
|
+
spanAttributes?: Record<
|
|
42
|
+
string,
|
|
43
|
+
SignalAttributeValue | (() => SignalAttributeValue)
|
|
44
|
+
>;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Object containing static properties used as resources attributes for the Node SDK initialization.
|
|
48
|
+
*/
|
|
49
|
+
resourceAttributes?: Record<string, SignalAttributeValue>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Faction value from 0 to 1, used by TraceIdRatioBasedSampler which it deterministically samples a percentage of traces that you pass in as a parameter.
|
|
53
|
+
*
|
|
54
|
+
* @default 1
|
|
55
|
+
*/
|
|
56
|
+
traceRatio?: number;
|
|
29
57
|
|
|
30
|
-
export interface NodeSDKConfig extends SDKConfig {
|
|
31
58
|
/**
|
|
32
59
|
* Flag to enable or disable the tracing for node:fs module
|
|
33
60
|
*
|
|
@@ -36,16 +63,30 @@ export interface NodeSDKConfig extends SDKConfig {
|
|
|
36
63
|
enableFS?: boolean;
|
|
37
64
|
|
|
38
65
|
/**
|
|
39
|
-
*
|
|
66
|
+
* Protocol used to send signals.
|
|
40
67
|
*
|
|
41
68
|
* @default grpc
|
|
42
69
|
*/
|
|
43
70
|
protocol?: SDKProtocol;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Grpc Metadata for the grpc-js client.
|
|
74
|
+
*
|
|
75
|
+
* @default { waitForReady: true }
|
|
76
|
+
*/
|
|
77
|
+
grpcMetadata?: Metadata;
|
|
44
78
|
}
|
|
45
79
|
|
|
80
|
+
export interface SamplerCondition {
|
|
81
|
+
type: "endsWith" | "includes" | "equals";
|
|
82
|
+
url: string;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export type SignalAttributeValue = string | number | boolean;
|
|
86
|
+
|
|
46
87
|
export type SDKCollectorMode = "single" | "batch";
|
|
47
88
|
|
|
48
|
-
export type SDKProtocol = "grpc" | "http";
|
|
89
|
+
export type SDKProtocol = "grpc" | "http" | "console";
|
|
49
90
|
|
|
50
91
|
export type SDKLogLevel =
|
|
51
92
|
| "NONE"
|
|
@@ -1,16 +1,23 @@
|
|
|
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
1
|
import { diag, DiagConsoleLogger, DiagLogLevel } from "@opentelemetry/api";
|
|
8
2
|
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
|
|
9
3
|
import { W3CTraceContextPropagator } from "@opentelemetry/core";
|
|
4
|
+
import { NodeSDK } from "@opentelemetry/sdk-node";
|
|
5
|
+
import {
|
|
6
|
+
AlwaysOffSampler,
|
|
7
|
+
ParentBasedSampler,
|
|
8
|
+
TraceIdRatioBasedSampler,
|
|
9
|
+
} from "@opentelemetry/sdk-trace-base";
|
|
10
|
+
import buildConsoleExporters from "./exporter/console.js";
|
|
11
|
+
import buildGrpcExporters from "./exporter/grpc.js";
|
|
12
|
+
import buildHttpExporters from "./exporter/http.js";
|
|
13
|
+
import type { Exporters } from "./exporter/index.js";
|
|
14
|
+
import type { NodeSDKConfig } from "./index.js";
|
|
15
|
+
import { ObservabilityResourceDetector } from "./resource.js";
|
|
16
|
+
import { UrlSampler } from "./url-sampler.js";
|
|
10
17
|
|
|
11
|
-
export default function buildNodeInstrumentation(
|
|
18
|
+
export default async function buildNodeInstrumentation(
|
|
12
19
|
config?: NodeSDKConfig,
|
|
13
|
-
): NodeSDK | undefined {
|
|
20
|
+
): Promise<NodeSDK | undefined> {
|
|
14
21
|
if (!config) {
|
|
15
22
|
console.warn(
|
|
16
23
|
"observability config not set. Skipping NodeJS OpenTelemetry instrumentation.",
|
|
@@ -32,35 +39,52 @@ export default function buildNodeInstrumentation(
|
|
|
32
39
|
return;
|
|
33
40
|
}
|
|
34
41
|
|
|
35
|
-
|
|
42
|
+
const urlSampler = new UrlSampler(
|
|
43
|
+
config.ignoreUrls,
|
|
44
|
+
new TraceIdRatioBasedSampler(config.traceRatio ?? 1),
|
|
45
|
+
);
|
|
36
46
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
47
|
+
const mainSampler = new ParentBasedSampler({
|
|
48
|
+
root: urlSampler,
|
|
49
|
+
remoteParentSampled: urlSampler,
|
|
50
|
+
remoteParentNotSampled: new AlwaysOffSampler(),
|
|
51
|
+
localParentSampled: urlSampler,
|
|
52
|
+
localParentNotSampled: new AlwaysOffSampler(),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
diag.setLogger(
|
|
56
|
+
new DiagConsoleLogger(),
|
|
57
|
+
config.diagLogLevel ? DiagLogLevel[config.diagLogLevel] : DiagLogLevel.INFO,
|
|
58
|
+
);
|
|
42
59
|
|
|
43
60
|
try {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
61
|
+
const nodeSdkInstrumentation = getNodeAutoInstrumentations({
|
|
62
|
+
"@opentelemetry/instrumentation-fs": {
|
|
63
|
+
enabled: config.enableFS ?? false,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
let exporter: Exporters;
|
|
68
|
+
|
|
69
|
+
if (config.protocol === "http") {
|
|
70
|
+
exporter = buildHttpExporters(config);
|
|
71
|
+
} else if (config.protocol === "console") {
|
|
72
|
+
exporter = buildConsoleExporters(config);
|
|
73
|
+
} else {
|
|
74
|
+
exporter = await buildGrpcExporters(config);
|
|
75
|
+
}
|
|
50
76
|
|
|
51
77
|
const sdk = new NodeSDK({
|
|
78
|
+
resourceDetectors: [
|
|
79
|
+
new ObservabilityResourceDetector(config.resourceAttributes),
|
|
80
|
+
],
|
|
81
|
+
spanProcessors: exporter.spans,
|
|
52
82
|
serviceName: config.serviceName,
|
|
53
|
-
traceExporter: exporter.traces,
|
|
54
83
|
metricReader: exporter.metrics,
|
|
55
84
|
logRecordProcessors: exporter.logs,
|
|
85
|
+
sampler: mainSampler,
|
|
56
86
|
textMapPropagator: new W3CTraceContextPropagator(),
|
|
57
|
-
instrumentations: [
|
|
58
|
-
getNodeAutoInstrumentations({
|
|
59
|
-
"@opentelemetry/instrumentation-fs": {
|
|
60
|
-
enabled: config.enableFS ?? false,
|
|
61
|
-
},
|
|
62
|
-
}),
|
|
63
|
-
],
|
|
87
|
+
instrumentations: [nodeSdkInstrumentation],
|
|
64
88
|
});
|
|
65
89
|
|
|
66
90
|
sdk.start();
|
|
@@ -73,3 +97,12 @@ export default function buildNodeInstrumentation(
|
|
|
73
97
|
);
|
|
74
98
|
}
|
|
75
99
|
}
|
|
100
|
+
|
|
101
|
+
function isUrl(url: string): boolean {
|
|
102
|
+
try {
|
|
103
|
+
new URL(url);
|
|
104
|
+
return true;
|
|
105
|
+
} catch (_) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|