@devopsplaybook.io/otel-utils-fastify 0.0.1-beta4 → 0.0.1-beta7
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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/src/RequqestOTel.d.ts +4 -0
- package/dist/src/RequqestOTel.js +2 -0
- package/dist/src/StandardTracerFastify.d.ts +2 -7
- package/dist/src/StandardTracerFastify.js +7 -45
- package/index.ts +1 -0
- package/package.json +3 -3
- package/src/RequqestOTel.ts +5 -0
- package/src/StandardTracerFastify.ts +32 -74
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Span } from "@opentelemetry/sdk-trace-base";
|
|
1
|
+
import { StandardLogger, StandardTracer } from "@devopsplaybook.io/otel-utils";
|
|
3
2
|
import { FastifyInstance } from "fastify";
|
|
4
|
-
export declare function StandardTracerFastifyRegisterHooks(fastify: FastifyInstance,
|
|
5
|
-
export declare function StandardTracerFastifyInit(fastify: FastifyInstance): void;
|
|
6
|
-
export interface FastifyRequestOTel {
|
|
7
|
-
span?: Span;
|
|
8
|
-
}
|
|
3
|
+
export declare function StandardTracerFastifyRegisterHooks(fastify: FastifyInstance, standardTracer: StandardTracer, standardLogger: StandardLogger): Promise<void>;
|
|
@@ -1,69 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.StandardTracerFastifyRegisterHooks = StandardTracerFastifyRegisterHooks;
|
|
4
|
-
exports.StandardTracerFastifyInit = StandardTracerFastifyInit;
|
|
5
|
-
const otel_utils_1 = require("@devopsplaybook.io/otel-utils");
|
|
6
4
|
const api_1 = require("@opentelemetry/api");
|
|
7
5
|
const core_1 = require("@opentelemetry/core");
|
|
8
6
|
const sdk_node_1 = require("@opentelemetry/sdk-node");
|
|
9
7
|
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
10
8
|
const propagator = new core_1.W3CTraceContextPropagator();
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
async function StandardTracerFastifyRegisterHooks(fastify, standardTracer, standardLogger) {
|
|
10
|
+
const logger = standardLogger.createModuleLogger("Fastify");
|
|
13
11
|
fastify.addHook("onRequest", async (req) => {
|
|
14
12
|
if (req.url.indexOf("/api") !== 0) {
|
|
15
13
|
return;
|
|
16
14
|
}
|
|
17
|
-
let spanName = `${req.method}-${req.url}`;
|
|
18
|
-
let urlName = req.url;
|
|
19
|
-
if (config.OPENTELEMETRY_COLLECTOR_AWS) {
|
|
20
|
-
spanName = `${config.SERVICE_ID}-${config.VERSION}`;
|
|
21
|
-
urlName = `${config.SERVICE_ID}-${config.VERSION}-${req.method}-${req.url}`;
|
|
22
|
-
}
|
|
23
|
-
const callerContext = propagator.extract(api_1.ROOT_CONTEXT, req.headers, api_1.defaultTextMapGetter);
|
|
24
|
-
sdk_node_1.api.context.with(callerContext, () => {
|
|
25
|
-
const span = (0, otel_utils_1.StandardTracerStartSpan)(spanName);
|
|
26
|
-
span.setAttribute(semantic_conventions_1.ATTR_HTTP_REQUEST_METHOD, req.method);
|
|
27
|
-
span.setAttribute(semantic_conventions_1.ATTR_URL_PATH, urlName);
|
|
28
|
-
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
29
|
-
req.tracerSpanApi = span;
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
fastify.addHook("onResponse", async (req, reply) => {
|
|
33
|
-
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
34
|
-
const span = req.tracerSpanApi;
|
|
35
|
-
if (reply.statusCode > 299) {
|
|
36
|
-
span.status.code = api_1.SpanStatusCode.ERROR;
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
span.status.code = api_1.SpanStatusCode.OK;
|
|
40
|
-
}
|
|
41
|
-
span.setAttribute(semantic_conventions_1.ATTR_HTTP_RESPONSE_STATUS_CODE, reply.statusCode);
|
|
42
|
-
span.end();
|
|
43
|
-
});
|
|
44
|
-
fastify.addHook("onError", async (req, reply, error) => {
|
|
45
|
-
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
46
|
-
const span = req.tracerSpanApi;
|
|
47
|
-
span.status.code = api_1.SpanStatusCode.ERROR;
|
|
48
|
-
span.recordException(error);
|
|
49
|
-
logger.error(error);
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
function StandardTracerFastifyInit(fastify) {
|
|
53
|
-
fastify.addHook("onRequest", async (req) => {
|
|
54
15
|
const spanName = `${req.method}-${req.url}`;
|
|
55
16
|
const urlName = req.url;
|
|
56
17
|
const callerContext = propagator.extract(api_1.ROOT_CONTEXT, req.headers, api_1.defaultTextMapGetter);
|
|
57
18
|
sdk_node_1.api.context.with(callerContext, () => {
|
|
58
|
-
const span =
|
|
19
|
+
const span = standardTracer.startSpan(spanName);
|
|
59
20
|
span.setAttribute(semantic_conventions_1.ATTR_HTTP_REQUEST_METHOD, req.method);
|
|
60
21
|
span.setAttribute(semantic_conventions_1.ATTR_URL_PATH, urlName);
|
|
61
|
-
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
62
23
|
req.tracerSpanApi = span;
|
|
63
24
|
});
|
|
64
25
|
});
|
|
65
26
|
fastify.addHook("onResponse", async (req, reply) => {
|
|
66
|
-
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
67
28
|
const span = req.tracerSpanApi;
|
|
68
29
|
if (reply.statusCode > 299) {
|
|
69
30
|
span.status.code = api_1.SpanStatusCode.ERROR;
|
|
@@ -75,9 +36,10 @@ function StandardTracerFastifyInit(fastify) {
|
|
|
75
36
|
span.end();
|
|
76
37
|
});
|
|
77
38
|
fastify.addHook("onError", async (req, reply, error) => {
|
|
78
|
-
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
79
40
|
const span = req.tracerSpanApi;
|
|
80
41
|
span.status.code = api_1.SpanStatusCode.ERROR;
|
|
81
42
|
span.recordException(error);
|
|
43
|
+
logger.error(error);
|
|
82
44
|
});
|
|
83
45
|
}
|
package/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devopsplaybook.io/otel-utils-fastify",
|
|
3
|
-
"version": "0.0.1-
|
|
3
|
+
"version": "0.0.1-beta7",
|
|
4
4
|
"description": "Utility to simplify integration with Open Telemetry for Fastify API Server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Open",
|
|
@@ -20,14 +20,14 @@
|
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"fastify": "^5.5.0",
|
|
23
|
-
"@devopsplaybook.io/otel-utils": "1.0.1-
|
|
23
|
+
"@devopsplaybook.io/otel-utils": "1.0.1-beta6"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@eslint/js": "^9.34.0",
|
|
27
27
|
"@types/node": "^24.3.0",
|
|
28
28
|
"@types/sqlite3": "^5.1.0",
|
|
29
29
|
"ts-node": "^10.9.2",
|
|
30
|
-
"typescript-eslint": "^8.
|
|
30
|
+
"typescript-eslint": "^8.41.0",
|
|
31
31
|
"typescript": "^5.9.2"
|
|
32
32
|
},
|
|
33
33
|
"publishConfig": {
|
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ConfigInterfaceOTel,
|
|
3
|
-
Logger,
|
|
4
|
-
StandardTracerStartSpan,
|
|
5
|
-
} from "@devopsplaybook.io/otel-utils";
|
|
1
|
+
import { StandardLogger, StandardTracer } from "@devopsplaybook.io/otel-utils";
|
|
6
2
|
import {
|
|
7
3
|
defaultTextMapGetter,
|
|
8
4
|
ROOT_CONTEXT,
|
|
@@ -16,63 +12,22 @@ import {
|
|
|
16
12
|
ATTR_HTTP_RESPONSE_STATUS_CODE,
|
|
17
13
|
ATTR_URL_PATH,
|
|
18
14
|
} from "@opentelemetry/semantic-conventions";
|
|
19
|
-
import { FastifyInstance } from "fastify";
|
|
15
|
+
import { FastifyInstance, FastifyRequest, FastifyReply } from "fastify";
|
|
20
16
|
|
|
21
17
|
const propagator = new W3CTraceContextPropagator();
|
|
22
|
-
const logger = new Logger("StandardTracerFastify");
|
|
23
18
|
|
|
24
19
|
export async function StandardTracerFastifyRegisterHooks(
|
|
25
20
|
fastify: FastifyInstance,
|
|
26
|
-
|
|
21
|
+
standardTracer: StandardTracer,
|
|
22
|
+
standardLogger: StandardLogger
|
|
27
23
|
): Promise<void> {
|
|
28
|
-
|
|
24
|
+
const logger = standardLogger.createModuleLogger("Fastify");
|
|
25
|
+
|
|
26
|
+
fastify.addHook("onRequest", async (req: FastifyRequest) => {
|
|
29
27
|
if (req.url.indexOf("/api") !== 0) {
|
|
30
28
|
return;
|
|
31
29
|
}
|
|
32
30
|
|
|
33
|
-
let spanName = `${req.method}-${req.url}`;
|
|
34
|
-
let urlName = req.url;
|
|
35
|
-
if (config.OPENTELEMETRY_COLLECTOR_AWS) {
|
|
36
|
-
spanName = `${config.SERVICE_ID}-${config.VERSION}`;
|
|
37
|
-
urlName = `${config.SERVICE_ID}-${config.VERSION}-${req.method}-${req.url}`;
|
|
38
|
-
}
|
|
39
|
-
const callerContext = propagator.extract(
|
|
40
|
-
ROOT_CONTEXT,
|
|
41
|
-
req.headers,
|
|
42
|
-
defaultTextMapGetter
|
|
43
|
-
);
|
|
44
|
-
api.context.with(callerContext, () => {
|
|
45
|
-
const span = StandardTracerStartSpan(spanName);
|
|
46
|
-
span.setAttribute(ATTR_HTTP_REQUEST_METHOD, req.method);
|
|
47
|
-
span.setAttribute(ATTR_URL_PATH, urlName);
|
|
48
|
-
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
49
|
-
(req as any).tracerSpanApi = span;
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
fastify.addHook("onResponse", async (req, reply) => {
|
|
54
|
-
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
55
|
-
const span = (req as any).tracerSpanApi as Span;
|
|
56
|
-
if (reply.statusCode > 299) {
|
|
57
|
-
span.status.code = SpanStatusCode.ERROR;
|
|
58
|
-
} else {
|
|
59
|
-
span.status.code = SpanStatusCode.OK;
|
|
60
|
-
}
|
|
61
|
-
span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, reply.statusCode);
|
|
62
|
-
span.end();
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
fastify.addHook("onError", async (req, reply, error) => {
|
|
66
|
-
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
|
67
|
-
const span = (req as any).tracerSpanApi as Span;
|
|
68
|
-
span.status.code = SpanStatusCode.ERROR;
|
|
69
|
-
span.recordException(error);
|
|
70
|
-
logger.error(error);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export function StandardTracerFastifyInit(fastify: FastifyInstance) {
|
|
75
|
-
fastify.addHook("onRequest", async (req) => {
|
|
76
31
|
const spanName = `${req.method}-${req.url}`;
|
|
77
32
|
const urlName = req.url;
|
|
78
33
|
const callerContext = propagator.extract(
|
|
@@ -81,34 +36,37 @@ export function StandardTracerFastifyInit(fastify: FastifyInstance) {
|
|
|
81
36
|
defaultTextMapGetter
|
|
82
37
|
);
|
|
83
38
|
api.context.with(callerContext, () => {
|
|
84
|
-
const span =
|
|
39
|
+
const span = standardTracer.startSpan(spanName);
|
|
85
40
|
span.setAttribute(ATTR_HTTP_REQUEST_METHOD, req.method);
|
|
86
41
|
span.setAttribute(ATTR_URL_PATH, urlName);
|
|
87
|
-
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
88
43
|
(req as any).tracerSpanApi = span;
|
|
89
44
|
});
|
|
90
45
|
});
|
|
91
46
|
|
|
92
|
-
fastify.addHook(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
span
|
|
97
|
-
|
|
98
|
-
|
|
47
|
+
fastify.addHook(
|
|
48
|
+
"onResponse",
|
|
49
|
+
async (req: FastifyRequest, reply: FastifyReply) => {
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
51
|
+
const span = (req as any).tracerSpanApi as Span;
|
|
52
|
+
if (reply.statusCode > 299) {
|
|
53
|
+
span.status.code = SpanStatusCode.ERROR;
|
|
54
|
+
} else {
|
|
55
|
+
span.status.code = SpanStatusCode.OK;
|
|
56
|
+
}
|
|
57
|
+
span.setAttribute(ATTR_HTTP_RESPONSE_STATUS_CODE, reply.statusCode);
|
|
58
|
+
span.end();
|
|
99
59
|
}
|
|
100
|
-
|
|
101
|
-
span.end();
|
|
102
|
-
});
|
|
60
|
+
);
|
|
103
61
|
|
|
104
|
-
fastify.addHook(
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
62
|
+
fastify.addHook(
|
|
63
|
+
"onError",
|
|
64
|
+
async (req: FastifyRequest, reply: FastifyReply, error) => {
|
|
65
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
66
|
+
const span = (req as any).tracerSpanApi as Span;
|
|
67
|
+
span.status.code = SpanStatusCode.ERROR;
|
|
68
|
+
span.recordException(error);
|
|
69
|
+
logger.error(error);
|
|
70
|
+
}
|
|
71
|
+
);
|
|
114
72
|
}
|