Class: Contrast::Agent::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/contrast/agent/scope/scope.rb,
ext/cs__scope/cs__scope.c

Overview

Scope lets us disable Contrast for certain code calls. We need to do this so that we don’t propagate through our own code.

Think logging: If you have something like “The source was ‘” + source + “’”, and source is tracked, you’ll trigger propagation with the + method. This in turn would cause propagation if you log there “The target ”” + target + “‘ was propagated’” Which would then cause another propagation with the ‘+’ method, forever.

Instead, we should say “If I’m already doing Contrast things, don’t track this”

Scope Exits… by design, can go below zero. every exit/enter pair (regardless of series) should cancel each other out.

so we prefer this sequence:

scope =  0
exit  = -1
enter =  0
enter =  1
exit  =  0
scope =  0

over this sequence:

scope =  0
exit  =  0
enter =  1
enter =  2
exit  =  1
scope =  1

This class have been moved to C and it’s called from there. Here remains the validation and wrapper methods.

Methods defined in C:

sets scope instance variables. def initialize end;

Check if we are in contrast scope. def in_contrast_scope? end; check if we are in deserialization scope. def in_deserialization_scope? end; check if we are in split scope. def in_split_scope? end; enter contrast scope. def enter_contrast_scope! end; enter deserialization scope. def enter_deserialization_scope! end; enter split scope. def enter_split_scope! end; check split scope depth. def split_scope_depth end; exit contrast scope. def exit_contrast_scope! end; exit deserialization scope. def exit_deserialization_scope! end; exit split scope. def exit_split_scope! end; Static methods to be used, the cases are defined by the usage from the above methods

check if we are in specific scope. def in_scope? name end; enter specific scope. def enter_scope! name end; exit specific scope. def exit_cope! name end;

Returns:

  • (Boolean)

    check if we are in contrast scope if the scope is above 0 return true.

  • (Boolean)

    check if we are in contrast scope if the scope is above 0 return true.

  • (Boolean)

    check if we are in contrast scope if the scope is above 0 return true.

  • (Boolean)

    check if we are in passed scope.

Constant Summary collapse

SCOPE_LIST =
%i[contrast deserialization split].cs__freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.valid_scope?(scope_sym) ⇒ Boolean

Validates scope. To be valid the scope must be one of: :contrast, :split, :deserialization

Parameters:

  • scope_sym (Symbol)

    scope to check.

Returns:

  • (Boolean)

    true | false



152
153
154
# File 'lib/contrast/agent/scope/scope.rb', line 152

def valid_scope? scope_sym
  Contrast::Agent::Scope::SCOPE_LIST.include?(scope_sym)
end

Instance Method Details

#with_app_scopeObject

Enter app scope



139
140
141
142
143
144
# File 'lib/contrast/agent/scope/scope.rb', line 139

def with_app_scope
  exit_contrast_scope!
  yield
ensure
  enter_contrast_scope!
end

#with_contrast_scopeObject

Wraps block to be executed in contrast scope. On completion exits scope.



113
114
115
116
117
118
# File 'lib/contrast/agent/scope/scope.rb', line 113

def with_contrast_scope
  enter_contrast_scope!
  yield
ensure
  exit_contrast_scope!
end

#with_deserialization_scopeObject

Wraps block to be executed in deserialization scope. On completion exits scope.



122
123
124
125
126
127
# File 'lib/contrast/agent/scope/scope.rb', line 122

def with_deserialization_scope
  enter_deserialization_scope!
  yield
ensure
  exit_deserialization_scope!
end

#with_split_scopeObject

Wraps block to be executed in split scope. On completion exits scope.



131
132
133
134
135
136
# File 'lib/contrast/agent/scope/scope.rb', line 131

def with_split_scope
  enter_split_scope!
  yield
ensure
  exit_split_scope!
end