Skip to content

Commit

Permalink
Initial metrics and traces providers.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 5, 2024
1 parent 1c70c17 commit 6b8b2ff
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 1 deletion.
8 changes: 8 additions & 0 deletions config/metrics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

def prepare
require 'metrics/provider/async/task'
end
8 changes: 7 additions & 1 deletion config/sus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@
require "covered/sus"
include Covered::Sus

ENV["CONSOLE_LEVEL"] ||= "fatal"
# ENV["CONSOLE_LEVEL"] ||= "fatal"

ENV["TRACES_BACKEND"] ||= "traces/backend/test"
require 'traces'

ENV["METRICS_BACKEND"] ||= "metrics/backend/test"
require 'metrics'
9 changes: 9 additions & 0 deletions config/traces.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

def prepare
require 'traces/provider/async/task'
# require 'traces/provider/async/scheduler'
end
3 changes: 3 additions & 0 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
gem "decode"
gem "rubocop"

gem "traces"
gem "metrics"

gem "sus-fixtures-async"
gem "sus-fixtures-console", "~> 0.3"

Expand Down
6 changes: 6 additions & 0 deletions lib/metrics/provider/async.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require_relative 'async/task'
17 changes: 17 additions & 0 deletions lib/metrics/provider/async/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require_relative '../../../async/task'
require 'metrics/provider'

Metrics::Provider(Async::Task) do
ASYNC_TASK_SCHEDULED = Metrics.metric("async.task.scheduled", :counter, description: "The number of tasks scheduled.")

def schedule(&block)
ASYNC_TASK_SCHEDULED.emit(1)

super(&block)
end
end
6 changes: 6 additions & 0 deletions lib/traces/provider/async.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require_relative 'async/task'
88 changes: 88 additions & 0 deletions lib/traces/provider/async/scheduler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require_relative '../../../async/scheduler'
require 'traces/provider'

# Traces::Provider(Async::Scheduler) do
# def yield
# Traces.trace("async.scheduler.yield") {super}
# end

# def block(blocker, timeout)
# attributes = {
# blocker: blocker.to_s,
# timeout: timeout
# }

# Traces.trace("async.scheduler.block", attributes: attributes) {super}
# end

# def transfer
# Traces.trace("async.scheduler.transfer") {super}
# end

# # @asynchronous May be non-blocking..
# def address_resolve(hostname)
# attributes = {
# hostname: hostname
# }

# Traces.trace("async.scheduler.address_resolve", attributes: attributes) {super}
# end

# def io_wait(io, events, timeout = nil)
# attributes = {
# io: io.to_s,
# events: events,
# timeout: timeout
# }

# Traces.trace("async.scheduler.io_wait", attributes: attributes) {super}
# end

# if Async::Scheduler.method_defined?(:io_read)
# def io_read(io, buffer, length, offset = 0)
# attributes = {
# io: io.to_s,
# buffer: buffer,
# length: length,
# offset: offset,
# }

# Traces.trace("async.scheduler.io_read", attributes: attributes) {super}
# end
# end

# if Async::Scheduler.method_defined?(:io_write)
# def io_write(io, buffer, length, offset = 0)
# attributes = {
# io: io.to_s,
# buffer: buffer,
# length: length,
# offset: offset,
# }

# Traces.trace("async.scheduler.io_write", attributes: attributes) {super}
# end
# end

# def process_wait(pid, flags)
# attributes = {
# pid: pid,
# flags: flags
# }

# Traces.trace("async.scheduler.process_wait", attributes: attributes) {super}
# end

# def with_timeout(duration, exception = Async::TimeoutError, message = "execution expired", &block)
# attributes = {
# duration: duration,
# }

# Traces.trace("async.scheduler.with_timeout", attributes: attributes) {super}
# end
# end
27 changes: 27 additions & 0 deletions lib/traces/provider/async/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2022, by Samuel Williams.

require_relative '../../../async/task'
require 'traces/provider'

Traces::Provider(Async::Task) do
def schedule(&block)
unless self.transient?
trace_context = Traces.trace_context
end

super do
Traces.trace_context = trace_context

attributes = {
"annotation" => self.annotation,
}

Traces.trace('async.task', attributes: attributes) do
yield
end
end
end
end

0 comments on commit 6b8b2ff

Please sign in to comment.