From 09a809cdc5cf30e78dcae6812da78b182d1955f8 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Fri, 8 Nov 2024 19:42:26 +1300 Subject: [PATCH] Expose traces and metrics providers. --- config/sus.rb | 6 ++++++ gems.rb | 3 +++ lib/metrics/provider/async.rb | 6 ++++++ lib/metrics/provider/async/task.rb | 17 ++++++++++++++++ lib/traces/provider/async.rb | 7 +++++++ lib/traces/provider/async/barrier.rb | 17 ++++++++++++++++ lib/traces/provider/async/task.rb | 29 ++++++++++++++++++++++++++++ releases.md | 8 ++++++++ 8 files changed, 93 insertions(+) create mode 100644 lib/metrics/provider/async.rb create mode 100644 lib/metrics/provider/async/task.rb create mode 100644 lib/traces/provider/async.rb create mode 100644 lib/traces/provider/async/barrier.rb create mode 100644 lib/traces/provider/async/task.rb diff --git a/config/sus.rb b/config/sus.rb index be5cd75b..8aa6d26d 100644 --- a/config/sus.rb +++ b/config/sus.rb @@ -5,3 +5,9 @@ require "covered/sus" include Covered::Sus + +ENV["TRACES_BACKEND"] ||= "traces/backend/test" +require "traces" + +ENV["METRICS_BACKEND"] ||= "metrics/backend/test" +require "metrics" diff --git a/gems.rb b/gems.rb index 706984e1..d00bf44b 100644 --- a/gems.rb +++ b/gems.rb @@ -25,6 +25,9 @@ gem "decode" gem "rubocop" + gem "traces" + gem "metrics" + gem "sus-fixtures-async" gem "sus-fixtures-console", "~> 0.3" diff --git a/lib/metrics/provider/async.rb b/lib/metrics/provider/async.rb new file mode 100644 index 00000000..a4252d48 --- /dev/null +++ b/lib/metrics/provider/async.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +# Released under the MIT License. +# Copyright, 2024, by Samuel Williams. + +require_relative "async/task" diff --git a/lib/metrics/provider/async/task.rb b/lib/metrics/provider/async/task.rb new file mode 100644 index 00000000..daf6f8e4 --- /dev/null +++ b/lib/metrics/provider/async/task.rb @@ -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 diff --git a/lib/traces/provider/async.rb b/lib/traces/provider/async.rb new file mode 100644 index 00000000..ba4de707 --- /dev/null +++ b/lib/traces/provider/async.rb @@ -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" diff --git a/lib/traces/provider/async/barrier.rb b/lib/traces/provider/async/barrier.rb new file mode 100644 index 00000000..ffa6721a --- /dev/null +++ b/lib/traces/provider/async/barrier.rb @@ -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 diff --git a/lib/traces/provider/async/task.rb b/lib/traces/provider/async/task.rb new file mode 100644 index 00000000..7cb1151b --- /dev/null +++ b/lib/traces/provider/async/task.rb @@ -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 diff --git a/releases.md b/releases.md index 34999f85..6fd92002 100644 --- a/releases.md +++ b/releases.md @@ -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