feat(js): Add _BASE attribute key exports for dynamic attribute keys#471
Conversation
Semver Impact of This PR🟡 Minor (new features) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨Attributes
Other
Internal Changes 🔧
🤖 This preview updates automatically when you update the PR. |
Would it be helpful for how we use it in the sentry repo? If so, happy to add that as a follow up, |
Rather than // conventions package
export const dynamicAttributeKey = (baseKey: <union literal of base keys>, dynamicKey: string) => `${baseKey}.${dynamicKey}`
// sdk/ui/etc.
span.setAttribute(dynamicAttributeKey(URL_PATH_PARAMETER_KEY_BASE, actualKey), actualValue)You could also probably do some funky type stuff around the base key to only allow base keys to be entered etc. |
|
ah yeah, brain fart form my end, sorry 🤦 I guess that still involves an extra function call but you're right, it adds some more safety. Either way, we need to expose the |
Description
Currently, the JS conventions package exports attribute key constants with a dynamic suffix like so:
This makes it a bit annoying to use this export in SDKs when setting such attributes because you have to write code like
this PR adds an additional export with a
_BASEsuffix, so that we exportwhich means we can more ergonomocally write
while this doesn't look much shorter, bundlers and minifiers can much more efficiently minify this to something like
plus, we also save a tiny bit of perf overhead by avoiding the replace call.
Alternatives Considered
The package or the respective SDK could expose a
dynamicAttributeKey(attributeKey, suffix): stringhelper which would take care of replacing.This avoids the export but we'd still call
.replaceon every attribute creation site. Bundle-size wise, it would most likelyalso be larger thanbe equal to using the constants and string templates (depending a bit on number of attributes).Both approaches fix one issue that's most important to me: We can ergonomically directly reference the attribute constants in code, and thereby get deprecation warnings when linting.
Happy to make the same modification to python code generation if desired.
PR Checklist
yarn testand verified that the tests pass.yarn generateto generate and format code and docs.If an attribute was added:
nextjs.function_id, notfunction_id)apply_scrubbing(i.e.manualorauto. Useneveronly for values that should never be scrubbed such as IDs)If an attribute was deprecated: