Skip to content

Commit ca94619

Browse files
kikorbpetergebala
andcommitted
Simplify instrumentation
Co-Authored-By: Piotr <piotr.gebala@profinda.com>
1 parent f6b6a8d commit ca94619

8 files changed

Lines changed: 49 additions & 57 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
/spec/reports/
88
/tmp/
99

10+
#macos
11+
.DS_Store
12+
1013
# rspec failure tracking
1114
.rspec_status

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Opera Changelog
22

3+
### 0.3.5 - February 24, 2025
4+
5+
- Simplify instrumentation configuration
6+
- Allow instrumentation logic to be defined at the source
7+
38
### 0.3.4 - February 19, 2025
49

510
- Add support for `benchmark` label

lib/opera/operation/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class << self
3333
def call(args = {})
3434
operation = new(params: args.fetch(:params, {}), dependencies: args.fetch(:dependencies, {}))
3535
executor = Executor.new(operation)
36-
Instrumentation.new(config).instrument(name: self.name, level: :operation) do
36+
Instrumentation.new(operation).instrument(name: self.name, level: :operation) do
3737
executor.evaluate_instructions(instructions)
3838
end
3939
executor.result

lib/opera/operation/config.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@ class Config
66
DEVELOPMENT_MODE = :development
77
PRODUCTION_MODE = :production
88

9-
attr_accessor :transaction_class, :transaction_method, :transaction_options,
10-
:instrumentation_class, :instrumentation_method, :instrumentation_options, :mode, :reporter
9+
attr_accessor :transaction_class, :transaction_method, :transaction_options, :instrumentation_class,
10+
:mode, :reporter
1111

1212
def initialize
1313
@transaction_class = self.class.transaction_class
1414
@transaction_method = self.class.transaction_method || :transaction
1515
@transaction_options = self.class.transaction_options
1616

1717
@instrumentation_class = self.class.instrumentation_class
18-
@instrumentation_method = self.class.instrumentation_method || :instrument
19-
@instrumentation_options = self.class.instrumentation_options || {}
2018

2119
@mode = self.class.mode || DEVELOPMENT_MODE
2220
@reporter = custom_reporter || self.class.reporter
@@ -41,8 +39,8 @@ def validate!
4139
end
4240

4341
class << self
44-
attr_accessor :transaction_class, :transaction_method, :transaction_options,
45-
:instrumentation_class, :instrumentation_method, :instrumentation_options, :mode, :reporter
42+
attr_accessor :transaction_class, :transaction_method, :transaction_options, :instrumentation_class,
43+
:mode, :reporter
4644

4745
def configure
4846
yield self

lib/opera/operation/instructions/executors/step.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Step < Executor
88
def call(instruction)
99
method = instruction[:method]
1010

11-
Instrumentation.new(config).instrument(name: "##{method}", level: :step) do
11+
Instrumentation.new(operation).instrument(name: "##{method}", level: :step) do
1212
operation.result.add_execution(method) unless production_mode?
1313
operation.send(method)
1414
end

lib/opera/operation/instrumentation.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@
33
module Opera
44
module Operation
55
class Instrumentation
6-
attr_reader :config
6+
class Base
7+
def self.instrument(operation, name:, level: :operation)
8+
raise NotImplementedError, "#{self.class} must implement #instrument"
9+
end
10+
end
11+
12+
attr_reader :operation
713

8-
def initialize(config)
9-
@config = config
14+
def initialize(operation)
15+
@operation = operation
1016
end
1117

1218
def instrument(name:, level: :operation)
1319
return yield if !instrumentation_enabled?
14-
return yield if level == :step && instrumentation_level != :step
20+
return yield if !instrumentation_compatible?
1521

16-
instrumentation_class.send(instrumentation_method, name, **instrumentation_options.except(:level)) do
22+
instrumentation_class.instrument(operation, name:, level: level) do
1723
yield
1824
end
1925
end
2026

2127
private
2228

