Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
zvkemp committed Dec 18, 2024
1 parent 5082b25 commit a745666
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
38 changes: 33 additions & 5 deletions instrumentation/base/lib/opentelemetry/instrumentation/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ def initialize(name, version, install_blk, present_blk,
@installed = false
@options = options
@tracer = OpenTelemetry::Trace::Tracer.new # default no-op tracer
@meter = OpenTelemetry::Metrics::Meter.new # default no-op meter

@meter = OpenTelemetry::Metrics::Meter.new if defined?(OpenTelemetry::Meter) # default no-op meter
end
# rubocop:enable Metrics/ParameterLists

Expand All @@ -221,15 +222,15 @@ def install(config = {})
return true if installed?

@config = config_options(config)

set_metrics_enabled

return false unless installable?(config)

instance_exec(@config, &@install_blk)
@tracer = OpenTelemetry.tracer_provider.tracer(name, version)

@meter = OpenTelemetry.meter_provider.meter(name, version: version) if metrics_enabled?
@installed = true

return unless config[:metrics]
@meter = OpenTelemetry.meter_provider.meter(name, version: version)
end

# Whether or not this instrumentation is installable in the current process. Will
Expand Down Expand Up @@ -269,8 +270,25 @@ def enabled?(config = nil)
true
end

# This is based on a variety of factors, and should be invalidated when @config changes.
# It should be explicitly set via `set_metrics_enabled` for now.
def metrics_enabled?
!!@metrics_enabled
end

# @api private
def with_meter
yield @meter if metrics_enabled?
end

private

def set_metrics_enabled
return unless defined?(OpenTelemetry::Metrics)

@metrics_enabled = !!@config[:metrics] || metrics_enabled_by_env_var?
end

# The config_options method is responsible for validating that the user supplied
# config hash is valid.
# Unknown configuration keys are not included in the final config hash.
Expand Down Expand Up @@ -334,6 +352,16 @@ def enabled_by_env_var?
ENV[var_name] != 'false'
end

def metrics_enabled_by_env_var?
var_name = name.dup
var_name.upcase!
var_name.gsub!('::', '_')
var_name.gsub!('OPENTELEMETRY_', 'OTEL_RUBY_')
var_name << 'METRICS_ENABLED'

ENV[var_name] != 'false'
end

# Checks to see if the user has passed any environment variables that set options
# for instrumentation. By convention, the environment variable will be the name
# of the instrumentation, uppercased, with '::' replaced by underscores,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,21 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
option :peer_service, default: nil, validate: :string
option :metrics, default: false, validate: :boolean

# FIXME: upstream
def get_counter(name, description: nil)
return unless metrics_enabled?

binding.pry
# FIXME: structural keys
# FIXME: mutex counter creation (& reads?)
INSTRUMENTS[[name, description]] ||= meter.create_counter(name, description: description)
end

private

# FIXME: upstream
INSTRUMENTS = {}

def gem_version
Gem::Version.new(::Sidekiq::VERSION)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,50 @@ def call(_worker_class, job, _queue, _redis_pool)
yield
end.tap do
# FIXME: is it possible to detect failures here? Does sidekiq bubble them up the middlewares?
if instrumentation_config[:metrics]
begin
counter_attributes = {
'messaging.operation.name' => 'enqueue', # FIXME: metrics semconv
'messaging.system' => 'sidekiq', # FIXME: metrics semconv
'messaging.destination.name' => job['queue'] # FIXME: metrics semconv
with_meter do |meter|
counter_attributes = metrics_attributes(job).merge(
{
'messaging.operation.name' => 'enqueue' # FIXME: metrics semconv
# server.address => # FIXME: required if available
# messaging.destination.partition.id => FIXME: recommended
# server.port => # FIXME: recommended
}
)

counter = meter.create_counter('messaging.client.sent.messages')
counter.add(1, attributes: counter_attributes)
end
# FIXME: avoid create_counter repetition?
binding.pry
counter = instrumentation.get_counter('messaging.client.sent.messages')
counter.add(1, attributes: counter_attributes)
end
end
end

private

def instrumentation
Sidekiq::Instrumentation.instance
end

def instrumentation_config
Sidekiq::Instrumentation.instance.config
instrumentation.config
end

def tracer
Sidekiq::Instrumentation.instance.tracer
instrumentation.tracer
end

def meter
Sidekiq::Instrumentation.instance.meter
def with_meter(&block)
instrumentation.with_meter(&block)
end

def metrics_attributes(job)
{
'messaging.system' => 'sidekiq', # FIXME: metrics semconv
'messaging.destination.name' => job['queue'] # FIXME: metrics semconv
# server.address => # FIXME: required if available
# messaging.destination.partition.id => FIXME: recommended
# server.port => # FIXME: recommended
}
end
end
end
Expand Down

0 comments on commit a745666

Please sign in to comment.