@appweaver/skill 0.0.0-stage → 3.0.0
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 +21 -0
- package/README.md +29 -2
- package/index.d.ts +39 -0
- package/index.js +105 -0
- package/package.json +41 -3
- package/skill/GUIDELINES.md +383 -0
- package/skill/SKILL.md +120 -0
- package/skill/references/ai.md +231 -0
- package/skill/references/audit.md +218 -0
- package/skill/references/cache.md +233 -0
- package/skill/references/cli.md +729 -0
- package/skill/references/client.md +884 -0
- package/skill/references/configuration.md +1159 -0
- package/skill/references/database.md +345 -0
- package/skill/references/dependency-injection.md +301 -0
- package/skill/references/errors.md +466 -0
- package/skill/references/events.md +203 -0
- package/skill/references/logging.md +156 -0
- package/skill/references/mailer.md +393 -0
- package/skill/references/mcp.md +245 -0
- package/skill/references/modules.md +500 -0
- package/skill/references/patterns.md +1216 -0
- package/skill/references/queue.md +281 -0
- package/skill/references/realtime.md +572 -0
- package/skill/references/resources.md +2228 -0
- package/skill/references/scheduler.md +228 -0
- package/skill/references/security.md +1185 -0
- package/skill/references/sms.md +143 -0
- package/skill/references/spatial.md +197 -0
- package/skill/references/storage.md +500 -0
- package/skill/references/telemetry.md +202 -0
- package/skill/references/templates.md +488 -0
- package/skill/references/timeseries.md +312 -0
- package/skill/references/workflow.md +223 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# Telemetry (OpenTelemetry)
|
|
2
|
+
|
|
3
|
+
Appweaver is instrumented with [OpenTelemetry](https://opentelemetry.io): the framework records traces and metrics
|
|
4
|
+
through the OpenTelemetry API, and the `@appweaver/opentelemetry` module starts the SDK exporting them, with the logs,
|
|
5
|
+
over OTLP. The config keys are listed in [configuration.md](./configuration.md#telemetry-telemetry_).
|
|
6
|
+
|
|
7
|
+
Two parts, on purpose:
|
|
8
|
+
|
|
9
|
+
- **Instrumentation** — in `@appweaver/common`, `core`, `data`, `web` and the drivers, through `@opentelemetry/api`
|
|
10
|
+
only. It records nothing, at no cost, until an SDK is registered, and reaches _any_ registered SDK.
|
|
11
|
+
- **SDK** — `@appweaver/opentelemetry`, enabled by installing it (`weaver module add telemetry`, or
|
|
12
|
+
`npm install @appweaver/opentelemetry`). It reads the `telemetry` config, exports over OTLP (HTTP), and is flushed
|
|
13
|
+
and stopped with the application.
|
|
14
|
+
|
|
15
|
+
There is no telemetry contract and no provider per vendor: OTLP is the standard every backend ingests, see
|
|
16
|
+
[Backends](#backends).
|
|
17
|
+
|
|
18
|
+
## What is recorded
|
|
19
|
+
|
|
20
|
+
| Part (`telemetry.instrumentations.<key>`) | Spans | Metrics |
|
|
21
|
+
| ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
22
|
+
| `http` (`@appweaver/web`) | A server span per request, `GET /api/posts/:id`, continuing the `traceparent` header, with the status and the `user.id` | `http.server.request.duration`, `http.server.active_requests` |
|
|
23
|
+
| `database` (`@appweaver/data`) | A client span per Prisma operation, `findMany Post` or `$queryRaw`, without the query and its arguments | `db.client.operation.duration` |
|
|
24
|
+
| `queue` (`memory`, `bullmq`) | A producer span `send <queue>` and a consumer span `process <queue>` per attempt, in the trace of the sender | `messaging.client.sent.messages`, `messaging.process.duration` |
|
|
25
|
+
| `scheduler` (`@appweaver/cron`) | The root span of a new trace per run, `scheduler <name>` | `appweaver.scheduler.job.duration` |
|
|
26
|
+
| `mailer`, `sms` | A client span per sent email (`mailer send`) and text message (`sms send`), inside the span of their queue job | — |
|
|
27
|
+
| `cache` | — | `appweaver.cache.requests` (`appweaver.cache.result`: `hit`, `miss`) |
|
|
28
|
+
| `task` (`weaver run`) | The root span of the task, `task <name>`, exported when the task ends | — |
|
|
29
|
+
| `realtime` (`@appweaver/realtime`) | A server span per action call, `realtime action <name>`, over the WebSocket or HTTP, until the end of a streaming action; a producer span per stream opened, `realtime stream <name>` | `appweaver.realtime.connections`, `appweaver.realtime.events` (`appweaver.realtime.outcome`: `delivered`, `denied`, `dropped`), `appweaver.realtime.messages`, `appweaver.realtime.streams`, `appweaver.realtime.stream.chunks`, `appweaver.realtime.stream.bytes`, `appweaver.realtime.input.bytes` |
|
|
30
|
+
| `ai` (`@appweaver/ai`) | A client span per model call, `chat <model>` or `embeddings <model>`, with the `gen_ai.*` attributes, and an `execute_tool <name>` span per tool call | `gen_ai.client.token.usage` (`gen_ai.token.type`: `input`, `output`), `gen_ai.client.operation.duration` |
|
|
31
|
+
| `mcp` (`@appweaver/mcp`) | A server span per tool call, `tools/call <tool>`, with `mcp.method.name` and `gen_ai.tool.name` | `mcp.server.operation.duration` |
|
|
32
|
+
| `audit` (`@appweaver/data`) | — | `appweaver.audit.written`, `appweaver.audit.dropped` |
|
|
33
|
+
| `fetch` (`@appweaver/opentelemetry`) | A client span per outbound `fetch` request, which also sends the `traceparent` header. Node.js only | `http.client.request.duration` |
|
|
34
|
+
|
|
35
|
+
The names and the attributes follow the OpenTelemetry semantic conventions (`http.*`, `db.*`, `messaging.*`, `gen_ai.*`), the ones
|
|
36
|
+
of the framework are prefixed with `appweaver.`.
|
|
37
|
+
|
|
38
|
+
- **HTTP**: only a `5xx` response fails the span (`error.type`, the exception). A `4xx` keeps it unset and adds the
|
|
39
|
+
error code as `appweaver.error.code`. The health check routes are left out (`telemetry.http.healthCheck`), and so
|
|
40
|
+
are the paths of `telemetry.http.ignorePaths` (`['/public/*', '/favicon.ico']`). An unknown route is named after its
|
|
41
|
+
method alone.
|
|
42
|
+
- **Queue**: the trace context travels with the job, so the request that sent a job, the job and everything the worker
|
|
43
|
+
does are one trace, whichever process runs the worker. BullMQ stores it in the `telemetry.metadata` option of the
|
|
44
|
+
job, which `bullmq-otel` of another service reads too.
|
|
45
|
+
- **Scheduler**: name a job to name its spans and its metric series, `addJob({ name: 'reports:daily', ... })`.
|
|
46
|
+
- **Problem details**: an error response of a traced request carries the `traceId` next to the `requestId`, to find
|
|
47
|
+
its trace from a bug report.
|
|
48
|
+
|
|
49
|
+
## Configuration
|
|
50
|
+
|
|
51
|
+
```jsonc
|
|
52
|
+
// appweaver.json
|
|
53
|
+
"telemetry": {
|
|
54
|
+
"endpoint": "http://${OTEL_HOST:-localhost}:4318",
|
|
55
|
+
"headers": ["x-api-key=${OTEL_API_KEY}"],
|
|
56
|
+
"traces": { "sampleRatio": 0.2 },
|
|
57
|
+
"logs": { "enabled": true, "level": "warn" },
|
|
58
|
+
"http": { "ignorePaths": ["/public/*"] },
|
|
59
|
+
"instrumentations": { "cache": false }
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
- `telemetry.enabled: false` turns everything off: the instrumentation, the log correlation and the SDK. A project
|
|
64
|
+
scaffolded with the `telemetry` module sets it in the `test` environment.
|
|
65
|
+
- `telemetry.exporter`: `otlp` (default), `console` (prints the telemetry, for development) or `none` (the default of
|
|
66
|
+
the `test` environment, exporting only through the processors of `configureTelemetry`).
|
|
67
|
+
- The traces and the metrics are exported by default, the logs with `telemetry.logs.enabled`.
|
|
68
|
+
- The standard `OTEL_*` environment variables are read where the config sets nothing: `OTEL_EXPORTER_OTLP_ENDPOINT`,
|
|
69
|
+
`OTEL_EXPORTER_OTLP_HEADERS`, `OTEL_SERVICE_NAME`, `OTEL_RESOURCE_ATTRIBUTES`. The `telemetry` keys win.
|
|
70
|
+
- The service is described by `service.name` (`telemetry.serviceName`, else `app.name`), `service.version`
|
|
71
|
+
(`app.version`) and `deployment.environment.name` (`app.env`), plus `telemetry.resourceAttributes`
|
|
72
|
+
(`['service.namespace=shop']`).
|
|
73
|
+
|
|
74
|
+
## Logs
|
|
75
|
+
|
|
76
|
+
The `log` config is untouched, telemetry adds two things to the logs:
|
|
77
|
+
|
|
78
|
+
- **Correlation** (`telemetry.logCorrelation`, on by default): every record written inside a span carries its
|
|
79
|
+
`trace_id`, `span_id` and `trace_flags`, on the console and in the log file, with or without the SDK module.
|
|
80
|
+
- **Export** (`telemetry.logs.enabled`, off by default): the records of every logger are also sent over OTLP, from
|
|
81
|
+
`telemetry.logs.level` up, with the logger name as `appweaver.logger`, the error as `exception.*` and the trace of
|
|
82
|
+
their span. The level never lowers what a logger writes (`log.level`, `log.levels`).
|
|
83
|
+
|
|
84
|
+
## Custom spans and metrics
|
|
85
|
+
|
|
86
|
+
`@appweaver/common` exports the helpers the framework itself uses, so application code needs no OpenTelemetry import:
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { currentSpan, telemetryCounter, withSpan } from '@appweaver/common';
|
|
90
|
+
|
|
91
|
+
const invoices = telemetryCounter('billing.invoices', {
|
|
92
|
+
description: 'Invoices issued.'
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
export async function issueInvoice(orderId: number) {
|
|
96
|
+
// The span of the request or the job this runs in
|
|
97
|
+
currentSpan()?.setAttribute('order.id', orderId);
|
|
98
|
+
|
|
99
|
+
// Ends when the promise settles, an error is recorded on it and thrown again
|
|
100
|
+
return withSpan(
|
|
101
|
+
'invoice render',
|
|
102
|
+
{ attributes: { 'order.id': orderId } },
|
|
103
|
+
async (span) => {
|
|
104
|
+
const pdf = await render(orderId);
|
|
105
|
+
span.setAttribute('invoice.size', pdf.length);
|
|
106
|
+
invoices().add(1, { currency: 'EUR' });
|
|
107
|
+
return pdf;
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
| Helper | Purpose |
|
|
114
|
+
| -------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- |
|
|
115
|
+
| `withSpan(name, options, fn)` | Runs `fn` in a span, the parent of what it calls. `options`: `kind`, `attributes`, `root`, `parent` |
|
|
116
|
+
| `startSpan(name, options)` / `runInTraceContext(context, fn)` | A span ended by the caller, and running code as its child |
|
|
117
|
+
| `currentSpan()` / `currentTraceId()` | The active span and the id of its trace |
|
|
118
|
+
| `telemetryCounter` / `telemetryUpDownCounter` / `telemetryHistogram` | Declare a metric once at module level, call the result to record: `counter().add(1)` |
|
|
119
|
+
| `injectTraceContext()` / `extractTraceContext(carrier)` | Carry a trace through a transport of your own (a message, a file, a webhook) |
|
|
120
|
+
| `traceQueueSend` / `traceQueueProcess` | The producer and consumer spans of a custom `Queue` driver |
|
|
121
|
+
| `telemetryEnabled(part?)` | Whether telemetry, or one part of it, is on |
|
|
122
|
+
| `telemetryTracer()` / `telemetryMeter()` | The tracer and the meter, for anything else |
|
|
123
|
+
|
|
124
|
+
Keep span names free of ids and other high-cardinality values, put them into attributes. For the full API install
|
|
125
|
+
`@opentelemetry/api` (the version of `@appweaver/common`), it shares the same global SDK.
|
|
126
|
+
|
|
127
|
+
## Backends
|
|
128
|
+
|
|
129
|
+
| Route | How | Use for |
|
|
130
|
+
| -------------------- | --------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
|
|
131
|
+
| OTLP config | `telemetry.endpoint` and `telemetry.headers`, or the `OTEL_EXPORTER_OTLP_*` variables | Honeycomb, Datadog, Sentry, Grafana, New Relic, an OpenTelemetry Collector |
|
|
132
|
+
| `configureTelemetry` | Add or replace exporters, processors, the sampler, the id generator, instrumentations | A vendor exporter, AWS X-Ray ids and propagator, Prisma engine spans |
|
|
133
|
+
| Own SDK | Do not install `@appweaver/opentelemetry`, start the SDK of the vendor before the application | `dd-trace`, the Sentry SDK, the Azure Monitor distro, ADOT |
|
|
134
|
+
|
|
135
|
+
```jsonc
|
|
136
|
+
// Honeycomb
|
|
137
|
+
"telemetry": { "endpoint": "https://api.honeycomb.io", "headers": ["x-honeycomb-team=${HONEYCOMB_API_KEY}"] }
|
|
138
|
+
// A collector or an agent on the host (Datadog Agent, Grafana Alloy, ADOT for X-Ray, ...)
|
|
139
|
+
"telemetry": { "endpoint": "http://localhost:4318" }
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
A backend that takes no OTLP directly (AWS X-Ray, Azure Application Insights) is reached through a collector, or its
|
|
143
|
+
exporter with `configureTelemetry`. Call it before the application starts, in an application file or the `main` part
|
|
144
|
+
of a module:
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
// src/features/telemetry/index.ts
|
|
148
|
+
import { configureTelemetry } from '@appweaver/opentelemetry';
|
|
149
|
+
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace';
|
|
150
|
+
import { PrismaInstrumentation } from '@prisma/instrumentation';
|
|
151
|
+
|
|
152
|
+
configureTelemetry((options) => {
|
|
153
|
+
// The parts created from the config, changed in place
|
|
154
|
+
options.spanProcessors.push(
|
|
155
|
+
new BatchSpanProcessor({ exporter: new MyVendorExporter() })
|
|
156
|
+
);
|
|
157
|
+
options.instrumentations.push(new PrismaInstrumentation());
|
|
158
|
+
options.resourceAttributes['service.namespace'] = 'shop';
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
`options` holds `resourceAttributes`, `spanProcessors`, `sampler`, `propagator`, `metricReaders`, `views`,
|
|
163
|
+
`logRecordProcessors`, `instrumentations`, `idGenerator` and `contextManager`. With `telemetry.exporter: 'none'` only
|
|
164
|
+
what the function adds is exported.
|
|
165
|
+
|
|
166
|
+
When another OpenTelemetry SDK is already registered, `@appweaver/opentelemetry` leaves it in place with a warning,
|
|
167
|
+
and the framework spans go to it. The instrumentations patching a library when it is imported (`pg`, `ioredis`) are not
|
|
168
|
+
included: the Prisma operations and the queue jobs are traced by the framework, and such an instrumentation must be
|
|
169
|
+
loaded with `node --import` before the application.
|
|
170
|
+
|
|
171
|
+
To look at traces locally without a vendor, run Jaeger and keep the default endpoint:
|
|
172
|
+
|
|
173
|
+
```sh
|
|
174
|
+
docker run --rm -p 16686:16686 -p 4318:4318 jaegertracing/jaeger
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Testing
|
|
178
|
+
|
|
179
|
+
`recordTelemetry` of `@appweaver/testing` registers an in-memory SDK, so a test reads the spans and the metrics of the
|
|
180
|
+
framework and of the application:
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
import { recordTelemetry, TelemetryRecorder } from '@appweaver/testing';
|
|
184
|
+
|
|
185
|
+
let telemetry: TelemetryRecorder;
|
|
186
|
+
beforeEach(() => (telemetry = recordTelemetry()));
|
|
187
|
+
afterEach(() => telemetry.stop());
|
|
188
|
+
|
|
189
|
+
test('traces the invoice', async () => {
|
|
190
|
+
await issueInvoice(7);
|
|
191
|
+
|
|
192
|
+
expect(telemetry.span('invoice render').attributes).toMatchObject({
|
|
193
|
+
'order.id': 7
|
|
194
|
+
});
|
|
195
|
+
expect(
|
|
196
|
+
(await telemetry.metric('billing.invoices'))?.dataPoints[0].value
|
|
197
|
+
).toBe(1);
|
|
198
|
+
});
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
`TelemetryService` of `@appweaver/opentelemetry` (`inject(TelemetryService)`) exposes the started `sdk` and
|
|
202
|
+
`forceFlush()`, for a process exiting without stopping the application.
|