Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions apps/server/src/telemetry/AnalyticsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, it } from "@effect/vitest";
import * as ConfigProvider from "effect/ConfigProvider";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Scope from "effect/Scope";
import * as TestClock from "effect/testing/TestClock";
import * as HttpClient from "effect/unstable/http/HttpClient";
import * as HttpServer from "effect/unstable/http/HttpServer";
import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest";
import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
Expand Down Expand Up @@ -191,4 +198,63 @@ it.layer(NodeServices.layer)("AnalyticsService test", (it) => {
assert.deepEqual(capturedPaths, []);
}),
);

it.effect("flush completes instead of hanging when the telemetry endpoint never responds", () =>
Effect.gen(function* () {
const serverConfigLayer = ServerConfig.ServerConfig.layerTest(process.cwd(), {
prefix: "t3-telemetry-hang-",
});
const configLayer = ConfigProvider.layer(
ConfigProvider.fromUnknown({
T3CODE_TELEMETRY_ENABLED: true,
T3CODE_POSTHOG_KEY: "phc_test_key",
T3CODE_POSTHOG_HOST: "http://localhost",
T3CODE_TELEMETRY_FLUSH_BATCH_SIZE: 20,
}),
);
// Simulates a wedged endpoint: the connection is accepted, but no
// response bytes ever arrive.
const hangingClient = HttpClient.make(() => Effect.never);
const telemetryLayer = AnalyticsService.layer.pipe(
Layer.provideMerge(serverConfigLayer),
Layer.provide(configLayer),
Layer.provide(
Layer.mergeAll(
Layer.succeed(HostProcessPlatform, "linux"),
Layer.succeed(HostProcessArchitecture, "arm64"),
),
),
Layer.provide(Layer.succeed(HttpClient.HttpClient, hangingClient)),
);

// The service registers a shutdown finalizer that flushes, so the
// scope is managed explicitly and the test clock keeps moving while
// it closes.
const scope = yield* Scope.make();
const context = yield* Layer.build(telemetryLayer).pipe(
Effect.provideService(Scope.Scope, scope),
);
const analytics = Context.get(context, AnalyticsService.AnalyticsService);

yield* analytics.record("test.hang", { index: 1 });

const flushFiber = yield* analytics.flush.pipe(Effect.forkIn(scope));
const joinFiber = yield* Fiber.join(flushFiber).pipe(
Effect.timeoutOption("20 seconds"),
Effect.forkDetach,
);
yield* TestClock.adjust("30 seconds");
const completed = yield* Fiber.join(joinFiber);
yield* Fiber.interrupt(flushFiber);

const closeFiber = yield* Scope.close(scope, Exit.void).pipe(Effect.forkDetach);
yield* TestClock.adjust("5 minutes");
yield* Fiber.join(closeFiber);

assert.isTrue(
Option.isSome(completed),
"flush must complete instead of hanging forever when the telemetry endpoint never responds",
);
}),
);
});
6 changes: 6 additions & 0 deletions apps/server/src/telemetry/AnalyticsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { ClientOs } from "@t3tools/contracts";
import * as Config from "effect/Config";
import * as Context from "effect/Context";
import * as DateTime from "effect/DateTime";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
Expand Down Expand Up @@ -44,6 +45,10 @@ const TelemetryEnvConfig = Config.all({
wslDistroName: Config.String("WSL_DISTRO_NAME").pipe(Config.option),
});

// A telemetry endpoint that accepts the connection but never responds must
// not park the flush loop or stall shutdown; telemetry stays best-effort.
const ANALYTICS_SEND_TIMEOUT = Duration.seconds(10);

export class AnalyticsService extends Context.Service<
AnalyticsService,
{
Expand Down Expand Up @@ -151,6 +156,7 @@ export const make = Effect.gen(function* () {
yield* HttpClientRequest.post(`${telemetryConfig.posthogHost}/batch/`).pipe(
HttpClientRequest.bodyJson(payload),
Effect.flatMap(httpClient.execute),
Effect.timeout(ANALYTICS_SEND_TIMEOUT),
Effect.flatMap(HttpClientResponse.filterStatusOk),
);
});
Expand Down
Loading