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

feat(honeycomb-opentelemetry-web): Add data attributtes for LCP. #309

Merged
merged 8 commits into from
Oct 9, 2024

Conversation

wolfgangcodes
Copy link
Contributor

@wolfgangcodes wolfgangcodes commented Sep 26, 2024

Which problem is this PR solving?

Adds data- attributes to LCP web vitals and a configuration to specify which data attributes to collect.

Closes #95

Short description of the changes

How to verify that this has the expected result

Add a data- to the LCP element and see it's value as an attribute span under element.data-ATTRIBUTE_NAME

CleanShot 2024-09-26 at 16 38 09@2x

@@ -280,41 +287,37 @@ export class WebVitalsInstrumentation extends InstrumentationAbstract {
private _setupWebVitalsCallbacks() {
if (this.vitalsToTrack.includes('CLS')) {
onCLS((vital) => {
this.onReportCLS(vital, this.clsOpts?.applyCustomAttributes);
this.onReportCLS(vital, this.clsOpts);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update all the this.onREportXYZ to take opts as the second argument, to minimize changes for additional options.

/**
* Will send the values of these data attributes if they appear on an LCP event
*/
dataAttributes?: string[];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern could be useful if we wanted to add in the ability to collect data- from nodes where attributes aren't available and we need to do a dom lookup. Maybe this is a way to let users opt-into something that might impact "performance"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmmm that's in interesting point, I agree that its nice to have this array if you only want to look up a certain subset of data- attributes. I was originally thinking that if any data- attrs are provided that we would just automatically send them all as attributes and then the array can filter them down if provided just to save the user a setup step but if this is more future proof than I'm happy with it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooo. default to sending all and filter down? I like that better for this instance.

@wolfgangcodes wolfgangcodes marked this pull request as ready for review September 27, 2024 21:16
@wolfgangcodes wolfgangcodes requested a review from a team as a code owner September 27, 2024 21:16
@@ -481,6 +482,25 @@ export class WebVitalsInstrumentation extends InstrumentationAbstract {
[`${attrPrefix}.resource_load_time`]: resourceLoadDuration,
});

lcpEntry?.element?.getAttributeNames().forEach((attrName) => {
// Skip non data-* attributes
if (!attrName.startsWith('data-')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be safer / more correct to use the dataset API in JavaScript rather than iterating over all the attributes if it's available!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OH. Heck yeah!

README.md Outdated
@@ -98,6 +98,7 @@ Pass these options to the HoneycombWebSDK:
| enabled | optional | boolean | `true` | Where or not to enable this auto instrumentation. |
| lcp| optional| VitalOpts | `undefined` | Pass-through config options for web-vitals. See [ReportOpts](https://github.com/GoogleChrome/web-vitals?tab=readme-ov-file#reportopts).
| lcp.applyCustomAttributes| optional| function | `undefined` | A function for adding custom attributes to core web vitals spans.
| lcp.dataAttributes| optional| `string[]` | `undefined` | an array of data-* attribute names to filter. By default it will send all `data-*` attribute-value pairs with the key `lcp.element.data-someAttr`, `[]` will send none.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is how the values end up being added as attrs, according to the code each one is an attribute by itself with .data. prefix, can we change that here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 I wanted to make sure we were 👍 with c3f6422 before updating all the docs. 😅

Copy link
Contributor

@pkanal pkanal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing! So exciting to have this in! Left a small comment about the documentation but can ship this after that is addressed!

@wolfgangcodes wolfgangcodes merged commit c77fb9d into main Oct 9, 2024
16 checks passed
@wolfgangcodes wolfgangcodes deleted the wg/lcp-data branch October 9, 2024 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feat] Web Vitals Instrumentation data attribute ID
2 participants