Skip to content

Commit b7de179

Browse files
committed
test server and docs for fastify
1 parent 61a081d commit b7de179

File tree

5 files changed

+94
-11
lines changed

5 files changed

+94
-11
lines changed

packages/express/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export OTEL_RESOURCE_ATTRIBUTES=at-project-key="<YOUR_API_KEY>" # Adds your API
2323
export OTEL_EXPORTER_OTLP_PROTOCOL="grpc" #Specifies the protocol to use for the OpenTelemetry exporter.
2424
export NODE_OPTIONS="--require @opentelemetry/auto-instrumentations-node/register"
2525

26-
2726
node server.js # starting your express server
2827
```
2928

packages/fastify/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<p>
2+
<img src="https://apitoolkit.io/assets/img/logo-full.svg" alt="APIToolkit" width="250px" />
3+
</p>
4+
5+
APIToolkit fastify Middleware is a middleware that can be used to monitor HTTP requests. It is provides additional functionalities on top of the open telemetry instrumentation which creates a custom span for each request capturing details about the request including request and response bodies.
6+
7+
### Installation
8+
9+
Run the following command to install the fastify js package from your projects root:
10+
11+
```sh
12+
npm install --save apitoolkit-fastify @opentelemetry/api @opentelemetry/auto-instrumentations-node
13+
```
14+
15+
### Setup Open Telemetry
16+
17+
Setting up open telemetry allows you to send traces, metrics and logs to the APIToolkit platform.
18+
19+
```sh
20+
export OTEL_EXPORTER_OTLP_ENDPOINT="http://otelcol.apitoolkit.io:4317"
21+
export OTEL_SERVICE_NAME="my-service" # Specifies the name of the service.
22+
export OTEL_RESOURCE_ATTRIBUTES=at-project-key="<YOUR_API_KEY>" # Adds your API KEY to the resource.
23+
export OTEL_EXPORTER_OTLP_PROTOCOL="grpc" #Specifies the protocol to use for the OpenTelemetry exporter.
24+
export NODE_OPTIONS="--require @opentelemetry/auto-instrumentations-node/register"
25+
26+
node server.js # starting your fastify server
27+
```
28+
29+
### HTTP Requests Monitoring
30+
31+
You can monitor http requests using APIToolkit's fastify middleware, this allows you to monitor all your http requests. including headers, response time, response status code, request body, response body, etc.
32+
33+
```js
34+
import fastify from "fastify";
35+
import { APIToolkit } from "./index";
36+
import axios from "axios";
37+
38+
const fastifyServer = fastify({});
39+
const apitoolkit = APIToolkit.NewClient({
40+
fastify: fastifyServer,
41+
serviceName: "my-service",
42+
serviceVersion: "1.0.0",
43+
tags: [],
44+
debug: true,
45+
monitorAxios: axios, // optional axios instance to monitor all axios requests
46+
});
47+
48+
apitoolkit.initializeHooks();
49+
50+
fastifyServer.get("/", async (request, reply) => {
51+
const response = await axios.get(
52+
"https://jsonplaceholder.typicode.com/todos/1"
53+
);
54+
return response.data;
55+
});
56+
57+
fastifyServer.listen({ port: 3000 });
58+
```
59+
60+
#### Quick overview of the configuration parameters
61+
62+
An object with the following optional fields can be passed to the middleware to configure it:
63+
64+
| Option | Description |
65+
| --------------------- | ------------------------------------------------------------------------------------------------- |
66+
| `debug` | Set to `true` to enable debug mode. |
67+
| `serviceName` | A defined string name of your application. |
68+
| `tags` | A list of defined tags for your services (used for grouping and filtering data on the dashboard). |
69+
| `serviceVersion` | A defined string version of your application (used for further debugging on the dashboard). |
70+
| `redactHeaders` | A list of HTTP header keys to redact. |
71+
| `redactResponseBody` | A list of JSONPaths from the response body to redact. |
72+
| `redactRequestBody` | A list of JSONPaths from the request body to redact. |
73+
| `captureRequestBody` | Default `false`, set to `true` if you want to capture the request body. |
74+
| `captureResponseBody` | Default `false`, set to `true` if you want to capture the response body. |
75+
76+
<br />
77+
78+
> [!IMPORTANT]
79+
>
80+
> To learn more configuration options (redacting fields, error reporting, outgoing requests, etc.) and complete integration guide, please read this [SDK documentation](https://apitoolkit.io/docs/sdks/nodejs/fastifyjs/utm_campaign=devrel&utm_medium=github&utm_source=sdks_readme).

packages/fastify/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
"dependencies": {
1313
"@apitoolkit/common": "workspace:*",
1414
"@opentelemetry/api": "^1.9.0",
15+
"@opentelemetry/auto-instrumentations-node": "^0.54.0",
1516
"axios": "^1.7.9",
1617
"fastify": "^5.1.0",
1718
"uuid": "^11.0.3"
1819
},
1920
"keywords": [],
2021
"author": "",
21-
"license": "ISC"
22+
"license": "ISC",
23+
"devDependencies": {
24+
"typescript": "^5.0.0"
25+
}
2226
}

packages/fastify/src/index.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from "@apitoolkit/common";
1212
import { trace } from "@opentelemetry/api";
1313

14-
export { ReportError, observeAxios } from "@apitoolkit/common";
14+
export { ReportError as reportError, observeAxios } from "@apitoolkit/common";
1515

1616
class APIToolkit {
1717
#config: Config & { fastify: FastifyInstance };
@@ -78,7 +78,7 @@ class APIToolkit {
7878

7979
public initializeHooks() {
8080
if (this.#config.monitorAxios) {
81-
observeAxiosGlobal(this.#config.monitorAxios, {});
81+
observeAxiosGlobal(this.#config.monitorAxios, this.#config);
8282
}
8383
this.#config.fastify.addHook("preHandler", (request, _reply, done) => {
8484
if (this.#config.debug) {
@@ -90,14 +90,7 @@ class APIToolkit {
9090
const span = trace
9191
.getTracer(this.#config.serviceName || "")
9292
.startSpan("apitoolkit-http-span");
93-
94-
asyncLocalStorage.getStore()!.set("AT_client", this);
9593
asyncLocalStorage.getStore()!.set("AT_span", span);
96-
asyncLocalStorage.getStore()!.set("AT_config", {
97-
tags: this.#config.tags,
98-
serviceVersion: this.#config.serviceVersion,
99-
});
100-
10194
asyncLocalStorage.getStore()!.set("AT_errors", []);
10295
const msg_id: string = uuidv4();
10396
asyncLocalStorage.getStore()!.set("AT_msg_id", msg_id);

pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)