Skip to content

Commit

Permalink
Merge branch 'feat/DEX-2616/composition-metrics' into 'master'
Browse files Browse the repository at this point in the history
[DEX-2616] Add composition metrics

Closes DEX-2616

See merge request nstmrt/rubygems/sbmt-strangler!25
  • Loading branch information
Черненков Алексей Юрьевич committed Oct 24, 2024
2 parents 728ae20 + 4bbec2d commit 5ebfc3c
Show file tree
Hide file tree
Showing 15 changed files with 388 additions and 214 deletions.
2 changes: 2 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ tests:
matrix:
- RUBY_VERSION: ['3.1', '3.2', '3.3']
before_script:
- gem sources --remove https://rubygems.org/
- gem sources --add https://nexus.sbmt.io/repository/rubygems/
- gem install bundler -v 2.5.7
- bin/setup
script:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

source "https://rubygems.org"
source "https://nexus.sbmt.io/repository/rubygems/"

gemspec
28 changes: 27 additions & 1 deletion config/initializers/yabeda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module Sbmt
module Strangler
module Metrics
module Yabeda
HTTP_BUCKETS = [0.01, 0.02, 0.04, 0.1, 0.2, 0.5, 0.8, 1, 1.5, 2, 5, 15, 30, 60].freeze
DEFAULT_BUCKETS = [0.01, 0.02, 0.04, 0.1, 0.2, 0.5, 0.8, 1, 1.5, 2, 5, 15, 30, 60].freeze
HTTP_BUCKETS = DEFAULT_BUCKETS
COMPOSITION_BUCKETS = DEFAULT_BUCKETS

::Yabeda.configure do
group :sbmt_strangler do
Expand Down Expand Up @@ -38,3 +40,27 @@ module Yabeda
end
end
end

# Declaring composition step duration metric in an `after_initialize` block
# allows user to customize buckets in his app-level configuration file:
#
# # config/initializers/strangler.rb
# Sbmt::Strangler.configure do |strangler|
# strangler.composition_step_duration_metric_buckets = [0.1, 0.2, 0.3]
# end
#
Rails.application.config.after_initialize do
::Yabeda.configure do
group :sbmt_strangler do
composition_buckets =
::Sbmt::Strangler.configuration.composition_step_duration_metric_buckets ||
::Sbmt::Strangler::Metrics::Yabeda::COMPOSITION_BUCKETS

histogram :composition_step_duration,
tags: %i[step part type level parent controller action],
unit: :seconds,
buckets: composition_buckets,
comment: "Composition step duration"
end
end
end
10 changes: 6 additions & 4 deletions lib/sbmt/strangler/action.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "action/composition"
require_relative "action/composition/step"

module Sbmt
module Strangler
Expand Down Expand Up @@ -36,9 +36,11 @@ def http_client
end

def composition(&)
return @composition unless block_given?

@composition = Sbmt::Strangler::Action::Composition.new(&)
if block_given?
@composition ||= Composition::Step.new(name: :root)
yield(@composition)
end
@composition
end

def composition?
Expand Down
15 changes: 0 additions & 15 deletions lib/sbmt/strangler/action/composition.rb

This file was deleted.

77 changes: 0 additions & 77 deletions lib/sbmt/strangler/action/composition/composable.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Sbmt
module Strangler
class Action
class Composition
module Composition
module Errors
class ConfigurationError < StandardError; end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
module Sbmt
module Strangler
class Action
class Composition
module Composition
module Errors
class MaxCompositionLevelError < StandardError; end
class MaxLevelError < StandardError; end
end
end
end
Expand Down
57 changes: 57 additions & 0 deletions lib/sbmt/strangler/action/composition/metrics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

module Sbmt
module Strangler
class Action
module Composition
module Metrics
private

def with_metrics(rails_controller:, part: nil)
result = nil
with_yabeda_duration_measurement(rails_controller: rails_controller, part: part) do
with_open_telemetry_tracing(part: part) do
result = yield
end
end
result
end

def with_yabeda_duration_measurement(rails_controller:, part: nil)
result = nil
yabeda_tags = {
step: name.to_s,
part: part&.to_s,
type: type.to_s,
level: level.to_s,
parent: parent&.name&.to_s,
controller: rails_controller.controller_path,
action: rails_controller.action_name
}
Yabeda.sbmt_strangler.composition_step_duration.measure(yabeda_tags) do
result = yield
end
result
end

def with_open_telemetry_tracing(part: nil)
return yield unless Object.const_defined?(:OpenTelemetry)

span_name = "Composition step: #{name}"
span_name += " (#{part})" unless part.nil?

span_attrs = {type: type.to_s, level: level}
span_attrs[:parent] = parent.name.to_s unless parent.nil?

result = nil
::OpenTelemetry.tracer_provider.tracer("Sbmt::Strangler")
.in_span(span_name, attributes: span_attrs, kind: :internal) do |_span|
result = yield
end
result
end
end
end
end
end
end
Loading

0 comments on commit 5ebfc3c

Please sign in to comment.