2022-11-24 19:48:51 +01:00
|
|
|
import env from "../environment"
|
2023-02-13 12:57:30 +01:00
|
|
|
import * as context from "../context"
|
2024-08-07 17:59:33 +02:00
|
|
|
import { PostHog, PostHogOptions } from "posthog-node"
|
2024-08-09 16:59:04 +02:00
|
|
|
import { IdentityType, UserCtx } from "@budibase/types"
|
2024-08-07 18:50:39 +02:00
|
|
|
import tracer from "dd-trace"
|
2024-08-07 16:26:04 +02:00
|
|
|
|
|
|
|
let posthog: PostHog | undefined
|
2024-08-07 17:59:33 +02:00
|
|
|
export function init(opts?: PostHogOptions) {
|
2024-08-09 16:59:04 +02:00
|
|
|
if (env.POSTHOG_TOKEN && env.POSTHOG_API_HOST) {
|
2024-08-12 16:33:48 +02:00
|
|
|
console.log("initializing posthog client...")
|
2024-08-07 16:26:04 +02:00
|
|
|
posthog = new PostHog(env.POSTHOG_TOKEN, {
|
2024-08-09 12:27:43 +02:00
|
|
|
host: env.POSTHOG_API_HOST,
|
2024-08-12 17:32:25 +02:00
|
|
|
personalApiKey: env.POSTHOG_PERSONAL_TOKEN,
|
2024-08-07 17:59:33 +02:00
|
|
|
...opts,
|
2024-08-07 16:26:04 +02:00
|
|
|
})
|
2024-08-12 16:33:48 +02:00
|
|
|
} else {
|
|
|
|
console.log("posthog disabled")
|
2024-08-07 16:26:04 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-20 21:52:29 +01:00
|
|
|
|
2024-08-14 16:41:26 +02:00
|
|
|
export function shutdown() {
|
|
|
|
posthog?.shutdown()
|
|
|
|
}
|
|
|
|
|
2024-08-09 12:27:43 +02:00
|
|
|
export abstract class Flag<T> {
|
2024-08-07 16:26:04 +02:00
|
|
|
static boolean(defaultValue: boolean): Flag<boolean> {
|
|
|
|
return new BooleanFlag(defaultValue)
|
2022-03-22 01:23:22 +01:00
|
|
|
}
|
|
|
|
|
2024-08-07 17:59:33 +02:00
|
|
|
static string(defaultValue: string): Flag<string> {
|
|
|
|
return new StringFlag(defaultValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
static number(defaultValue: number): Flag<number> {
|
|
|
|
return new NumberFlag(defaultValue)
|
|
|
|
}
|
|
|
|
|
2024-08-07 16:26:04 +02:00
|
|
|
protected constructor(public defaultValue: T) {}
|
|
|
|
|
|
|
|
abstract parse(value: any): T
|
|
|
|
}
|
|
|
|
|
2024-08-09 12:27:43 +02:00
|
|
|
type UnwrapFlag<F> = F extends Flag<infer U> ? U : never
|
|
|
|
|
|
|
|
export type FlagValues<T> = {
|
|
|
|
[K in keyof T]: UnwrapFlag<T[K]>
|
|
|
|
}
|
|
|
|
|
|
|
|
type KeysOfType<T, U> = {
|
|
|
|
[K in keyof T]: T[K] extends Flag<U> ? K : never
|
|
|
|
}[keyof T]
|
|
|
|
|
2024-08-07 16:26:04 +02:00
|
|
|
class BooleanFlag extends Flag<boolean> {
|
|
|
|
parse(value: any) {
|
|
|
|
if (typeof value === "string") {
|
|
|
|
return ["true", "t", "1"].includes(value.toLowerCase())
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof value === "boolean") {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`could not parse value "${value}" as boolean`)
|
|
|
|
}
|
2024-07-22 18:43:53 +02:00
|
|
|
}
|
2022-03-22 01:23:22 +01:00
|
|
|
|
2024-08-07 17:59:33 +02:00
|
|
|
class StringFlag extends Flag<string> {
|
|
|
|
parse(value: any) {
|
|
|
|
if (typeof value === "string") {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
throw new Error(`could not parse value "${value}" as string`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class NumberFlag extends Flag<number> {
|
|
|
|
parse(value: any) {
|
|
|
|
if (typeof value === "number") {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof value === "string") {
|
|
|
|
const parsed = parseFloat(value)
|
|
|
|
if (!isNaN(parsed)) {
|
|
|
|
return parsed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`could not parse value "${value}" as number`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 12:27:43 +02:00
|
|
|
export class FlagSet<V extends Flag<any>, T extends { [key: string]: V }> {
|
2024-08-14 16:41:26 +02:00
|
|
|
// This is used to safely cache flags sets in the current request context.
|
|
|
|
// Because multiple sets could theoretically exist, we don't want the cache of
|
|
|
|
// one to leak into another.
|
|
|
|
private readonly setId: string
|
|
|
|
|
|
|
|
constructor(private readonly flagSchema: T) {
|
|
|
|
this.setId = crypto.randomUUID()
|
|
|
|
}
|
2022-03-22 01:23:22 +01:00
|
|
|
|
2024-08-09 12:27:43 +02:00
|
|
|
defaults(): FlagValues<T> {
|
2024-08-12 10:58:44 +02:00
|
|
|
return Object.keys(this.flagSchema).reduce((acc, key) => {
|
2024-08-09 12:27:43 +02:00
|
|
|
const typedKey = key as keyof T
|
2024-08-12 10:58:44 +02:00
|
|
|
acc[typedKey] = this.flagSchema[key].defaultValue
|
2024-08-09 12:27:43 +02:00
|
|
|
return acc
|
|
|
|
}, {} as FlagValues<T>)
|
|
|
|
}
|
2022-03-22 01:23:22 +01:00
|
|
|
|
2024-08-09 12:27:43 +02:00
|
|
|
isFlagName(name: string | number | symbol): name is keyof T {
|
2024-08-12 10:58:44 +02:00
|
|
|
return this.flagSchema[name as keyof T] !== undefined
|
2024-08-09 12:27:43 +02:00
|
|
|
}
|
2024-07-22 18:43:53 +02:00
|
|
|
|
2024-08-09 16:59:04 +02:00
|
|
|
async get<K extends keyof T>(
|
|
|
|
key: K,
|
|
|
|
ctx?: UserCtx
|
|
|
|
): Promise<FlagValues<T>[K]> {
|
|
|
|
const flags = await this.fetch(ctx)
|
2024-08-09 12:27:43 +02:00
|
|
|
return flags[key]
|
|
|
|
}
|
2024-07-22 18:43:53 +02:00
|
|
|
|
2024-08-09 16:59:04 +02:00
|
|
|
async isEnabled<K extends KeysOfType<T, boolean>>(
|
|
|
|
key: K,
|
|
|
|
ctx?: UserCtx
|
|
|
|
): Promise<boolean> {
|
|
|
|
const flags = await this.fetch(ctx)
|
2024-08-09 12:27:43 +02:00
|
|
|
return flags[key]
|
|
|
|
}
|
2024-07-22 18:43:53 +02:00
|
|
|
|
2024-08-09 16:59:04 +02:00
|
|
|
async fetch(ctx?: UserCtx): Promise<FlagValues<T>> {
|
2024-08-09 12:27:43 +02:00
|
|
|
return await tracer.trace("features.fetch", async span => {
|
2024-08-14 16:56:12 +02:00
|
|
|
const cachedFlags = context.getFeatureFlags<FlagValues<T>>(this.setId)
|
2024-08-14 16:41:26 +02:00
|
|
|
if (cachedFlags) {
|
|
|
|
span?.addTags({ fromCache: true })
|
|
|
|
return cachedFlags
|
|
|
|
}
|
|
|
|
|
2024-08-09 12:27:43 +02:00
|
|
|
const tags: Record<string, any> = {}
|
2024-08-12 10:58:44 +02:00
|
|
|
const flagValues = this.defaults()
|
2024-08-09 12:27:43 +02:00
|
|
|
const currentTenantId = context.getTenantId()
|
|
|
|
const specificallySetFalse = new Set<string>()
|
2024-08-07 16:26:04 +02:00
|
|
|
|
2024-08-09 12:27:43 +02:00
|
|
|
const split = (env.TENANT_FEATURE_FLAGS || "")
|
|
|
|
.split(",")
|
|
|
|
.map(x => x.split(":"))
|
|
|
|
for (const [tenantId, ...features] of split) {
|
|
|
|
if (!tenantId || (tenantId !== "*" && tenantId !== currentTenantId)) {
|
2024-08-07 18:50:39 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-08-12 16:33:48 +02:00
|
|
|
tags[`readFromEnvironmentVars`] = true
|
|
|
|
|
2024-08-09 12:27:43 +02:00
|
|
|
for (let feature of features) {
|
|
|
|
let value = true
|
|
|
|
if (feature.startsWith("!")) {
|
|
|
|
feature = feature.slice(1)
|
|
|
|
value = false
|
|
|
|
specificallySetFalse.add(feature)
|
|
|
|
}
|
2024-08-07 18:50:39 +02:00
|
|
|
|
2024-08-09 12:27:43 +02:00
|
|
|
if (!this.isFlagName(feature)) {
|
|
|
|
throw new Error(`Feature: ${feature} is not an allowed option`)
|
|
|
|
}
|
|
|
|
|
2024-08-12 10:58:44 +02:00
|
|
|
if (typeof flagValues[feature] !== "boolean") {
|
2024-08-09 12:27:43 +02:00
|
|
|
throw new Error(`Feature: ${feature} is not a boolean`)
|
|
|
|
}
|
2024-08-07 18:50:39 +02:00
|
|
|
|
2024-08-12 10:58:44 +02:00
|
|
|
// @ts-expect-error - TS does not like you writing into a generic type,
|
|
|
|
// but we know that it's okay in this case because it's just an object.
|
|
|
|
flagValues[feature] = value
|
2024-08-09 12:27:43 +02:00
|
|
|
tags[`flags.${feature}.source`] = "environment"
|
2024-08-07 18:50:39 +02:00
|
|
|
}
|
2024-08-07 16:26:04 +02:00
|
|
|
}
|
|
|
|
|
2024-08-09 16:59:04 +02:00
|
|
|
const license = ctx?.user?.license
|
|
|
|
if (license) {
|
2024-08-12 16:33:48 +02:00
|
|
|
tags[`readFromLicense`] = true
|
|
|
|
|
2024-08-09 16:59:04 +02:00
|
|
|
for (const feature of license.features) {
|
2024-08-12 10:58:44 +02:00
|
|
|
if (!this.isFlagName(feature)) {
|
2024-08-09 16:59:04 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-08-12 10:58:44 +02:00
|
|
|
if (
|
|
|
|
flagValues[feature] === true ||
|
|
|
|
specificallySetFalse.has(feature)
|
|
|
|
) {
|
2024-08-09 16:59:04 +02:00
|
|
|
// If the flag is already set to through environment variables, we
|
|
|
|
// don't want to override it back to false here.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-08-12 10:58:44 +02:00
|
|
|
// @ts-expect-error - TS does not like you writing into a generic type,
|
|
|
|
// but we know that it's okay in this case because it's just an object.
|
|
|
|
flagValues[feature] = true
|
2024-08-09 16:59:04 +02:00
|
|
|
tags[`flags.${feature}.source`] = "license"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 12:27:43 +02:00
|
|
|
const identity = context.getIdentity()
|
2024-08-12 16:33:48 +02:00
|
|
|
tags[`identity.type`] = identity?.type
|
|
|
|
tags[`identity.tenantId`] = identity?.tenantId
|
|
|
|
tags[`identity._id`] = identity?._id
|
|
|
|
|
2024-08-14 16:41:26 +02:00
|
|
|
if (posthog && identity?.type === IdentityType.USER) {
|
2024-08-12 16:33:48 +02:00
|
|
|
tags[`readFromPostHog`] = true
|
|
|
|
|
2024-08-12 17:32:25 +02:00
|
|
|
const personProperties: Record<string, string> = {}
|
|
|
|
if (identity.tenantId) {
|
|
|
|
personProperties.tenantId = identity.tenantId
|
|
|
|
}
|
|
|
|
|
|
|
|
const posthogFlags = await posthog.getAllFlagsAndPayloads(
|
|
|
|
identity._id,
|
|
|
|
{
|
|
|
|
personProperties,
|
|
|
|
}
|
|
|
|
)
|
2024-08-12 16:34:23 +02:00
|
|
|
|
2024-08-09 12:27:43 +02:00
|
|
|
for (const [name, value] of Object.entries(posthogFlags.featureFlags)) {
|
2024-08-12 10:58:44 +02:00
|
|
|
if (!this.isFlagName(name)) {
|
2024-08-09 12:27:43 +02:00
|
|
|
// We don't want an unexpected PostHog flag to break the app, so we
|
|
|
|
// just log it and continue.
|
|
|
|
console.warn(`Unexpected posthog flag "${name}": ${value}`)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-08-12 10:58:44 +02:00
|
|
|
if (flagValues[name] === true || specificallySetFalse.has(name)) {
|
2024-08-09 12:27:43 +02:00
|
|
|
// If the flag is already set to through environment variables, we
|
|
|
|
// don't want to override it back to false here.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const payload = posthogFlags.featureFlagPayloads?.[name]
|
2024-08-12 10:58:44 +02:00
|
|
|
const flag = this.flagSchema[name]
|
2024-08-09 12:27:43 +02:00
|
|
|
try {
|
2024-08-12 10:58:44 +02:00
|
|
|
// @ts-expect-error - TS does not like you writing into a generic
|
|
|
|
// type, but we know that it's okay in this case because it's just
|
|
|
|
// an object.
|
|
|
|
flagValues[name] = flag.parse(payload || value)
|
2024-08-09 12:27:43 +02:00
|
|
|
tags[`flags.${name}.source`] = "posthog"
|
|
|
|
} catch (err) {
|
|
|
|
// We don't want an invalid PostHog flag to break the app, so we just
|
|
|
|
// log it and continue.
|
|
|
|
console.warn(`Error parsing posthog flag "${name}": ${value}`, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-07 18:50:39 +02:00
|
|
|
|
2024-08-14 16:56:12 +02:00
|
|
|
context.setFeatureFlags(this.setId, flagValues)
|
2024-08-12 10:58:44 +02:00
|
|
|
for (const [key, value] of Object.entries(flagValues)) {
|
2024-08-09 12:27:43 +02:00
|
|
|
tags[`flags.${key}.value`] = value
|
|
|
|
}
|
|
|
|
span?.addTags(tags)
|
2022-03-22 01:23:22 +01:00
|
|
|
|
2024-08-12 10:58:44 +02:00
|
|
|
return flagValues
|
2024-08-09 12:27:43 +02:00
|
|
|
})
|
|
|
|
}
|
2024-07-22 18:43:53 +02:00
|
|
|
}
|
|
|
|
|
2024-08-09 12:27:43 +02:00
|
|
|
// This is the primary source of truth for feature flags. If you want to add a
|
|
|
|
// new flag, add it here and use the `fetch` and `get` functions to access it.
|
|
|
|
// All of the machinery in this file is to make sure that flags have their
|
|
|
|
// default values set correctly and their types flow through the system.
|
|
|
|
export const flags = new FlagSet({
|
2024-08-14 16:41:26 +02:00
|
|
|
DEFAULT_VALUES: Flag.boolean(false),
|
2024-08-09 12:27:43 +02:00
|
|
|
})
|