Skip to content

Commit 0b4930d

Browse files
authored
Expose traces and metrics providers. (#353)
1 parent 7144ff0 commit 0b4930d

File tree

10 files changed

+117
-0
lines changed

10 files changed

+117
-0
lines changed

config/metrics.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2024, by Samuel Williams.
5+
6+
def prepare
7+
require "metrics/provider/async"
8+
end

config/sus.rb

+14
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,17 @@
55

66
require "covered/sus"
77
include Covered::Sus
8+
9+
ENV["TRACES_BACKEND"] ||= "traces/backend/test"
10+
ENV["METRICS_BACKEND"] ||= "metrics/backend/test"
11+
12+
def prepare_instrumentation!
13+
require "traces"
14+
require "metrics"
15+
end
16+
17+
def before_tests(...)
18+
prepare_instrumentation!
19+
20+
super
21+
end

config/traces.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2024, by Samuel Williams.
5+
6+
def prepare
7+
require "traces/provider/async"
8+
end

gems.rb

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
gem "decode"
2626
gem "rubocop"
2727

28+
gem "traces"
29+
gem "metrics"
30+
2831
gem "sus-fixtures-async"
2932
gem "sus-fixtures-console", "~> 0.3"
3033

lib/metrics/provider/async.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2024, by Samuel Williams.
5+
6+
require_relative "async/task"

lib/metrics/provider/async/task.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2024, by Samuel Williams.
5+
6+
require_relative "../../../async/task"
7+
require "metrics/provider"
8+
9+
Metrics::Provider(Async::Task) do
10+
ASYNC_TASK_SCHEDULED = Metrics.metric("async.task.scheduled", :counter, description: "The number of tasks scheduled.")
11+
12+
def schedule(&block)
13+
ASYNC_TASK_SCHEDULED.emit(1)
14+
15+
super(&block)
16+
end
17+
end

lib/traces/provider/async.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2024, by Samuel Williams.
5+
6+
require_relative "async/task"
7+
require_relative "async/barrier"

lib/traces/provider/async/barrier.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2022, by Samuel Williams.
5+
6+
require_relative "../../../async/barrier"
7+
require "traces/provider"
8+
9+
Traces::Provider(Async::Barrier) do
10+
def wait
11+
attributes = {
12+
"size" => self.size
13+
}
14+
15+
Traces.trace("async.barrier.wait", attributes: attributes) {super}
16+
end
17+
end

lib/traces/provider/async/task.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2022, by Samuel Williams.
5+
6+
require_relative "../../../async/task"
7+
require "traces/provider"
8+
9+
Traces::Provider(Async::Task) do
10+
def schedule(&block)
11+
unless self.transient?
12+
trace_context = Traces.trace_context
13+
end
14+
15+
super do
16+
Traces.trace_context = trace_context
17+
18+
if annotation = self.annotation
19+
attributes = {
20+
"annotation" => annotation
21+
}
22+
end
23+
24+
Traces.trace("async.task", attributes: attributes) do
25+
yield
26+
end
27+
end
28+
end
29+
end

releases.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Releases
22

3+
## Unreleased
4+
5+
### Traces and Metrics Providers
6+
7+
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.
8+
9+
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.
10+
311
## v2.19.0
412

513
### Async::Scheduler Debugging

0 commit comments

Comments
 (0)