| @@ -4,7 +4,11 @@ import { assert, it } from "@effect/vitest"; · 3 unmodified lines |
| 4 | 4 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 5 | 5 | import * as Effect from "effect/Effect"; |
| 6 | 6 | import * as Layer from "effect/Layer"; |
| 7 | +import * as Logger from "effect/Logger"; |
| 8 | +import * as References from "effect/References"; |
| 9 | +import * as Schema from "effect/Schema"; |
| 7 | 10 | import * as HttpServer from "effect/unstable/http/HttpServer"; |
| 11 | +import * as HttpClientError from "effect/unstable/http/HttpClientError"; |
| 8 | 12 | import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest"; |
| 9 | 13 | import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse"; |
| 10 | 14 | |
| @@ -35,10 +39,25 @@ interface RecordedBatchBody { · 24 unmodified lines |
| 35 | 39 | }>; |
| 36 | 40 | } |
| 37 | 41 | |
| 42 | +interface CapturedLog { |
| 43 | + readonly message: unknown; |
| 44 | + readonly annotations: Readonly<Record<string, unknown>>; |
| 45 | +} |
| 46 | + |
| 47 | +const isAnalyticsBatchDeliveryError = Schema.is(AnalyticsService.AnalyticsBatchDeliveryError); |
| 48 | + |
| 38 | 49 | it.layer(NodeServices.layer)("AnalyticsService test", (it) => { |
| 39 | | − it.effect("flush drains all buffered events across multiple batches", () => |
| 50 | + it.effect("flush drains buffered events and retries failed batches with structured context", () => |
| 40 | 51 | Effect.gen(function* () { |
| 41 | 52 | const capturedRequests: Array<RecordedBatchRequest> = []; |
| 53 | + const capturedLogs: CapturedLog[] = []; |
| 54 | + let rejectNextBatch = false; |
| 55 | + const logger = Logger.make(({ fiber, message }) => { |
| 56 | + capturedLogs.push({ |
| 57 | + message, |
| 58 | + annotations: fiber.getRef(References.CurrentLogAnnotations), |
| 59 | + }); |
| 60 | + }); |
| 42 | 61 | const serverConfigLayer = ServerConfig.ServerConfig.layerTest(process.cwd(), { |
| 43 | 62 | prefix: "t3-telemetry-base-", |
| 44 | 63 | }); |
| @@ -66,12 +85,20 @@ it.layer(NodeServices.layer)("AnalyticsService test", (it) => { · 21 unmodified lines |
| 66 | 85 | |
| 67 | 86 | capturedRequests.push({ path: request.url, body: payload }); |
| 68 | 87 | |
| 88 | + if (rejectNextBatch) { |
| 89 | + rejectNextBatch = false; |
| 90 | + return HttpServerResponse.empty({ status: 503 }); |
| 91 | + } |
| 92 | + |
| 69 | 93 | return HttpServerResponse.jsonUnsafe({}); |
| 70 | 94 | }), |
| 71 | 95 | ); |
| 72 | | − const runtimeLayer = telemetryLayer.pipe( |
| 73 | | − Layer.provide(configLayer), |
| 74 | | − Layer.provideMerge(NodeHttpServer.layerTest), |
| 96 | + const runtimeLayer = Layer.merge( |
| 97 | + telemetryLayer.pipe( |
| 98 | + Layer.provide(configLayer), |
| 99 | + Layer.provideMerge(NodeHttpServer.layerTest), |
| 100 | + ), |
| 101 | + Logger.layer([logger], { mergeWithExisting: false }), |
| 75 | 102 | ); |
| 76 | 103 | |
| 77 | 104 | yield* Effect.gen(function* () { |
| @@ -85,12 +112,17 @@ it.layer(NodeServices.layer)("AnalyticsService test", (it) => { · 7 unmodified lines |
| 85 | 112 | } |
| 86 | 113 | |
| 87 | 114 | yield* analytics.flush; |
| 115 | + yield* analytics.record("test.flush.retry", { index: 45 }); |
| 116 | + rejectNextBatch = true; |
| 117 | + yield* analytics.flush; |
| 118 | + yield* analytics.flush; |
| 88 | 119 | }).pipe(Effect.provide(runtimeLayer)); |
| 89 | 120 | |
| 90 | | − const batchRequests = capturedRequests.filter( |
| 91 | | − (request): request is RecordedBatchRequest & { readonly body: RecordedBatchBody } => |
| 121 | + const batchRequests = capturedRequests |
| 122 | + .slice(0, 3) |
| 123 | + .filter((request): request is RecordedBatchRequest & { readonly body: RecordedBatchBody } => |
| 92 | 124 | Array.isArray(request.body?.batch), |
| 93 | | − ); |
| 125 | + ); |
| 94 | 126 | assert.equal(batchRequests.length, 3); |
| 95 | 127 | assert.equal( |
| 96 | 128 | batchRequests.every((request) => request.path === "/batch/" || request.path === "/batch"), |
| @@ -115,6 +147,53 @@ it.layer(NodeServices.layer)("AnalyticsService test", (it) => { · 18 unmodified lines |
| 115 | 147 | ), |
| 116 | 148 | true, |
| 117 | 149 | ); |
| 150 | + |
| 151 | + const retryRequests = capturedRequests.slice(3); |
| 152 | + assert.equal(retryRequests.length, 2); |
| 153 | + assert.deepEqual(retryRequests[0]?.body, retryRequests[1]?.body); |
| 154 | + |
| 155 | + const deliveryLog = capturedLogs.find((log) => |
| 156 | + isAnalyticsBatchDeliveryError(log.annotations.cause), |
| 157 | + ); |
| 158 | + assert.isDefined(deliveryLog); |
| 159 | + assert.equal( |
| 160 | + deliveryLog?.message, |
| 161 | + "Failed to deliver 1 analytics event to PostHog (endpoint input length 7).", |
| 162 | + ); |
| 163 | + assert.equal("endpoint" in deliveryLog.annotations, false); |
| 164 | + assert.equal(deliveryLog.annotations.endpointInputLength, 7); |
| 165 | + |
| 166 | + const error = deliveryLog?.annotations.cause; |
| 167 | + assert.instanceOf(error, AnalyticsService.AnalyticsBatchDeliveryError); |
| 168 | + if (isAnalyticsBatchDeliveryError(error)) { |
| 169 | + assert.equal(error.endpointInputLength, 7); |
| 170 | + assert.equal(error.endpointProtocol, undefined); |
| 171 | + assert.equal(error.endpointHostname, undefined); |
| 172 | + assert.equal(error.eventCount, 1); |
| 173 | + assert.instanceOf(error.cause, HttpClientError.HttpClientError); |
| 174 | + if (error.cause instanceof HttpClientError.HttpClientError) { |
| 175 | + assert.equal(error.cause.reason._tag, "StatusCodeError"); |
| 176 | + assert.equal(error.cause.response?.status, 503); |
| 177 | + } |
| 178 | + } |
| 118 | 179 | }), |
| 119 | 180 | ); |
| 120 | 181 | }); |
| 182 | + |
| 183 | +it("keeps configured PostHog endpoint secrets out of direct error and log context", () => { |
| 184 | + const endpoint = |
| 185 | + "https://user:password@posthog.example.test/private/project?api_key=secret#fragment"; |
| 186 | + const cause = new Error("delivery failed"); |
| 187 | + const error = AnalyticsService.AnalyticsBatchDeliveryError.fromEndpoint({ |
| 188 | + endpoint, |
| 189 | + eventCount: 2, |
| 190 | + cause, |
| 191 | + }); |
| 192 | + |
| 193 | + assert.equal(error.endpointInputLength, endpoint.length); |
| 194 | + assert.equal(error.endpointProtocol, "https:"); |
| 195 | + assert.equal(error.endpointHostname, "posthog.example.test"); |
| 196 | + assert.equal(error.cause, cause); |
| 197 | + assert.equal("endpoint" in error, false); |
| 198 | + assert.equal(/user|password|private|project|api_key|secret|fragment/.test(error.message), false); |
| 199 | +}); |