Class: Datadog::Statsd::Schema::MetricDefinition

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/datadog/statsd/schema/metric_definition.rb

Overview

Represents a metric definition within a schema namespace Defines the metric type, allowed/required tags, validation rules, and metadata

Examples:

Basic metric definition

metric_def = MetricDefinition.new(
  name: :page_views,
  type: :counter,
  allowed_tags: [:controller, :action],
  required_tags: [:controller]
)

Metric with description and units

metric_def = MetricDefinition.new(
  name: :request_duration,
  type: :distribution,
  description: "HTTP request processing time",
  units: "milliseconds",
  allowed_tags: [:controller, :action, :status_code],
  required_tags: [:controller, :action]
)

Since:

  • 0.1.0

Defined Under Namespace

Modules: Types

Constant Summary collapse

VALID_METRIC_TYPES =

Valid metric types supported by StatsD

Since:

  • 0.1.0

i[counter gauge histogram distribution timing set].freeze

Instance Method Summary collapse

Instance Method Details

#allowed_tagsArray<Symbol>

Array of tag names that are allowed for this metric

Returns:

  • (Array<Symbol>)

    Allowed tag names (empty array means all tags allowed)

Since:

  • 0.1.0



55
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 55

attribute :allowed_tags, Types::Array.of(Types::Symbol).default([].freeze)

#allows_tag?(tag_name) ⇒ Boolean

Check if a tag is allowed for this metric

Examples:

metric_def.allows_tag?(:controller)  # => true
metric_def.allows_tag?(:invalid_tag) # => false (if restrictions exist)

Parameters:

  • tag_name (String, Symbol)

    Tag name to check

Returns:

  • (Boolean)

    true if tag is allowed (or no restrictions exist)

Since:

  • 0.1.0



90
91
92
93
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 90

def allows_tag?(tag_name)
  tag_symbol = tag_name.to_sym
  allowed_tags.empty? || allowed_tags.include?(tag_symbol)
end

#counting_metric?Boolean

Check if this is a counting metric

Returns:

  • (Boolean)

    true for counter metrics

Since:

  • 0.1.0



147
148
149
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 147

def counting_metric?
  i[counter].include?(type)
end

#descriptionString?

Human-readable description of what this metric measures

Returns:

  • (String, nil)

    Description text

Since:

  • 0.1.0



51
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 51

attribute :description, Types::String.optional.default(nil)

#effective_allowed_tags(schema_registry = nil) ⇒ Array<Symbol>

Get effective allowed tags by merging with inherited tags if present

Parameters:

  • schema_registry (Object) (defaults to: nil)

    Registry to look up inherited metrics

Returns:

  • (Array<Symbol>)

    Combined allowed tags including inherited ones

Since:

  • 0.1.0



166
167
168
169
170
171
172
173
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 166

def effective_allowed_tags(schema_registry = nil)
  return allowed_tags unless inherit_tags && schema_registry

  inherited_metric = schema_registry.find_metric(inherit_tags)
  return allowed_tags unless inherited_metric

  (inherited_metric.effective_allowed_tags(schema_registry) + allowed_tags).uniq
end

#effective_required_tags(schema_registry = nil) ⇒ Array<Symbol>

Get effective required tags by merging with inherited tags if present

Parameters:

  • schema_registry (Object) (defaults to: nil)

    Registry to look up inherited metrics

Returns:

  • (Array<Symbol>)

    Combined required tags including inherited ones

Since:

  • 0.1.0



178
179
180
181
182
183
184
185
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 178

def effective_required_tags(schema_registry = nil)
  return required_tags unless inherit_tags && schema_registry

  inherited_metric = schema_registry.find_metric(inherit_tags)
  return required_tags unless inherited_metric

  (inherited_metric.effective_required_tags(schema_registry) + required_tags).uniq
end

#full_name(namespace_path = []) ⇒ String

Get the full metric name including namespace path

Examples:

metric_def.full_name([:web, :api])  # => "web.api.page_views"

Parameters:

  • namespace_path (Array<Symbol>) (defaults to: [])

    Array of namespace names leading to this metric

Returns:

  • (String)

    Fully qualified metric name

Since:

  • 0.1.0



78
79
80
81
82
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 78

def full_name(namespace_path = [])
  return name.to_s if namespace_path.empty?

  "#{namespace_path.join(".")}.#{name}"
end

#gauge_metric?Boolean

Check if this is a gauge metric

Returns:

  • (Boolean)

    true for gauge metrics

Since:

  • 0.1.0



153
154
155
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 153

def gauge_metric?
  type == :gauge
end

#inherit_tagsString?

Path to another metric to inherit tags from

Returns:

  • (String, nil)

    Dot-separated path to parent metric

Since:

  • 0.1.0



63
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 63

attribute :inherit_tags, Types::String.optional.default(nil)

#invalid_tags(provided_tags) ⇒ Array<Symbol>

Get all invalid tags from a provided tag set

Examples:

metric_def.invalid_tags(controller: "users", invalid: "value")
# => [:invalid] (if only controller is allowed)

Parameters:

  • provided_tags (Hash)

    Hash of tag names to values

Returns:

  • (Array<Symbol>)

    Array of invalid tag names

Since:

  • 0.1.0



123
124
125
126
127
128
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 123

def invalid_tags(provided_tags)
  return [] if allowed_tags.empty? # No restrictions

  provided_tag_symbols = provided_tags.keys.map(&:to_sym)
  provided_tag_symbols - allowed_tags
end

#missing_required_tags(provided_tags) ⇒ Array<Symbol>

Get all missing required tags from a provided tag set

Examples:

metric_def.missing_required_tags(controller: "users")
# => [:action] (if action is also required)

Parameters:

  • provided_tags (Hash)

    Hash of tag names to values

Returns:

  • (Array<Symbol>)

    Array of missing required tag names

Since:

  • 0.1.0



112
113
114
115
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 112

def missing_required_tags(provided_tags)
  provided_tag_symbols = provided_tags.keys.map(&:to_sym)
  required_tags - provided_tag_symbols
end

#nameSymbol

The metric name as a symbol

Returns:

  • (Symbol)

    Metric name

Since:

  • 0.1.0



43
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 43

attribute :name, Types::Strict::Symbol

#namespaceSymbol?

The namespace this metric belongs to

Returns:

  • (Symbol, nil)

    Namespace name

Since:

  • 0.1.0



71
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 71

attribute :namespace, Types::Strict::Symbol.optional.default(nil)

#required_tagsArray<Symbol>

Array of tag names that are required for this metric

Returns:

  • (Array<Symbol>)

    Required tag names

Since:

  • 0.1.0



59
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 59

attribute :required_tags, Types::Array.of(Types::Symbol).default([].freeze)

#requires_tag?(tag_name) ⇒ Boolean

Check if a tag is required for this metric

Examples:

metric_def.requires_tag?(:controller)  # => true
metric_def.requires_tag?(:optional_tag) # => false

Parameters:

  • tag_name (String, Symbol)

    Tag name to check

Returns:

  • (Boolean)

    true if tag is required

Since:

  • 0.1.0



101
102
103
104
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 101

def requires_tag?(tag_name)
  tag_symbol = tag_name.to_sym
  required_tags.include?(tag_symbol)
end

#set_metric?Boolean

Check if this is a set metric

Returns:

  • (Boolean)

    true for set metrics

Since:

  • 0.1.0



159
160
161
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 159

def set_metric?
  type == :set
end

#timing_metric?Boolean

Check if this is a timing-based metric

Returns:

  • (Boolean)

    true for timing, distribution, or histogram metrics

Since:

  • 0.1.0



141
142
143
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 141

def timing_metric?
  i[timing distribution histogram].include?(type)
end

#typeSymbol

The metric type (counter, gauge, histogram, distribution, timing, set)

Returns:

  • (Symbol)

    One of the valid metric types

Since:

  • 0.1.0



47
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 47

attribute :type, Types::Strict::Symbol.constrained(included_in: VALID_METRIC_TYPES)

#unitsString?

Units of measurement for this metric (e.g., “milliseconds”, “bytes”)

Returns:

  • (String, nil)

    Unit description

Since:

  • 0.1.0



67
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 67

attribute :units, Types::String.optional.default(nil)

#valid_tags?(provided_tags) ⇒ Boolean

Validate a complete tag set for this metric

Examples:

metric_def.valid_tags?(controller: "users", action: "show")  # => true

Parameters:

  • provided_tags (Hash)

    Hash of tag names to values

Returns:

  • (Boolean)

    true if all tags are valid

Since:

  • 0.1.0



135
136
137
# File 'lib/datadog/statsd/schema/metric_definition.rb', line 135

def valid_tags?(provided_tags)
  missing_required_tags(provided_tags).empty? && invalid_tags(provided_tags).empty?
end