@@ -24,7 +24,8 @@ class Scope
2424 :span ,
2525 :session ,
2626 :attachments ,
27- :propagation_context
27+ :propagation_context ,
28+ :attributes
2829 ]
2930
3031 attr_reader ( *ATTRIBUTES )
@@ -84,7 +85,10 @@ def apply_to_event(event, hint = nil)
8485 # @param telemetry [MetricEvent, LogEvent] the telemetry event to apply scope context to
8586 # @return [MetricEvent, LogEvent] the telemetry event with scope context applied
8687 def apply_to_telemetry ( telemetry )
87- # TODO-neel when new scope set_attribute api is added: add them here
88+ attributes . each do |key , value |
89+ telemetry . attributes [ key ] = value unless telemetry . attributes . key? ( key )
90+ end
91+
8892 trace_context = get_trace_context
8993 telemetry . trace_id = trace_context [ :trace_id ]
9094 telemetry . span_id = trace_context [ :span_id ]
@@ -136,6 +140,7 @@ def dup
136140 copy . propagation_context = propagation_context . deep_dup
137141 copy . attachments = attachments . dup
138142 copy . event_processors = event_processors . dup
143+ copy . attributes = attributes . deep_dup
139144 copy
140145 end
141146
@@ -154,6 +159,7 @@ def update_from_scope(scope)
154159 self . span = scope . span
155160 self . propagation_context = scope . propagation_context
156161 self . attachments = scope . attachments
162+ self . attributes = scope . attributes
157163 end
158164
159165 # Updates the scope's data from the given options.
@@ -256,6 +262,31 @@ def set_context(key, value)
256262 set_contexts ( key => value )
257263 end
258264
265+ # Updates the scope's attributes by merging with the old value.
266+ # @param attributes_hash [Hash]
267+ # @return [Hash]
268+ def set_attributes ( attributes_hash )
269+ check_argument_type! ( attributes_hash , Hash )
270+ @attributes . merge! ( attributes_hash )
271+ end
272+
273+ # Sets a single attribute on the scope.
274+ # @param key [String, Symbol]
275+ # @param value [Object]
276+ # @param unit [String, Symbol, nil] an optional measurement unit for the value
277+ # @return [Hash]
278+ def set_attribute ( key , value , unit : nil )
279+ value = { value : value , unit : unit } unless unit . nil?
280+ set_attributes ( key => value )
281+ end
282+
283+ # Removes a single attribute from the scope. No-op if the attribute is not set.
284+ # @param key [String, Symbol]
285+ # @return [void]
286+ def remove_attribute ( key )
287+ @attributes . delete ( key )
288+ end
289+
259290 # Sets the scope's level attribute.
260291 # @param level [String, Symbol]
261292 # @return [void]
@@ -361,6 +392,7 @@ def set_default_value
361392 @span = nil
362393 @session = nil
363394 @attachments = [ ]
395+ @attributes = { }
364396 generate_propagation_context
365397 set_new_breadcrumb_buffer
366398 end
0 commit comments