23-
def instrumentation_options
24-
config.instrumentation_options
25-
end
26-
27-
def instrumentation_method
28-
config.instrumentation_method
29+
def config
30+
operation.config
2931
end
3032

3133
def instrumentation_class
@@ -36,8 +38,8 @@ def instrumentation_enabled?
3638
!!config.instrumentation_class
3739
end
3840

39-
def instrumentation_level
40-
instrumentation_options[:level] || :operation
41+
def instrumentation_compatible?
42+
config.instrumentation_class.ancestors.include?(Opera::Operation::Instrumentation::Base)
4143
end
4244
end
4345
end

lib/opera/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Opera
4-
VERSION = '0.3.4'
4+
VERSION = '0.3.5'
55
end

spec/opera/operation/base_spec.rb

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,10 +1044,11 @@ def step_3
10441044
end
10451045

10461046
context 'for instrumentation' do
1047-
let(:instrumentation_class) do
1048-
Class.new do
1049-
def self.trace(name)
1050-
puts "Trace #{name}"
1047+
let(:instrumentation_wrapper) do
1048+
Class.new(Opera::Operation::Instrumentation::Base) do
1049+
1050+
def self.instrument(operation, name:, level:)
1051+
puts "Trace #{name} with level #{level}"
10511052
yield
10521053
end
10531054
end
@@ -1083,42 +1084,25 @@ def step_5
10831084
end
10841085
end
10851086

1086-
context 'for operation level' do
1087-
before do
1088-
Operation::Config.configure do |config|
1089-
config.instrumentation_class = instrumentation_class
1090-
config.instrumentation_method = :trace
1091-
end
1092-
end
1093-
1094-
it 'evaluates all steps' do
1095-
expect(subject.executions).to match_array(%i[step_1 step_2 step_3 step_4 step_5])
1096-
end
1097-
1098-
it 'calls instrumentation with correct params' do
1099-
expect(instrumentation_class).to receive(:trace).with('Opera::FakeName').and_call_original
1100-
subject
1087+
before do
1088+
Operation::Config.configure do |config|
1089+
config.instrumentation_class = instrumentation_wrapper
11011090
end
11021091
end
11031092

1104-
context 'for step level' do
1105-
before do
1106-
Operation::Config.configure do |config|
1107-
config.instrumentation_class = instrumentation_class
1108-
config.instrumentation_method = :trace
1109-
config.instrumentation_options = { level: :step }
1110-
end
1111-
end
1112-
1113-
it 'evaluates all steps' do
1114-
expect(subject.executions).to match_array(%i[step_1 step_2 step_3 step_4 step_5])
1115-
end
1093+
it 'evaluates all steps' do
1094+
expect(subject.executions).to match_array(%i[step_1 step_2 step_3 step_4 step_5])
1095+
end
11161096

1117-
it 'calls instrumentation with correct params' do
1118-
expect(instrumentation_class).to receive(:trace).exactly(6).times.and_call_original
1097+
it 'calls instrumentation with correct params' do
1098+
expect(instrumentation_wrapper).to receive(:instrument).with(anything, name: 'Opera::FakeName', level: :operation).once.and_call_original
1099+
expect(instrumentation_wrapper).to receive(:instrument).with(anything, name: '#step_1', level: :step).once.and_call_original
1100+
expect(instrumentation_wrapper).to receive(:instrument).with(anything, name: '#step_2', level: :step).once.and_call_original
1101+
expect(instrumentation_wrapper).to receive(:instrument).with(anything, name: '#step_3', level: :step).once.and_call_original
1102+
expect(instrumentation_wrapper).to receive(:instrument).with(anything, name: '#step_4', level: :step).once.and_call_original
1103+
expect(instrumentation_wrapper).to receive(:instrument).with(anything, name: '#step_5', level: :step).once
11191104

1120-
subject
1121-
end
1105+
subject
11221106
end
11231107
end
11241108

0 commit comments

Comments
 (0)