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: [plugin/prometheus] allow to skip observation based on context #2317

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

EmrysMyrddin
Copy link
Collaborator

@EmrysMyrddin EmrysMyrddin commented Oct 11, 2024

Description

This PR aims to give more control over exposed metrics.

Today, the configuration is very confusing about what metrics are enabled or not, which can lead to silently broken configuration.

I propose a new way to configure which metrics should be enabled, for which execution phases, with which labels. The goal is also to offer a more dynamic approach by giving a way to skip metric observation based on execution context.

Fixes #2312

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

Copy link

changeset-bot bot commented Oct 11, 2024

🦋 Changeset detected

Latest commit: 27b09e2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@envelop/prometheus Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Oct 11, 2024

💻 Website Preview

The latest changes are available as preview in: https://75427464.envelop.pages.dev

@theguild-bot
Copy link
Collaborator

theguild-bot commented Oct 14, 2024

✅ Benchmark Results

     ✓ no_errors
     ✓ expected_result

     checks.............................................: 100.00% ✓ 782910      ✗ 0     
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: 100.00% ✓ 167322      ✗ 0     
     ✓ { mode:envelop-cache-jit }.......................: 100.00% ✓ 344284      ✗ 0     
     ✓ { mode:envelop-just-cache }......................: 100.00% ✓ 167390      ✗ 0     
     ✓ { mode:graphql-js }..............................: 100.00% ✓ 103914      ✗ 0     
     data_received......................................: 3.0 GB  25 MB/s
     data_sent..........................................: 170 MB  1.4 MB/s
     envelop_init.......................................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     envelop_total......................................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     event_loop_lag.....................................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     graphql_context....................................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     graphql_execute....................................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     graphql_parse......................................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     graphql_validate...................................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-cache-jit }.......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:envelop-just-cache }......................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     ✓ { mode:graphql-js }..............................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     http_req_blocked...................................: avg=2.73µs  min=1.09µs   med=2.53µs  max=7.3ms    p(90)=3.25µs  p(95)=3.59µs 
     http_req_connecting................................: avg=12ns    min=0s       med=0s      max=642.66µs p(90)=0s      p(95)=0s     
     http_req_duration..................................: avg=2.79ms  min=151.26µs med=2.76ms  max=43.65ms  p(90)=5.03ms  p(95)=6.01ms 
       { expected_response:true }.......................: avg=2.79ms  min=151.26µs med=2.76ms  max=43.65ms  p(90)=5.03ms  p(95)=6.01ms 
     ✓ { mode:envelop-cache-and-no-internal-tracing }...: avg=3.33ms  min=451.65µs med=2.98ms  max=16.72ms  p(90)=5.68ms  p(95)=6.13ms 
     ✓ { mode:envelop-cache-jit }.......................: avg=1.45ms  min=151.26µs med=1.21ms  max=18.4ms   p(90)=2.44ms  p(95)=2.58ms 
     ✓ { mode:envelop-just-cache }......................: avg=3.32ms  min=416.07µs med=2.97ms  max=22.37ms  p(90)=5.64ms  p(95)=6.11ms 
     ✓ { mode:graphql-js }..............................: avg=5.51ms  min=822.52µs med=4.78ms  max=43.65ms  p(90)=9.22ms  p(95)=9.97ms 
     http_req_failed....................................: 0.00%   ✓ 0           ✗ 391455
     http_req_receiving.................................: avg=34.27µs min=11.83µs  med=29.88µs max=12.37ms  p(90)=45.95µs p(95)=49.27µs
     http_req_sending...................................: avg=12.68µs min=4.14µs   med=10.35µs max=13.13ms  p(90)=19.14µs p(95)=20.58µs
     http_req_tls_handshaking...........................: avg=0s      min=0s       med=0s      max=0s       p(90)=0s      p(95)=0s     
     http_req_waiting...................................: avg=2.74ms  min=122.73µs med=2.71ms  max=43.2ms   p(90)=4.98ms  p(95)=5.95ms 
     http_reqs..........................................: 391455  3262.088293/s
     iteration_duration.................................: avg=3.05ms  min=352.17µs med=3ms     max=44.3ms   p(90)=5.31ms  p(95)=6.28ms 
     iterations.........................................: 391455  3262.088293/s
     vus................................................: 10      min=10        max=10  
     vus_max............................................: 20      min=20        max=20  

@EmrysMyrddin
Copy link
Collaborator Author

@klippx Hey! What do you think of this new configuration API ? Would it cover you needs ?
You can see the README changes to see how to use it :-)

fillLabelsFn: ({ operationName }, _rawContext) => ({
operation_name: operationName
}),
shouldObserve: context => TRACKED_OPERATIONS.includes(context?.params?.operationName)
Copy link
Contributor

Choose a reason for hiding this comment

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

I like it!

But I think the example should be updated:

Suggested change
shouldObserve: context => TRACKED_OPERATIONS.includes(context?.params?.operationName)
shouldObserve: (_, context) => TRACKED_OPERATIONS.includes(context?.params?.operationName)

because I think the signature is the same as fillLabelsFn.

LabelNames extends string,
Params extends Record<string, any> = FillLabelsFnParams,
>(options: {
registry: Registry;
histogram: Omit<HistogramConfiguration<LabelNames>, 'registers'>;
fillLabelsFn: FillLabelsFn<LabelNames, Params>;
}): HistogramAndLabels<LabelNames, Params> {
phases: Phases;
shouldObserve?: ShouldObservePredicate<Params>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you ensure that if I hover this a helpful jsdoc will appear to explains what this option is doing, and what the default is?

Such as

  /**
   * A function that determines whether the metric should be recorded or not.
   * 
   * Defaults to `() => true`.
   */

Might need to be repeated 2 other places below.

@klippx
Copy link
Contributor

klippx commented Nov 5, 2024

I think this would cover my needs for sure, it looks great!

Copy link
Contributor

github-actions bot commented Nov 8, 2024

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@envelop/prometheus 12.0.0-alpha-20241108094650-27b09e2884dc86dc36c03c988f3291db439e49ca npm ↗︎ unpkg ↗︎

@EmrysMyrddin EmrysMyrddin marked this pull request as ready for review November 8, 2024 13:26
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.

[@envelop/plugin-prometheus] Need a way to filter which metrics are recorded
4 participants