Skip to content

Feature flag debug UI #16010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 29, 2025
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
1 change: 1 addition & 0 deletions packages/backend-core/src/constants/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export enum Cookie {
ACCOUNT_RETURN_URL = "budibase:account:returnurl",
DatasourceAuth = "budibase:datasourceauth",
OIDC_CONFIG = "budibase:oidc:config",
FeatureFlags = "budibase:featureflags",
}

export { Header } from "@budibase/shared-core"
Expand Down
11 changes: 11 additions & 0 deletions packages/backend-core/src/context/mainContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,17 @@ export function setFeatureFlags(key: string, value: Record<string, boolean>) {
context.featureFlagCache[key] = value
}

export function getFeatureFlagOverrides(): Record<string, boolean> {
return getCurrentContext()?.featureFlagOverrides || {}
}

export async function doInFeatureFlagOverrideContext<T>(
value: Record<string, boolean>,
callback: () => Promise<T>
) {
return await newContext({ featureFlagOverrides: value }, callback)
}

export function getTableForView(viewId: string): Table | undefined {
const context = getCurrentContext()
if (!context) {
Expand Down
1 change: 1 addition & 0 deletions packages/backend-core/src/context/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ export type ContextMap = {
featureFlagCache?: {
[key: string]: Record<string, boolean>
}
featureFlagOverrides?: Record<string, boolean>
viewToTableCache?: Record<string, Table>
}
15 changes: 15 additions & 0 deletions packages/backend-core/src/features/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,21 @@ export class FlagSet<T extends { [name: string]: boolean }> {
}
}

const overrides = context.getFeatureFlagOverrides()
for (const [key, value] of Object.entries(overrides)) {
if (!this.isFlagName(key)) {
continue
}

if (typeof value !== "boolean") {
continue
}

// @ts-expect-error - TS does not like you writing into a generic type.
flagValues[key] = value
tags[`flags.${key}.source`] = "override"
}

context.setFeatureFlags(this.setId, flagValues)
for (const [key, value] of Object.entries(flagValues)) {
tags[`flags.${key}.value`] = value
Expand Down
13 changes: 13 additions & 0 deletions packages/backend-core/src/middleware/featureFlagCookie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Ctx, FeatureFlagCookie } from "@budibase/types"
import { Middleware, Next } from "koa"
import { getCookie } from "../utils"
import { Cookie } from "../constants"
import { doInFeatureFlagOverrideContext } from "../context"

export default (async (ctx: Ctx, next: Next) => {
const cookie = getCookie<FeatureFlagCookie>(ctx, Cookie.FeatureFlags)
const flags = cookie?.flags || {}
await doInFeatureFlagOverrideContext(flags, async () => {
await next()
})
}) as Middleware
1 change: 1 addition & 0 deletions packages/backend-core/src/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ export { default as correlation } from "../logging/correlation/middleware"
export { default as errorHandling } from "./errorHandling"
export { default as querystringToBody } from "./querystringToBody"
export { default as csp } from "./contentSecurityPolicy"
export { default as featureFlagCookie } from "./featureFlagCookie"
export * as joiValidator from "./joi-validator"
export { default as ip } from "./ip"
Loading
Loading