From f7a0ce16751ccb8def788d04d478527f43e4390e Mon Sep 17 00:00:00 2001 From: Victor Pellan Date: Mon, 25 Nov 2024 17:46:33 +0100 Subject: [PATCH] Add meta_struct methods --- lib/datadog/tracing/metadata/tagging.rb | 31 ++++++++++++++++++++++++ lib/datadog/tracing/span.rb | 3 +++ sig/datadog/tracing/metadata/tagging.rbs | 5 ++++ sig/datadog/tracing/span.rbs | 3 ++- 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/datadog/tracing/metadata/tagging.rb b/lib/datadog/tracing/metadata/tagging.rb index 48c3a174f59..ef89cd769d2 100644 --- a/lib/datadog/tracing/metadata/tagging.rb +++ b/lib/datadog/tracing/metadata/tagging.rb @@ -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 @@ -125,6 +152,10 @@ def meta def metrics @metrics ||= {} end + + def meta_struct + @meta_struct ||= {} + end end end end diff --git a/lib/datadog/tracing/span.rb b/lib/datadog/tracing/span.rb index be827c78c65..a569dd09896 100644 --- a/lib/datadog/tracing/span.rb +++ b/lib/datadog/tracing/span.rb @@ -21,6 +21,7 @@ class Span :end_time, :id, :meta, + :meta_struct, :metrics, :name, :parent_id, @@ -53,6 +54,7 @@ def initialize( end_time: nil, id: nil, meta: nil, + meta_struct: nil, metrics: nil, parent_id: 0, resource: name, @@ -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 diff --git a/sig/datadog/tracing/metadata/tagging.rbs b/sig/datadog/tracing/metadata/tagging.rbs index 4185b0c156f..14bac2cc964 100644 --- a/sig/datadog/tracing/metadata/tagging.rbs +++ b/sig/datadog/tracing/metadata/tagging.rbs @@ -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 diff --git a/sig/datadog/tracing/span.rbs b/sig/datadog/tracing/span.rbs index eec1c4d5c7a..cd57ec1207c 100644 --- a/sig/datadog/tracing/span.rbs +++ b/sig/datadog/tracing/span.rbs @@ -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 @@ -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