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 { 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,9 +120,13 @@ 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> {
return await tracer.trace("features.fetch", async span => {
const tags: Record<string, any> = {}
const flags = defaultFlags() const flags = defaultFlags()
const currentTenantId = context.getTenantId() const currentTenantId = context.getTenantId()
const specificallySetFalse = new Set<string>()
const split = (env.TENANT_FEATURE_FLAGS || "") const split = (env.TENANT_FEATURE_FLAGS || "")
.split(",") .split(",")
@ -136,6 +141,7 @@ export async function fetch(): Promise<Flags> {
if (feature.startsWith("!")) { if (feature.startsWith("!")) {
feature = feature.slice(1) feature = feature.slice(1)
value = false value = false
specificallySetFalse.add(feature)
} }
if (!isFlagName(feature)) { if (!isFlagName(feature)) {
@ -148,6 +154,7 @@ export async function fetch(): Promise<Flags> {
// @ts-ignore // @ts-ignore
flags[feature] = value flags[feature] = value
tags[`flags.${feature}.source`] = "environment"
} }
} }
@ -164,11 +171,18 @@ export async function fetch(): Promise<Flags> {
continue 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] const payload = posthogFlags.featureFlagPayloads?.[name]
try { try {
// @ts-ignore // @ts-ignore
flags[key] = flag.parse(payload || value) flags[key] = flag.parse(payload || value)
tags[`flags.${key}.source`] = "posthog"
} catch (err) { } catch (err) {
// We don't want an invalid PostHog flag to break the app, so we just // We don't want an invalid PostHog flag to break the app, so we just
// log it and continue. // 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 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

View File

@ -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)
})
}
)
})
}) })
}) })