@nestjs/observe 0.3.21 → 0.3.23
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/README.md +100 -14
- package/dist/agent/degraded-ingest.protocol.d.ts +14 -0
- package/dist/agent/degraded-ingest.protocol.js +19 -0
- package/dist/agent/detached-observe-worker.assembly.d.ts +2 -0
- package/dist/agent/detached-observe-worker.assembly.js +5 -1
- package/dist/agent/detached-observe-worker.d.ts +10 -0
- package/dist/agent/detached-observe-worker.js +27 -2
- package/dist/agent/observe-agent.shared-buffer.d.ts +63 -1
- package/dist/agent/observe-agent.shared-buffer.js +117 -2
- package/dist/agent/observe-agent.worker.d.ts +27 -20
- package/dist/agent/observe-agent.worker.js +109 -62
- package/dist/agent/observe-reporting.util.d.ts +35 -0
- package/dist/agent/observe-reporting.util.js +69 -0
- package/dist/interfaces/observe-options.interface.d.ts +100 -8
- package/dist/observe.constants.d.ts +17 -0
- package/dist/observe.constants.js +17 -0
- package/dist/observe.module.js +53 -4
- package/dist/otlp/otlp-encoders.d.ts +26 -0
- package/dist/otlp/otlp-encoders.js +296 -0
- package/dist/otlp/otlp-export-grant.d.ts +34 -0
- package/dist/otlp/otlp-export-grant.js +22 -0
- package/dist/otlp/otlp-options.interface.d.ts +59 -0
- package/dist/otlp/otlp-options.interface.js +1 -0
- package/dist/otlp/otlp-settings.util.d.ts +38 -0
- package/dist/otlp/otlp-settings.util.js +154 -0
- package/dist/otlp/otlp-span.mapper.d.ts +73 -0
- package/dist/otlp/otlp-span.mapper.js +345 -0
- package/dist/otlp/otlp-trace-exporter.service.d.ts +130 -0
- package/dist/otlp/otlp-trace-exporter.service.js +497 -0
- package/dist/outgoing/http.integration.d.ts +36 -3
- package/dist/outgoing/http.integration.js +84 -17
- package/dist/outgoing/outgoing-observe-agent.service.d.ts +14 -1
- package/dist/outgoing/outgoing-observe-agent.service.js +24 -9
- package/dist/outgoing/outgoing-span.recorder.d.ts +11 -1
- package/dist/outgoing/outgoing-span.recorder.js +8 -2
- package/dist/outgoing/sql-statement.util.js +2 -2
- package/dist/protocols/http-observe-agent.service.d.ts +1 -8
- package/dist/protocols/http-observe-agent.service.js +44 -14
- package/dist/protocols/job-trace-runner.d.ts +11 -0
- package/dist/protocols/job-trace-runner.js +42 -8
- package/dist/protocols/rpc-observe-agent.service.d.ts +5 -0
- package/dist/protocols/rpc-observe-agent.service.js +43 -3
- package/dist/services/operation-trace.registry.d.ts +46 -3
- package/dist/services/operation-trace.registry.js +69 -13
- package/dist/services/tracer.service.d.ts +6 -0
- package/dist/services/tracer.service.js +11 -2
- package/dist/utils/default-trace-id-generator.util.d.ts +18 -0
- package/dist/utils/default-trace-id-generator.util.js +39 -2
- package/dist/utils/opentelemetry.util.d.ts +57 -0
- package/dist/utils/opentelemetry.util.js +100 -0
- package/dist/utils/trace-context.util.d.ts +91 -0
- package/dist/utils/trace-context.util.js +219 -0
- package/dist/utils/trace-handoff.util.d.ts +45 -0
- package/dist/utils/trace-handoff.util.js +90 -0
- package/dist/utils/trace-id-generator-origin.util.d.ts +11 -0
- package/dist/utils/trace-id-generator-origin.util.js +26 -0
- package/package.json +26 -13
package/README.md
CHANGED
|
@@ -43,16 +43,55 @@ unreachable collector never holds a request up. Recording it does cost the
|
|
|
43
43
|
request path a microsecond or two per traced call - [Overhead](#overhead) has
|
|
44
44
|
the numbers.
|
|
45
45
|
|
|
46
|
-
###
|
|
46
|
+
### Observe and OpenTelemetry
|
|
47
|
+
|
|
48
|
+
OpenTelemetry is the vendor-neutral standard across a polyglot estate, and
|
|
49
|
+
this agent isn't a replacement for it: it works on its own, or beside the
|
|
50
|
+
OpenTelemetry you already have. What it brings to a Nest service is no
|
|
51
|
+
collector to run, no exporter pipeline to configure, no manual instrumentation
|
|
52
|
+
for the framework's own lifecycle — and spans named after your Nest classes
|
|
53
|
+
and methods, which generic Node.js instrumentation cannot produce because it
|
|
54
|
+
cannot see them. If you would otherwise spend a week wiring a collector and
|
|
55
|
+
still not know which provider spent the time, this is the shortcut.
|
|
56
|
+
|
|
57
|
+
It also runs beside OpenTelemetry, with `openTelemetry: true`. The agent then
|
|
58
|
+
continues and sends W3C `traceparent`, so a trace that runs through services
|
|
59
|
+
instrumented with OpenTelemetry stays one trace. In a Nest service that already
|
|
60
|
+
starts the OpenTelemetry Node SDK, it reports each request under the trace id
|
|
61
|
+
the SDK gave it - also when the request started the trace - and leaves
|
|
62
|
+
`traceparent` to the SDK on calls out of the service, so a request has one
|
|
63
|
+
trace id in both tools and every call carries one `traceparent`. The SDK keeps
|
|
64
|
+
its own spans, the agent adds the class-level ones. Queue jobs and microservice
|
|
65
|
+
messages stay in the request's trace in both tools too: a BullMQ (Pro included)
|
|
66
|
+
or Bull job, or a message over the TCP or Redis transport (`@nestjs/microservices`
|
|
67
|
+
12.0.4 and later), carries W3C trace context beside its trace id, and the agent
|
|
68
|
+
that runs it hands that context to the SDK there, which has no instrumentation
|
|
69
|
+
of its own for either hop. This is tested against the SDK with its
|
|
70
|
+
auto-instrumentations, on Express and Fastify, over `fetch`, `node:http`, `pg`,
|
|
71
|
+
BullMQ, TCP microservices and Nest's own instrumentation. Without the option,
|
|
72
|
+
the agent speaks `x-request-id` alone and leaves any OpenTelemetry SDK in the
|
|
73
|
+
process alone.
|
|
74
|
+
|
|
75
|
+
It works only beside reporting to NestJS Observe. It turns on with the first
|
|
76
|
+
batch the collector accepts, and stays on through collector outages and rate
|
|
77
|
+
limits, so traces through the services around yours stay whole while NestJS
|
|
78
|
+
Observe is down. It pauses - saying so in the log - only while the collector
|
|
79
|
+
refuses the app's credentials or the agent's worker is down, resuming with the
|
|
80
|
+
next batch the collector accepts. With `enabled: false`, or without credentials
|
|
81
|
+
for the hosted collector, it never turns on.
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
ObserveModule.forRoot({
|
|
85
|
+
serviceId: "orders-api",
|
|
86
|
+
openTelemetry: true,
|
|
87
|
+
});
|
|
88
|
+
```
|
|
47
89
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
because it cannot see them. If you would otherwise spend a week wiring a
|
|
54
|
-
collector and still not know which provider spent the time, this is the
|
|
55
|
-
shortcut.
|
|
90
|
+
With `otlp` set as well, the agent exports the spans it records to the
|
|
91
|
+
OpenTelemetry backend you already run - a collector, Jaeger, Tempo or any
|
|
92
|
+
vendor - on the same trace ids, so the Nest service's spans sit in the same
|
|
93
|
+
waterfall as the rest of the trace. See
|
|
94
|
+
[Exporting to OpenTelemetry](#exporting-to-opentelemetry).
|
|
56
95
|
|
|
57
96
|
## Seeing it before installing
|
|
58
97
|
|
|
@@ -375,7 +414,8 @@ Once the module and the instrument are in place, the agent reports, with no furt
|
|
|
375
414
|
|
|
376
415
|
- **Requests, GraphQL operations, RPC messages, WebSocket gateway messages, queue jobs and scheduled jobs**, each with its tree of provider method calls.
|
|
377
416
|
- **Database queries**, as a span under the method that ran them, for `pg`, `mysql2`, `mssql` and `mongodb`. Anything built on those drivers is covered without being known by name: TypeORM, Drizzle, MikroORM and Mongoose are tested in this repository, and others (Knex, Sequelize) go through the same driver calls. SQL Server is the exception: `mssql` is what TypeORM and Prisma's SQL Server adapter run on, but Knex, Sequelize and MikroORM talk to SQL Server through `tedious` directly, which is not covered yet (nor is `mssql/msnodesqlv8`). The statement is recorded with every value removed (`SELECT "o"."id" FROM "orders" "o" WHERE "o"."customer_id" = $1`). Prisma's Rust query engine does not go through these drivers and is not covered yet.
|
|
378
|
-
- **Outbound HTTP calls** made with `fetch`/`undici` or `node:http`/`https` (so Axios too), as `POST api.stripe.com`. The current trace id is forwarded as `x-request-id` - by `fetch` on every supported Node version, by `node:http`/`https` from Node 22.12, the first to let a request's headers still be set at that point - so a service that also runs the agent continues the same trace; a header you set yourself is never overwritten.
|
|
417
|
+
- **Outbound HTTP calls** made with `fetch`/`undici` or `node:http`/`https` (so Axios too), as `POST api.stripe.com`. The current trace id is forwarded as `x-request-id` - by `fetch` on every supported Node version, by `node:http`/`https` from Node 22.12, the first to let a request's headers still be set at that point - so a service that also runs the agent continues the same trace; a header you set yourself is never overwritten. With `openTelemetry: true`, a W3C `traceparent` goes with it, naming the trace and the call's span, so a service instrumented with OpenTelemetry - in Go, Python, Java or anything else - continues it too, as does a service running the agent behind it. When an OpenTelemetry SDK that propagates W3C Trace Context runs in the same process, `traceparent` is left to it: its instrumentations write the header on the calls they cover, and a request takes only one. `outgoing: { http: { traceContext: false } }` sends `x-request-id` alone even then, and `propagateTraceId: (url) => boolean` keeps both headers away from the URLs you name.
|
|
418
|
+
- **Trace context from OpenTelemetry**, with `openTelemetry: true`: a request arriving with a W3C `traceparent` - from a service instrumented with OpenTelemetry, a mesh, or a load balancer that starts traces - is reported under that trace id, written as a UUID (`4bf92f35-77b3-4da6-a3ce-929d0e0e4736`), so the same id finds it in both tools. With an OpenTelemetry SDK in the same process - or another tracer registered with the OpenTelemetry API, as dd-trace is once its `TracerProvider` is registered - the trace is the one the SDK's server span is in, so a request that starts a trace gets the SDK's id too. That holds when a mesh or router stamped an `x-request-id` of its own on the request too, as Envoy and Heroku's router do: an `x-request-id` is kept only when it names the same trace, which is what another service running the agent sends, so custom ids still carry from one such service to the next. BullMQ (Pro included) and Bull jobs, and messages over the TCP and Redis transports (`@nestjs/microservices` 12.0.4 and later), carry W3C trace context beside the trace id, so an OpenTelemetry SDK in the process that runs the job or handles the message continues the request's trace; gRPC and the other transports carry the trace id as before. Without the option, a request is named by its `x-request-id`, else by a UUIDv7 of its own, and no `traceparent` is read or sent.
|
|
379
419
|
- **Model calls**: a `fetch` to OpenAI, Azure OpenAI, Anthropic, Gemini or Vertex, Bedrock, Mistral, Groq, OpenRouter, Together, DeepSeek, xAI, Cohere or Perplexity also reports the model and the input, output and cached tokens its response states - read as the response passes through to your code, streams included, so it arrives exactly as it would have and a stream you cancel is cancelled at the provider too. That covers the official SDKs, the Vercel AI SDK and LangChain, which all use `fetch`; calls made through `node:http` (axios, the AWS SDK) keep their plain span. Prompts and completions are never reported. A gateway in front of the providers is added with `outgoing: { llm: { hosts: ['llm-gateway.internal:4000'] } }`, and `outgoing: { llm: false }` turns this off and leaves the global `fetch` as it was.
|
|
380
420
|
- **Trace ids across queues**: a BullMQ (Pro included) or Bull job enqueued while handling a request carries that request's trace id, so the request and its job show up as one trace. Repeatable (cron) jobs start their own.
|
|
381
421
|
- **The code around an error's throw site**: for each in-app frame of a captured error, a few lines of source either side, read from the running process, so the dashboard shows the failing code next to the stack. This sends fragments of your application's source to Observe, stored with the error. Frames in `node_modules` and Node internals are never read. `createObserveModule({ sourceContext: false })` turns it off; the [documentation](https://www.observe.nestjs.com/documentation/sdk#error-source-context) covers tuning it instead.
|
|
@@ -485,6 +525,49 @@ Declarations are checked when the application starts, and whatever fails is logg
|
|
|
485
525
|
|
|
486
526
|
SLOs need a plan that has them - Scale or Enterprise. On any other plan, declarations are sent and ignored. The [documentation](https://www.observe.nestjs.com/documentation/sdk#declaring-objectives-in-code) has the rest.
|
|
487
527
|
|
|
528
|
+
## Exporting to OpenTelemetry
|
|
529
|
+
|
|
530
|
+
With `openTelemetry: true`, `otlp` exports the traces the agent records as OpenTelemetry spans, over OTLP/HTTP, to a receiver of your own - an OpenTelemetry Collector, Jaeger, Grafana Tempo, Honeycomb, Datadog's agent, or any vendor that takes OTLP. A request that arrived with a `traceparent` goes under the caller's span, and each call it makes is the parent of what the service it calls exports, so the Nest service's spans land in the same trace, in the same waterfall, as the rest of your system.
|
|
531
|
+
|
|
532
|
+
```typescript
|
|
533
|
+
ObserveModule.forRoot({
|
|
534
|
+
serviceId: "orders-api",
|
|
535
|
+
openTelemetry: true,
|
|
536
|
+
otlp: {
|
|
537
|
+
endpoint: "https://api.honeycomb.io",
|
|
538
|
+
headers: { "x-honeycomb-team": process.env.HONEYCOMB_API_KEY! },
|
|
539
|
+
},
|
|
540
|
+
});
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
Export runs beside what the agent sends to NestJS Observe, never instead of it, and every plan includes it:
|
|
544
|
+
|
|
545
|
+
- **Paid plans** export every trace the agent records, ahead of anything NestJS Observe does with its own copy: an overage trim or a paused overage changes what the dashboard keeps, not what is exported.
|
|
546
|
+
- **Free** exports what NestJS Observe keeps. Once your apps send 30,000 events in one UTC day, or the month's allowance is used, most waterfalls are paused, and the requests and jobs behind them are exported as their own spans, without the spans under them; server errors, failed jobs and anything over 1 s keep their full traces in both places. While the collector can't be reached, what NestJS Observe keeps can't be known, so Free's export pauses a few minutes into the outage and resumes with the next batch the collector accepts.
|
|
547
|
+
|
|
548
|
+
Which applies is the collector's to say: its reply to every batch the agent sends carries what the app's plan includes - the plan of the account that pays for the project. Export starts with the first batch the collector accepts and pauses whenever OpenTelemetry interop does, and the log says when it turns on and how. Without `openTelemetry: true`, without the `appKey` and `appSecret` the hosted collector needs, or with `enabled: false`, nothing is exported, and the agent says why at start-up. A proxy at `endpoint` that adds the credentials on the way to NestJS Observe counts as reporting to it.
|
|
549
|
+
|
|
550
|
+
`otlp: true` takes everything from the standard variables instead, as an OpenTelemetry SDK would: `OTEL_EXPORTER_OTLP_ENDPOINT` (or `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`), `OTEL_EXPORTER_OTLP_HEADERS`, `OTEL_EXPORTER_OTLP_PROTOCOL`, `OTEL_EXPORTER_OTLP_COMPRESSION`, `OTEL_EXPORTER_OTLP_TIMEOUT`, `OTEL_SERVICE_NAME` and `OTEL_RESOURCE_ATTRIBUTES`, with their `_TRACES_` forms. Options set in code win over them. The variables never turn export on by themselves: an application that also runs an OpenTelemetry SDK has them set for that SDK, and would export every trace twice.
|
|
551
|
+
|
|
552
|
+
| Option | Default | What it does |
|
|
553
|
+
| -------------------- | -------------------------------- | --------------------------------------------------------------------------------------------------------- |
|
|
554
|
+
| `endpoint` | `http://localhost:4318` | The receiver's base URL; `/v1/traces` is appended unless it is already there. |
|
|
555
|
+
| `headers` | none | Sent with every export - a vendor's API key, usually. |
|
|
556
|
+
| `protocol` | `'http/protobuf'` | Or `'http/json'`. gRPC is not supported: point at the receiver's HTTP port. |
|
|
557
|
+
| `compression` | `'gzip'` | Or `'none'`. |
|
|
558
|
+
| `timeoutMillis` | `10000` | How long one export may take before it is abandoned and retried. |
|
|
559
|
+
| `serviceName` | `serviceId` | The `service.name` spans are reported under. |
|
|
560
|
+
| `resourceAttributes` | none | Added to the agent's own (`service.version`, `service.instance.id`, `host.name`, `process.pid`, ...). |
|
|
561
|
+
|
|
562
|
+
What a backend receives, for each operation:
|
|
563
|
+
|
|
564
|
+
- **A SERVER span** for every request, GraphQL operation, RPC message and gateway message, named like the dashboard names it (`GET /orders/:id`), with the HTTP semantic conventions - `http.request.method`, `http.route`, `http.response.status_code`, `url.path` - plus `enduser.id`, your `tags`, and the headers `http.capture` recorded. A queue job is a CONSUMER span, a scheduled run an INTERNAL one.
|
|
565
|
+
- **INTERNAL spans** for the provider methods and manual spans under it, named `OrdersService.findOne`, and **CLIENT spans** for queries and outbound HTTP calls, named for what they did (`SELECT orders`, `GET api.stripe.com`).
|
|
566
|
+
- **The same trace ids `traceparent` carries.** A request that arrived with a `traceparent` goes under the caller's span, and a call this service makes names its CLIENT span as the parent the callee sees - so services instrumented with OpenTelemetry, and other services running the agent, assemble into one trace in the backend.
|
|
567
|
+
- **Errors** as span status and an `exception` event, with the class, message and stack the dashboard shows - redacted the same way.
|
|
568
|
+
|
|
569
|
+
What is exported is what is recorded: after `tracesSampleRate`, `skipSpans` and `spanCollapse`, and on Free after what NestJS Observe holds back. A request whose caller sampled its trace out (`traceparent` flags `00`) is not exported, as an OpenTelemetry SDK sampling by its parent would leave it out. Spans wait in a queue of at most 10,000 while an export is retried (429, 502, 503 and 504, honouring `Retry-After`); past that, whole operations are dropped and counted. Exports run on the application's thread, outside its traces, and whatever is queued is exported when the application closes, within `shutdownFlushTimeout`. Traces only, for now: runtime metrics, custom metrics and logs are not exported.
|
|
570
|
+
|
|
488
571
|
## Configuration
|
|
489
572
|
|
|
490
573
|
Every option, with its default. The [documentation](https://www.observe.nestjs.com/documentation/sdk) covers each in more depth.
|
|
@@ -498,6 +581,7 @@ Every option, with its default. The [documentation](https://www.observe.nestjs.c
|
|
|
498
581
|
| `serviceId` | required | Names the service in the dashboard, in at most 100 characters - a longer one is cut to fit, with a warning at startup. |
|
|
499
582
|
| `serviceVersion` | inferred | The release the process runs, at most 50 characters - see [What it records without any code](#what-it-records-without-any-code). `false` reports none. |
|
|
500
583
|
| `endpoint` | `https://observe-api.nestjs.com` | The collector's base URL - NestJS Observe, or a proxy that forwards to it. `OBSERVE_ENDPOINT` in the environment changes the default without touching the config. |
|
|
584
|
+
| `otlp` | `false` | Exports traces as OpenTelemetry spans to a receiver of your own, with `openTelemetry: true` - see [Exporting to OpenTelemetry](#exporting-to-opentelemetry). `true` reads the `OTEL_EXPORTER_OTLP_*` variables. |
|
|
501
585
|
| `flushInterval` | `5000` | Milliseconds between sends of buffered telemetry; at least `1000`. |
|
|
502
586
|
| `shutdownFlushTimeout` | `2000` | Milliseconds closing the app waits for buffered telemetry to reach the collector before stopping the agent. `0` skips the final send - for test suites that boot the app with real credentials. |
|
|
503
587
|
| `maxTracesPerBatch` | `1000` | The most traces one send carries. |
|
|
@@ -508,7 +592,8 @@ Every option, with its default. The [documentation](https://www.observe.nestjs.c
|
|
|
508
592
|
| `runtimeMetricsInterval` | `60000` | Milliseconds between runtime samples; at least `30000`. |
|
|
509
593
|
| `forwardLogs` | `false` | Sends the application's log lines, each correlated to the trace it was written in - see [Logs](https://www.observe.nestjs.com/documentation/sdk#logs). |
|
|
510
594
|
| `redaction` | on | How log lines, error messages and captured requests are scrubbed before they leave the process: `enabled`, `useDefaultPatterns`, `patterns`, `keys` and `replacement`. |
|
|
511
|
-
| `outgoing` | on | Query and outbound-HTTP spans. `false` turns both off; `{ database: false }` or `{ http: false }` one of them.
|
|
595
|
+
| `outgoing` | on | Query and outbound-HTTP spans. `false` turns both off; `{ database: false }` or `{ http: false }` one of them. `{ http: { propagateTraceId, traceContext } }` tunes the `x-request-id` and `traceparent` headers outbound calls carry - `traceparent` only with `openTelemetry`. |
|
|
596
|
+
| `openTelemetry` | `false` | Continues and sends W3C `traceparent`, names a request after the W3C trace it is in, and follows an OpenTelemetry SDK running in the same process - see [Observe and OpenTelemetry](#observe-and-opentelemetry). Works only beside reporting to NestJS Observe, and stays on through collector outages. Off, the agent reads and sends `x-request-id` alone. |
|
|
512
597
|
| `http` | | `ignore`, `tags`, `setAttributes`, `getUserId`, `queryParamsObfuscateRegex`, `tracePreflights` and `capture` - see [Capturing failed and slow requests](#capturing-failed-and-slow-requests). |
|
|
513
598
|
| `rpc`, `grpc` | | `ignore`, `tags`, `setAttributes` and `getUserId` for microservice and gRPC handlers. |
|
|
514
599
|
| `graphql` | | The same four, once per GraphQL operation. |
|
|
@@ -523,7 +608,7 @@ Every option, with its default. The [documentation](https://www.observe.nestjs.c
|
|
|
523
608
|
| Option | Default | What it does |
|
|
524
609
|
| --------------------- | ---------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
525
610
|
| `sourceContext` | `true` | Attaches the source around each in-app frame of a captured error - see [What it records without any code](#what-it-records-without-any-code). `{ linesOfContext, maxFrames, sourceMaps }` tunes it. |
|
|
526
|
-
| `traceIdGenerator` | the `x-request-id` header, else a UUIDv7 | Gives each request its trace id.
|
|
611
|
+
| `traceIdGenerator` | the `x-request-id` header, else a UUIDv7 | Gives each request its trace id. With `openTelemetry` on, the default takes the W3C trace id first and keeps an `x-request-id` that names the same trace, and the W3C trace is still read beside whatever this returns, so outbound calls stay in the caller's trace. |
|
|
527
612
|
| `traceIdKey` | `'traceId'` | The key the trace id is stored under in the request context. |
|
|
528
613
|
| `attachTraceIdToLogs` | `true` | Adds the trace id to the `ConsoleLogger`'s own output, on Nest 11.2 and later. Sending lines to the dashboard is `forwardLogs`. |
|
|
529
614
|
| `skipInstrumentation` | `() => false` | Returns `true` for a provider instance that must not be instrumented. Nest's `Reflector` and the `mongodb` driver's `MongoClient` and `Db` never are, whatever this returns. |
|
|
@@ -556,16 +641,17 @@ $ npm run typecheck
|
|
|
556
641
|
$ npm run test:int
|
|
557
642
|
```
|
|
558
643
|
|
|
559
|
-
The MSSQL unit suite uses the real driver's public request APIs with stubbed transport methods and needs no SQL Server. The queue suites need a Redis,
|
|
644
|
+
The MSSQL unit suite uses the real driver's public request APIs with stubbed transport methods and needs no SQL Server. The queue suites need a Redis, the query-span integration suites a PostgreSQL, MySQL and MongoDB, and the OTLP export suite's Jaeger case a Jaeger. Each integration suite skips itself when its server does not answer, so without them `test:int` passes having tested less. CI starts them with these ports and credentials, which the suites default to:
|
|
560
645
|
|
|
561
646
|
```bash
|
|
562
647
|
$ docker run -d -p 6379:6379 redis:7-alpine
|
|
563
648
|
$ docker run -d -p 54321:5432 -e POSTGRES_PASSWORD=postgres-whisprr postgres:16-alpine
|
|
564
649
|
$ docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=test mysql:8
|
|
565
650
|
$ docker run -d -p 27027:27017 mongo:7
|
|
651
|
+
$ docker run -d -p 4318:4318 -p 16686:16686 jaegertracing/jaeger:2.22.0
|
|
566
652
|
```
|
|
567
653
|
|
|
568
|
-
To point them elsewhere, set `REDIS_HOST`/`REDIS_PORT`, `PGHOST`/`PGPORT`/`PGUSER`/`PGPASSWORD`/`PGDATABASE`, `MYSQL_HOST`/`MYSQL_PORT`/`MYSQL_USER`/`MYSQL_PASSWORD`/`MYSQL_DATABASE` or `
|
|
654
|
+
To point them elsewhere, set `REDIS_HOST`/`REDIS_PORT`, `PGHOST`/`PGPORT`/`PGUSER`/`PGPASSWORD`/`PGDATABASE`, `MYSQL_HOST`/`MYSQL_PORT`/`MYSQL_USER`/`MYSQL_PASSWORD`/`MYSQL_DATABASE`, `MONGO_HOST`/`MONGO_PORT`, or `JAEGER_OTLP_ENDPOINT`/`JAEGER_QUERY_URL` for the suite that exports traces into Jaeger.
|
|
569
655
|
|
|
570
656
|
## Module format
|
|
571
657
|
|
|
@@ -104,6 +104,20 @@ export declare function parseOveragePauseReason(msg: string): OveragePauseReason
|
|
|
104
104
|
export declare const PAUSED_MESSAGE_PREFIX = "observe:paused-until=";
|
|
105
105
|
/** The time sends are paused until, or null if this is not that line. */
|
|
106
106
|
export declare function parsePausedMessage(msg: string): number | null;
|
|
107
|
+
/**
|
|
108
|
+
* How the detached worker tells the main thread a batch did not get in:
|
|
109
|
+
* `refused` when the collector turned the credentials away (401 or 403), and
|
|
110
|
+
* `failed` for anything else that kept a batch out - a 429, an unreachable
|
|
111
|
+
* collector, any other status, a fault in the worker. A batch that got in
|
|
112
|
+
* says so with its degraded verdict instead.
|
|
113
|
+
*
|
|
114
|
+
* Read for OpenTelemetry interop, which a refusal pauses and any other failure
|
|
115
|
+
* leaves as it is - see `ObserveAgentSharedBuffer.isIngesting`.
|
|
116
|
+
*/
|
|
117
|
+
export declare const NOT_INGESTED_MESSAGE_PREFIX = "observe:not-ingested=";
|
|
118
|
+
export type NotIngestedReason = "refused" | "failed";
|
|
119
|
+
/** Why a batch did not get in, or null if this is not that line. */
|
|
120
|
+
export declare function parseNotIngestedMessage(msg: string): NotIngestedReason | null;
|
|
107
121
|
/**
|
|
108
122
|
* Duration past which the collector treats an execution as notable, in
|
|
109
123
|
* milliseconds.
|
|
@@ -86,6 +86,25 @@ export function parsePausedMessage(msg) {
|
|
|
86
86
|
const until = Number(msg.slice(PAUSED_MESSAGE_PREFIX.length));
|
|
87
87
|
return Number.isFinite(until) ? until : null;
|
|
88
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* How the detached worker tells the main thread a batch did not get in:
|
|
91
|
+
* `refused` when the collector turned the credentials away (401 or 403), and
|
|
92
|
+
* `failed` for anything else that kept a batch out - a 429, an unreachable
|
|
93
|
+
* collector, any other status, a fault in the worker. A batch that got in
|
|
94
|
+
* says so with its degraded verdict instead.
|
|
95
|
+
*
|
|
96
|
+
* Read for OpenTelemetry interop, which a refusal pauses and any other failure
|
|
97
|
+
* leaves as it is - see `ObserveAgentSharedBuffer.isIngesting`.
|
|
98
|
+
*/
|
|
99
|
+
export const NOT_INGESTED_MESSAGE_PREFIX = "observe:not-ingested=";
|
|
100
|
+
/** Why a batch did not get in, or null if this is not that line. */
|
|
101
|
+
export function parseNotIngestedMessage(msg) {
|
|
102
|
+
if (!msg.startsWith(NOT_INGESTED_MESSAGE_PREFIX)) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
const reason = msg.slice(NOT_INGESTED_MESSAGE_PREFIX.length);
|
|
106
|
+
return reason === "refused" || reason === "failed" ? reason : null;
|
|
107
|
+
}
|
|
89
108
|
/**
|
|
90
109
|
* Duration past which the collector treats an execution as notable, in
|
|
91
110
|
* milliseconds.
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { DEGRADED_MESSAGE_PREFIX, PAUSED_MESSAGE_PREFIX, } from "./degraded-ingest.protocol.js";
|
|
1
|
+
import { DEGRADED_MESSAGE_PREFIX, NOT_INGESTED_MESSAGE_PREFIX, PAUSED_MESSAGE_PREFIX, } from "./degraded-ingest.protocol.js";
|
|
2
2
|
import { detachedObserveWorker } from "./detached-observe-worker.js";
|
|
3
3
|
import { createTelemetrySanitizer, SECTION_SHAPES, } from "./telemetry-wire-contract.js";
|
|
4
|
+
import { OTLP_EXPORT_MESSAGE_PREFIX } from "../otlp/otlp-export-grant.js";
|
|
4
5
|
import { describeIngestRefusal } from "../utils/ingest-refusal.util.js";
|
|
5
6
|
import { readSdkVersion } from "../utils/sdk-version.util.js";
|
|
6
7
|
/**
|
|
@@ -27,6 +28,9 @@ export function detachedWorkerData(input) {
|
|
|
27
28
|
wireShapes: SECTION_SHAPES,
|
|
28
29
|
degradedPrefix: DEGRADED_MESSAGE_PREFIX,
|
|
29
30
|
pausedPrefix: PAUSED_MESSAGE_PREFIX,
|
|
31
|
+
notIngestedPrefix: NOT_INGESTED_MESSAGE_PREFIX,
|
|
32
|
+
otlpExportPrefix: OTLP_EXPORT_MESSAGE_PREFIX,
|
|
33
|
+
asksForOtlpExport: input.asksForOtlpExport === true,
|
|
30
34
|
sdkVersion: readSdkVersion(),
|
|
31
35
|
};
|
|
32
36
|
return { sharedBuffer: input.sharedBuffer, config };
|
|
@@ -23,6 +23,16 @@ export interface DetachedWorkerConfig {
|
|
|
23
23
|
degradedPrefix: string;
|
|
24
24
|
/** `PAUSED_MESSAGE_PREFIX`, as data, for the same reason. */
|
|
25
25
|
pausedPrefix: string;
|
|
26
|
+
/** `NOT_INGESTED_MESSAGE_PREFIX`, as data, for the same reason. */
|
|
27
|
+
notIngestedPrefix: string;
|
|
28
|
+
/** `OTLP_EXPORT_MESSAGE_PREFIX`, as data, for the same reason. */
|
|
29
|
+
otlpExportPrefix: string;
|
|
30
|
+
/**
|
|
31
|
+
* Whether `otlp` is set up to export, so batches ask the collector what
|
|
32
|
+
* the app's plan allows it (`OTLP_EXPORT_INGEST_MODE`) and the answer is
|
|
33
|
+
* posted back. False leaves every batch and every line as they were.
|
|
34
|
+
*/
|
|
35
|
+
asksForOtlpExport: boolean;
|
|
26
36
|
/**
|
|
27
37
|
* This package's version (`readSdkVersion`), read on the main thread: the
|
|
28
38
|
* worker runs as eval'd source and cannot resolve a manifest of its own.
|
|
@@ -51,6 +51,10 @@ describeRefusal) => {
|
|
|
51
51
|
// that `parseDegradedMessage` still matches.
|
|
52
52
|
const degradedPrefix = config.degradedPrefix;
|
|
53
53
|
const pausedPrefix = config.pausedPrefix;
|
|
54
|
+
const notIngestedPrefix = config.notIngestedPrefix;
|
|
55
|
+
const otlpExportPrefix = config.otlpExportPrefix;
|
|
56
|
+
// Read as truthy, so a config without it asks nothing and posts no line.
|
|
57
|
+
const asksForOtlpExport = config.asksForOtlpExport;
|
|
54
58
|
/**
|
|
55
59
|
* Built here, used only when the collector answers 400. The shapes travel
|
|
56
60
|
* through `workerData` (they are plain JSON); the factory travels as source.
|
|
@@ -66,8 +70,11 @@ describeRefusal) => {
|
|
|
66
70
|
// degraded-ingest.protocol.ts. A literal rather than data from `config`
|
|
67
71
|
// like the prefixes: nothing in the agent reads it back, and a missing
|
|
68
72
|
// config key would put a broken header on every batch. The value is
|
|
69
|
-
// pinned by worker-degraded-reply.spec.ts.
|
|
70
|
-
|
|
73
|
+
// pinned by worker-degraded-reply.spec.ts. `otlp-export`
|
|
74
|
+
// (`OTLP_EXPORT_INGEST_MODE`) asks what the app's plan allows `otlp` to
|
|
75
|
+
// export, and is listed only by an agent set up to export.
|
|
76
|
+
"x-observe-ingest-modes": "reduced, trimmed, paced, errors-only" +
|
|
77
|
+
(asksForOtlpExport ? ", otlp-export" : ""),
|
|
71
78
|
// Which agent sent the batch, so the collector can tell an application
|
|
72
79
|
// still on an old release that a newer one exists.
|
|
73
80
|
"x-observe-sdk-version": config.sdkVersion,
|
|
@@ -367,6 +374,7 @@ describeRefusal) => {
|
|
|
367
374
|
const retryBody = await retry.text().catch(() => "");
|
|
368
375
|
parentPort.postMessage(`Error: Telemetry batch was rejected (400), repaired (${summary}), and rejected again ` +
|
|
369
376
|
`(${retry.status})${retryBody ? ` - ${retryBody.slice(0, 500)}` : ""}. The batch was dropped.`);
|
|
377
|
+
parentPort.postMessage(`${notIngestedPrefix}failed`);
|
|
370
378
|
}
|
|
371
379
|
return true;
|
|
372
380
|
}
|
|
@@ -431,6 +439,7 @@ describeRefusal) => {
|
|
|
431
439
|
: `Error: Could not reach the collector at ${telemetryUrl}` +
|
|
432
440
|
(reason ? ` (${reason})` : "") +
|
|
433
441
|
`. Check that it is running and that \`endpoint\` points at it.`);
|
|
442
|
+
parentPort.postMessage(`${notIngestedPrefix}failed`);
|
|
434
443
|
// Discarded rather than retried - `clearBuffer` in `finally` - which
|
|
435
444
|
// is the right call for a fixed-size buffer the application is still
|
|
436
445
|
// writing into: holding it for a retry would block every batch behind
|
|
@@ -451,6 +460,7 @@ describeRefusal) => {
|
|
|
451
460
|
// The slot is left alone until then, which a final flush would
|
|
452
461
|
// otherwise read as a send still in progress.
|
|
453
462
|
parentPort.postMessage(`${pausedPrefix}${pausedUntil}`);
|
|
463
|
+
parentPort.postMessage(`${notIngestedPrefix}refused`);
|
|
454
464
|
// Said plainly, and once. The batch is well-formed and the
|
|
455
465
|
// credentials are not accepted for this application; left as a
|
|
456
466
|
// generic transport failure this reads as a network problem, and the
|
|
@@ -476,6 +486,7 @@ describeRefusal) => {
|
|
|
476
486
|
// The slot is left alone until then, which a final flush would
|
|
477
487
|
// otherwise read as a send still in progress.
|
|
478
488
|
parentPort.postMessage(`${pausedPrefix}${pausedUntil}`);
|
|
489
|
+
parentPort.postMessage(`${notIngestedPrefix}failed`);
|
|
479
490
|
// The body says which refusal this is - a quota, a quota frozen by
|
|
480
491
|
// a declined payment, or a rate cap - and each needs a different
|
|
481
492
|
// fix from the operator. Parsed defensively: a rate-cap body is
|
|
@@ -501,6 +512,7 @@ describeRefusal) => {
|
|
|
501
512
|
const errorBody = await response.text().catch(() => "");
|
|
502
513
|
parentPort.postMessage(`Error: Failed to send data. ${response.statusText} (${response.status})` +
|
|
503
514
|
(errorBody ? ` - ${errorBody.slice(0, 500)}` : ""));
|
|
515
|
+
parentPort.postMessage(`${notIngestedPrefix}failed`);
|
|
504
516
|
return true;
|
|
505
517
|
}
|
|
506
518
|
// Accepted, so whatever refused these credentials no longer does: a
|
|
@@ -523,6 +535,18 @@ describeRefusal) => {
|
|
|
523
535
|
*/
|
|
524
536
|
const accepted = await response.json().catch(() => undefined);
|
|
525
537
|
const reply = (typeof accepted === "object" && accepted !== null ? accepted : {});
|
|
538
|
+
// What the app's plan allows `otlp` to export - see `OtlpExportGrant`.
|
|
539
|
+
// Every accepted reply says it again, and a reply that says nothing
|
|
540
|
+
// grants nothing: a collector that predates export, or a plan without
|
|
541
|
+
// it. Ahead of the verdict below, which the main thread reads as the
|
|
542
|
+
// collector taking the app's telemetry - by then it knows what export
|
|
543
|
+
// may do with it.
|
|
544
|
+
if (asksForOtlpExport) {
|
|
545
|
+
const grant = reply.otlpExport === "full" || reply.otlpExport === "capped"
|
|
546
|
+
? reply.otlpExport
|
|
547
|
+
: "off";
|
|
548
|
+
parentPort.postMessage(`${otlpExportPrefix}${grant}`);
|
|
549
|
+
}
|
|
526
550
|
// `false`, `true`, `trimmed`, `paced` or `errors-only`. `trimmed`,
|
|
527
551
|
// `paced` and `errorsOnly` say only why or how the spans are discarded,
|
|
528
552
|
// so they count only beside `degraded`; a collector that sends none of
|
|
@@ -572,6 +596,7 @@ describeRefusal) => {
|
|
|
572
596
|
(causeText === undefined ? "" : ` (cause: ${causeText})`) +
|
|
573
597
|
`. This is most likely a fault in the observe agent rather than a ` +
|
|
574
598
|
`connectivity problem; please report it.`);
|
|
599
|
+
parentPort.postMessage(`${notIngestedPrefix}failed`);
|
|
575
600
|
return true;
|
|
576
601
|
}
|
|
577
602
|
finally {
|
|
@@ -5,6 +5,8 @@ import { JobSnapshot } from "../interfaces/job-snapshot.interface.js";
|
|
|
5
5
|
import { NodeRuntimeMetrics } from "../interfaces/node-runtime-metrics.interface.js";
|
|
6
6
|
import { ObserveModuleOptionsWithDefaults } from "../interfaces/observe-options.interface.js";
|
|
7
7
|
import { RequestSnapshot } from "../interfaces/request-snapshot.interface.js";
|
|
8
|
+
import type { OtlpExportGrant } from "../otlp/otlp-export-grant.js";
|
|
9
|
+
import { OtlpTraceExporter } from "../otlp/otlp-trace-exporter.service.js";
|
|
8
10
|
/**
|
|
9
11
|
* A log line waiting to be shipped.
|
|
10
12
|
*
|
|
@@ -47,6 +49,7 @@ interface BufferedLogEntry {
|
|
|
47
49
|
export declare class ObserveAgentSharedBuffer {
|
|
48
50
|
private readonly options;
|
|
49
51
|
private readonly objectivesRegistry?;
|
|
52
|
+
private readonly otlpTraceExporter?;
|
|
50
53
|
private readonly _sharedBuffer;
|
|
51
54
|
private readonly lock;
|
|
52
55
|
private readonly sharedBytes;
|
|
@@ -66,7 +69,7 @@ export declare class ObserveAgentSharedBuffer {
|
|
|
66
69
|
*/
|
|
67
70
|
private readonly _awaitingFlushAck;
|
|
68
71
|
get sharedBuffer(): SharedArrayBuffer;
|
|
69
|
-
constructor(options: ObserveModuleOptionsWithDefaults, objectivesRegistry?: ObjectivesRegistry | undefined);
|
|
72
|
+
constructor(options: ObserveModuleOptionsWithDefaults, objectivesRegistry?: ObjectivesRegistry | undefined, otlpTraceExporter?: OtlpTraceExporter | undefined);
|
|
70
73
|
insertRequestSnapshot(snapshot: RequestSnapshot): void;
|
|
71
74
|
insertJobSnapshot(jobSnapshot: JobSnapshot): void;
|
|
72
75
|
/**
|
|
@@ -84,6 +87,12 @@ export declare class ObserveAgentSharedBuffer {
|
|
|
84
87
|
* not a moment longer.
|
|
85
88
|
*/
|
|
86
89
|
private errorsOnly;
|
|
90
|
+
/**
|
|
91
|
+
* Whether this app reports to NestJS Observe - see `isIngesting`. False
|
|
92
|
+
* until the collector accepts a first batch: what has not been shown to
|
|
93
|
+
* reach NestJS Observe does not count as reaching it.
|
|
94
|
+
*/
|
|
95
|
+
private ingesting;
|
|
87
96
|
/**
|
|
88
97
|
* Requests and jobs that errors-only ingest withheld from the batch being
|
|
89
98
|
* built, cleared with it.
|
|
@@ -115,6 +124,59 @@ export declare class ObserveAgentSharedBuffer {
|
|
|
115
124
|
isDegraded(): boolean;
|
|
116
125
|
/** Whether only server errors are being sent, until the same window lapses. */
|
|
117
126
|
isErrorsOnly(): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Whether this app reports to NestJS Observe: from the first batch the
|
|
129
|
+
* collector accepts, until the collector refuses the app's credentials or
|
|
130
|
+
* the worker stops - and again from the next batch it accepts.
|
|
131
|
+
*
|
|
132
|
+
* A batch that fails to get in for any other reason - a collector that is
|
|
133
|
+
* down or slow to answer, a 5xx, a 429 pause, however long - leaves it as
|
|
134
|
+
* it is: that is an outage at NestJS Observe, and the traces running through
|
|
135
|
+
* the services around this one must not break over it. Refused credentials
|
|
136
|
+
* are a verdict on this app, and a stopped worker sends nothing until a new
|
|
137
|
+
* one gets a batch in.
|
|
138
|
+
*
|
|
139
|
+
* OpenTelemetry interop runs on it (see `openTelemetry`): the agent reads
|
|
140
|
+
* and sends `traceparent`, and follows an OpenTelemetry SDK in the process,
|
|
141
|
+
* only beside reporting to NestJS Observe. So an agent that sends Observe
|
|
142
|
+
* nothing - `enabled: false`, no credentials, credentials the collector
|
|
143
|
+
* refuses - never turns it on.
|
|
144
|
+
*/
|
|
145
|
+
isIngesting(): boolean;
|
|
146
|
+
/** The collector accepted a batch, whatever it then kept of it. */
|
|
147
|
+
recordAccepted(): void;
|
|
148
|
+
/** The collector refused the app's credentials. */
|
|
149
|
+
recordRefused(): void;
|
|
150
|
+
/** The worker stopped; a restarted one has to get a batch in again. */
|
|
151
|
+
recordStopped(): void;
|
|
152
|
+
/**
|
|
153
|
+
* What the collector's last accepted reply granted `otlp` - see
|
|
154
|
+
* `OtlpExportGrant`. Nothing until one says otherwise: what this agent may
|
|
155
|
+
* export is the collector's to say, for the plan the app's account is on.
|
|
156
|
+
*/
|
|
157
|
+
private otlpExport;
|
|
158
|
+
/**
|
|
159
|
+
* Until when a `capped` grant holds (epoch ms), refreshed by every reply
|
|
160
|
+
* that repeats it.
|
|
161
|
+
*
|
|
162
|
+
* A capped export is what NestJS Observe keeps, and the agent learns what
|
|
163
|
+
* that is only from replies. With none coming - an account the collector
|
|
164
|
+
* refuses at its limit, an outage - it would fall back on exporting every
|
|
165
|
+
* span once `isDegraded` lapsed. So the grant lapses first: it is set just
|
|
166
|
+
* before the verdict for the same reply, on the same TTL. A `full` grant
|
|
167
|
+
* holds until a reply says otherwise, and export then runs for as long as
|
|
168
|
+
* `isIngesting` does.
|
|
169
|
+
*/
|
|
170
|
+
private otlpExportCappedUntil;
|
|
171
|
+
/** Records what the collector's last accepted reply granted `otlp`. */
|
|
172
|
+
setOtlpExport(grant: OtlpExportGrant, now?: number): void;
|
|
173
|
+
/**
|
|
174
|
+
* What `otlp` may export right now: the collector's grant, while the
|
|
175
|
+
* collector takes this app's telemetry (`isIngesting`) - export runs beside
|
|
176
|
+
* reporting to NestJS Observe, never instead of it - and `off` otherwise.
|
|
177
|
+
* Asked once per operation, as it is handed over.
|
|
178
|
+
*/
|
|
179
|
+
otlpExportGrant(now?: number): OtlpExportGrant;
|
|
118
180
|
/**
|
|
119
181
|
* What errors-only ingest makes of one execution: whether it is sent.
|
|
120
182
|
*
|