@devopsplaybook.io/otel-utils-fastify 1.0.0 → 1.0.1-beta2
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.
|
@@ -1,3 +1,8 @@
|
|
|
1
1
|
import { StandardLogger, StandardTracer } from "@devopsplaybook.io/otel-utils";
|
|
2
|
+
import { Span } from "@opentelemetry/sdk-trace-base";
|
|
2
3
|
import { FastifyInstance } from "fastify";
|
|
3
|
-
export declare function StandardTracerFastifyRegisterHooks(fastify: FastifyInstance, standardTracer: StandardTracer, standardLogger: StandardLogger): void;
|
|
4
|
+
export declare function StandardTracerFastifyRegisterHooks(fastify: FastifyInstance, standardTracer: StandardTracer, standardLogger: StandardLogger, options?: StandardTracerFastifyRegisterHooksOptions): void;
|
|
5
|
+
export interface StandardTracerFastifyRegisterHooksOptions {
|
|
6
|
+
rootApiPath?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function OTelRequestSpan(req: any): Span;
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.StandardTracerFastifyRegisterHooks = StandardTracerFastifyRegisterHooks;
|
|
4
|
+
exports.OTelRequestSpan = OTelRequestSpan;
|
|
4
5
|
const api_1 = require("@opentelemetry/api");
|
|
5
6
|
const core_1 = require("@opentelemetry/core");
|
|
6
7
|
const sdk_node_1 = require("@opentelemetry/sdk-node");
|
|
7
8
|
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
8
9
|
const propagator = new core_1.W3CTraceContextPropagator();
|
|
9
|
-
function StandardTracerFastifyRegisterHooks(fastify, standardTracer, standardLogger) {
|
|
10
|
+
function StandardTracerFastifyRegisterHooks(fastify, standardTracer, standardLogger, options) {
|
|
10
11
|
const logger = standardLogger.createModuleLogger("Fastify");
|
|
11
12
|
fastify.addHook("onRequest", async (req) => {
|
|
12
|
-
if (req.url.
|
|
13
|
+
if (!req.url.startsWith((options === null || options === void 0 ? void 0 : options.rootApiPath) || "/api")) {
|
|
13
14
|
return;
|
|
14
15
|
}
|
|
15
16
|
const spanName = `${req.method}-${req.url}`;
|
|
@@ -43,3 +44,7 @@ function StandardTracerFastifyRegisterHooks(fastify, standardTracer, standardLog
|
|
|
43
44
|
logger.error(error);
|
|
44
45
|
});
|
|
45
46
|
}
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
48
|
+
function OTelRequestSpan(req) {
|
|
49
|
+
return req.tracerSpanApi;
|
|
50
|
+
}
|
package/package.json
CHANGED
|
@@ -12,19 +12,20 @@ import {
|
|
|
12
12
|
ATTR_HTTP_RESPONSE_STATUS_CODE,
|
|
13
13
|
ATTR_URL_PATH,
|
|
14
14
|
} from "@opentelemetry/semantic-conventions";
|
|
15
|
-
import { FastifyInstance,
|
|
15
|
+
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
|
16
16
|
|
|
17
17
|
const propagator = new W3CTraceContextPropagator();
|
|
18
18
|
|
|
19
19
|
export function StandardTracerFastifyRegisterHooks(
|
|
20
20
|
fastify: FastifyInstance,
|
|
21
21
|
standardTracer: StandardTracer,
|
|
22
|
-
standardLogger: StandardLogger
|
|
22
|
+
standardLogger: StandardLogger,
|
|
23
|
+
options?: StandardTracerFastifyRegisterHooksOptions
|
|
23
24
|
): void {
|
|
24
25
|
const logger = standardLogger.createModuleLogger("Fastify");
|
|
25
26
|
|
|
26
27
|
fastify.addHook("onRequest", async (req: FastifyRequest) => {
|
|
27
|
-
if (req.url.
|
|
28
|
+
if (!req.url.startsWith(options?.rootApiPath || "/api")) {
|
|
28
29
|
return;
|
|
29
30
|
}
|
|
30
31
|
|
|
@@ -70,3 +71,12 @@ export function StandardTracerFastifyRegisterHooks(
|
|
|
70
71
|
}
|
|
71
72
|
);
|
|
72
73
|
}
|
|
74
|
+
|
|
75
|
+
export interface StandardTracerFastifyRegisterHooksOptions {
|
|
76
|
+
rootApiPath?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
80
|
+
export function OTelRequestSpan(req: any): Span {
|
|
81
|
+
return req.tracerSpanApi;
|
|
82
|
+
}
|