Skip to content
Merged
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
4 changes: 3 additions & 1 deletion packages/api/src/server/effect/services/observability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export interface RequestObservabilityContext {
readonly workerName: string
readonly logFormat: "pretty" | "json"
readonly consoleOutputEnabled: boolean
readonly awaitTelemetryFlush: boolean
readonly traceparent: string | null
readonly tracestate: string | null
readonly cfRay: string | null
Expand Down Expand Up @@ -158,6 +159,7 @@ export function makeRequestObservability(
workerName: env.WORKER_NAME,
logFormat: stageMetadata.infra.apiObservabilityLogFormat,
consoleOutputEnabled: stageMetadata.infra.apiObservabilityConsoleOutputEnabled,
awaitTelemetryFlush: stageMetadata.infra.apiObservabilityAwaitTelemetryFlush,
traceparent: request.headers.get("traceparent"),
tracestate: request.headers.get("tracestate"),
cfRay,
Expand Down Expand Up @@ -643,7 +645,7 @@ export function createApiRequestObserver(

const telemetryFlush = Promise.allSettled(pendingTelemetryRuns).then(() => undefined)
ctx.waitUntil(telemetryFlush)
await Match.value(context.stage === "dev").pipe(
await Match.value(context.awaitTelemetryFlush).pipe(
Match.when(true, () => telemetryFlush),
Match.orElse(() => Promise.resolve()),
)
Expand Down
4 changes: 3 additions & 1 deletion packages/infra/src/database-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
PLANETSCALE_SERVICE_TOKEN_ID_ENV,
parseAppDbMode,
parseS0DatabaseEngine,
stageDefaultsToLocalAppDatabase,
type AppDbMode,
type S0DatabaseEngine,
} from "@solzero/shared"
Expand All @@ -21,7 +22,8 @@ export function appDbModeForStage(
configured: unknown,
alchemyDev: boolean,
): AppDbMode {
const fallback: AppDbMode = alchemyDev || stage === "dev" || stage === "test" ? "local" : "remote"
const fallback: AppDbMode =
alchemyDev || stageDefaultsToLocalAppDatabase(stage) ? "local" : "remote"

return parseAppDbMode(configured, fallback)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/infra/src/stack-flags.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { APP_DB_MODE_ENV, DATABASE_ENV } from "@solzero/shared"
import { APP_DB_MODE_ENV, DATABASE_ENV, stageDefaultsToAlchemyDev } from "@solzero/shared"
import {
appDbModeForStage,
bindPlanetscaleAlchemyAuthFromServiceTokens,
Expand All @@ -20,7 +20,7 @@ export function planetscaleStackFlags(
const mode = appDbModeForStage(
stage,
input.appDbMode ?? process.env[APP_DB_MODE_ENV],
input.alchemyDev ?? stage === "dev",
input.alchemyDev ?? stageDefaultsToAlchemyDev(stage),
)

const planetscale = needsPlanetscaleProviders(engine, mode)
Expand Down
11 changes: 3 additions & 8 deletions packages/shared/src/s0-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "./provider-config"
import { type S0DatabaseEngine } from "./database-engine"
import { resolveS0Brand, type S0BrandEnvironment } from "./s0-brand"
import { stageMetadataTagForName } from "./stageMetadata"
import {
normalizeSecretReference,
SecretReferenceSchema,
Expand Down Expand Up @@ -224,14 +225,8 @@ export const S0_CONFIG_JSON_SCHEMA_OPTIONS = {
const S0_CONFIG_PROFILE_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/

export function s0ConfigStageForStage(stage: string): S0ConfigStageName {
return Match.value(stage.trim().toLowerCase()).pipe(
Match.when(
(normalized) => normalized.startsWith("pre-"),
(): S0ConfigStageName => "pre",
),
Match.when(Match.is(...S0_CONFIG_STAGE_NAMES), (normalized) => normalized),
Match.option,
Result.fromOption(
return stageMetadataTagForName(stage.trim()).pipe(
Result.mapError(
() =>
new ConfigValidationError({
message: `Invalid stage '${stage}' for s0 config file selection. Expected dev, test, pre, pre-*, or prod`,
Expand Down
77 changes: 65 additions & 12 deletions packages/shared/src/stageMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ export interface InfraStageProps {
readonly apiObservabilityLogFormat: "pretty" | "json"
/** Whether API request logs should be written to the console. */
readonly apiObservabilityConsoleOutputEnabled: boolean
/**
* Local dev waits for request telemetry before the response returns, so the
* workerd console shows the log on the same request. Other stages record it
* with waitUntil and do not block the response.
*/
readonly apiObservabilityAwaitTelemetryFlush: boolean
/** Cloudflare Workers Observability destinations for exported API logs. */
readonly apiObservabilityLogsDestinations: readonly string[]
/** Cloudflare Workers Observability destinations for exported API traces. */
Expand Down Expand Up @@ -166,6 +172,7 @@ const InfraStagePropsSchema = Schema.Struct({
apiObservabilityTracesHeadSamplingRate: Schema.Number,
apiObservabilityLogFormat: Schema.Literals(["pretty", "json"]),
apiObservabilityConsoleOutputEnabled: Schema.Boolean,
apiObservabilityAwaitTelemetryFlush: Schema.Boolean,
apiObservabilityLogsDestinations: Schema.Array(Schema.String),
apiObservabilityTracesDestinations: Schema.Array(Schema.String),
alchemyStateStore: Schema.Literals(["local", "cloudflare"]),
Expand Down Expand Up @@ -224,7 +231,10 @@ function invalidStageError(stageStr: string): InvalidStageError {

function localInfraStageProps(
config: StageInfraConfig,
consoleOutputEnabled: boolean,
options: {
readonly consoleOutputEnabled: boolean
readonly awaitTelemetryFlush: boolean
},
): InfraStageProps {
const zone = "localhost"
const authBaseUrl = "http://localhost:3000"
Expand Down Expand Up @@ -255,7 +265,8 @@ function localInfraStageProps(
() => 1,
),
apiObservabilityLogFormat: "pretty",
apiObservabilityConsoleOutputEnabled: consoleOutputEnabled,
apiObservabilityConsoleOutputEnabled: options.consoleOutputEnabled,
apiObservabilityAwaitTelemetryFlush: options.awaitTelemetryFlush,
apiObservabilityLogsDestinations: Option.getOrElse(
config.apiObservabilityLogsDestinations,
() => [],
Expand Down Expand Up @@ -327,6 +338,7 @@ function deployedInfraStageProps(input: {
),
apiObservabilityLogFormat: "json",
apiObservabilityConsoleOutputEnabled: true,
apiObservabilityAwaitTelemetryFlush: false,
apiObservabilityLogsDestinations: Option.getOrElse(
config.apiObservabilityLogsDestinations,
() => [],
Expand Down Expand Up @@ -356,7 +368,10 @@ export class Dev extends Data.TaggedClass("dev")<StageProps> {
return new Dev({
name: "dev",
app,
infra: localInfraStageProps(config, true),
infra: localInfraStageProps(config, {
consoleOutputEnabled: true,
awaitTelemetryFlush: true,
}),
})
}
}
Expand All @@ -378,7 +393,10 @@ export class Test extends Data.TaggedClass("test")<StageProps> {
return new Test({
name: "test",
app,
infra: localInfraStageProps(config, false),
infra: localInfraStageProps(config, {
consoleOutputEnabled: false,
awaitTelemetryFlush: false,
}),
})
}
}
Expand Down Expand Up @@ -440,6 +458,42 @@ export class Prod extends Data.TaggedClass("prod")<StageProps> {
}
}

export type StageMetadataTag = "dev" | "test" | "pre" | "prod"

/** The stage class for a stage name. `pre` and `pre-*` are both preview. */
export function stageMetadataTagForName(
stage: string,
): Result.Result<StageMetadataTag, InvalidStageError> {
const lowerCaseStage = stage.toLowerCase()

return Match.value(lowerCaseStage).pipe(
Match.when("dev", () => Result.succeed("dev" as const)),
Match.when("test", () => Result.succeed("test" as const)),
Match.when("prod", () => Result.succeed("prod" as const)),
Match.when(
(value) => value === "pre" || value.startsWith("pre-"),
() => Result.succeed("pre" as const),
),
Match.orElse(() => Result.fail(invalidStageError(stage))),
)
}

/** Dev and test use a local application database unless the operator selects remote. */
export function stageDefaultsToLocalAppDatabase(stage: string): boolean {
return Result.match(stageMetadataTagForName(stage), {
onFailure: () => false,
onSuccess: (tag) => tag === "dev" || tag === "test",
})
}

/** Alchemy's local dev process is the default only for the dev stage. */
export function stageDefaultsToAlchemyDev(stage: string): boolean {
return Result.match(stageMetadataTagForName(stage), {
onFailure: () => false,
onSuccess: (tag) => tag === "dev",
})
}

export function getStageMetadataFromStr(
stageStr: string,
config: StageInfraConfig = EMPTY_STAGE_INFRA_CONFIG,
Expand All @@ -451,15 +505,14 @@ export function getStageMetadataFromStr(
Option.map(Option.fromUndefinedOr(application), configuredAppStageProps),
)

return Match.value(lowerCaseStage).pipe(
Match.when("dev", () => Result.succeed(Dev.make(config, app))),
Match.when("test", () => Result.succeed(Test.make(config, app))),
Match.when("prod", () => Result.succeed(Prod.make(config, app))),
Match.when(
(stage) => stage === "pre" || stage.startsWith("pre-"),
(stage) => Result.succeed(Pre.make(stage, config, app)),
return Result.map(stageMetadataTagForName(stageStr), (tag) =>
Match.value(tag).pipe(
Match.when("dev", () => Dev.make(config, app)),
Match.when("test", () => Test.make(config, app)),
Match.when("prod", () => Prod.make(config, app)),
Match.when("pre", () => Pre.make(lowerCaseStage, config, app)),
Match.exhaustive,
),
Match.orElse(() => Result.fail(invalidStageError(stageStr))),
)
}

Expand Down
Loading