@geonosis/observability 1.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 +202 -0
- package/README.md +333 -0
- package/bin/geonosis-observability.mjs +4 -0
- package/dist/chunk-2ZTQU3OO.js +161 -0
- package/dist/chunk-EJS3A3ZV.js +82 -0
- package/dist/chunk-UFR2R2VM.js +141 -0
- package/dist/health/index.d.ts +63 -0
- package/dist/health/index.js +89 -0
- package/dist/index.d.ts +204 -0
- package/dist/index.js +145 -0
- package/dist/observability-cli.js +229 -0
- package/dist/ports-Ct5eq-HS.d.ts +116 -0
- package/dist/posthog/index.d.ts +58 -0
- package/dist/posthog/index.js +7 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
# @geonosis/observability
|
|
2
|
+
|
|
3
|
+
**Ports whose answer can say *failed*, a vocabulary that refuses a person as a tenant, and a probe
|
|
4
|
+
that plants an error and goes and looks for it.**
|
|
5
|
+
|
|
6
|
+
Extracted from during.day's PostHog sink and health endpoint, dielime's provider seam, and Midday's
|
|
7
|
+
tiered health registry — after an afternoon in which four instruments reported green while measuring
|
|
8
|
+
nothing (`docs/rules-backlog.md` #42). Zero runtime dependencies. Node ≥ 22 and workerd: every flush
|
|
9
|
+
is explicit or returned as a promise the host can `waitUntil`, and no timer is ever left running.
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pnpm add @geonosis/observability better-result
|
|
13
|
+
pnpm add posthog-node # only if you use the /posthog adapter
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
| Import | What is behind it |
|
|
17
|
+
|---|---|
|
|
18
|
+
| `@geonosis/observability` | the ports, the vocabulary, `createMemorySink`, `createSwallowingSink`, `createMemoryTracer`, `withDedupe`, `withLastEvent`, `proveObservability` |
|
|
19
|
+
| `@geonosis/observability/health` | `createHealthRegistry` — tiers, TTL, timeouts, `ready()` vs `dependencies()` |
|
|
20
|
+
| `@geonosis/observability/posthog` | `createPostHogSink` — the vendor is injected, never imported |
|
|
21
|
+
| `geonosis-observability prove` | the CLI. Four verdicts, exit 0 or 2 |
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## 1. The ports
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
type ErrorSink = {
|
|
29
|
+
capture: (error: Error, attributes: Attributes) => Promise<Result<CaptureReceipt, SinkFailure>>
|
|
30
|
+
flush: () => Promise<Result<void, SinkFailure>>
|
|
31
|
+
kind: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type CaptureReceipt = { deduplicated: boolean; id: string }
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`better-result` is a **required peer**: `Result` is in every return type, so you cannot call this
|
|
38
|
+
package without it, and a second copy in a `dependencies` field would put two `Ok` classes in one
|
|
39
|
+
tree.
|
|
40
|
+
|
|
41
|
+
**Why not `Promise<number>`.** during.day's seam answers with a count: `events.length` from the real
|
|
42
|
+
sink, `0` from the one with no key, a rejected promise from a dead transport — handed to
|
|
43
|
+
`waitUntil`, where nothing reads it. A count cannot say *failed*. That is the shape `no-void-port`
|
|
44
|
+
refuses, wearing a number instead of `void`.
|
|
45
|
+
|
|
46
|
+
**Why the receipt carries an id.** React strips an error's message on the way to a browser; a deploy
|
|
47
|
+
renames every stack frame. The id the sink filed the report under survives both, which is why it is
|
|
48
|
+
in the answer rather than in a log line.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
const receipt = await sink.capture(error, { envelopeId, service: 'api', tenant: orgId })
|
|
52
|
+
if (receipt.isErr()) log.warn('the report did not land', receipt.error.sink, receipt.error.message)
|
|
53
|
+
else response.headers.set('x-error-id', receipt.value.id)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### The tracer
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
tracer.span('queue.handle-batch', { service: 'api', tenant: orgId }, async () => doTheWork())
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Returns whatever the body returned and rethrows whatever it threw. It is a recorder, not a gate: a
|
|
63
|
+
tracer that swallowed a throw would make a failed request answer 200. Its own failures are its own
|
|
64
|
+
business and never the caller's.
|
|
65
|
+
|
|
66
|
+
**Span names** are `<area>.<action>`, lowercase, at least two dot-separated segments, dashes inside a
|
|
67
|
+
segment: `queue.handle-batch`, `db.cell.query`, `http.request`. `isSpanName(name)` decides;
|
|
68
|
+
`SPAN_NAME_CONVENTION` states it. Never enforced by a throw — a bad span name must not become a 500
|
|
69
|
+
— so assert it in your own tests if you want it held.
|
|
70
|
+
|
|
71
|
+
## 2. The vocabulary
|
|
72
|
+
|
|
73
|
+
| Field | Meaning |
|
|
74
|
+
|---|---|
|
|
75
|
+
| `service` | **required.** A report nobody can route to a service is a report nobody reads. |
|
|
76
|
+
| `tenant` | Always a **group id**, never a person. Refused if it contains `@`. |
|
|
77
|
+
| `envelopeId` | The id this occurrence already has upstream. The dedupe key, and the id it is filed under. |
|
|
78
|
+
| `runtime` | `node`, `workerd`, `browser`, a container id. An open string. |
|
|
79
|
+
| `release` | Which build. |
|
|
80
|
+
| `actor` | The one field that MAY carry an identity. |
|
|
81
|
+
| `extra` | Anything else. May not shadow a field above. |
|
|
82
|
+
|
|
83
|
+
`release` and `runtime` are here because **neither consumer attaches either**, and a production error
|
|
84
|
+
that cannot be pinned to a deploy is the first of the four lying instruments.
|
|
85
|
+
|
|
86
|
+
`tenant` refusing an `@` is the one rule that is not a shape check. A group id is the join key every
|
|
87
|
+
dashboard fans out on, and the one field an erasure request cannot reach once an address is in it —
|
|
88
|
+
and it is the mistake both repos would make first, because `distinctId` legitimately holds a person
|
|
89
|
+
and the two fields sit one line apart.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { parseAttributes, attributesSchema } from '@geonosis/observability'
|
|
93
|
+
import { z } from 'zod'
|
|
94
|
+
|
|
95
|
+
parseAttributes(value) // → Result<Attributes, InvalidAttributes>, no zod anywhere
|
|
96
|
+
attributesSchema(z) // → the same rules, built over YOUR zod
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Two doors, one set of rules. Both consumers run different zod versions (4.2.0, 4.4.3 and 4.5.4
|
|
100
|
+
measured across the two trees), so this package imports none.
|
|
101
|
+
|
|
102
|
+
## 3. The sinks
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
const sink = createMemorySink() // a real implementation: what tests and the probe read
|
|
106
|
+
const sink = createSwallowingSink() // answers ok, keeps nothing — shipped so --prove can catch it
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`createSwallowingSink` is during.day's `silentAnalyticsSink` with the `console.info` removed. It is
|
|
110
|
+
here on purpose: it is what both consumers actually run whenever no key is set, and a probe that
|
|
111
|
+
could not tell it from a real sink would prove nothing.
|
|
112
|
+
|
|
113
|
+
### Dedupe
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
withDedupe(sink) // an in-memory Map
|
|
117
|
+
withDedupe(sink, { seen, remember }) // your store: a KV namespace, an audit table
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
At-least-once in, at-most-once counted. A repeat of a known `envelopeId` answers with the id the
|
|
121
|
+
first capture was filed under and `deduplicated: true`, and the inner sink is not called. An
|
|
122
|
+
envelope whose capture **failed** is not remembered — buying at-most-once by losing at-least-once
|
|
123
|
+
would make one transport error permanent silence for that envelope.
|
|
124
|
+
|
|
125
|
+
An attributes bag with no `envelopeId` always passes through: during.day mints a fresh uuid for a
|
|
126
|
+
`request`-door failure precisely because it has none, and dropping the second of two would lose a
|
|
127
|
+
real second failure.
|
|
128
|
+
|
|
129
|
+
### The last-event file
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
withLastEvent(sink, (record) => writeFile('.geonosis/last-event.json', JSON.stringify(record)))
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Writes `{ at, id, sink, release? }` after a capture the sink accepted — nothing for a refusal,
|
|
136
|
+
nothing for a deduplicated repeat. This is the file `geonosis-doctor`'s `observability` check reads.
|
|
137
|
+
The writer is injected because a file is a Node fact; on workerd the same record goes to KV.
|
|
138
|
+
|
|
139
|
+
## 4. The PostHog adapter
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import { PostHog } from 'posthog-node'
|
|
143
|
+
import { createPostHogSink } from '@geonosis/observability/posthog'
|
|
144
|
+
|
|
145
|
+
const sink = createPostHogSink({
|
|
146
|
+
newClient: (batchSize) =>
|
|
147
|
+
new PostHog(env.POSTHOG_API_KEY, {
|
|
148
|
+
host: env.POSTHOG_HOST,
|
|
149
|
+
flushAt: batchSize,
|
|
150
|
+
flushInterval: 0, // no long-lived process to run a timer in
|
|
151
|
+
}),
|
|
152
|
+
groupName: 'organization', // default
|
|
153
|
+
identityOf: (a) => a.actor ?? `org:${a.tenant}`, // during.day's
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
// in a worker
|
|
157
|
+
ctx.waitUntil(sink.flushable().promise)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
`posthog-node` is never imported here — you pass the constructor. That keeps this package at zero
|
|
161
|
+
runtime dependencies, keeps your key and host out of it, and makes the whole adapter drivable
|
|
162
|
+
against a stub server with no network.
|
|
163
|
+
|
|
164
|
+
`flushable()` returns `{ pending, promise }`. Read `pending` before awaiting to skip the call when
|
|
165
|
+
there is nothing to send; the promise **resolves**, never rejects, so a host handing it to
|
|
166
|
+
`waitUntil` is not handed a rejection it never asked for. Read the `Result` if you want to know.
|
|
167
|
+
|
|
168
|
+
**Two vendor behaviours this is built around**, both measured against `posthog-node@5.50.0`:
|
|
169
|
+
|
|
170
|
+
1. **A `uuid` that is not a valid UUID is silently replaced.** during.day's ids are
|
|
171
|
+
`crypto.randomUUID()`, so its dedupe works; ids like `evt_7`, a ULID, a nanoid or a Medusa
|
|
172
|
+
`order_01H…` would be dedupe keys that never arrive. The adapter passes a UUID through untouched
|
|
173
|
+
and derives a **stable UUIDv5** from anything else — same envelope, same uuid, every process,
|
|
174
|
+
verified against CPython's `uuid5` — and puts the original on the wire as `properties.envelopeId`.
|
|
175
|
+
2. **`shutdown()` resolves over a batch the server refused.** A 500 and a dead port both leave the
|
|
176
|
+
await resolving normally; the failure is reported only through an `on('error')` listener. The
|
|
177
|
+
adapter subscribes and turns what it hears into the `SinkFailure`.
|
|
178
|
+
|
|
179
|
+
**The browser half is yours.** `posthog-js` is a bundler and framework fact — a key-gated, memoised
|
|
180
|
+
dynamic `import('posthog-js')`, `identify(userId)` on sign-in, `group('organization', orgId)` so the
|
|
181
|
+
browser session and the server events join on the same key. Use the same group name you passed here
|
|
182
|
+
and the two halves meet; that is all this package can usefully say about it.
|
|
183
|
+
|
|
184
|
+
## 5. `--prove`
|
|
185
|
+
|
|
186
|
+
```bash
|
|
187
|
+
geonosis-observability prove --sink memory
|
|
188
|
+
geonosis-observability prove --sink posthog-stub
|
|
189
|
+
geonosis-observability prove --sink posthog --verify-command 'my-posthog-query "$GEONOSIS_TRACE_ID"'
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Plants an error under a trace id nothing else in the world has, then goes and LOOKS.
|
|
193
|
+
|
|
194
|
+
| Verdict | Meaning | Exit |
|
|
195
|
+
|---|---|---|
|
|
196
|
+
| `PROVEN observability: the planted error reached <sink> as <trace id>` | found | 0 |
|
|
197
|
+
| `CANNOT FAIL: the sink swallowed it` | answered ok, kept nothing | 2 |
|
|
198
|
+
| `MISREAD: …` | kept something, but not the plant | 2 |
|
|
199
|
+
| `CANNOT MEASURE: …` | nothing configured, a refused plant, or a failed flush | 2 |
|
|
200
|
+
|
|
201
|
+
`--sink posthog` **refuses without `--verify-command`**. A key proves you can send; sending is not
|
|
202
|
+
arriving, and "the SDK did not complain" is exactly the evidence that was found to be worthless.
|
|
203
|
+
Nothing here queries anybody's project — your command runs with `GEONOSIS_TRACE_ID` in its
|
|
204
|
+
environment and its exit code is the answer.
|
|
205
|
+
|
|
206
|
+
Put your own sink behind the same four verdicts with the exported `proveObservability`:
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
const proof = await proveObservability({ probe: { kind, sink, seen: (id) => lookItUp(id) } })
|
|
210
|
+
process.exit(exitCodeOf(proof))
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## 6. The health contract
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { createHealthRegistry } from '@geonosis/observability/health'
|
|
217
|
+
|
|
218
|
+
const health = createHealthRegistry({
|
|
219
|
+
dependencies: [
|
|
220
|
+
{ name: 'cell', tier: 1, check: () => pingCell(db), timeoutMs: 2000, ttlMs: 5000 },
|
|
221
|
+
{ name: 'controlPlane', tier: 1, check: () => pingControl(control), timeoutMs: 2000 },
|
|
222
|
+
{ name: 'search', tier: 4, check: () => pingSearch(), timeoutMs: 1000, ttlMs: 30_000 },
|
|
223
|
+
],
|
|
224
|
+
})
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
`ready()` runs **tier 1 only** and answers `{ ok, status: 200 | 503, degraded, dependencies }`.
|
|
228
|
+
`dependencies()` runs every tier; its `status` still follows tier 1, so a degraded search index does
|
|
229
|
+
not take a pod out. A registry with no tier-1 dependency answers 503 — a 200 from a registry that
|
|
230
|
+
verified nothing is the `/health` over an unreachable database, built out of an empty array.
|
|
231
|
+
|
|
232
|
+
`timeoutMs` (5s default) makes a hang a failure with `timed out after Nms`, and the timer is cleared
|
|
233
|
+
on the path where the check answered first. `ttlMs` caches a good answer and marks it
|
|
234
|
+
`cached: true`; a failure is **never** cached, because a service reported dead for a minute after it
|
|
235
|
+
recovered is worse than one nobody predicted.
|
|
236
|
+
|
|
237
|
+
### Fixture — Hono
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
app.get('/health/ready', async (c) => {
|
|
241
|
+
const report = await health.ready()
|
|
242
|
+
return c.json(report, report.status)
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
app.get('/health/dependencies', async (c) => {
|
|
246
|
+
const report = await health.dependencies()
|
|
247
|
+
return c.json(report, report.status)
|
|
248
|
+
})
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### Fixture — Node `http`
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
createServer(async (request, response) => {
|
|
255
|
+
const report =
|
|
256
|
+
request.url === '/health/ready' ? await health.ready() : await health.dependencies()
|
|
257
|
+
|
|
258
|
+
response.writeHead(report.status, { 'content-type': 'application/json' })
|
|
259
|
+
response.end(JSON.stringify(report))
|
|
260
|
+
})
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Both are examples, not code this package ships: a health registry that depended on one server could
|
|
264
|
+
not be used by the other.
|
|
265
|
+
|
|
266
|
+
## 7. The doctor line
|
|
267
|
+
|
|
268
|
+
`geonosis-doctor` gains an `observability` check when `geonosis.json` has a block:
|
|
269
|
+
|
|
270
|
+
```json
|
|
271
|
+
{
|
|
272
|
+
"observability": {
|
|
273
|
+
"sink": "posthog",
|
|
274
|
+
"endpoint": "https://eu.i.posthog.com",
|
|
275
|
+
"lastEventFile": ".geonosis/last-event.json",
|
|
276
|
+
"maxAgeSeconds": 3600,
|
|
277
|
+
"probe": "geonosis-observability prove --sink posthog-stub"
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
It asks whether a sink is configured (a `memory`/`noop`/`swallowing` one is a WARN), whether the
|
|
283
|
+
endpoint answers a HEAD, and whether an event arrived inside `maxAgeSeconds`. It reads the FILE
|
|
284
|
+
`withLastEvent` wrote and **imports nothing of this package** — a check that needed the library it
|
|
285
|
+
checks cannot run in the tree where the library is missing, which is the first case it exists to
|
|
286
|
+
find. It names your `probe` command; it never runs it.
|
|
287
|
+
|
|
288
|
+
---
|
|
289
|
+
|
|
290
|
+
## Migrating during.day
|
|
291
|
+
|
|
292
|
+
`apps/api/src/lib/posthog-mirror.ts` becomes a `createPostHogSink` instance plus `withDedupe`.
|
|
293
|
+
|
|
294
|
+
| Today | With this package |
|
|
295
|
+
|---|---|
|
|
296
|
+
| `AnalyticsSink { capture: (events) => Promise<number> }` | `ErrorSink` returning `Result<CaptureReceipt, SinkFailure>` |
|
|
297
|
+
| `distinctId: message.actor ?? \`org:${orgId}\`` | `identityOf: (a) => a.actor ?? \`org:${a.tenant}\`` — the `org:` prefix stays yours |
|
|
298
|
+
| `groups: { organization: orgId }` | `tenant: orgId` + `groupName: 'organization'` (the default) |
|
|
299
|
+
| `uuid: message.id` | `envelopeId: message.id` — already a UUID, so it goes on the wire unchanged |
|
|
300
|
+
| `new PostHog(key, { flushAt: events.length, flushInterval: 0 })` per batch | your `newClient(batchSize)`, verbatim |
|
|
301
|
+
| `context.waitUntil(analytics.capture(mirrored))` | `for (…) await sink.capture(…)`, then `context.waitUntil(sink.flushable().promise)` |
|
|
302
|
+
| `await client.shutdown()` then `return events.length` | the `Result` — `shutdown()` resolving over a refused batch is why |
|
|
303
|
+
| `silentAnalyticsSink` when no key | `createSwallowingSink()`, and `--prove` says `CANNOT FAIL` over it |
|
|
304
|
+
| `handleQueue({ seen: (id) => hasAuditEntryForEvent(db, { eventId: id }) })` | keep it — and add `withDedupe(sink, { seen, remember })` for the counting |
|
|
305
|
+
| `FailureDoor = 'cron' \| 'mail' \| 'queue' \| 'request' \| 'workflow'` | `extra: { door }` — the five values are a worker's, not a vocabulary's |
|
|
306
|
+
| `failureEventFor` minting `crypto.randomUUID()` | leave `envelopeId` off; the sink mints one |
|
|
307
|
+
| `healthStatus(version, probes)` | `createHealthRegistry`, with `cell` and `controlPlane` at tier 1 and a `timeoutMs` each |
|
|
308
|
+
| one `/health` | `/health/ready` from `ready()`, `/health/dependencies` from `dependencies()` |
|
|
309
|
+
| nothing | `release` and `runtime` on every capture, and a `probe` line in `geonosis.json` |
|
|
310
|
+
|
|
311
|
+
`apps/web/lib/analytics/browser.ts` does not move. It is `posthog-js`, a bundler-visible env read and
|
|
312
|
+
a dynamic import — all three are the app's. Keep `group('organization', orgId)` matching `groupName`.
|
|
313
|
+
|
|
314
|
+
## Migrating dielime
|
|
315
|
+
|
|
316
|
+
dielime has **no error sink and no health endpoint at all**. Its migration is adoption, not
|
|
317
|
+
replacement, and its own best-shaped seam is the template:
|
|
318
|
+
`packages/medusa-plugins/customer-service/src/lib/llm-provider.ts` resolves three ways
|
|
319
|
+
(`anthropic` | `claude-cli` | `dev`) and its no-key path is a real deterministic implementation, not
|
|
320
|
+
a null object.
|
|
321
|
+
|
|
322
|
+
1. Put `createMemorySink()` behind that same shape — a resolver that returns the memory sink with no
|
|
323
|
+
key and a real adapter with one. dielime's no-key path is then readable, which
|
|
324
|
+
`silentAnalyticsSink` never was.
|
|
325
|
+
2. `service` is the plugin or the app; `tenant` is nothing until it has one — leave it off rather
|
|
326
|
+
than putting a customer in it.
|
|
327
|
+
3. Medusa entity ids (`order_01H…`) are **not** UUIDs. Pass them as `envelopeId` anyway: the adapter
|
|
328
|
+
derives a stable UUIDv5 and keeps the original as `properties.envelopeId`. Passing one straight to
|
|
329
|
+
`posthog-node` would lose the dedupe silently.
|
|
330
|
+
4. `withDevFallback`'s `[dev-fallback]` marker is the same idea as `deduplicated: true` and the
|
|
331
|
+
`CANNOT FAIL` verdict — a run that did less than it looks like it did says so.
|
|
332
|
+
5. There is no `/health` to migrate. Add `createHealthRegistry` with Postgres and Redis at tier 1
|
|
333
|
+
and the search provider at tier 3, and `--prove` in the gate.
|