Skip to content

Commit

Permalink
Rename use of AppSec::Context scope into context
Browse files Browse the repository at this point in the history
  • Loading branch information
Strech committed Jan 9, 2025
1 parent 23fa3e1 commit 219e419
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions lib/datadog/appsec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def enabled?
Datadog.configuration.appsec.enabled
end

def active_scope
Datadog::AppSec::Context.active_scope
def active_context
Datadog::AppSec::Context.active_context
end

def processor
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/appsec/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def finalize

class << self
def activate_context(trace, service_entry_span, processor)
raise ActiveScopeError, 'another scope is active, nested scopes are not supported' if active_context
raise ActiveScopeError, 'another context is active, nested contexts are not supported' if active_context

context = processor.new_context
self.active_context = new(trace, service_entry_span, context)
Expand Down
6 changes: 3 additions & 3 deletions lib/datadog/appsec/contrib/rack/gateway/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ module Rack
module Gateway
# Gateway Response argument.
class Response < Instrumentation::Gateway::Argument
attr_reader :body, :status, :headers, :scope
attr_reader :body, :status, :headers, :context

def initialize(body, status, headers, scope:)
def initialize(body, status, headers, context:)
super()
@body = body
@status = status
@headers = headers.each_with_object({}) { |(k, v), h| h[k.downcase] = v }
@scope = scope
@context = context
end

def response
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/appsec/contrib/rack/gateway/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def watch_request(gateway = Instrumentation.gateway)
def watch_response(gateway = Instrumentation.gateway)
gateway.watch('rack.response', :appsec) do |stack, gateway_response|
event = nil
context = gateway_response.scope
context = gateway_response.context
engine = AppSec::Reactive::Engine.new

Rack::Reactive::Response.subscribe(engine, context.processor_context) do |result|
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/appsec/contrib/rack/request_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def call(env)
request_return[2],
request_return[0],
request_return[1],
scope: ctx,
context: ctx,
)

_response_return, response_response = Instrumentation.gateway.push('rack.response', gateway_response)
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/kit/appsec/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def track(event, trace = nil, span = nil, **others)
private

def set_trace_and_span_context(method, trace = nil, span = nil)
if (appsec_context = Datadog::AppSec.active_scope)
if (appsec_context = Datadog::AppSec.active_context)
trace = appsec_context.trace
span = appsec_context.service_entry_span
end
Expand Down
4 changes: 2 additions & 2 deletions lib/datadog/kit/identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def set_user(
active_span.set_tag("usr.#{k}", v) unless v.nil?
end

if Datadog::AppSec.active_scope
if Datadog::AppSec.active_context
user = ::Datadog::AppSec::Instrumentation::Gateway::User.new(id)
::Datadog::AppSec::Instrumentation.gateway.push('identity.set_user', user)
end
Expand All @@ -78,7 +78,7 @@ def set_user(
private

def set_trace_and_span_context(method, trace = nil, span = nil)
if (appsec_context = Datadog::AppSec.active_scope)
if (appsec_context = Datadog::AppSec.active_context)
trace = appsec_context.trace
span = appsec_context.service_entry_span
end
Expand Down
18 changes: 9 additions & 9 deletions spec/datadog/appsec/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
end

describe '.activate_context' do
context 'with no active scope' do
context 'with no active context' do
subject(:activate_context) { described_class.activate_context(trace, span, processor) }

it 'returns a new scope' do
it 'returns a new context' do
expect(activate_context).to be_a described_class
end

it 'sets the active scope' do
it 'sets the active context' do
expect { activate_context }.to change { described_class.active_context }.from(nil).to be_a described_class
end
end

context 'with an active scope' do
context 'with an active context' do
before do
described_class.activate_context(trace, span, processor)
end
Expand All @@ -40,26 +40,26 @@
expect { activate_context }.to raise_error Datadog::AppSec::Context::ActiveScopeError
end

it 'does not change the active scope' do
it 'does not change the active context' do
expect { activate_context rescue nil }.to_not(change { described_class.active_context })
end
end
end

describe '.deactivate_context' do
context 'with no active scope' do
context 'with no active context' do
subject(:deactivate_context) { described_class.deactivate_context }

it 'raises ActiveContextError' do
expect { deactivate_context }.to raise_error Datadog::AppSec::Context::InactiveScopeError
end

it 'does not change the active scope' do
it 'does not change the active context' do
expect { deactivate_context rescue nil }.to_not(change { described_class.active_context })
end
end

context 'with an active scope' do
context 'with an active context' do
let(:active_context) { described_class.active_context }
subject(:deactivate_context) { described_class.deactivate_context }

Expand All @@ -71,7 +71,7 @@
expect(active_context).to receive(:finalize).and_call_original
end

it 'unsets the active scope' do
it 'unsets the active context' do
expect { deactivate_context }.to change { described_class.active_context }.from(active_context).to nil
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def initialize(id, email, username)
end
end

context 'AppSec scope is nil' do
context 'AppSec context is nil' do
let(:appsec_enabled) { true }
let(:track_user_events_enabled) { true }
let(:mode) { 'safe' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def try(value)
end
end

context 'AppSec scope is nil ' do
context 'AppSec context is nil ' do
let(:appsec_enabled) { true }
let(:track_user_events_enabled) { true }
let(:mode) { 'safe' }
Expand Down
2 changes: 1 addition & 1 deletion spec/datadog/appsec/contrib/rack/gateway/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
body,
200,
headers,
scope: instance_double(Datadog::AppSec::Context)
context: instance_double(Datadog::AppSec::Context)
)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/datadog/appsec/contrib/rack/reactive/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
body,
200,
headers,
scope: context,
context: context,
)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/datadog/kit/appsec/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
let(:trace_op) { Datadog::Tracing::TraceOperation.new }

shared_context 'uses AppSec context' do
before { allow(Datadog::AppSec).to receive(:active_scope).and_return(appsec_active_context) }
before { allow(Datadog::AppSec).to receive(:active_context).and_return(appsec_active_context) }
let(:appsec_span) { trace_op.build_span('root') }

context 'when is present' do
Expand All @@ -19,7 +19,7 @@
Datadog::AppSec::Context.new(trace_op, appsec_span, processor)
end

it 'sets tags on AppSec scope' do
it 'sets tags on AppSec span' do
event
expect(appsec_span.has_tag?(event_tag)).to eq true
end
Expand Down
2 changes: 1 addition & 1 deletion spec/datadog/kit/identity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@

context 'appsec' do
let(:appsec_active_context) { nil }
before { allow(Datadog::AppSec).to receive(:active_scope).and_return(appsec_active_context) }
before { allow(Datadog::AppSec).to receive(:active_context).and_return(appsec_active_context) }

context 'when is enabled' do
let(:appsec_active_context) do
Expand Down

0 comments on commit 219e419

Please sign in to comment.