1
0
Fork 0
mirror of synced 2024-09-20 11:27:56 +12:00

Add DataDog tracing to feature flags.

This commit is contained in:
Sam Rose 2024-08-07 17:50:39 +01:00
parent eb6c0d7891
commit 8803109496
No known key found for this signature in database
2 changed files with 105 additions and 49 deletions

View file

@ -3,6 +3,7 @@ import * as context from "../context"
import { cloneDeep } from "lodash"
import { PostHog, PostHogOptions } from "posthog-node"
import { IdentityType } from "@budibase/types"
import tracer from "dd-trace"
let posthog: PostHog | undefined
export function init(opts?: PostHogOptions) {
@ -119,9 +120,13 @@ function isFlagName(name: string): name is keyof Flags {
* they will be accessed through this function as well.
*/
export async function fetch(): Promise<Flags> {
return await tracer.trace("features.fetch", async span => {
const tags: Record<string, any> = {}
const flags = defaultFlags()
const currentTenantId = context.getTenantId()
const specificallySetFalse = new Set<string>()
const split = (env.TENANT_FEATURE_FLAGS || "")
.split(",")
@ -136,6 +141,7 @@ export async function fetch(): Promise<Flags> {
if (feature.startsWith("!")) {
feature = feature.slice(1)
value = false
specificallySetFalse.add(feature)
}
if (!isFlagName(feature)) {
@ -148,6 +154,7 @@ export async function fetch(): Promise<Flags> {
// @ts-ignore
flags[feature] = value
tags[`flags.${feature}.source`] = "environment"
}
}
@ -164,11 +171,18 @@ export async function fetch(): Promise<Flags> {
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.
@ -177,7 +191,13 @@ export async function fetch(): Promise<Flags> {
}
}
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

View file

@ -79,7 +79,7 @@ describe("feature flags", () => {
describe("posthog", () => {
const identity: IdentityContext = {
_id: "us_1234",
tenantId: "budibase",
tenantId: "tenant1",
type: IdentityType.USER,
email: "test@example.com",
firstName: "Test",
@ -171,5 +171,41 @@ describe("feature flags", () => {
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)
})
}
)
})
})
})