@ogcio/o11y-sdk-node 0.1.0-beta.6 → 0.1.0-beta.8

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.
Files changed (51) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +59 -0
  3. package/dist/index.d.ts +2 -2
  4. package/dist/index.js +1 -0
  5. package/dist/lib/exporter/console.d.ts +3 -0
  6. package/dist/lib/exporter/console.js +16 -0
  7. package/dist/lib/exporter/grpc.d.ts +3 -0
  8. package/dist/lib/{grpc.js → exporter/grpc.js} +9 -5
  9. package/dist/lib/exporter/http.d.ts +3 -0
  10. package/dist/lib/{http.js → exporter/http.js} +9 -5
  11. package/dist/lib/index.d.ts +17 -5
  12. package/dist/lib/instrumentation.node.js +27 -9
  13. package/dist/lib/metrics.d.ts +12 -12
  14. package/dist/lib/metrics.js +6 -10
  15. package/dist/lib/options.d.ts +5 -3
  16. package/dist/lib/processor/enrich-span-processor.d.ts +11 -0
  17. package/dist/lib/processor/enrich-span-processor.js +22 -0
  18. package/dist/lib/traces.d.ts +1 -0
  19. package/dist/lib/traces.js +4 -0
  20. package/dist/lib/url-sampler.d.ts +3 -2
  21. package/dist/lib/url-sampler.js +5 -8
  22. package/dist/lib/utils.d.ts +4 -2
  23. package/dist/lib/utils.js +8 -3
  24. package/dist/package.json +21 -17
  25. package/dist/vitest.config.js +15 -1
  26. package/index.ts +2 -2
  27. package/lib/exporter/console.ts +27 -0
  28. package/lib/{grpc.ts → exporter/grpc.ts} +13 -7
  29. package/lib/{http.ts → exporter/http.ts} +13 -7
  30. package/lib/index.ts +24 -4
  31. package/lib/instrumentation.node.ts +49 -11
  32. package/lib/metrics.ts +34 -29
  33. package/lib/options.ts +5 -3
  34. package/lib/processor/enrich-span-processor.ts +39 -0
  35. package/lib/traces.ts +5 -0
  36. package/lib/url-sampler.ts +13 -14
  37. package/lib/utils.ts +16 -4
  38. package/package.json +21 -17
  39. package/test/enrich-span-processor.test.ts +105 -0
  40. package/test/index.test.ts +9 -0
  41. package/test/integration/integration.test.ts +21 -0
  42. package/test/integration/run.sh +5 -2
  43. package/test/metrics.test.ts +9 -9
  44. package/test/node-config.test.ts +49 -36
  45. package/test/url-sampler.test.ts +215 -0
  46. package/vitest.config.ts +15 -1
  47. package/dist/lib/console.d.ts +0 -3
  48. package/dist/lib/console.js +0 -12
  49. package/dist/lib/grpc.d.ts +0 -3
  50. package/dist/lib/http.d.ts +0 -3
  51. package/lib/console.ts +0 -15
