Skip to content
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

♻️ Custom sanitizer for Context Manager #3290

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge enforceTypeProperties and checkRequiredProperties
  • Loading branch information
nulrich committed Jan 23, 2025
commit c354a1cef0f722964e5d2beb3d124c741232ae2e
25 changes: 12 additions & 13 deletions packages/core/src/domain/context/contextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@ export type PropertiesConfig = {
}
}

function enforceTypeProperties(context: Context, propertiesConfig: PropertiesConfig) {
function ensureProperties(context: Context, propertiesConfig: PropertiesConfig, name: string) {
const newContext = { ...context }
for (const [key, { type }] of Object.entries(propertiesConfig)) {

for (const [key, { required, type }] of Object.entries(propertiesConfig)) {
/**
* Ensure specified properties are strings as defined here:
* https://docs.datadoghq.com/logs/log_configuration/attributes_naming_convention/#user-related-attributes
*/
if (type === 'string' && key in newContext) {
/* eslint-disable @typescript-eslint/no-base-to-string */
newContext[key] = String(newContext[key])
}
}

return newContext
}

function checkRequiredProperties(context: Context, propertiesConfig: PropertiesConfig, name: string) {
for (const [key, { required }] of Object.entries(propertiesConfig)) {
if (required && !(key in context)) {
display.warn(`The property ${key} of ${name} context is required; context will not be sent to the intake.`)
}
}

return newContext
}

export function createContextManager(
Expand All @@ -53,26 +54,24 @@ export function createContextManager(

setContext: (newContext: Context) => {
if (getType(newContext) === 'object') {
context = sanitize(enforceTypeProperties(newContext, propertiesConfig))
context = sanitize(ensureProperties(newContext, propertiesConfig, name))
customerDataTracker?.updateCustomerData(context)
} else {
contextManager.clearContext()
}
checkRequiredProperties(context, propertiesConfig, name)
changeObservable.notify()
},

setContextProperty: (key: string, property: any) => {
context[key] = sanitize(enforceTypeProperties({ [key]: property }, propertiesConfig)[key])
context[key] = sanitize(ensureProperties({ [key]: property }, propertiesConfig, name)[key])
customerDataTracker?.updateCustomerData(context)
checkRequiredProperties(context, propertiesConfig, name)
changeObservable.notify()
},

removeContextProperty: (key: string) => {
delete context[key]
customerDataTracker?.updateCustomerData(context)
checkRequiredProperties(context, propertiesConfig, name)
ensureProperties(context, propertiesConfig, name)
changeObservable.notify()
},

Expand Down
Loading