@ogcio/o11y-sdk-node 0.1.0-beta.6 → 0.1.0-beta.7
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 +7 -0
- package/README.md +59 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -0
- package/dist/lib/exporter/console.d.ts +3 -0
- package/dist/lib/exporter/console.js +16 -0
- package/dist/lib/exporter/grpc.d.ts +3 -0
- package/dist/lib/{grpc.js → exporter/grpc.js} +9 -5
- package/dist/lib/exporter/http.d.ts +3 -0
- package/dist/lib/{http.js → exporter/http.js} +9 -5
- package/dist/lib/index.d.ts +17 -5
- package/dist/lib/instrumentation.node.js +26 -7
- package/dist/lib/options.d.ts +5 -3
- package/dist/lib/processor/enrich-span-processor.d.ts +11 -0
- package/dist/lib/processor/enrich-span-processor.js +22 -0
- package/dist/lib/traces.d.ts +1 -0
- package/dist/lib/traces.js +4 -0
- package/dist/lib/url-sampler.d.ts +3 -2
- package/dist/lib/url-sampler.js +5 -8
- package/dist/lib/utils.d.ts +4 -2
- package/dist/lib/utils.js +8 -3
- package/dist/package.json +18 -14
- package/dist/vitest.config.js +15 -1
- package/index.ts +2 -2
- package/lib/exporter/console.ts +27 -0
- package/lib/{grpc.ts → exporter/grpc.ts} +13 -7
- package/lib/{http.ts → exporter/http.ts} +13 -7
- package/lib/index.ts +24 -4
- package/lib/instrumentation.node.ts +48 -9
- package/lib/options.ts +5 -3
- package/lib/processor/enrich-span-processor.ts +39 -0
- package/lib/traces.ts +5 -0
- package/lib/url-sampler.ts +13 -14
- package/lib/utils.ts +16 -4
- package/package.json +18 -14
- package/test/enrich-span-processor.test.ts +105 -0
- package/test/index.test.ts +9 -0
- package/test/integration/integration.test.ts +21 -0
- package/test/integration/run.sh +2 -2
- package/test/node-config.test.ts +49 -36
- package/test/url-sampler.test.ts +215 -0
- package/vitest.config.ts +15 -1
- package/dist/lib/console.d.ts +0 -3
- package/dist/lib/console.js +0 -12
- package/dist/lib/grpc.d.ts +0 -3
- package/dist/lib/http.d.ts +0 -3
- package/lib/console.ts +0 -15
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { Context } from "@opentelemetry/api";
|
|
2
|
+
import {
|
|
3
|
+
SamplingDecision,
|
|
4
|
+
SamplingResult,
|
|
5
|
+
} from "@opentelemetry/sdk-trace-base";
|
|
6
|
+
import { describe, expect, test, vi } from "vitest";
|
|
7
|
+
import { UrlSampler } from "../lib/url-sampler";
|
|
8
|
+
|
|
9
|
+
describe("url sampler", () => {
|
|
10
|
+
// mock sampler to be sure every trace after UrlSamper has RECORD status
|
|
11
|
+
const mockSampler = {
|
|
12
|
+
shouldSample: vi
|
|
13
|
+
.fn()
|
|
14
|
+
.mockImplementation(
|
|
15
|
+
(_context, _traceId, _spanName, _spanKind, attributes, _links) => {
|
|
16
|
+
return {
|
|
17
|
+
decision: SamplingDecision.RECORD,
|
|
18
|
+
attributes: attributes,
|
|
19
|
+
} as SamplingResult;
|
|
20
|
+
},
|
|
21
|
+
),
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
test("should add custom span attributes to trace", async () => {
|
|
25
|
+
const sampler: UrlSampler = new UrlSampler(
|
|
26
|
+
[
|
|
27
|
+
{
|
|
28
|
+
type: "endsWith",
|
|
29
|
+
url: "/health",
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
mockSampler,
|
|
33
|
+
{
|
|
34
|
+
"signal.namespace": "unittest",
|
|
35
|
+
"signal.callback.result": () => "test",
|
|
36
|
+
},
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
expect(sampler).not.toBeNull();
|
|
40
|
+
|
|
41
|
+
const result = sampler.shouldSample(
|
|
42
|
+
{} as Context,
|
|
43
|
+
"traceId",
|
|
44
|
+
"span",
|
|
45
|
+
0,
|
|
46
|
+
{ "http.target": "/track" },
|
|
47
|
+
[],
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
expect(sampler.toString()).toBe("UrlSampler");
|
|
51
|
+
expect(result.decision).toBe(SamplingDecision.RECORD);
|
|
52
|
+
expect(result.attributes).not.toBeNull();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("should not record trace about /health api", async () => {
|
|
56
|
+
const sampler: UrlSampler = new UrlSampler(
|
|
57
|
+
[
|
|
58
|
+
{
|
|
59
|
+
type: "endsWith",
|
|
60
|
+
url: "/health",
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
mockSampler,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const result = sampler.shouldSample(
|
|
67
|
+
{} as Context,
|
|
68
|
+
"traceId",
|
|
69
|
+
"span",
|
|
70
|
+
0,
|
|
71
|
+
{ "http.target": "/health" },
|
|
72
|
+
[],
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
expect(sampler.toString()).toBe("UrlSampler");
|
|
76
|
+
expect(result.decision).toBe(SamplingDecision.NOT_RECORD);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("should record every other trace which is not /health api", async () => {
|
|
80
|
+
const sampler: UrlSampler = new UrlSampler(
|
|
81
|
+
[
|
|
82
|
+
{
|
|
83
|
+
type: "endsWith",
|
|
84
|
+
url: "/health",
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
mockSampler,
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
let result = sampler.shouldSample(
|
|
91
|
+
{} as Context,
|
|
92
|
+
"traceId",
|
|
93
|
+
"span",
|
|
94
|
+
0,
|
|
95
|
+
{ "http.target": "/test" },
|
|
96
|
+
[],
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
expect(result.decision).toBe(SamplingDecision.RECORD);
|
|
100
|
+
|
|
101
|
+
result = sampler.shouldSample(
|
|
102
|
+
{} as Context,
|
|
103
|
+
"traceId",
|
|
104
|
+
"span",
|
|
105
|
+
0,
|
|
106
|
+
{ "http.target": "/another/url" },
|
|
107
|
+
[],
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
expect(result.decision).toBe(SamplingDecision.RECORD);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("operator 'includes', should not record every trace which include /block in url", async () => {
|
|
114
|
+
const sampler: UrlSampler = new UrlSampler(
|
|
115
|
+
[
|
|
116
|
+
{
|
|
117
|
+
type: "includes",
|
|
118
|
+
url: "/block",
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
mockSampler,
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
expect(sampler).not.toBeNull();
|
|
125
|
+
|
|
126
|
+
const result = sampler.shouldSample(
|
|
127
|
+
{} as Context,
|
|
128
|
+
"traceId",
|
|
129
|
+
"span",
|
|
130
|
+
0,
|
|
131
|
+
{ "http.target": "/namespace/block/example/12" },
|
|
132
|
+
[],
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
expect(sampler.toString()).toBe("UrlSampler");
|
|
136
|
+
expect(result.decision).toBe(SamplingDecision.NOT_RECORD);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("operator 'endsWith', should not record only trace which ends with /block in url", async () => {
|
|
140
|
+
const sampler: UrlSampler = new UrlSampler(
|
|
141
|
+
[
|
|
142
|
+
{
|
|
143
|
+
type: "endsWith",
|
|
144
|
+
url: "/block",
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
mockSampler,
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
expect(sampler).not.toBeNull();
|
|
151
|
+
|
|
152
|
+
// expect traced with block in the URL middle
|
|
153
|
+
let result = sampler.shouldSample(
|
|
154
|
+
{} as Context,
|
|
155
|
+
"traceId",
|
|
156
|
+
"span",
|
|
157
|
+
0,
|
|
158
|
+
{ "http.target": "/namespace/block/example/12" },
|
|
159
|
+
[],
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
expect(sampler.toString()).toBe("UrlSampler");
|
|
163
|
+
expect(result.decision).toBe(SamplingDecision.RECORD);
|
|
164
|
+
|
|
165
|
+
// should stop trace with block at the end
|
|
166
|
+
result = sampler.shouldSample(
|
|
167
|
+
{} as Context,
|
|
168
|
+
"traceId",
|
|
169
|
+
"span",
|
|
170
|
+
0,
|
|
171
|
+
{ "http.target": "/namespace/example/block" },
|
|
172
|
+
[],
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
expect(sampler.toString()).toBe("UrlSampler");
|
|
176
|
+
expect(result.decision).toBe(SamplingDecision.NOT_RECORD);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("operator 'equals', should not record trace which is equal to /block in url", async () => {
|
|
180
|
+
const sampler: UrlSampler = new UrlSampler(
|
|
181
|
+
[
|
|
182
|
+
{
|
|
183
|
+
type: "equals",
|
|
184
|
+
url: "/block",
|
|
185
|
+
},
|
|
186
|
+
],
|
|
187
|
+
mockSampler,
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
expect(sampler).not.toBeNull();
|
|
191
|
+
|
|
192
|
+
let result = sampler.shouldSample(
|
|
193
|
+
{} as Context,
|
|
194
|
+
"traceId",
|
|
195
|
+
"span",
|
|
196
|
+
0,
|
|
197
|
+
{ "http.target": "/namespace/block/example/12" },
|
|
198
|
+
[],
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
expect(sampler.toString()).toBe("UrlSampler");
|
|
202
|
+
expect(result.decision).toBe(SamplingDecision.RECORD);
|
|
203
|
+
|
|
204
|
+
result = sampler.shouldSample(
|
|
205
|
+
{} as Context,
|
|
206
|
+
"traceId",
|
|
207
|
+
"span",
|
|
208
|
+
0,
|
|
209
|
+
{ "http.target": "/block" },
|
|
210
|
+
[],
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
expect(result.decision).toBe(SamplingDecision.NOT_RECORD);
|
|
214
|
+
});
|
|
215
|
+
});
|
package/vitest.config.ts
CHANGED
|
@@ -4,7 +4,6 @@ export default defineConfig({
|
|
|
4
4
|
test: {
|
|
5
5
|
globals: true,
|
|
6
6
|
watch: false,
|
|
7
|
-
include: ["**/test/*.test.ts", "**/test/integration/*.test.ts"],
|
|
8
7
|
exclude: ["**/fixtures/**", "**/dist/**"],
|
|
9
8
|
poolOptions: {
|
|
10
9
|
threads: {
|
|
@@ -22,5 +21,20 @@ export default defineConfig({
|
|
|
22
21
|
},
|
|
23
22
|
reporters: ["default", ["junit", { outputFile: "test-report.xml" }]],
|
|
24
23
|
environment: "node",
|
|
24
|
+
pool: "threads",
|
|
25
|
+
workspace: [
|
|
26
|
+
{
|
|
27
|
+
test: {
|
|
28
|
+
include: ["**/test/*.test.ts"],
|
|
29
|
+
name: "unit",
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
test: {
|
|
34
|
+
include: ["**/test/integration/*.test.ts"],
|
|
35
|
+
name: "integration",
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
],
|
|
25
39
|
},
|
|
26
40
|
});
|
package/dist/lib/console.d.ts
DELETED
package/dist/lib/console.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { logs, metrics, tracing } from "@opentelemetry/sdk-node";
|
|
2
|
-
export default function buildConsoleExporters(_) {
|
|
3
|
-
return {
|
|
4
|
-
traces: new tracing.ConsoleSpanExporter(),
|
|
5
|
-
metrics: new metrics.PeriodicExportingMetricReader({
|
|
6
|
-
exporter: new metrics.ConsoleMetricExporter(),
|
|
7
|
-
}),
|
|
8
|
-
logs: [
|
|
9
|
-
new logs.SimpleLogRecordProcessor(new logs.ConsoleLogRecordExporter()),
|
|
10
|
-
],
|
|
11
|
-
};
|
|
12
|
-
}
|
package/dist/lib/grpc.d.ts
DELETED
package/dist/lib/http.d.ts
DELETED
package/lib/console.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { NodeSDKConfig } from "./index.js";
|
|
2
|
-
import type { Exporters } from "./options.js";
|
|
3
|
-
import { logs, metrics, tracing } from "@opentelemetry/sdk-node";
|
|
4
|
-
|
|
5
|
-
export default function buildConsoleExporters(_: NodeSDKConfig): Exporters {
|
|
6
|
-
return {
|
|
7
|
-
traces: new tracing.ConsoleSpanExporter(),
|
|
8
|
-
metrics: new metrics.PeriodicExportingMetricReader({
|
|
9
|
-
exporter: new metrics.ConsoleMetricExporter(),
|
|
10
|
-
}),
|
|
11
|
-
logs: [
|
|
12
|
-
new logs.SimpleLogRecordProcessor(new logs.ConsoleLogRecordExporter()),
|
|
13
|
-
],
|
|
14
|
-
};
|
|
15
|
-
}
|