From 8a10ee489cf53a88d2cf4b5eee8488944a97e97f 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/metrics.rb | 8 ++++++++ config/sus.rb | 14 ++++++++++++++ config/traces.rb | 8 ++++++++ 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 ++++++++ 10 files changed, 117 insertions(+) create mode 100644 config/metrics.rb create mode 100644 config/traces.rb 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/metrics.rb b/config/metrics.rb new file mode 100644 index 0000000..cdffbbf --- /dev/null +++ b/config/metrics.rb @@ -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 \ No newline at end of file diff --git a/config/sus.rb b/config/sus.rb index be5cd75..ed859a7 100644 --- a/config/sus.rb +++ b/config/sus.rb @@ -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 diff --git a/config/traces.rb b/config/traces.rb new file mode 100644 index 0000000..b2d0541 --- /dev/null +++ b/config/traces.rb @@ -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 \ No newline at end of file diff --git a/gems.rb b/gems.rb index 706984e..d00bf44 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 0000000..a4252d4 --- /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 0000000..daf6f8e --- /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 0000000..ba4de70 --- /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 0000000..ffa6721 --- /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 0000000..7cb1151 --- /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 34999f8..6fd9200 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