Skip to content

Commit

Permalink
Merge pull request open-telemetry#5 from khushijain21/rubybridge2
Browse files Browse the repository at this point in the history
feat: methods to set custom logger name and version
  • Loading branch information
kaylareopelle authored May 15, 2024
2 parents 4c6d6f1 + 2c9a3e3 commit fb0b5c8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class Instrumentation < OpenTelemetry::Instrumentation::Base
defined?(::Logger)
end

option :name, default: OpenTelemetry::Instrumentation::Logger::NAME, validate: :string
option :version, default: OpenTelemetry::Instrumentation::Logger::VERSION, validate: :string

private

def patch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ module Logger
module Patches
# Instrumention for methods from Ruby's Logger class
module Logger
attr_writer :skip_instrumenting

# TODO: Make sure OTel logs aren't instrumented
# TODO: How to pass attributes?
def format_message(severity, datetime, progname, msg)
formatted_message = super(severity, datetime, progname, msg)
return formatted_message if skip_instrumenting?

OpenTelemetry.logger_provider.logger(
name: OpenTelemetry::Instrumentation::Logger::NAME,
version: OpenTelemetry::Instrumentation::Logger::VERSION
name: Instrumentation.instance.config[:name],
version: Instrumentation.instance.config[:version]
).on_emit(
severity_text: severity,
severity_number: severity_number(severity),
timestamp: datetime,
body: msg # New Relic uses formatted_message here. This also helps us with not recording progname, because it is included in the formatted message by default. Which seems more appropriate?
body: msg, # New Relic uses formatted_message here. This also helps us with not recording progname, because it is included in the formatted message by default. Which seems more appropriate?
context: OpenTelemetry::Context.current
)
formatted_message
end

private

# Placeholder for now
def instrumentation_config; end

def skip_instrumenting?
instance_variable_get(:@skip_instrumenting)
@skip_instrumenting || false
end

def severity_number(severity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@
assert_match(/DEBUG -- : #{msg}/, log_stream.string)
end

it 'sets the OTel logger instrumentation name and version' do
it 'sets the OTel logger instrumentation name and version (default case)' do
ruby_logger.debug(msg)
assert_equal(OpenTelemetry::Instrumentation::Logger::NAME, log_record.instrumentation_scope.name)
assert_equal(OpenTelemetry::Instrumentation::Logger::VERSION, log_record.instrumentation_scope.version)
end

it 'sets the OTel logger instrumentation name and version (user defined)' do
ruby_logger.debug(msg)
skip 'TODO: write tests for configuration options'
end

it 'sets log record attributes based on the Ruby log' do
timestamp = Time.now
Time.stub(:now, timestamp) do
Expand Down

0 comments on commit fb0b5c8

Please sign in to comment.