Skip to content

Commit

Permalink
Add meta_struct methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vpellan committed Jan 14, 2025
1 parent bf756e9 commit f7a0ce1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
31 changes: 31 additions & 0 deletions lib/datadog/tracing/metadata/tagging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,33 @@ def clear_metric(key)
metrics.delete(key)
end

# Getter for meta_struct
def get_struct_tag(key)
meta_struct[key]
end

# Setter for meta_struct, key must be a string, value can be anything
# Keys can be duplicated in meta_struct and meta/metrics
def set_struct_tag(key, value)
meta_struct[key] = value
rescue StandardError => e
Datadog.logger.debug("Unable to set the tag #{key} in meta_struct, ignoring it. Caused by: #{e}")
end

# Returns true if the provided `tag` was set to a non-nil value.
# False otherwise.
#
# @param [String] tag the tag or metric to check for presence
# @return [Boolean] if the tag is present and not nil
def has_struct_tag?(tag) # rubocop:disable Naming/PredicateName
!get_struct_tag(tag).nil? # nil is considered not present, thus we can't use `Hash#has_key?`
end

# This method removes a tag for the given key.
def clear_struct_tag(key)
meta_struct.delete(key)
end

# Returns a copy of all metadata.
# Keys for `@meta` and `@metrics` don't collide, by construction.
def tags
Expand All @@ -125,6 +152,10 @@ def meta
def metrics
@metrics ||= {}
end

def meta_struct
@meta_struct ||= {}
end
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/datadog/tracing/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Span
:end_time,
:id,
:meta,
:meta_struct,
:metrics,
:name,
:parent_id,
Expand Down Expand Up @@ -53,6 +54,7 @@ def initialize(
end_time: nil,
id: nil,
meta: nil,
meta_struct: nil,
metrics: nil,
parent_id: 0,
resource: name,
Expand All @@ -75,6 +77,7 @@ def initialize(
@trace_id = trace_id || Tracing::Utils.next_id

@meta = meta || {}
@meta_struct = meta_struct || {}
@metrics = metrics || {}
@status = status || 0

Expand Down
5 changes: 5 additions & 0 deletions sig/datadog/tracing/metadata/tagging.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ module Datadog
def get_metric: (untyped key) -> untyped
def set_metric: (untyped key, untyped value) -> untyped
def clear_metric: (untyped key) -> untyped
def get_struct_tag: (untyped key) -> untyped
def set_struct_tag: (untyped key, ?untyped? value) -> untyped
def has_struct_tag?: (untyped tag) -> untyped
def clear_struct_tag: (untyped key) -> untyped
def meta: () -> untyped
def metrics: () -> untyped
def meta_struct: () -> untyped
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion sig/datadog/tracing/span.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Datadog
attr_accessor end_time: ::Time?
attr_accessor id: ::Integer
attr_accessor meta: ::Hash[::String, untyped]
attr_accessor meta_struct: ::Hash[::String, untyped]
attr_accessor metrics: ::Hash[::String, untyped]
attr_accessor name: ::String
attr_accessor parent_id: ::Integer
Expand All @@ -19,7 +20,7 @@ module Datadog
attr_accessor trace_id: ::Integer

attr_writer duration: ::Float?
def initialize: (::String name, ?duration: ::Float?, ?end_time: ::Time?, ?id: ::Integer?, ?meta: ::Hash[::String, untyped]?, ?metrics: ::Hash[::String, untyped]?, ?parent_id: ::Integer, ?resource: ::String, ?service: ::String?, ?start_time: ::Time?, ?status: ::Integer, ?type: ::String?, ?trace_id: ::Integer?, ?service_entry: bool?, ?links: ::Array[Datadog::Tracing::SpanLink]?, ?events: ::Array[Datadog::Tracing::SpanEvent]?) -> void
def initialize: (::String name, ?duration: ::Float?, ?end_time: ::Time?, ?id: ::Integer?, ?meta: ::Hash[::String, untyped]?, ?meta_struct: ::Hash[::String, untyped]?, ?metrics: ::Hash[::String, untyped]?, ?parent_id: ::Integer, ?resource: ::String, ?service: ::String?, ?start_time: ::Time?, ?status: ::Integer, ?type: ::String?, ?trace_id: ::Integer?, ?service_entry: bool?, ?links: ::Array[Datadog::Tracing::SpanLink]?, ?events: ::Array[Datadog::Tracing::SpanEvent]?) -> void
def started?: () -> bool
def stopped?: () -> bool

Expand Down

0 comments on commit f7a0ce1

Please sign in to comment.