Skip to content

Commit

Permalink
Add Proof of Concept instrumentation for Solid Process
Browse files Browse the repository at this point in the history
  • Loading branch information
tomascco committed May 3, 2024
1 parent 46c87d7 commit ea65d09
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
8 changes: 7 additions & 1 deletion config/initializers/solid_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@
require "solid/process/event_logs/json_logger_listener"
# require "solid/process/event_logs/basic_logger_listener"

require "solid/process/active_support_publisher"

require "open_telemetry_tracer"
OpenTelemetryTracer.subscribe

# Solid::Process::EventLogs::BasicLoggerListener.logger = Rails.logger

Solid::Result.configuration do |config|
config.event_logs.listener = Solid::Result::EventLogs::Listeners[
Solid::Process::EventLogs::JsonLoggerListener,
Solid::Process::EventLogs::Record::Listener
Solid::Process::EventLogs::Record::Listener,
Solid::Process::ActiveSupportPublisher
]
end
53 changes: 53 additions & 0 deletions lib/open_telemetry_tracer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module OpenTelemetryTracer
SolidResultTracer = ::OpenTelemetry.tracer_provider.tracer("solid-result")

module ProcessListener
def self.start(name, _id, payload)
scope = payload[:scope]

span = SolidResultTracer.start_span("#{scope[:name]}#call", attributes: {
"desc" => scope[:desc].inspect
})

token = OpenTelemetry::Context.attach(::OpenTelemetry::Trace.context_with_span(span))
payload.merge!(__otel: {span:, token:})
end

def self.finish(_name, _id, payload)
otel = payload.delete(:__otel)

otel => { span:, token: }

span.finish
OpenTelemetry::Context.detach(token)
end
end

module AndThenListener
def self.start(name, _id, payload)
payload => { scope:, and_then: }

span = SolidResultTracer.start_span("#{scope[:name]}##{and_then[:method_name] || "block"}", attributes: {
"type" => and_then[:type].to_s,
"arg" => and_then[:arg].inspect
})

token = OpenTelemetry::Context.attach(::OpenTelemetry::Trace.context_with_span(span))
payload.merge!(__otel: {span:, token:})
end

def self.finish(_name, _id, payload)
otel = payload.delete(:__otel)

otel => { span:, token: }

span.finish
OpenTelemetry::Context.detach(token)
end
end

def self.subscribe
ActiveSupport::Notifications.monotonic_subscribe("start_process.solid_process", ProcessListener)
ActiveSupport::Notifications.monotonic_subscribe("and_then.solid_process", AndThenListener)
end
end
19 changes: 19 additions & 0 deletions lib/solid/process/active_support_publisher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solid::Process::ActiveSupportPublisher
include Solid::Result::EventLogs::Listener

def self.around_event_logs?
true
end

def self.around_and_then?
true
end

def around_event_logs(scope:, &)
ActiveSupport::Notifications.instrument("start_process.solid_process", scope:, &)
end

def around_and_then(scope:, and_then:, **, &)
ActiveSupport::Notifications.instrument("and_then.solid_process", scope:, and_then:, &)
end
end

0 comments on commit ea65d09

Please sign in to comment.