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

fix: use custom headers for metrics requests too #148

Merged
merged 6 commits into from
Mar 16, 2023

Conversation

thomasheartman
Copy link
Contributor

@thomasheartman thomasheartman commented Mar 16, 2023

This PR updates how custom headers work. Previously, any custom headers you added would only get added to feature get/post requests. Now we also add them to metrics requests. This is in line with what the documentation implies and what we think it's reasonable to expect. As such, I consider this a bug fix.

About the changes

The most relevant changes are in the src/metrics.ts. In short, we perform the same kind of method here as in src/index.ts to enrich the standard headers with any custom headers that are provided.

We also take care to pass the custom headers from the client constructor to the metrics constructor.

Tests

The change comes with the following tests:

  1. Verify that the custom headers are passed on from the client constructor to the metrics constructor
  2. Verify that custom headers ...
    • are sent
    • override preset headers, but only if they have a valid value
    • that are set to null or undefined don't get sent.

Discussion points

At the time of writing, the current set of changes are essentially just a copy-paste of what we were doing in the index.ts file. While it's tempting to refactor this and extract it, that also seems like a bit of overkill to me. As far as I'm aware, there's only two copies, and they behave slightly differently (different headers), so I think the duplication is accidental more than intentional. We could parameterize it into a shared function (and I'm happy to do that), but I'm not sure it's worth it.

Second, we currently allow setting headers that are empty or all-whitespace strings. This is consistent with how the index.ts file does it. We can't change the index file, but we don't have to copy this behavior. Do we want to allow empty strings? I'm leaning towards yes because it's less surprising, but I'm open to hearing arguments.

Finally, I also considered applying the custom headers to the fetch parameter that we send in (by wrapping it in another function). This would allow us to not change anything inside the metrics.ts file at all, but I decided against it because I thought it might be harder to reason about and to test. Again, though, happy to do it if we think it's better.


Closes #142

@thomasheartman thomasheartman requested a review from Tymek March 16, 2023 09:21
Copy link
Member

@Tymek Tymek left a comment

Choose a reason for hiding this comment

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

LGTM.

We don't need to improve it over the behavior we have in index.ts. I think not dealing with whitespace etc is OK, because custom headers is an optional, advanced use case.

I'm not sure about how we handle null / undefined headers. When I set
{ Accept: undefined } I would expect this to remove the default Accept: 'application/json' header.

@thomasheartman
Copy link
Contributor Author

I'm not sure about how we handle null / undefined headers. When I set
{ Accept: undefined } I would expect this to remove the default Accept: 'application/json' header.

I think this is a valid point, and it's how I thought I would do it myself. At the same time, that's not how custom headers work when you fetch features. I think having them work differently would be very confusing, so I made sure that if you add a null/undefined header, then it doesn't affect existing headers (there's a test for that).

I'd be happy to change it, but that would probably require a major version bump because we should change the handling for toggle fetching too in that case.

So my suggestion is:

  • Merge this now as it stands
  • Document this behavior (I'll add that before merging)
  • Optionally change how it works in v3

Thoughts?

@thomasheartman thomasheartman force-pushed the fix/custom-headers-to-metrics branch from 77956a5 to 13bd2fe Compare March 16, 2023 12:34
@Tymek
Copy link
Member

Tymek commented Mar 16, 2023

Yes! Let's do that ✅

@thomasheartman thomasheartman merged commit d697ae7 into main Mar 16, 2023
@thomasheartman thomasheartman deleted the fix/custom-headers-to-metrics branch March 16, 2023 14:42
thomasheartman added a commit that referenced this pull request Mar 20, 2023
This commit prepares to release v2.4.4-beta.0 to release the custom header update in #148 .
Tymek added a commit that referenced this pull request Jun 14, 2023
fix: Update tests (#147)

This PR does three things:

- Adds tests for undefined/null properties
- Updates test titles
- Formats the test suites

The new tests were brought on by #146

I've added two (three) new tests.
1. `src/util.test.ts`: Verify that object properties set to `null` or `undefined` are excluded from query params
2. `src/index.test.ts`: Verify that setting a context field to `null` or `undefined` removes it from the context

Two of the tests in `src/util.test.ts` seems to have had copy-paste mistakes in their titles. Looking at them, I believe the "not" in their descriptions should be removed.

This repo has prettier listed in its deps, but the test files were pretty unformatted. My editor auto-formats on save, so this led to a lot of changes. However, I've isolated the formatting changes into separate commits, so we can easily cut those out if we don't want them.

Diffing with **hidden whitespace** is encouraged.

* fix: reword test names + change ts-ignore to ts-expect-error

Looking at the tests, I can only assume that the "not" in their names
was a copy-paste error.

* format: format test suite

* fix: add new test that checks null and undefined properties

* fix: add test that clearing context works as expected

* format: run prettier on tests

* Fix: update ts-expect-error comment

* add check for wrapped strings of null or undefined when filtering params

* revert

---------

Co-authored-by: andreas-unleash <andreas@getunleash.ai>

fix: use custom headers for metrics requests too (#148)

This PR updates how custom headers work. Previously, any custom headers you added would only get added to feature get/post requests. Now we also add them to metrics requests. This is in line with what the documentation implies and what we think it's reasonable to expect. As such, I consider this a bug fix.

The most relevant changes are in the `src/metrics.ts`. In short, we perform the same kind of method here as in `src/index.ts` to enrich the standard headers with any custom headers that are provided.

We also take care to pass the custom headers from the client constructor to the metrics constructor.

The change comes with the following tests:

1. Verify that the custom headers are passed on from the client constructor to the metrics constructor
2. Verify that custom headers ...
    - are sent
    - override preset headers, but only if they have a valid value
    - that are set to `null` or `undefined` don't get sent.

At the time of writing, the current set of changes are essentially just a copy-paste of what we were doing in the `index.ts` file. While it's tempting to refactor this and extract it, that also seems like a bit of overkill to me. As far as I'm aware, there's only two copies, and they behave slightly differently (different headers), so I think the duplication is accidental more than intentional. We could parameterize it into a shared function (and I'm happy to do that), but I'm not sure it's worth it.

Second, we currently allow setting headers that are empty or all-whitespace strings. This is consistent with how the `index.ts` file does it. We can't change the index file, but we don't have to copy this behavior. Do we want to allow empty strings? I'm leaning towards yes because it's less surprising, but I'm open to hearing arguments.

Finally, I also considered applying the custom headers to the `fetch` parameter that we send in (by wrapping it in another function). This would allow us to not change anything inside the `metrics.ts` file at all, but I decided against it because I thought it might be harder to reason about and to test. Again, though, happy to do it if we think it's better.

---

Closes #142

* fix: add initial tests for custom headers

* fix: add custom headers to metrics call

* fix: test that custom headers are passed along

* fix: pass custom headers to metrics

* fix: elaborate on ts-expect-error message

* docs: document how the sdk handles `null`/`undefined`

Bump version to 2.4.4-beta.0 (#149)

This commit prepares to release v2.4.4-beta.0 to release the custom header update in #148 .

feat: report metrics for variants (#150)

2.4.4-beta.1 (#151)

2.5.0 (#154)

build cjs

prepack

remove unused var

add browser entrypoint

chore: update ci (#156)
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.

customHeaders aren't included in metrics requests
2 participants