@vicacha-devs/strapi-plugin-opentelemetry 0.0.0-stage → 1.0.1
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/LICENSE.md +21 -0
- package/README.md +110 -2
- package/dist/admin/index.d.ts +7 -0
- package/dist/admin/index.js +12 -0
- package/dist/admin/index.mjs +12 -0
- package/dist/admin/pluginId.d.ts +1 -0
- package/dist/server/config.d.ts +26 -0
- package/dist/server/documents.d.ts +12 -0
- package/dist/server/index.d.ts +22 -0
- package/dist/server/index.js +544 -0
- package/dist/server/index.mjs +544 -0
- package/dist/server/logger-hook.d.ts +12 -0
- package/dist/server/middleware.d.ts +20 -0
- package/dist/server/sanitize.d.ts +22 -0
- package/dist/server/sdk.d.ts +10 -0
- package/package.json +97 -3
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vicacha Devs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,111 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@vicacha-devs/strapi-plugin-opentelemetry`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Vendor-neutral **OpenTelemetry** error tracking and performance monitoring (APM) plugin for **Strapi v5**.
|
|
4
|
+
|
|
5
|
+
Instruments Node.js, HTTP/Koa requests, PostgreSQL/SQLite queries, Strapi v5 Document Service operations, and caught plugin errors (`strapi.log.error`), exporting traces and metrics over **OTLP/HTTP** to **SigNoz**, **Grafana Tempo / Alloy**, **Sentry OTLP**, **Datadog**, **Jaeger**, or any OpenTelemetry Collector.
|
|
6
|
+
|
|
7
|
+
## Key Features
|
|
8
|
+
|
|
9
|
+
- **Automatic Node.js & Database Instrumentation**: Traces incoming/outgoing HTTP (`http`, `undici`) and database calls (`pg`, etc.) via `@opentelemetry/auto-instrumentations-node` (with noisy `fs` instrumentation disabled).
|
|
10
|
+
- **HTTP Request Tracking & APM Metrics**: Records `http.server.request.duration` histograms, `http.server.errors` counters, `http.route`, `http.response.status_code`, and authenticated `enduser.id`.
|
|
11
|
+
- **Strapi v5 Document Service Tracing**: Wraps `strapi.documents.use(...)` in child spans (`strapi.document.<action> <uid>`) with `strapi.document.uid`, `strapi.document.action`, and `strapi.plugin` attributes.
|
|
12
|
+
- **Per-Plugin Failure Attribution**: Automatically extracts `strapi.plugin` from `plugin::<name>.<model>` UIDs and plugin route paths (e.g., `/api/bookings/...` -> `bookings`).
|
|
13
|
+
- **Caught Plugin Error Capture**: Hooks `strapi.log.error` so errors caught and logged inside plugins, email lifecycles, or background jobs are recorded as `exception` span events and counted in `strapi.log.errors`.
|
|
14
|
+
- **Log-Trace Correlation**: Logs `trace_id`, `span_id`, `method`, `path`, and `plugin` on unhandled request errors.
|
|
15
|
+
- **Built-In Secret & PII Redaction**: Scrubs `Bearer`/`Basic` tokens, URL credentials (`user:pass@`), sensitive query parameters (`token`, `api_key`, `secret`, `password`, etc.), and email addresses from error messages, stack traces, and outgoing `url.full`/`http.url`/`url.query` span attributes before export.
|
|
16
|
+
- **Graceful Shutdown**: Flushes buffered spans and metrics in Strapi's `destroy()` lifecycle hook.
|
|
17
|
+
|
|
18
|
+
## Installation & Configuration
|
|
19
|
+
|
|
20
|
+
Add `@vicacha-devs/strapi-plugin-opentelemetry` to your Strapi v5 project and configure it in `config/plugins.ts`:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
export default ({ env }) => ({
|
|
24
|
+
opentelemetry: {
|
|
25
|
+
enabled: env.bool("OTEL_ENABLED", true),
|
|
26
|
+
config: {
|
|
27
|
+
serviceName: env("OTEL_SERVICE_NAME", "strapi-backend"),
|
|
28
|
+
serviceVersion: env("npm_package_version", "1.0.0"),
|
|
29
|
+
environment: env("NODE_ENV", "development"),
|
|
30
|
+
otlpEndpoint: env("OTEL_EXPORTER_OTLP_ENDPOINT", ""),
|
|
31
|
+
sampleRate: Number(env("OTEL_TRACES_SAMPLE_RATE", "1.0")),
|
|
32
|
+
ignorePaths: ["/_health", "/favicon.ico"],
|
|
33
|
+
captureDocumentService: true,
|
|
34
|
+
consoleExporter: env.bool("OTEL_CONSOLE_EXPORTER", false),
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Configuration Reference
|
|
41
|
+
|
|
42
|
+
All options are validated at Strapi boot time by `validateOtelConfig`. Standard OpenTelemetry environment variables (`OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_SERVICE_NAME`, `OTEL_EXPORTER_OTLP_HEADERS`) are automatically merged.
|
|
43
|
+
|
|
44
|
+
| Option | Env Variable | Type | Default | Description |
|
|
45
|
+
| :--- | :--- | :--- | :--- | :--- |
|
|
46
|
+
| `enabled` | `OTEL_ENABLED` | `boolean` | `true` | Enable or disable SDK, middleware, and hooks |
|
|
47
|
+
| `serviceName` | `OTEL_SERVICE_NAME` | `string` | `"strapi"` | Service name reported in `service.name` |
|
|
48
|
+
| `serviceVersion` | — | `string` | `"1.0.0"` | Service version reported in `service.version` |
|
|
49
|
+
| `environment` | `NODE_ENV` | `string` | `process.env.NODE_ENV \|\| "development"` | Reported in `deployment.environment.name` |
|
|
50
|
+
| `otlpEndpoint` | `OTEL_EXPORTER_OTLP_ENDPOINT` | `string` | `""` | Base OTLP/HTTP URL (exports to `/v1/traces` and `/v1/metrics`) |
|
|
51
|
+
| `otlpHeaders` | `OTEL_EXPORTER_OTLP_HEADERS` | `Record<string, string>` | `{}` | Auth/tenant headers (env format: `k1=v1,k2=v2`) |
|
|
52
|
+
| `sampleRate` | `OTEL_TRACES_SAMPLE_RATE` | `number` (`0..1`) | `1.0` | Root trace sampling ratio wrapped in `ParentBasedSampler` |
|
|
53
|
+
| `ignorePaths` | — | `string[]` | `["/_health", "/favicon.ico"]` | URL path prefixes excluded from HTTP tracing/metrics |
|
|
54
|
+
| `captureDocumentService` | — | `boolean` | `true` | Wrap `strapi.documents.use(...)` calls in child spans |
|
|
55
|
+
| `consoleExporter` | `OTEL_CONSOLE_EXPORTER` | `boolean` | `false` | Export spans to stdout when `otlpEndpoint` is empty |
|
|
56
|
+
| `metricExportIntervalMs` | — | `number` (`>= 1000`) | `60000` | Interval in ms for `PeriodicExportingMetricReader` |
|
|
57
|
+
|
|
58
|
+
## Telemetry Emitted
|
|
59
|
+
|
|
60
|
+
### Span Attributes
|
|
61
|
+
- **HTTP Requests**: `http.request.method`, `url.path`, `http.route`, `http.response.status_code`, `strapi.plugin` (when matched), `enduser.id` (when authenticated).
|
|
62
|
+
- **Document Service**: `strapi.document.uid`, `strapi.document.action`, `strapi.plugin` (for `plugin::*` UIDs).
|
|
63
|
+
- **Errors**: `exception` span event with sanitized `Error` + `SpanStatusCode.ERROR` on unhandled exceptions, HTTP `>= 500` responses, and `strapi.log.error(...)` calls.
|
|
64
|
+
|
|
65
|
+
### Metrics
|
|
66
|
+
- `http.server.request.duration` (Histogram, `ms`) — Request latency by method, route, status/error type, and `strapi.plugin`.
|
|
67
|
+
- `http.server.errors` (Counter) — Count of `>= 500` and unhandled HTTP errors.
|
|
68
|
+
- `strapi.log.errors` (Counter) — Count of errors logged via `strapi.log.error`.
|
|
69
|
+
|
|
70
|
+
## Security & Privacy
|
|
71
|
+
|
|
72
|
+
- **Zero body/header logging**: Request/response payloads, `Authorization`, `Cookie`, and user emails/names are never attached to spans.
|
|
73
|
+
- **Automatic redaction**: `SanitizingSpanExporter`, `sanitizeError`, and `sanitizeTelemetryText` scrub `Bearer`/`Basic` tokens, URL credentials, sensitive query keys (`token`, `access_token`, `api_key`, `key`, `secret`, `password`, `auth`, `signature`, `code`), and email addresses before telemetry leaves the process.
|
|
74
|
+
- **Production checklist**:
|
|
75
|
+
- Use `https://` for `OTEL_EXPORTER_OTLP_ENDPOINT` outside localhost/private VPCs.
|
|
76
|
+
- Keep `OTEL_CONSOLE_EXPORTER=false` in production.
|
|
77
|
+
- Store `OTEL_EXPORTER_OTLP_HEADERS` in your secrets manager.
|
|
78
|
+
|
|
79
|
+
## Collector Examples
|
|
80
|
+
|
|
81
|
+
- **SigNoz / Grafana Alloy / Local OTel Collector:**
|
|
82
|
+
```env
|
|
83
|
+
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
|
|
84
|
+
```
|
|
85
|
+
- **Sentry OTLP Ingest:**
|
|
86
|
+
```env
|
|
87
|
+
OTEL_EXPORTER_OTLP_ENDPOINT=https://o0.ingest.sentry.io/api/<project-id>/integration/otlp
|
|
88
|
+
OTEL_EXPORTER_OTLP_HEADERS="x-sentry-auth=sentry sentry_key=<public-key>"
|
|
89
|
+
```
|
|
90
|
+
- **Local Development (stdout only):**
|
|
91
|
+
```env
|
|
92
|
+
OTEL_CONSOLE_EXPORTER=true
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Custom Instrumentation
|
|
96
|
+
|
|
97
|
+
Use `@opentelemetry/api` anywhere in your Strapi services, controllers, or plugins—spans will automatically nest under the active request trace:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { trace } from "@opentelemetry/api";
|
|
101
|
+
|
|
102
|
+
const tracer = trace.getTracer("my-plugin");
|
|
103
|
+
|
|
104
|
+
await tracer.startActiveSpan("bookings.syncCalendar", async (span) => {
|
|
105
|
+
try {
|
|
106
|
+
// your work
|
|
107
|
+
} finally {
|
|
108
|
+
span.end();
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const PLUGIN_ID = "opentelemetry";
|
|
4
|
+
const index = {
|
|
5
|
+
register(app) {
|
|
6
|
+
app.registerPlugin({
|
|
7
|
+
id: PLUGIN_ID,
|
|
8
|
+
name: PLUGIN_ID
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
exports.default = index;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const PLUGIN_ID = "opentelemetry";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface OtelPluginConfig {
|
|
2
|
+
enabled: boolean;
|
|
3
|
+
serviceName: string;
|
|
4
|
+
serviceVersion: string;
|
|
5
|
+
environment: string;
|
|
6
|
+
otlpEndpoint: string;
|
|
7
|
+
otlpHeaders: Record<string, string>;
|
|
8
|
+
sampleRate: number;
|
|
9
|
+
ignorePaths: string[];
|
|
10
|
+
captureDocumentService: boolean;
|
|
11
|
+
consoleExporter: boolean;
|
|
12
|
+
metricExportIntervalMs: number;
|
|
13
|
+
}
|
|
14
|
+
export declare const defaultConfig: OtelPluginConfig;
|
|
15
|
+
/**
|
|
16
|
+
* Parses an OTLP header string (`key1=val1,key2=val2`) into a plain object.
|
|
17
|
+
*/
|
|
18
|
+
export declare function parseOtlpHeaders(raw: string | undefined): Record<string, string>;
|
|
19
|
+
/**
|
|
20
|
+
* Merges plugin config with standard `OTEL_*` environment variables.
|
|
21
|
+
*/
|
|
22
|
+
export declare function resolveOtelConfig(rawConfig?: Partial<OtelPluginConfig>): OtelPluginConfig;
|
|
23
|
+
/**
|
|
24
|
+
* Validates the OpenTelemetry plugin configuration at Strapi boot time.
|
|
25
|
+
*/
|
|
26
|
+
export declare function validateOtelConfig(pluginConfig: unknown): void;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { OtelPluginConfig } from './config';
|
|
2
|
+
export interface DocumentServiceContext {
|
|
3
|
+
uid: string;
|
|
4
|
+
action: string;
|
|
5
|
+
}
|
|
6
|
+
export interface StrapiDocumentsLike {
|
|
7
|
+
use: (middleware: (context: DocumentServiceContext, next: () => Promise<unknown>) => Promise<unknown>) => void;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Instruments Strapi v5 Document Service operations with child spans and error recording.
|
|
11
|
+
*/
|
|
12
|
+
export declare function registerDocumentServiceInstrumentation(documents: StrapiDocumentsLike | undefined, config: OtelPluginConfig): void;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Core } from '@strapi/strapi';
|
|
2
|
+
import { validateOtelConfig, OtelPluginConfig } from './config';
|
|
3
|
+
import { KoaLikeContext } from './middleware';
|
|
4
|
+
declare const _default: {
|
|
5
|
+
register({ strapi }: {
|
|
6
|
+
strapi: Core.Strapi;
|
|
7
|
+
}): void;
|
|
8
|
+
bootstrap({ strapi }: {
|
|
9
|
+
strapi: Core.Strapi;
|
|
10
|
+
}): void;
|
|
11
|
+
destroy(): Promise<void>;
|
|
12
|
+
config: {
|
|
13
|
+
default: OtelPluginConfig;
|
|
14
|
+
validator: typeof validateOtelConfig;
|
|
15
|
+
};
|
|
16
|
+
middlewares: {
|
|
17
|
+
tracker: (_config: unknown, { strapi }: {
|
|
18
|
+
strapi: Core.Strapi;
|
|
19
|
+
}) => (ctx: KoaLikeContext, next: () => Promise<void>) => Promise<void>;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
export default _default;
|