Add DataDog tracing to feature flags.
This commit is contained in:
parent
eb6c0d7891
commit
8803109496
|
@ -3,6 +3,7 @@ import * as context from "../context"
|
||||||
import { cloneDeep } from "lodash"
|
import { cloneDeep } from "lodash"
|
||||||
import { PostHog, PostHogOptions } from "posthog-node"
|
import { PostHog, PostHogOptions } from "posthog-node"
|
||||||
import { IdentityType } from "@budibase/types"
|
import { IdentityType } from "@budibase/types"
|
||||||
|
import tracer from "dd-trace"
|
||||||
|
|
||||||
let posthog: PostHog | undefined
|
let posthog: PostHog | undefined
|
||||||
export function init(opts?: PostHogOptions) {
|
export function init(opts?: PostHogOptions) {
|
||||||
|
@ -119,65 +120,84 @@ function isFlagName(name: string): name is keyof Flags {
|
||||||
* they will be accessed through this function as well.
|
* they will be accessed through this function as well.
|
||||||
*/
|
*/
|
||||||
export async function fetch(): Promise<Flags> {
|
export async function fetch(): Promise<Flags> {
|
||||||
const flags = defaultFlags()
|
return await tracer.trace("features.fetch", async span => {
|
||||||
|
const tags: Record<string, any> = {}
|
||||||
|
|
||||||
const currentTenantId = context.getTenantId()
|
const flags = defaultFlags()
|
||||||
|
|
||||||
const split = (env.TENANT_FEATURE_FLAGS || "")
|
const currentTenantId = context.getTenantId()
|
||||||
.split(",")
|
const specificallySetFalse = new Set<string>()
|
||||||
.map(x => x.split(":"))
|
|
||||||
for (const [tenantId, ...features] of split) {
|
|
||||||
if (!tenantId || (tenantId !== "*" && tenantId !== currentTenantId)) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let feature of features) {
|
const split = (env.TENANT_FEATURE_FLAGS || "")
|
||||||
let value = true
|
.split(",")
|
||||||
if (feature.startsWith("!")) {
|
.map(x => x.split(":"))
|
||||||
feature = feature.slice(1)
|
for (const [tenantId, ...features] of split) {
|
||||||
value = false
|
if (!tenantId || (tenantId !== "*" && tenantId !== currentTenantId)) {
|
||||||
}
|
|
||||||
|
|
||||||
if (!isFlagName(feature)) {
|
|
||||||
throw new Error(`Feature: ${feature} is not an allowed option`)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof flags[feature] !== "boolean") {
|
|
||||||
throw new Error(`Feature: ${feature} is not a boolean`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
flags[feature] = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const identity = context.getIdentity()
|
|
||||||
if (posthog && identity?.type === IdentityType.USER) {
|
|
||||||
const posthogFlags = await posthog.getAllFlagsAndPayloads(identity._id)
|
|
||||||
for (const [name, value] of Object.entries(posthogFlags.featureFlags)) {
|
|
||||||
const key = name as keyof typeof FLAGS
|
|
||||||
const flag = FLAGS[key]
|
|
||||||
if (!flag) {
|
|
||||||
// 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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
const payload = posthogFlags.featureFlagPayloads?.[name]
|
for (let feature of features) {
|
||||||
|
let value = true
|
||||||
|
if (feature.startsWith("!")) {
|
||||||
|
feature = feature.slice(1)
|
||||||
|
value = false
|
||||||
|
specificallySetFalse.add(feature)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isFlagName(feature)) {
|
||||||
|
throw new Error(`Feature: ${feature} is not an allowed option`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof flags[feature] !== "boolean") {
|
||||||
|
throw new Error(`Feature: ${feature} is not a boolean`)
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
flags[key] = flag.parse(payload || value)
|
flags[feature] = value
|
||||||
} catch (err) {
|
tags[`flags.${feature}.source`] = "environment"
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return flags
|
const identity = context.getIdentity()
|
||||||
|
if (posthog && identity?.type === IdentityType.USER) {
|
||||||
|
const posthogFlags = await posthog.getAllFlagsAndPayloads(identity._id)
|
||||||
|
for (const [name, value] of Object.entries(posthogFlags.featureFlags)) {
|
||||||
|
const key = name as keyof typeof FLAGS
|
||||||
|
const flag = FLAGS[key]
|
||||||
|
if (!flag) {
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flags[key] === true || specificallySetFalse.has(key)) {
|
||||||
|
// 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]
|
||||||
|
|
||||||
|
try {
|
||||||
|
// @ts-ignore
|
||||||
|
flags[key] = flag.parse(payload || value)
|
||||||
|
tags[`flags.${key}.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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [key, value] of Object.entries(flags)) {
|
||||||
|
tags[`flags.${key}.value`] = value
|
||||||
|
}
|
||||||
|
span?.addTags(tags)
|
||||||
|
|
||||||
|
return flags
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets a single feature flag value. This is a convenience function for
|
// Gets a single feature flag value. This is a convenience function for
|
||||||
|
|
|
@ -79,7 +79,7 @@ describe("feature flags", () => {
|
||||||
describe("posthog", () => {
|
describe("posthog", () => {
|
||||||
const identity: IdentityContext = {
|
const identity: IdentityContext = {
|
||||||
_id: "us_1234",
|
_id: "us_1234",
|
||||||
tenantId: "budibase",
|
tenantId: "tenant1",
|
||||||
type: IdentityType.USER,
|
type: IdentityType.USER,
|
||||||
email: "test@example.com",
|
email: "test@example.com",
|
||||||
firstName: "Test",
|
firstName: "Test",
|
||||||
|
@ -171,5 +171,41 @@ describe("feature flags", () => {
|
||||||
await expect(fetch()).resolves.not.toThrow()
|
await expect(fetch()).resolves.not.toThrow()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should not override flags set in the environment", async () => {
|
||||||
|
mockFlags({
|
||||||
|
featureFlags: {
|
||||||
|
_TEST_BOOLEAN: false,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await withEnv(
|
||||||
|
{ TENANT_FEATURE_FLAGS: `${identity.tenantId}:_TEST_BOOLEAN` },
|
||||||
|
async () => {
|
||||||
|
await context.doInIdentityContext(identity, async () => {
|
||||||
|
const flags = await fetch()
|
||||||
|
expect(flags._TEST_BOOLEAN).toBe(true)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should not override flags set in the environment with a ! prefix", async () => {
|
||||||
|
mockFlags({
|
||||||
|
featureFlags: {
|
||||||
|
_TEST_BOOLEAN: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await withEnv(
|
||||||
|
{ TENANT_FEATURE_FLAGS: `${identity.tenantId}:!_TEST_BOOLEAN` },
|
||||||
|
async () => {
|
||||||
|
await context.doInIdentityContext(identity, async () => {
|
||||||
|
const flags = await fetch()
|
||||||
|
expect(flags._TEST_BOOLEAN).toBe(false)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue