Class: Datadog::Statsd::Schema::Namespace
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- Datadog::Statsd::Schema::Namespace
- Defined in:
- lib/datadog/statsd/schema/namespace.rb
Overview
Represents a namespace in the metric schema hierarchy Namespaces contain tags, metrics, and nested namespaces, providing organization and scoping
Defined Under Namespace
Modules: Types
Instance Method Summary collapse
-
#add_metric(metric_definition) ⇒ Namespace
Add a new metric to this namespace (returns new namespace instance).
-
#add_namespace(namespace) ⇒ Namespace
Add a nested namespace (returns new namespace instance).
-
#add_tag(tag_definition) ⇒ Namespace
Add a new tag definition to this namespace (returns new namespace instance).
-
#all_metrics(path = []) ⇒ Hash
Get all metrics recursively including from nested namespaces.
-
#description ⇒ String?
Human-readable description of this namespace.
-
#effective_tags(parent_tags = {}) ⇒ Hash<Symbol, TagDefinition>
Get all tag definitions including inherited from parent namespaces.
-
#find_metric(metric_name) ⇒ MetricDefinition?
Find a metric by name within this namespace.
-
#find_metric_by_path(path) ⇒ MetricDefinition?
Find metric by path (e.g., “request.duration” within web namespace).
-
#find_namespace(namespace_name) ⇒ Namespace?
Find a nested namespace by name.
-
#find_namespace_by_path(path) ⇒ Namespace?
Get namespace by path (e.g., “web.request”).
-
#find_tag(tag_name) ⇒ TagDefinition?
Find a tag definition by name within this namespace.
-
#full_path(parent_path = []) ⇒ Array<Symbol>
Get the full path of this namespace including parent namespaces.
-
#has_metric?(metric_name) ⇒ Boolean
Check if this namespace contains a metric.
-
#has_namespace?(namespace_name) ⇒ Boolean
Check if this namespace contains a nested namespace.
-
#has_tag?(tag_name) ⇒ Boolean
Check if this namespace contains a tag definition.
-
#metric_names ⇒ Array<Symbol>
Get all metric names in this namespace (not including nested).
-
#metrics ⇒ Hash<Symbol, MetricDefinition>
Hash of metric definitions within this namespace.
-
#name ⇒ Symbol
The namespace name as a symbol.
-
#namespace_names ⇒ Array<Symbol>
Get all nested namespace names.
-
#namespaces ⇒ Hash<Symbol, Namespace>
Hash of nested namespaces within this namespace.
-
#tag_names ⇒ Array<Symbol>
Get all tag names in this namespace.
-
#tags ⇒ Hash<Symbol, TagDefinition>
Hash of tag definitions within this namespace.
-
#total_metrics_count ⇒ Integer
Count total metrics including nested namespaces.
-
#total_namespaces_count ⇒ Integer
Count total namespaces including nested.
-
#validate_tag_references ⇒ Array<String>
Validate that all tag references in metrics exist.
Instance Method Details
#add_metric(metric_definition) ⇒ Namespace
Add a new metric to this namespace (returns new namespace instance)
99 100 101 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 99 def add_metric(metric_definition) new(metrics: metrics.merge(metric_definition.name => metric_definition)) end |
#add_namespace(namespace) ⇒ Namespace
Add a nested namespace (returns new namespace instance)
113 114 115 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 113 def add_namespace(namespace) new(namespaces: namespaces.merge(namespace.name => namespace)) end |
#add_tag(tag_definition) ⇒ Namespace
Add a new tag definition to this namespace (returns new namespace instance)
106 107 108 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 106 def add_tag(tag_definition) new(tags: .merge(tag_definition.name => tag_definition)) end |
#all_metrics(path = []) ⇒ Hash
Get all metrics recursively including from nested namespaces
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 170 def all_metrics(path = []) # Filter out :root from the path to avoid it appearing in metric names current_path = path + [name] filtered_path = current_path.reject { |part| part == :root } result = {} # Add metrics from this namespace metrics.each do |_metric_name, metric_def| full_metric_name = metric_def.full_name(filtered_path) result[full_metric_name] = { definition: metric_def, namespace_path: filtered_path, namespace: self } end # Add metrics from nested namespaces recursively namespaces.each do |_, nested_namespace| result.merge!(nested_namespace.all_metrics(current_path)) end result end |
#description ⇒ String?
Human-readable description of this namespace
53 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 53 attribute :description, Types::String.optional.default(nil) |
#effective_tags(parent_tags = {}) ⇒ Hash<Symbol, TagDefinition>
Get all tag definitions including inherited from parent namespaces
197 198 199 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 197 def ( = {}) .merge() end |
#find_metric(metric_name) ⇒ MetricDefinition?
Find a metric by name within this namespace
71 72 73 74 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 71 def find_metric(metric_name) metric_symbol = metric_name.to_sym metrics[metric_symbol] end |
#find_metric_by_path(path) ⇒ MetricDefinition?
Find metric by path (e.g., “request.duration” within web namespace)
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 231 def find_metric_by_path(path) parts = path.split(".") if parts.length == 1 # Single part, look for metric in this namespace find_metric(parts.first) else # Multiple parts, navigate to nested namespace namespace_name = parts.first remaining_path = parts[1..-1].join(".") nested_namespace = find_namespace(namespace_name) return nil unless nested_namespace nested_namespace.find_metric_by_path(remaining_path) end end |
#find_namespace(namespace_name) ⇒ Namespace?
Find a nested namespace by name
91 92 93 94 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 91 def find_namespace(namespace_name) namespace_symbol = namespace_name.to_sym namespaces[namespace_symbol] end |
#find_namespace_by_path(path) ⇒ Namespace?
Get namespace by path (e.g., “web.request”)
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 254 def find_namespace_by_path(path) return self if path.empty? parts = path.split(".") if parts.length == 1 find_namespace(parts.first) else namespace_name = parts.first remaining_path = parts[1..-1].join(".") nested_namespace = find_namespace(namespace_name) return nil unless nested_namespace nested_namespace.find_namespace_by_path(remaining_path) end end |
#find_tag(tag_name) ⇒ TagDefinition?
Find a tag definition by name within this namespace
81 82 83 84 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 81 def find_tag(tag_name) tag_symbol = tag_name.to_sym [tag_symbol] end |
#full_path(parent_path = []) ⇒ Array<Symbol>
Get the full path of this namespace including parent namespaces
60 61 62 63 64 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 60 def full_path(parent_path = []) return [name] if parent_path.empty? parent_path + [name] end |
#has_metric?(metric_name) ⇒ Boolean
Check if this namespace contains a metric
138 139 140 141 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 138 def has_metric?(metric_name) metric_symbol = metric_name.to_sym metrics.key?(metric_symbol) end |
#has_namespace?(namespace_name) ⇒ Boolean
Check if this namespace contains a nested namespace
154 155 156 157 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 154 def has_namespace?(namespace_name) namespace_symbol = namespace_name.to_sym namespaces.key?(namespace_symbol) end |
#has_tag?(tag_name) ⇒ Boolean
Check if this namespace contains a tag definition
146 147 148 149 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 146 def has_tag?(tag_name) tag_symbol = tag_name.to_sym .key?(tag_symbol) end |
#metric_names ⇒ Array<Symbol>
Get all metric names in this namespace (not including nested)
119 120 121 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 119 def metric_names metrics.keys end |
#metrics ⇒ Hash<Symbol, MetricDefinition>
Hash of metric definitions within this namespace
45 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 45 attribute :metrics, Types::Hash.map(Types::Symbol, MetricDefinition).default({}.freeze) |
#name ⇒ Symbol
The namespace name as a symbol
37 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 37 attribute :name, Types::Strict::Symbol |
#namespace_names ⇒ Array<Symbol>
Get all nested namespace names
131 132 133 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 131 def namespace_names namespaces.keys end |
#namespaces ⇒ Hash<Symbol, Namespace>
Hash of nested namespaces within this namespace
49 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 49 attribute :namespaces, Types::Hash.map(Types::Symbol, Namespace).default({}.freeze) |
#tag_names ⇒ Array<Symbol>
Get all tag names in this namespace
125 126 127 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 125 def tag_names .keys end |
#tags ⇒ Hash<Symbol, TagDefinition>
Hash of tag definitions within this namespace
41 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 41 attribute :tags, Types::Hash.map(Types::Symbol, TagDefinition).default({}.freeze) |
#total_metrics_count ⇒ Integer
Count total metrics including nested namespaces
274 275 276 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 274 def total_metrics_count metrics.count + namespaces.values.sum(&:total_metrics_count) end |
#total_namespaces_count ⇒ Integer
Count total namespaces including nested
280 281 282 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 280 def total_namespaces_count namespaces.count + namespaces.values.sum(&:total_namespaces_count) end |
#validate_tag_references ⇒ Array<String>
Validate that all tag references in metrics exist
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/datadog/statsd/schema/namespace.rb', line 203 def validate_tag_references errors = [] metrics.each do |metric_name, metric_def| # Check allowed tags metric_def..each do |tag_name| errors << "Metric #{metric_name} references unknown tag: #{tag_name}" unless has_tag?(tag_name) end # Check required tags metric_def..each do |tag_name| errors << "Metric #{metric_name} requires unknown tag: #{tag_name}" unless has_tag?(tag_name) end end # Validate nested namespaces recursively namespaces.each do |_, nested_namespace| errors.concat(nested_namespace.validate_tag_references) end errors end |