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: report metrics for variants #150

Merged
merged 1 commit into from
May 4, 2023
Merged

Conversation

kwasniew
Copy link
Contributor

@kwasniew kwasniew commented May 4, 2023

About the changes

Main capability:

  • adding reporting of metrics for variants

Changes:

  • introducing SENT event for easier testing and integration with other tools. Same event is present in node.js SDK
  • added metrics unit test for counting variants
  • added component tests for the whole SDK client for metrics reporting
  • formatting code

Next steps:

  • Proxy/Edge should accumulate client SDK metrics

Important files

Discussion points

@@ -142,6 +156,19 @@ export default class Metrics {
return true;
}

public countVariant(name: string, variant: string): boolean {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

similar to Node.js implementation

@@ -217,6 +219,9 @@ export class UnleashClient extends TinyEmitter {
const enabled = toggle?.enabled || false;
const variant = toggle ? toggle.variant : defaultVariant;

if (variant.name) {
this.metrics.countVariant(toggleName, variant.name);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

variant is counted in addition to regular count for enabled/disabled

@kwasniew kwasniew merged commit 1c34d61 into main May 4, 2023
4 checks passed
@kwasniew kwasniew deleted the report-metrics-for-variants branch May 4, 2023 13:54
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
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

2 participants