Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions instrumentation/base/lib/opentelemetry/instrumentation/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def infer_version
end
end

attr_reader :name, :version, :config, :installed, :tracer
attr_reader :name, :version, :config, :installed, :tracer, :meter

alias installed? installed

Expand All @@ -205,6 +205,7 @@ def initialize(name, version, install_blk, present_blk,
@installed = false
@options = options
@tracer = OpenTelemetry::Trace::Tracer.new
@meter = nil
end
# rubocop:enable Metrics/ParameterLists

Expand All @@ -222,6 +223,18 @@ def install(config = {})
instance_exec(@config, &@install_blk)
@tracer = OpenTelemetry.tracer_provider.tracer(name, version)
@installed = true

if enable_metrics?(config) && defined?(OpenTelemetry.meter_provider) && OpenTelemetry.meter_provider.instance_of?(OpenTelemetry::SDK::Metrics::MeterProvider)
@meter = OpenTelemetry.meter_provider.meter(name, version: version)
end

initialize_metrics
end

# Each instrumentation can implement the initialize_metrics function to
# include the desired metrics instrument
def initialize_metrics
OpenTelemetry.logger.info 'Instrumentation should implement the function'
end

# Whether or not this instrumentation is installable in the current process. Will
Expand Down Expand Up @@ -261,6 +274,19 @@ def enabled?(config = nil)
true
end

# Whether this instrumentation has metrics is enabled. It first checks to see if it's enabled
# by an environment variable and will proceed to check if it's enabled
# by local config, if given. By default, it will be disabled.
#
# @param [optional Hash] config The local config
def enable_metrics?(config = nil)
env_value = metrics_env_var_value
return env_value == 'true' if %w[true false].include?(env_value)
return config[:metrics] if config&.key?(:metrics)

false
end

private

# The config_options method is responsible for validating that the user supplied
Expand Down Expand Up @@ -304,7 +330,7 @@ def config_options(user_config)
h[option_name] = option[:default]
end

dropped_config_keys = user_config.keys - validated_config.keys - [:enabled]
dropped_config_keys = user_config.keys - validated_config.keys - %i[enabled metrics]
OpenTelemetry.logger.warn("Instrumentation #{name} ignored the following unknown configuration options #{dropped_config_keys}") unless dropped_config_keys.empty?

validated_config
Expand All @@ -326,6 +352,21 @@ def enabled_by_env_var?
ENV[var_name] != 'false'
end

# Returns the value of the metrics environment variable. By convention, the
# environment variable will be the instrumentation name upper cased, with '::'
# replaced by underscores, OPENTELEMETRY shortened to OTEL_{LANG} and _METRICS_ENABLED appended.
# For example, the environment variable name for OpenTelemetry::Instrumentation::Sinatra
# will be OTEL_RUBY_INSTRUMENTATION_SINATRA_METRICS_ENABLED.
def metrics_env_var_value
var_name = name.dup.tap do |n|
n.upcase!
n.gsub!('::', '_')
n.gsub!('OPENTELEMETRY_', 'OTEL_RUBY_')
n << '_METRICS_ENABLED'
end
ENV.fetch(var_name, nil)
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
Loading