File: task.rb

package info (click to toggle)
ruby-async 2.36.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 400 kB
  • sloc: ruby: 1,938; makefile: 4
file content (40 lines) | stat: -rw-r--r-- 939 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

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

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

Traces::Provider(Async::Task) do
	def schedule(&block)
		# If we are not actively tracing anything, then we can skip this:
		unless Traces.active?
			return super(&block)
		end
		
		unless self.transient?
			trace_context = Traces.current_context
		end
		
		attributes = {
			# We use the instance variable as it corresponds to the user-provided block.
			"block" => @block.to_s,
			"transient" => self.transient?,
		}
		
		# Run the trace in the context of the child task:
		super do
			Traces.with_context(trace_context)
			
			if annotation = self.annotation
				attributes["annotation"] = annotation
			end
			
			Traces.trace("async.task", attributes: attributes) do
				# Yes, this is correct, we already called super above:
				yield
			end
		end
	end
end