Skip to content

Commit

Permalink
Expose traces and metrics providers.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 8, 2024
1 parent fe19601 commit 8a10ee4
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 0 deletions.
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"
end
14 changes: 14 additions & 0 deletions config/sus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,17 @@

require "covered/sus"
include Covered::Sus

def prepare_instrumentation!
ENV["TRACES_BACKEND"] ||= "traces/backend/test"
require "traces"

ENV["METRICS_BACKEND"] ||= "metrics/backend/test"
require "metrics"
end

def before_tests
prepare_instrumentation!

super
end
8 changes: 8 additions & 0 deletions config/traces.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 "traces/provider/async"
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
7 changes: 7 additions & 0 deletions lib/traces/provider/async.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

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

require_relative "async/task"
require_relative "async/barrier"
17 changes: 17 additions & 0 deletions lib/traces/provider/async/barrier.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, 2022, by Samuel Williams.

require_relative "../../../async/barrier"
require "traces/provider"

Traces::Provider(Async::Barrier) do
def wait
attributes = {
"size" => self.size
}

Traces.trace("async.barrier.wait", attributes: attributes) {super}
end
end
29 changes: 29 additions & 0 deletions lib/traces/provider/async/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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

if annotation = self.annotation
attributes = {
"annotation" => annotation
}
end

Traces.trace("async.task", attributes: attributes) do
yield
end
end
end
end
8 changes: 8 additions & 0 deletions releases.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Releases

## Unreleased

### Traces and Metrics Providers

Async now has [traces](https://github.com/socketry/traces) and [metrics](https://github.com/socketry/metrics) providers for various core classes. This allows you to emit traces and metrics to a suitable backend (including DataDog, New Relic, OpenTelemetry, etc.) for monitoring and debugging purposes.

To take advantage of this feature, you will need to introduce your own `config/traces.rb` and `config/metrics.rb`. Async's own repository includes these files for testing purposes, you could copy them into your own project and modify them as needed.

## v2.19.0

### Async::Scheduler Debugging
Expand Down

0 comments on commit 8a10ee4

Please sign in to comment.