|
| 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). |
0 commit comments