-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
Codecov Report
@@ 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
📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today! |
3525a0d
to
9cc22eb
Compare
9cc22eb
to
a1a2857
Compare
… multiple examples
lib/datadog/ci/context/local.rb
Outdated
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
lib/datadog/ci/recorder.rb
Outdated
private | ||
|
||
def create_datadog_span(span_name, span_options: {}, tags: {}, &block) | ||
# create_datadog_span(span_name, span_options: span_options, tags: tags, &block) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
What does this PR do?
Adds public
Datadog::CI.active_test
andDatadog::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:
or like this
Now it should be done like this:
active_test
is stored in CI-specific fiber-local context (seeDatadog::CI::Context::Local
)active_span
is fetched using existingDatadog::Tracing.active_span
methodMotivation
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