Class: Datadog::Statsd::Schema::SchemaBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/statsd/schema/schema_builder.rb

Overview

Builder class for constructing metric schemas using a DSL Provides a fluent interface for defining namespaces, tags, and metrics

Examples:

Basic schema building

builder = SchemaBuilder.new
builder.namespace :web do
  tags do
    tag :controller, values: %w[users posts]
  end
  metrics do
    counter :page_views, tags: { required: [:controller] }
  end
end
schema = builder.build

Since:

  • 0.1.0

Defined Under Namespace

Classes: MetricBuilder, MetricsBuilder, NamespaceBuilder, TagsBuilder, TransformerBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchemaBuilder

Initialize a new schema builder

Since:

  • 0.1.0



38
39
40
41
# File 'lib/datadog/statsd/schema/schema_builder.rb', line 38

def initialize
  @transformers = {}
  @root_namespace = Namespace.new(name: :root)
end

Instance Attribute Details

#root_namespaceNamespace (readonly)

The root namespace of the schema being built

Returns:

Since:

  • 0.1.0



35
36
37
# File 'lib/datadog/statsd/schema/schema_builder.rb', line 35

def root_namespace
  @root_namespace
end

#transformers {|TransformerBuilder| ... } ⇒ Hash<Symbol, Proc> (readonly)

Define transformers that can be used by tag definitions

Examples:

builder.transformers do
  underscore { |value| value.to_s.underscore }
  downcase { |value| value.to_s.downcase }
end

Yields:

Returns:

  • (Hash<Symbol, Proc>)

    Hash of defined transformers

Since:

  • 0.1.0



31
32
33
# File 'lib/datadog/statsd/schema/schema_builder.rb', line 31

def transformers
  @transformers
end

Instance Method Details

#buildNamespace

Build the final schema (returns the root namespace)

Returns:

  • (Namespace)

    The root namespace containing the entire schema

Since:

  • 0.1.0



76
77
78
# File 'lib/datadog/statsd/schema/schema_builder.rb', line 76

def build
  @root_namespace
end

#namespace(name) {|NamespaceBuilder| ... } ⇒ void

This method returns an undefined value.

Define a namespace

Examples:

builder.namespace :web do
  description "Web application metrics"
  # ... tags and metrics definitions
end

Parameters:

  • name (Symbol)

    Name of the namespace

Yields:

Since:

  • 0.1.0



66
67
68
69
70
71
72
# File 'lib/datadog/statsd/schema/schema_builder.rb', line 66

def namespace(name, &)
  builder = NamespaceBuilder.new(name, @transformers)
  builder.instance_eval(&) if block_given?
  namespace_def = builder.build

  @root_namespace = @root_namespace.add_namespace(namespace_def)
end

#validate!void

This method returns an undefined value.

Validate the schema for consistency

Raises:

Since:

  • 0.1.0



83
84
85
86
# File 'lib/datadog/statsd/schema/schema_builder.rb', line 83

def validate!
  errors = @root_namespace.validate_tag_references
  raise SchemaError, "Schema validation failed: #{errors.join(", ")}" unless errors.empty?
end