Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental support for Open telemetry logs #292

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Send keys as strings
and move more attributes to the formatter
reidmorrison committed Jul 30, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 6c912dd7d15fd053eb285c60c73a0d9f5025d5c8
36 changes: 10 additions & 26 deletions lib/semantic_logger/appender/open_telemetry.rb
Original file line number Diff line number Diff line change
@@ -43,15 +43,18 @@ def initialize(name: "SemanticLogger",
def log(log)
# return log_metric(log) if metrics && log.metric_only?

ap formatter.call(log, self)
ap log.payload
body = formatter.call(log, self)
level = body.delete(:level)
level_index = body.delete(:level_index)
time = body.delete(:time)
payload = body.delete(:payload)

@logger.on_emit(
severity_text: log.level.to_s,
severity_number: severity_number(log.level),
timestamp: log.time,
body: formatter.call(log, self),
attributes: log.payload,
severity_text: level,
severity_number: level_index,
timestamp: time,
body: body.transform_keys!(&:to_s),
attributes: payload,
context: log.context[:open_telemetry] || ::OpenTelemetry::Context.current
)
true
@@ -76,25 +79,6 @@ def close
# ap log.payload
# true
# end

def severity_number(severity)
case severity.downcase
when :trace
::OpenTelemetry::Logs::SeverityNumber::SEVERITY_NUMBER_TRACE
when :debug
::OpenTelemetry::Logs::SeverityNumber::SEVERITY_NUMBER_DEBUG
when :info
::OpenTelemetry::Logs::SeverityNumber::SEVERITY_NUMBER_INFO
when :warn
::OpenTelemetry::Logs::SeverityNumber::SEVERITY_NUMBER_WARN
when :error
::OpenTelemetry::Logs::SeverityNumber::SEVERITY_NUMBER_ERROR
when :fatal
::OpenTelemetry::Logs::SeverityNumber::SEVERITY_NUMBER_FATAL
else
::OpenTelemetry::Logs::SeverityNumber::SEVERITY_NUMBER_UNSPECIFIED
end
end
end
end
end
30 changes: 26 additions & 4 deletions lib/semantic_logger/formatters/open_telemetry.rb
Original file line number Diff line number Diff line change
@@ -2,16 +2,38 @@
module SemanticLogger
module Formatters
class OpenTelemetry < Raw
# Remove the following fields since they are sent via the OpenTelemetry API
def time
end

# Log level
def level
hash[:level] = log.level.to_s
hash[:level_index] = severity_number(log.level_index)
end

# Payload is submitted directly as attributes
def payload
return unless log.payload.respond_to?(:empty?) && !log.payload.empty?

hash[:payload] = log.payload.transform_keys!(&:to_s)
end

private

def severity_number(severity)
case severity
when :trace
::OpenTelemetry::Logs::SeverityNumber::SEVERITY_NUMBER_TRACE
when :debug
::OpenTelemetry::Logs::SeverityNumber::SEVERITY_NUMBER_DEBUG
when :info
::OpenTelemetry::Logs::SeverityNumber::SEVERITY_NUMBER_INFO
when :warn
::OpenTelemetry::Logs::SeverityNumber::SEVERITY_NUMBER_WARN
when :error
::OpenTelemetry::Logs::SeverityNumber::SEVERITY_NUMBER_ERROR
when :fatal
::OpenTelemetry::Logs::SeverityNumber::SEVERITY_NUMBER_FATAL
else
::OpenTelemetry::Logs::SeverityNumber::SEVERITY_NUMBER_UNSPECIFIED
end
end
end
end