|
7 | 7 | * @module AnalyticsService |
8 | 8 | */ |
9 | 9 | import { HostProcessArchitecture, HostProcessPlatform } from "@t3tools/shared/hostProcess"; |
| 10 | +import { getUrlDiagnostics } from "@t3tools/shared/urlDiagnostics"; |
10 | 11 | import * as Config from "effect/Config"; |
11 | 12 | import * as Context from "effect/Context"; |
12 | 13 | import * as DateTime from "effect/DateTime"; |
@@ -47,14 +48,32 @@ const TelemetryEnvConfig = Config.all({ |
47 | 48 | export class AnalyticsBatchDeliveryError extends Schema.TaggedErrorClass<AnalyticsBatchDeliveryError>()( |
48 | 49 | "AnalyticsBatchDeliveryError", |
49 | 50 | { |
50 | | - endpoint: Schema.String, |
| 51 | + endpointInputLength: Schema.Number, |
| 52 | + endpointProtocol: Schema.optionalKey(Schema.String), |
| 53 | + endpointHostname: Schema.optionalKey(Schema.String), |
51 | 54 | eventCount: Schema.Int.check(Schema.isGreaterThan(0)), |
52 | 55 | cause: Schema.Defect(), |
53 | 56 | }, |
54 | 57 | ) { |
| 58 | + static fromEndpoint(input: { |
| 59 | + readonly endpoint: string; |
| 60 | + readonly eventCount: number; |
| 61 | + readonly cause: unknown; |
| 62 | + }): AnalyticsBatchDeliveryError { |
| 63 | + const diagnostics = getUrlDiagnostics(input.endpoint); |
| 64 | + return new AnalyticsBatchDeliveryError({ |
| 65 | + endpointInputLength: diagnostics.inputLength, |
| 66 | + ...(diagnostics.protocol === undefined ? {} : { endpointProtocol: diagnostics.protocol }), |
| 67 | + ...(diagnostics.hostname === undefined ? {} : { endpointHostname: diagnostics.hostname }), |
| 68 | + eventCount: input.eventCount, |
| 69 | + cause: input.cause, |
| 70 | + }); |
| 71 | + } |
| 72 | + |
55 | 73 | override get message(): string { |
56 | 74 | const eventLabel = this.eventCount === 1 ? "event" : "events"; |
57 | | - return `Failed to deliver ${this.eventCount} analytics ${eventLabel} to PostHog at '${this.endpoint}'.`; |
| 75 | + const destination = this.endpointHostname ? ` at ${this.endpointHostname}` : ""; |
| 76 | + return `Failed to deliver ${this.eventCount} analytics ${eventLabel} to PostHog${destination} (endpoint input length ${this.endpointInputLength}).`; |
58 | 77 | } |
59 | 78 | } |
60 | 79 |
|
@@ -146,13 +165,12 @@ export const make = Effect.gen(function* () { |
146 | 165 | HttpClientRequest.bodyJson(payload), |
147 | 166 | Effect.flatMap(httpClient.execute), |
148 | 167 | Effect.flatMap(HttpClientResponse.filterStatusOk), |
149 | | - Effect.mapError( |
150 | | - (cause) => |
151 | | - new AnalyticsBatchDeliveryError({ |
152 | | - endpoint: batchEndpoint, |
153 | | - eventCount: events.length, |
154 | | - cause, |
155 | | - }), |
| 168 | + Effect.mapError((cause) => |
| 169 | + AnalyticsBatchDeliveryError.fromEndpoint({ |
| 170 | + endpoint: batchEndpoint, |
| 171 | + eventCount: events.length, |
| 172 | + cause, |
| 173 | + }), |
156 | 174 | ), |
157 | 175 | ); |
158 | 176 | }); |
@@ -186,7 +204,13 @@ export const make = Effect.gen(function* () { |
186 | 204 | AnalyticsBatchDeliveryError: (error) => |
187 | 205 | Effect.logError(error.message).pipe( |
188 | 206 | Effect.annotateLogs({ |
189 | | - endpoint: error.endpoint, |
| 207 | + endpointInputLength: error.endpointInputLength, |
| 208 | + ...(error.endpointProtocol === undefined |
| 209 | + ? {} |
| 210 | + : { endpointProtocol: error.endpointProtocol }), |
| 211 | + ...(error.endpointHostname === undefined |
| 212 | + ? {} |
| 213 | + : { endpointHostname: error.endpointHostname }), |
190 | 214 | eventCount: error.eventCount, |
191 | 215 | cause: error, |
192 | 216 | }), |
|
0 commit comments