Class: Datadog::Statsd::Schema::TagDefinition

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

Overview

Represents a tag definition within a schema namespace Defines validation rules, allowed values, transformations, and type constraints for tags

Examples:

Basic tag definition

tag_def = TagDefinition.new(
  name: :environment,
  values: [:production, :staging, :development],
  type: :symbol
)

Tag with custom validation

tag_def = TagDefinition.new(
  name: :user_id,
  type: :integer,
  validate: ->(value) { value > 0 }
)

Since:

  • 0.1.0

Defined Under Namespace

Modules: Types

Instance Method Summary collapse

Instance Method Details

#allows_value?(value) ⇒ Boolean

Check if a value is allowed for this tag according to the values constraint

Examples:

tag_def = TagDefinition.new(name: :env, values: [:prod, :dev])
tag_def.allows_value?(:prod)  # => true
tag_def.allows_value?(:test)  # => false

Parameters:

  • value (Object)

    The value to check

Returns:

  • (Boolean)

    true if the value is allowed

Since:

  • 0.1.0



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/datadog/statsd/schema/tag_definition.rb', line 65

def allows_value?(value)
  return true if values.nil? # No restrictions

  case values
  when Array
    values.include?(value) || values.include?(value.to_s) || values.include?(value.to_sym)
  when Regexp
    values.match?(value.to_s)
  when Proc
    values.call(value)
  else
    values == value
  end
end

#nameSymbol

The tag name as a symbol

Returns:

  • (Symbol)

    Tag name

Since:

  • 0.1.0



36
# File 'lib/datadog/statsd/schema/tag_definition.rb', line 36

attribute :name, Types::Strict::Symbol

#namespaceSymbol?

The namespace this tag belongs to

Returns:

  • (Symbol, nil)

    Namespace name

Since:

  • 0.1.0



56
# File 'lib/datadog/statsd/schema/tag_definition.rb', line 56

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

#transformArray<Symbol>

Array of transformation functions to apply to values

Returns:

  • (Array<Symbol>)

    Transformation function names

Since:

  • 0.1.0



48
# File 'lib/datadog/statsd/schema/tag_definition.rb', line 48

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

#transform_value(value, transformers = {}) ⇒ Object

Apply transformations to a value using the provided transformer functions

Examples:

tag_def = TagDefinition.new(name: :service, transform: [:downcase])
transformers = { downcase: ->(val) { val.to_s.downcase } }
tag_def.transform_value("WEB-SERVICE", transformers)  # => "web-service"

Parameters:

  • value (Object)

    The value to transform

  • transformers (Hash<Symbol, Proc>) (defaults to: {})

    Hash of transformer name to proc mappings

Returns:

  • (Object)

    The transformed value

Since:

  • 0.1.0



88
89
90
91
92
93
94
95
# File 'lib/datadog/statsd/schema/tag_definition.rb', line 88

def transform_value(value, transformers = {})
  return value if transform.empty?

  transform.reduce(value) do |val, transformer_name|
    transformer = transformers[transformer_name]
    transformer ? transformer.call(val) : val
  end
end

#typeSymbol

The expected data type for tag values

Returns:

  • (Symbol)

    Type constraint (:string, :integer, :symbol)

Since:

  • 0.1.0



44
# File 'lib/datadog/statsd/schema/tag_definition.rb', line 44

attribute :type, Types::Strict::Symbol.default(:string)

#valid_value?(value, transformers = {}) ⇒ Boolean

Validate a value using type checking, transformations, and custom validation

Examples:

tag_def = TagDefinition.new(
  name: :port,
  type: :integer,
  validate: ->(val) { val > 0 && val < 65536 }
)
tag_def.valid_value?(8080)   # => true
tag_def.valid_value?(-1)     # => false

Parameters:

  • value (Object)

    The value to validate

  • transformers (Hash<Symbol, Proc>) (defaults to: {})

    Hash of transformer name to proc mappings

Returns:

  • (Boolean)

    true if the value is valid

Since:

  • 0.1.0



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/datadog/statsd/schema/tag_definition.rb', line 109

def valid_value?(value, transformers = {})
  transformed_value = transform_value(value, transformers)

  # Apply type validation
  case type
  when :integer
    return false unless transformed_value.is_a?(Integer) || transformed_value.to_s.match?(/^\d+$/)
  when :string
    # strings are generally permissive
  when :symbol
    # symbols are generally permissive, will be converted
  end

  # Apply custom validation if present
  return validate.call(transformed_value) if validate.is_a?(Proc)

  # Apply value restrictions
  allows_value?(transformed_value)
end

#validateProc?

Custom validation procedure for tag values

Returns:

  • (Proc, nil)

    Custom validation function

Since:

  • 0.1.0



52
# File 'lib/datadog/statsd/schema/tag_definition.rb', line 52

attribute :validate, Types::Any.optional.default(nil)

#valuesArray, ...

Allowed values for this tag (can be Array, Regexp, Proc, or single value)

Returns:

  • (Array, Regexp, Proc, Object, nil)

    Allowed values constraint

Since:

  • 0.1.0



40
# File 'lib/datadog/statsd/schema/tag_definition.rb', line 40

attribute :values, Types::Any.optional.default(nil)