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

[CIVIS-2903] Add fiber-local context and public API to get active test #64

Merged
merged 15 commits into from
Nov 20, 2023

Conversation

anmarchenko
Copy link
Member

@anmarchenko anmarchenko commented Nov 3, 2023

What does this PR do?
Adds public Datadog::CI.active_test and Datadog::CI.active_span methods in order to provide an official way to get currently running span or test.

This will allow us to get rid of adhoc saving the current test in our instrumentations, for example like this:

# cucumber
@current_feature_span = Datadog::CI.start_test()
# ...
@current_feature_span.finish

or like this

# minitest
test_span = Datadog::CI.start_test()
Thread.current[:_datadog_test_span] = test_span
# ...
test_span = Thread.current[:_datadog_test_span]
test_span.finish

Now it should be done like this:

Datadog::CI.start_test()
# ...
test_span = Datadog::CI.active_test
test_span.finish

active_test is stored in CI-specific fiber-local context (see Datadog::CI::Context::Local)
active_span is fetched using existing Datadog::Tracing.active_span method

Motivation
Finishing the manual API for the current state of test-level visibility, start to handle CI-specific context in this library

How to test the change?
By running instrumented Ruby project and making sure nothing has changed in submitted tests

@codecov-commenter
Copy link

codecov-commenter commented Nov 3, 2023

Codecov Report

Merging #64 (e1a9bb6) into main (ef6e234) will increase coverage by 0.03%.
The diff coverage is 98.53%.

@@            Coverage Diff             @@
##             main      #64      +/-   ##
==========================================
+ Coverage   99.16%   99.20%   +0.03%     
==========================================
  Files         106      110       +4     
  Lines        3606     3781     +175     
  Branches      131      140       +9     
==========================================
+ Hits         3576     3751     +175     
  Misses         30       30              
Files Coverage Δ
lib/datadog/ci.rb 100.00% <100.00%> (ø)
lib/datadog/ci/context/local.rb 100.00% <100.00%> (ø)
lib/datadog/ci/contrib/minitest/hooks.rb 96.55% <100.00%> (-0.23%) ⬇️
lib/datadog/ci/recorder.rb 100.00% <100.00%> (ø)
lib/datadog/ci/span.rb 100.00% <100.00%> (ø)
lib/datadog/ci/test.rb 100.00% <100.00%> (ø)
spec/datadog/ci/context/local_spec.rb 100.00% <100.00%> (ø)
...atadog/ci/contrib/cucumber/instrumentation_spec.rb 100.00% <100.00%> (ø)
...atadog/ci/contrib/minitest/instrumentation_spec.rb 100.00% <100.00%> (ø)
...c/datadog/ci/contrib/rspec/instrumentation_spec.rb 100.00% <100.00%> (ø)
... and 5 more

📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today!

@anmarchenko anmarchenko force-pushed the anmarchenko/context_provider branch from 3525a0d to 9cc22eb Compare November 6, 2023 08:30
@anmarchenko anmarchenko force-pushed the anmarchenko/context_provider branch from 9cc22eb to a1a2857 Compare November 7, 2023 08:09
@anmarchenko anmarchenko changed the title WIP context Add fiber-local context and public API to get active test Nov 7, 2023
@anmarchenko anmarchenko marked this pull request as ready for review November 8, 2023 09:20
@anmarchenko anmarchenko requested review from a team and juan-fernandez November 8, 2023 09:20
Comment on lines 38 to 55
def active_test
Thread.current[@key]
end

UNIQUE_INSTANCE_MUTEX = Mutex.new
UNIQUE_INSTANCE_GENERATOR = Datadog::Core::Utils::Sequence.new

private_constant :UNIQUE_INSTANCE_MUTEX, :UNIQUE_INSTANCE_GENERATOR

def self.next_instance_id
UNIQUE_INSTANCE_MUTEX.synchronize { UNIQUE_INSTANCE_GENERATOR.next }
end

private

def active_test=(test)
Thread.current[@key] = test
end
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we could get away with not having a unique id per context. Is it really that common that we'll have more than one context alive in an app?

I'm asking because this has the same potential memory leak issues I shared in our private chat; namely, that the unique keys will live forever until the Ruby process dies.

Copy link
Member Author

Choose a reason for hiding this comment

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

it is very convenient for testing though, not sure how often would it be used in real world case

what is important for us is that CI session is short lived so there would be one recorder instance per forked process and there is no real danger in creating really bad memory leak in my opinion

Copy link
Member

Choose a reason for hiding this comment

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

Note that I'm not saying to create a single context forever; what I'm saying is, if the semantics is that in the usual case only one context would be alive, it seems like you could reuse the key?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, this is true, let me try to do that and see how many tests break...

Copy link
Member Author

Choose a reason for hiding this comment

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

@ivoanjo it worked without issues and made the code simpler! thank you for the suggestion

Copy link
Member

Choose a reason for hiding this comment

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

Cool, glad to know! The profiler does something similar (for ~slightly different reasons) and it worked out reasonably as well.

@anmarchenko anmarchenko changed the title Add fiber-local context and public API to get active test [CIVIS-2903] Add fiber-local context and public API to get active test Nov 8, 2023
private

def create_datadog_span(span_name, span_options: {}, tags: {}, &block)
# create_datadog_span(span_name, span_options: span_options, tags: tags, &block)

Choose a reason for hiding this comment

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

Is this meant as documentation or just left behind?

Copy link
Member Author

Choose a reason for hiding this comment

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

thank you! this should not be here

# Enables the RSpec instrumentation
c.ci.instrument :rspec, **options
# Only activates test instrumentation on CI
if ENV["DD_ENV"] == "ci"
Copy link
Member

@marcotc marcotc Nov 10, 2023

Choose a reason for hiding this comment

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

Offtopic: what do you think of making this check ENV["DD_ENV"] == "ci" by default internally, and assume most clients want this? And then provide an option to override it, for the exceptional clients that don't want this.

Choose a reason for hiding this comment

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

I don't think making internal assumptions on when to turn things on or off based on DD_ENV would make sense to me as a user. It's much more likely I'd end up wondering why things aren't working because I missed the part in the docs that says DD_ENV has to be set and equal to ci for things to work.

Choose a reason for hiding this comment

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

We don't make a lot of (if any) assumptions/decisions on the value of DD_ENV in our tracers' CI Visibility integrations.

Copy link
Member Author

Choose a reason for hiding this comment

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

agree with @romainkomorndatadog, there is a very fine line between doing "magic" and surprising customers with unexpected default behaviour. Many clients use "test" as a DD_ENV value for CI for example. Here in the docs we just want to point out that they should enable CI visibility mode only when running on CI to avoid local test runs to be reported.

@anmarchenko anmarchenko merged commit 7bcfb29 into main Nov 20, 2023
11 checks passed
@anmarchenko anmarchenko deleted the anmarchenko/context_provider branch November 20, 2023 13:09
@github-actions github-actions bot added this to the 0.4.0 milestone Nov 20, 2023
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.

5 participants