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

do not collect environment tags when CI is not enabled #87

Merged

Conversation

anmarchenko
Copy link
Member

Fixes DataDog/dd-trace-rb#3314

What does this PR do?
Prevents Datadog::CI::Ext::Environment from collecting environment information and validating it if CI mode is not enabled.

Motivation
Regression introduced in 0.5.0 release: ddtrace started to emit error logs in production about CI environment tags not being provided

How to test the change?
Run ruby application with ddtrace in production mode

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (a6e08e2) 99.13% compared to head (0802d17) 99.13%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #87   +/-   ##
=======================================
  Coverage   99.13%   99.13%           
=======================================
  Files         135      135           
  Lines        5220     5226    +6     
  Branches      188      189    +1     
=======================================
+ Hits         5175     5181    +6     
  Misses         45       45           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@anmarchenko anmarchenko added this to the 0.5.1 milestone Dec 11, 2023
Comment on lines 31 to 37
@enabled = enabled
@test_suite_level_visibility_enabled = enabled && test_suite_level_visibility_enabled

@environment_tags = Ext::Environment.tags(ENV).freeze
@environment_tags = @enabled ? Ext::Environment.tags(ENV).freeze : {}
@local_context = Context::Local.new
@global_context = Context::Global.new
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 haven't looked at the CI stuff in a while, so pardon me if I'm asking a stupid question but... I don't understand -- why is a TestVisibility::Recorder being instanced at all for folks which are not running CI?

E.g. have you considered doing something like what we're doing for profiling

https://github.com/DataDog/dd-trace-rb/blob/22c03703a37362cfc930388f659cda4fde8fc60e/lib/datadog/profiling/component.rb#L15

...where we don't instance anything if it's off?

I'm asking because this seems to have a potential to be a sharp edge. E.g. if we're skipping things one-by-one when CI is disabled, it seems easy to "oops" other things that shouldn't be loaded as well, and it seems a bit of a potential whack-a-mole of needing to remember "hey this gets instanced even when CI is not in use, so remember to disable the things that need to be disabled!".

Copy link
Member Author

@anmarchenko anmarchenko Dec 11, 2023

Choose a reason for hiding this comment

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

Yes, I think this was not the right call.
Let us ship this fix now and I made a note for myself to remove Recorder instance completely when CI is not enabled.

My "genious" thinking was that Datadog::CI calls will fail when there is no recorder instance when running tests with CI instrumentation disabled. This is an expected use case: we enable CI instrumentation only in CI environments and we run tests locally without instrumentation. So this means that when users use Datadog::CI entrypoint to manually instrument their tests we need to make sure that these calls do not fail. As I hate nil checks everywhere, I decided to just let Recorder be there, just skip traces creation when CI is not enabled.

Of course, I did not remember the fact that Recorder's constructor has a side effect: it collects environment tags and validates them. It is done in the constructor for performance reasons to make sure that they are being collected in the beginning of test run and cached for the whole run with no risk of this call being made multiple times.

I guess better way to make Datadog::CI calls work when CI instrumentation is disabled would be to introduce ni-checks or introduce a NullRecorder class that would return NullSpan all the time to skip tracing. Anyway, this is a topic for a follow-up PR and I will definitely do it before 0.6.0 version of datadog-ci gem.

Copy link
Member

@ivoanjo ivoanjo left a comment

Choose a reason for hiding this comment

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

Ack, seems reasonable, here sir have my approval 👍

@anmarchenko anmarchenko merged commit d41ab75 into main Dec 11, 2023
12 checks passed
@anmarchenko anmarchenko deleted the anmarchenko/do_not_validate_environment_when_ci_disabled branch December 11, 2023 10:08
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.

Datadog CI is loading in production code and producing warnings
4 participants