package/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.1.0-beta.8](https://github.com/ogcio/o11y/compare/@ogcio/o11y-sdk-node@v0.1.0-beta.7...@ogcio/o11y-sdk-node@v0.1.0-beta.8) (2025-03-07)
4
+
5
+
6
+ ### Features
7
+
8
+ * o11y showcase AB[#25895](https://github.com/ogcio/o11y/issues/25895) ([#84](https://github.com/ogcio/o11y/issues/84)) ([f8f10af](https://github.com/ogcio/o11y/commit/f8f10af97d9f5c188e3e65f7d62d5c673edce25a))
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * improve getMetric attributes types ([#76](https://github.com/ogcio/o11y/issues/76)) ([243649c](https://github.com/ogcio/o11y/commit/243649c4bfe750687a729fbd836772cce16e0cb1))
14
+
15
+ ## [0.1.0-beta.7](https://github.com/ogcio/o11y/compare/@ogcio/o11y-sdk-node@v0.1.0-beta.6...@ogcio/o11y-sdk-node@v0.1.0-beta.7) (2025-02-18)
16
+
17
+
18
+ ### Features
19
+
20
+ * **sdk-node:** add span customization AB[#25358](https://github.com/ogcio/o11y/issues/25358) ([46ba97b](https://github.com/ogcio/o11y/commit/46ba97bac4004ff326a954592f45213ce0e4d683))
21
+
3
22
  ## [0.1.0-beta.6](https://github.com/ogcio/o11y/compare/@ogcio/o11y-sdk-node@v0.1.0-beta.5...@ogcio/o11y-sdk-node@v0.1.0-beta.6) (2025-02-06)
4
23
 
5
24
 
package/README.md CHANGED
@@ -27,6 +27,15 @@ import("@ogcio/o11y-sdk-node/lib/index").then((sdk) =>
27
27
  sdk.instrumentNode({
28
28
  serviceName: "node-microservice",
29
29
  collectorUrl: "http://localhost:4317",
30
+ resourceAttributes: {
31
+ "team.infra.cluster": "dev-01",
32
+ "team.infra.pod": "01",
33
+ "team.service.type": "fastify",
34
+ },
35
+ spanAttributes: {
36
+ "signal.namespace": "documentation",
37
+ },
38
+ ignoreUrls: [{ type: "equals", url: "/api/health" }],
30
39
  }),
31
40
  );
32
41
  ```
@@ -47,6 +56,56 @@ Or setup inside your package.json
47
56
  }
48
57
  ```
49
58
 
59
+ ## Span Customization
60
+
61
+ It is possible to customize spans such as traces and logs globally or in a single code statement using predefined functions.
62
+
63
+ ### Global Configuration
64
+
65
+ In the SDK configuration, you can set the following properties:
66
+
67
+ - `spanAttributes` Object containing static properties or functions used to evaluate custom attributes for every logs and traces.
68
+ - `resourceAttributes` Object containing static properties used as resources attributes for any signal.
69
+ - `traceRatio` Faction value from 0 to 1, used by TraceIdRatioBasedSampler which it deterministically samples a percentage of traces that you pass in as a parameter.
70
+
71
+ ```typescript
72
+ function generateRandomString(): string {
73
+ return Math.random() + "_" + Date.now();
74
+ }
75
+
76
+ instrumentNode({
77
+ resourceAttributes: {
78
+ "property.name.one": "value_one",
79
+ "property.name.two": "value_two",
80
+ },
81
+ spanAttributes: {
82
+ "custom.span.value": "example",
83
+ "custom.span.value_with_function": generateRandomString,
84
+ },
85
+ });
86
+ ```
87
+
88
+ ### Edit Active Span
89
+
90
+ Using `getActiveSpan` function, you can access to current transaction span and customize it.
91
+
92
+ You can use the function everywhere in your code, and set some custom attributes that are enabled for that single span
93
+
94
+ ```typescript
95
+ import { getActiveSpan } from "@ogcio/o11y-sdk-node";
96
+
97
+ async function routes(app: FastifyInstance) {
98
+ app.get("/", async (req, reply) => {
99
+ // validation and business logic
100
+
101
+ // set span attribute
102
+ getActiveSpan()?.setAttribute("business.info", "dummy");
103
+
104
+ reply.status(200).send(response);
105
+ });
106
+ }
107
+ ```
108
+
50
109
  ## Sending Custom Metrics
51
110
 
52
111
  This package gives the possibility to send custom metrics and define them as desired in the code, you can choose between sync metrics and observable async metrics.
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
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
  export type * from "./lib/index.js";
5
- export type { NodeSDKConfig, NodeSDK };
4
+ export type { NodeSDK };
6
5
  export { buildNodeInstrumentation as instrumentNode };
7
6
  export * from "./lib/metrics.js";
7
+ export * from "./lib/traces.js";
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  import buildNodeInstrumentation from "./lib/instrumentation.node.js";
2
2
  export { buildNodeInstrumentation as instrumentNode };
3
3
  export * from "./lib/metrics.js";
4
+ export * from "./lib/traces.js";
@@ -0,0 +1,3 @@
1
+ import { NodeSDKConfig } from "../index.js";
2
+ import { Exporters } from "../options.js";
3
+ export default function buildConsoleExporters(config: NodeSDKConfig): Exporters;
@@ -0,0 +1,16 @@
1
+ import { ConsoleLogRecordExporter, SimpleLogRecordProcessor, } from "@opentelemetry/sdk-logs";
2
+ import { metrics } from "@opentelemetry/sdk-node";
3
+ import { ConsoleSpanExporter, SimpleSpanProcessor, } from "@opentelemetry/sdk-trace-base";
4
+ import { EnrichSpanProcessor } from "../processor/enrich-span-processor.js";
5
+ export default function buildConsoleExporters(config) {
6
+ return {
7
+ spans: [
8
+ new SimpleSpanProcessor(new ConsoleSpanExporter()),
9
+ new EnrichSpanProcessor(config.spanAttributes),
10
+ ],
11
+ metrics: new metrics.PeriodicExportingMetricReader({
12
+ exporter: new metrics.ConsoleMetricExporter(),
13
+ }),
14
+ logs: [new SimpleLogRecordProcessor(new ConsoleLogRecordExporter())],
15
+ };
16
+ }
@@ -0,0 +1,3 @@
1
+ import { NodeSDKConfig } from "../index.js";
2
+ import { Exporters } from "../options.js";
3
+ export default function buildGrpcExporters(config: NodeSDKConfig): Exporters;
@@ -1,15 +1,19 @@
1
- import { LogRecordProcessorMap } from "./utils.js";
2
1
  import { metrics } from "@opentelemetry/sdk-node";
3
2
  import { CompressionAlgorithm } from "@opentelemetry/otlp-exporter-base";
4
3
  import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-grpc";
5
4
  import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-grpc";
6
5
  import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-grpc";
6
+ import { LogRecordProcessorMap, SpanProcessorMap } from "../utils.js";
7
+ import { EnrichSpanProcessor } from "../processor/enrich-span-processor.js";
7
8
  export default function buildGrpcExporters(config) {
8
9
  return {
9
- traces: new OTLPTraceExporter({
10
- url: `${config.collectorUrl}`,
11
- compression: CompressionAlgorithm.GZIP,
12
- }),
10
+ spans: [
11
+ new SpanProcessorMap[config.collectorMode ?? "batch"](new OTLPTraceExporter({
12
+ url: `${config.collectorUrl}`,
13
+ compression: CompressionAlgorithm.GZIP,
14
+ })),
15
+ new EnrichSpanProcessor(config.spanAttributes),
16
+ ],
13
17
  metrics: new metrics.PeriodicExportingMetricReader({
14
18
  exporter: new OTLPMetricExporter({
15
19
  url: `${config.collectorUrl}`,
@@ -0,0 +1,3 @@
1
+ import { Exporters } from "../options.js";
2
+ import { NodeSDKConfig } from "../index.js";
3
+ export default function buildHttpExporters(config: NodeSDKConfig): Exporters;
@@ -1,18 +1,22 @@
1
- import { LogRecordProcessorMap } from "./utils.js";
2
1
  import { metrics } from "@opentelemetry/sdk-node";
3
2
  import { CompressionAlgorithm } from "@opentelemetry/otlp-exporter-base";
4
3
  import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
5
4
  import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
6
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";
7
8
  export default function buildHttpExporters(config) {
8
9
  if (config.collectorUrl.endsWith("/")) {
9
10
  config.collectorUrl = config.collectorUrl.slice(0, -1);
10
11
  }
11
12
  return {
12
- traces: new OTLPTraceExporter({
13
- url: `${config.collectorUrl}/v1/traces`,
14
- compression: CompressionAlgorithm.GZIP,
15
- }),
13
+ spans: [
14
+ new SpanProcessorMap[config.collectorMode ?? "batch"](new OTLPTraceExporter({
15
+ url: `${config.collectorUrl}/v1/traces`,
16
+ compression: CompressionAlgorithm.GZIP,
17
+ })),
18
+ new EnrichSpanProcessor(config.spanAttributes),
19
+ ],
16
20
  metrics: new metrics.PeriodicExportingMetricReader({
17
21
  exporter: new OTLPMetricExporter({
18
22
  url: `${config.collectorUrl}/v1/metrics`,
@@ -1,4 +1,4 @@
1
- interface SDKConfig {
1
+ export interface NodeSDKConfig {
2
2
  /**
3
3
  * The opentelemetry collector entrypoint GRPC url.
4
4
  * If the collectoUrl is null or undefined, the instrumentation will not be activated.
@@ -32,8 +32,20 @@ interface SDKConfig {
32
32
  * @default []
33
33
  */
34
34
  ignoreUrls?: SamplerCondition[];
35
- }
36
- export interface NodeSDKConfig extends SDKConfig {
35
+ /**
36
+ * Object containing static properties or functions used to evaluate custom attributes for every logs and traces.
37
+ */
38
+ spanAttributes?: Record<string, SignalAttributeValue | (() => SignalAttributeValue)>;
39
+ /**
40
+ * Object containing static properties used as resources attributes for the Node SDK initialization.
41
+ */
42
+ resourceAttributes?: Record<string, SignalAttributeValue>;
43
+ /**
44
+ * Faction value from 0 to 1, used by TraceIdRatioBasedSampler which it deterministically samples a percentage of traces that you pass in as a parameter.
45
+ *
46
+ * @default 1
47
+ */
48
+ traceRatio?: number;
37
49
  /**
38
50
  * Flag to enable or disable the tracing for node:fs module
39
51
  *
@@ -41,7 +53,7 @@ export interface NodeSDKConfig extends SDKConfig {
41
53
  */
42
54
  enableFS?: boolean;
43
55
  /**
44
- * protocol used to send signals.
56
+ * Protocol used to send signals.
45
57
  *
46
58
  * @default grpc
47
59
  */
@@ -51,7 +63,7 @@ export interface SamplerCondition {
51
63
  type: "endsWith" | "includes" | "equals";
52
64
  url: string;
53
65
  }
66
+ export type SignalAttributeValue = string | number | boolean;
54
67
  export type SDKCollectorMode = "single" | "batch";
55
68
  export type SDKProtocol = "grpc" | "http" | "console";
56
69
  export type SDKLogLevel = "NONE" | "ERROR" | "WARN" | "INFO" | "DEBUG" | "VERBOSE" | "ALL";
57
- export {};
@@ -1,11 +1,12 @@
1
- import { NodeSDK, resources } from "@opentelemetry/sdk-node";
2
- import buildHttpExporters from "./http.js";
3
- import buildGrpcExporters from "./grpc.js";
4
- import buildConsoleExporters from "./console.js";
5
- import { diag, DiagConsoleLogger, DiagLogLevel } from "@opentelemetry/api";
1
+ import { diag, DiagConsoleLogger, DiagLogLevel, } from "@opentelemetry/api";
6
2
  import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
7
3
  import { W3CTraceContextPropagator } from "@opentelemetry/core";
4
+ import { NodeSDK, resources } from "@opentelemetry/sdk-node";
5
+ import { AlwaysOffSampler, ParentBasedSampler, TraceIdRatioBasedSampler, } from "@opentelemetry/sdk-trace-base";
8
6
  import packageJson from "../package.json" with { type: "json" };
7
+ import buildConsoleExporters from "./exporter/console.js";
8
+ import buildGrpcExporters from "./exporter/grpc.js";
9
+ import buildHttpExporters from "./exporter/http.js";
9
10
  import { UrlSampler } from "./url-sampler.js";
10
11
  export default function buildNodeInstrumentation(config) {
11
12
  if (!config) {
@@ -30,6 +31,14 @@ export default function buildNodeInstrumentation(config) {
30
31
  else {
31
32
  exporter = buildGrpcExporters(config);
32
33
  }
34
+ const urlSampler = new UrlSampler(config.ignoreUrls, new TraceIdRatioBasedSampler(config.traceRatio ?? 1));
35
+ const mainSampler = new ParentBasedSampler({
36
+ root: urlSampler,
37
+ remoteParentSampled: urlSampler,
38
+ remoteParentNotSampled: new AlwaysOffSampler(),
39
+ localParentSampled: urlSampler,
40
+ localParentNotSampled: new AlwaysOffSampler(),
41
+ });
33
42
  try {
34
43
  diag.setLogger(new DiagConsoleLogger(), config.diagLogLevel
35
44
  ? DiagLogLevel[config.diagLogLevel]
@@ -38,18 +47,28 @@ export default function buildNodeInstrumentation(config) {
38
47
  resource: new resources.Resource({
39
48
  "o11y.sdk.name": packageJson.name,
40
49
  "o11y.sdk.version": packageJson.version,
50
+ ...config.resourceAttributes,
41
51
  }),
52
+ spanProcessors: exporter.spans,
42
53
  serviceName: config.serviceName,
43
- traceExporter: exporter.traces,
44
54
  metricReader: exporter.metrics,
45
55
  logRecordProcessors: exporter.logs,
46
- sampler: new UrlSampler(config.ignoreUrls),
56
+ sampler: mainSampler,
47
57
  textMapPropagator: new W3CTraceContextPropagator(),
48
58
  instrumentations: [
49
59
  getNodeAutoInstrumentations({
50
60
  "@opentelemetry/instrumentation-fs": {
51
61
  enabled: config.enableFS ?? false,
52
62
  },
63
+ "@opentelemetry/instrumentation-pino": {
64
+ logHook: (_span, record, _level) => {
65
+ if (config.spanAttributes != undefined) {
66
+ for (const [key, value] of Object.entries(config.spanAttributes)) {
67
+ record[key] = typeof value === "function" ? value() : value;
68
+ }
69
+ }
70
+ },
71
+ },
53
72
  }),
54
73
  ],
55
74
  });
@@ -66,8 +85,7 @@ function isUrl(url) {
66
85
  new URL(url);
67
86
  return true;
68
87
  }
69
- catch (err) {
70
- console.error(err);
88
+ catch (_) {
71
89
  return false;
72
90
  }
73
91
  }
@@ -1,18 +1,18 @@
1
- import { Counter, Gauge, Histogram, MetricOptions, ObservableCounter, ObservableGauge, ObservableUpDownCounter, UpDownCounter } from "@opentelemetry/api";
2
- declare const MetricsMap: {
3
- gauge: Gauge;
4
- histogram: Histogram;
5
- counter: Counter;
6
- updowncounter: UpDownCounter;
7
- "async-counter": ObservableCounter;
8
- "async-updowncounter": ObservableUpDownCounter;
9
- "async-gauge": ObservableGauge;
1
+ import { Counter, Gauge, Histogram, MetricOptions, ObservableCounter, ObservableGauge, ObservableUpDownCounter, UpDownCounter, Attributes } from "@opentelemetry/api";
2
+ type MetricTypeMap<TAttributes extends Attributes> = {
3
+ counter: Counter<TAttributes>;
4
+ histogram: Histogram<TAttributes>;
5
+ gauge: Gauge<TAttributes>;
6
+ updowncounter: UpDownCounter<TAttributes>;
7
+ "async-counter": ObservableCounter<TAttributes>;
8
+ "async-updowncounter": ObservableUpDownCounter<TAttributes>;
9
+ "async-gauge": ObservableGauge<TAttributes>;
10
10
  };
11
- type MetricType = keyof typeof MetricsMap;
11
+ export type MetricType = keyof MetricTypeMap<Attributes>;
12
12
  export interface MetricsParams {
13
+ meterName: string;
13
14
  metricName: string;
14
- attributeName: string;
15
15
  options?: MetricOptions;
16
16
  }
17
- export declare function getMetric<T extends MetricType>(type: T, p: MetricsParams): (typeof MetricsMap)[T];
17
+ export declare function getMetric<T extends MetricType, TAttributes extends Attributes = Attributes>(type: T, p: MetricsParams): MetricTypeMap<TAttributes>[T];
18
18
  export {};
@@ -8,21 +8,17 @@ const MetricsFactoryMap = {
8
8
  "async-updowncounter": (meter) => meter.createObservableUpDownCounter,
9
9
  "async-gauge": (meter) => meter.createObservableGauge,
10
10
  };
11
- function getMeter({ metricName, attributeName }) {
12
- let meter;
13
- if (!metricName || !attributeName) {
14
- console.error("Invaid metric configuration!");
15
- meter = createNoopMeter();
11
+ function getMeter({ meterName }) {
12
+ if (!meterName) {
13
+ console.error("Invalid metric name!");
14
+ return createNoopMeter();
16
15
  }
17
- else {
18
- meter = metrics.getMeter(`custom_metric.${metricName}`);
19
- }
20
- return meter;
16
+ return metrics.getMeter(`custom_metric.${meterName}`);
21
17
  }
22
18
  export function getMetric(type, p) {
23
19
  const meter = getMeter(p);
24
20
  if (!MetricsFactoryMap[type]) {
25
21
  throw new Error(`Unsupported metric type: ${type}`);
26
22
  }
27
- return MetricsFactoryMap[type](meter).bind(meter)(p.attributeName, p.options);
23
+ return MetricsFactoryMap[type](meter).bind(meter)(p.metricName, p.options);
28
24
  }
@@ -1,6 +1,8 @@
1
- import type { logs, tracing, metrics } from "@opentelemetry/sdk-node";
1
+ import { LogRecordProcessor } from "@opentelemetry/sdk-logs";
2
+ import type { metrics } from "@opentelemetry/sdk-node";
3
+ import { SpanProcessor } from "@opentelemetry/sdk-trace-base";
2
4
  export type Exporters = {
3
- traces: tracing.SpanExporter;
5
+ spans: SpanProcessor[];
4
6
  metrics: metrics.MetricReader;
5
- logs: logs.LogRecordProcessor[];
7
+ logs: LogRecordProcessor[];
6
8
  };
@@ -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 @@
1
+ export declare function getActiveSpan(): import("@opentelemetry/api").Span | undefined;
@@ -0,0 +1,4 @@
1
+ import { trace } from "@opentelemetry/api";
2
+ export function getActiveSpan() {
3
+ return trace.getActiveSpan();
4
+ }
@@ -1,9 +1,10 @@
1
- import { Context, SpanKind, Link, Attributes } from "@opentelemetry/api";
1
+ import { Attributes, Context, Link, SpanKind } from "@opentelemetry/api";
2
2
  import { Sampler, SamplingResult } from "@opentelemetry/sdk-trace-base";
3
3
  import { SamplerCondition } from "./index.js";
4
4
  export declare class UrlSampler implements Sampler {
5
5
  private _samplerCondition;
6
- constructor(samplerCondition?: SamplerCondition[]);
6
+ private _nextSampler;
7
+ constructor(samplerCondition: SamplerCondition[] | undefined, nextSampler: Sampler);
7
8
  shouldSample(_context: Context, traceId: string, _spanName: string, _spanKind: SpanKind, attributes: Attributes, _links: Link[]): SamplingResult;
8
9
  toString(): string;
9
10
  }
@@ -1,9 +1,10 @@
1
- import { isValidTraceId, } from "@opentelemetry/api";
2
1
  import { SamplingDecision, } from "@opentelemetry/sdk-trace-base";
3
2
  export class UrlSampler {
4
3
  _samplerCondition;
5
- constructor(samplerCondition = []) {
4
+ _nextSampler;
5
+ constructor(samplerCondition = [], nextSampler) {
6
6
  this._samplerCondition = samplerCondition;
7
+ this._nextSampler = nextSampler;
7
8
  }
8
9
  shouldSample(_context, traceId, _spanName, _spanKind, attributes, _links) {
9
10
  const url = attributes["http.target"]?.toString();
@@ -16,13 +17,9 @@ export class UrlSampler {
16
17
  }
17
18
  }
18
19
  }
19
- return {
20
- decision: isValidTraceId(traceId)
21
- ? SamplingDecision.RECORD_AND_SAMPLED
22
- : SamplingDecision.NOT_RECORD,
23
- };
20
+ return this._nextSampler.shouldSample(_context, traceId, _spanName, _spanKind, attributes, _links);
24
21
  }
25
22
  toString() {
26
- throw "UrlSampler";
23
+ return "UrlSampler";
27
24
  }
28
25
  }
@@ -1,3 +1,5 @@
1
+ import { BatchLogRecordProcessor, SimpleLogRecordProcessor } from "@opentelemetry/sdk-logs";
1
2
  import { SDKCollectorMode } from "./index.js";
2
- import { logs } from "@opentelemetry/sdk-node";
3
- export declare const LogRecordProcessorMap: Record<SDKCollectorMode, typeof logs.SimpleLogRecordProcessor | typeof logs.BatchLogRecordProcessor>;
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 { logs } from "@opentelemetry/sdk-node";
1
+ import { BatchLogRecordProcessor, SimpleLogRecordProcessor, } from "@opentelemetry/sdk-logs";
2
+ import { tracing } from "@opentelemetry/sdk-node";
2
3
  export const LogRecordProcessorMap = {
3
- single: logs.SimpleLogRecordProcessor,
4
- batch: logs.BatchLogRecordProcessor,
4
+ single: SimpleLogRecordProcessor,
5
+ batch: BatchLogRecordProcessor,
6
+ };
7
+ export const SpanProcessorMap = {
8
+ single: tracing.SimpleSpanProcessor,
9
+ batch: tracing.BatchSpanProcessor,
5
10
  };
package/dist/package.json CHANGED
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "name": "@ogcio/o11y-sdk-node",
3
- "version": "0.1.0-beta.6",
3
+ "version": "0.1.0-beta.8",
4
4
  "description": "Opentelemetry standard instrumentation SDK for NodeJS based project",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "build": "rm -rf dist && tsc -p tsconfig.json",
9
9
  "test": "vitest",
10
- "prepublishOnly": "pnpm i && pnpm build"
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"
11
14
  },
12
15
  "exports": {
13
16
  ".": "./dist/index.js",
@@ -25,25 +28,26 @@
25
28
  "license": "ISC",
26
29
  "dependencies": {
27
30
  "@opentelemetry/api": "^1.9.0",
28
- "@opentelemetry/auto-instrumentations-node": "^0.56.0",
31
+ "@opentelemetry/auto-instrumentations-node": "^0.56.1",
29
32
  "@opentelemetry/core": "1.30.1",
30
- "@opentelemetry/exporter-logs-otlp-grpc": "^0.57.1",
31
- "@opentelemetry/exporter-logs-otlp-http": "^0.57.1",
32
- "@opentelemetry/exporter-metrics-otlp-grpc": "^0.57.1",
33
- "@opentelemetry/exporter-metrics-otlp-http": "^0.57.1",
34
- "@opentelemetry/exporter-trace-otlp-grpc": "^0.57.1",
35
- "@opentelemetry/exporter-trace-otlp-http": "^0.57.1",
36
- "@opentelemetry/instrumentation": "^0.57.1",
37
- "@opentelemetry/otlp-exporter-base": "^0.57.1",
33
+ "@opentelemetry/exporter-logs-otlp-grpc": "^0.57.2",
34
+ "@opentelemetry/exporter-logs-otlp-http": "^0.57.2",
35
+ "@opentelemetry/exporter-metrics-otlp-grpc": "^0.57.2",
36
+ "@opentelemetry/exporter-metrics-otlp-http": "^0.57.2",
37
+ "@opentelemetry/exporter-trace-otlp-grpc": "^0.57.2",
38
+ "@opentelemetry/exporter-trace-otlp-http": "^0.57.2",
39
+ "@opentelemetry/instrumentation": "^0.57.2",
40
+ "@opentelemetry/otlp-exporter-base": "^0.57.2",
41
+ "@opentelemetry/sdk-logs": "^0.57.2",
38
42
  "@opentelemetry/sdk-metrics": "^1.30.1",
39
- "@opentelemetry/sdk-node": "^0.57.1",
43
+ "@opentelemetry/sdk-node": "^0.57.2",
40
44
  "@opentelemetry/sdk-trace-base": "^1.30.1"
41
45
  },
42
46
  "devDependencies": {
43
- "@types/node": "^22.12.0",
44
- "@vitest/coverage-v8": "^3.0.4",
45
- "tsx": "^4.19.2",
46
- "typescript": "^5.7.3",
47
- "vitest": "^3.0.4"
47
+ "@types/node": "^22.13.9",
48
+ "@vitest/coverage-v8": "^3.0.8",
49
+ "tsx": "^4.19.3",
50
+ "typescript": "^5.8.2",
51
+ "vitest": "^3.0.8"
48
52
  }
49
53
  }
@@ -3,7 +3,6 @@ export default defineConfig({
3
3
  test: {
4
4
  globals: true,
5
5
  watch: false,
6
- include: ["**/test/*.test.ts", "**/test/integration/*.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"],
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,9 +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 { NodeSDKConfig, NodeSDK };
5
+ export type { NodeSDK };
7
6
  export { buildNodeInstrumentation as instrumentNode };
8
7
 
9
8
  export * from "./lib/metrics.js";
9
+ export * from "./lib/traces.js";
@@ -0,0 +1,27 @@
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 "../options.js";
12
+ import { EnrichSpanProcessor } from "../processor/enrich-span-processor.js";
13
+
14
+ export default function buildConsoleExporters(
15
+ config: NodeSDKConfig,
16
+ ): Exporters {
17
+ return {
18
+ spans: [
19
+ new SimpleSpanProcessor(new ConsoleSpanExporter()),
20
+ new EnrichSpanProcessor(config.spanAttributes),
21
+ ],
22
+ metrics: new metrics.PeriodicExportingMetricReader({
23
+ exporter: new metrics.ConsoleMetricExporter(),
24
+ }),
25
+ logs: [new SimpleLogRecordProcessor(new ConsoleLogRecordExporter())],
26
+ };
27
+ }