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

Add cache_key_enabled option for ActiveSupport #4022

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

bouwkast
Copy link

@bouwkast bouwkast commented Oct 23, 2024

Change log entry

Adds a new option for ActiveSupport to disable adding the cache_key as a Span Tag with the cache_key_enabled option.

What does this PR do?

  • Adds cache_key_enabled to ActiveSupport (defaults to true)
  • When cache_key_enabled is false it will no longer set the cache_key as a Span Tag

Motivation:

Customer feature request #4017

Additional Notes:

I have basic grasp on Ruby.
An alternative here is to obfuscate/quantize cache_key values when we add them, the issue here is I'm not entirely sure what these values would look like and if they'd be valuable if they were obfuscated/quantized.

How to test the change?

On Windows can't install Ruby :(
Can't get WSL to work after Windows Update
Can't find tests for ActiveSupport that assert on tags
Help

Unsure? Have a question? Request a review!

@pr-commenter
Copy link

pr-commenter bot commented Oct 23, 2024

Benchmarks

Benchmark execution time: 2024-10-23 21:38:15

Comparing candidate commit 45ef5e6 in PR branch steven/activesupport-cachekey-config with baseline commit 38f6cc1 in branch master.

Found 0 performance improvements and 1 performance regressions! Performance is the same for 23 metrics, 2 unstable metrics.

scenario:profiler - sample timeline=false

  • 🟥 throughput [-0.675op/s; -0.658op/s] or [-9.803%; -9.558%]

@bouwkast bouwkast force-pushed the steven/activesupport-cachekey-config branch from 8049a04 to e8188fc Compare October 23, 2024 15:36
@@ -58,7 +58,8 @@ def trace(action, store, key: nil, multi_key: nil)
end

span.set_tag(Ext::TAG_CACHE_BACKEND, store) if store
set_cache_key(span, key, multi_key)

set_cache_key(span, key, multi_key) if Datadog.configuration.tracing[:active_support][:cache_key_enabled]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
set_cache_key(span, key, multi_key) if Datadog.configuration.tracing[:active_support][:cache_key_enabled]
set_cache_key(span, key, multi_key) if configuration[:cache_key_enabled]

Copy link
Author

Choose a reason for hiding this comment

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

@marcotc this seems to be not work (I'm likely doing something wrong)

      Failure/Error: set_cache_key(span, key, multi_key) if configuration[:cache_key_enabled]
      
      NameError:
        undefined local variable or method `configuration' for Datadog::Tracing::Contrib::ActiveSupport::Cache::Instrumentation:Module

Copy link
Member

@marcotc marcotc left a comment

Choose a reason for hiding this comment

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

Great work! Clean and well tested 👍

I thing that is missing is the public facing documentation, at https://github.com/DataDog/dd-trace-rb/blob/master/docs/GettingStarted.md#active-support.


expect(read_multi).to eq(Hash[multi_keys.zip([51, 52, 53])])
expect(spans).to have(1 + multi_keys.size).items
get = spans[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

Code Quality Violation

Suggested change
get = spans[0]
get = spans.first
Improve readability with first (...read more)

This rule encourages the use of first and last methods over array indexing to access the first and last elements of an array, respectively. The primary reason behind this rule is to improve code readability. Using first and last makes it immediately clear that you are accessing the first or last element of the array, which might not be immediately obvious with array indexing, especially for developers who are new to Ruby.

The use of these methods also helps to make your code more idiomatic, which is a crucial aspect of writing effective Ruby code. Idiomatic code is easier to read, understand, and maintain. It also tends to be more efficient, as idioms often reflect patterns that are optimized for the language.

To adhere to this rule, replace the use of array indexing with first or last methods when you want to access the first and last elements of an array. For instance, instead of arr[0] use arr.first and instead of arr[-1] use arr.last. However, note that this rule should be applied only when reading values. When modifying the first or last elements, array indexing should still be used. For example, arr[0] = 'new_value' and arr[-1] = 'new_value'.

View in Datadog  Leave us feedback  Documentation

it do
expect(read_multi).to eq(Hash[multi_keys.zip([51, 52, 53])])
expect(spans).to have(1 + multi_keys.size).items
get = spans[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

Code Quality Violation

Suggested change
get = spans[0]
get = spans.first
Improve readability with first (...read more)

This rule encourages the use of first and last methods over array indexing to access the first and last elements of an array, respectively. The primary reason behind this rule is to improve code readability. Using first and last makes it immediately clear that you are accessing the first or last element of the array, which might not be immediately obvious with array indexing, especially for developers who are new to Ruby.

The use of these methods also helps to make your code more idiomatic, which is a crucial aspect of writing effective Ruby code. Idiomatic code is easier to read, understand, and maintain. It also tends to be more efficient, as idioms often reflect patterns that are optimized for the language.

To adhere to this rule, replace the use of array indexing with first or last methods when you want to access the first and last elements of an array. For instance, instead of arr[0] use arr.first and instead of arr[-1] use arr.last. However, note that this rule should be applied only when reading values. When modifying the first or last elements, array indexing should still be used. For example, arr[0] = 'new_value' and arr[-1] = 'new_value'.

View in Datadog  Leave us feedback  Documentation

This reverts commit 1f31567.

This didn't work.
This reverts commit 866c888.

It's missing a require_relative
@ivoanjo
Copy link
Member

ivoanjo commented Oct 24, 2024

(I guess this is related to #4017 ?)

@bouwkast
Copy link
Author

(I guess this is related to #4017 ?)

Yes it is @ivoanjo! I posted in the slack channel some context surrounding this

I didn't link to it yet as this was just a draft and was waiting to determine whether or not this is something acceptable to do within the Ruby Datadog SDK or if the expected solution would be to add obfuscation rules to this.

Copy link
Contributor

@delner delner left a comment

Choose a reason for hiding this comment

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

Overall the implementation is clean! I do have one suggestion for the public API, I think should be weighed before we merge.

@@ -39,6 +39,11 @@ class Settings < Contrib::Configuration::Settings
)
end
end

option :cache_key_enabled do |o|
Copy link
Contributor

@delner delner Oct 24, 2024

Choose a reason for hiding this comment

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

I want to caution that any addition of configuration like this is an expansion of the public API, so in that sense, we're bound to this method, and other measures (in other integrations) should be consistent with this style.

Allowing this to be disabled is value-add in capability. But users may find it insufficient, and may also need a more sophisticated quantization feature that itself may require more configuration options.

If we stick with cache_key_enabled, and we wanted to add regex capability (for example), then we would likely have to do cache_key_exclude_pattern, cache_key_include_pattern, etc... this is OK, but idiomatically, it may be better to have a subgroup instead (e.g. cache_key.enabled, cache_key.include_pattern, cache_key.exclude_pattern.)

So the time to decide on this behavior is now. I would recommend nesting this setting in its own subgroup to produce the cache_key.enabled behavior, such that we have the option of idiomatically expanding the API later without introducing a breaking change down the line.

Copy link
Author

Choose a reason for hiding this comment

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

If we stick with cache_key_enabled, and we wanted to add regex capability (for example), then we would likely have to do cache_key_exclude_pattern, cache_key_include_pattern, etc... this is OK, but idiomatically, it may be better to have a subgroup instead (e.g. cache_key.enabled, cache_key.include_pattern, cache_key.exclude_pattern.)

I like this idea, it seems much more effective and better long-term so I'll refactor to that.

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.

4 participants