datadog-statsd-schema 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.envrc +2 -0
- data/.rspec +2 -1
- data/.rubocop.yml +10 -0
- data/.rubocop_todo.yml +32 -47
- data/FUTURE_DIRECTION.md +32 -0
- data/README.md +309 -258
- data/Rakefile +7 -7
- data/examples/schema/example_marathon.rb +29 -0
- data/examples/shared.rb +1 -1
- data/exe/dss +8 -0
- data/lib/datadog/statsd/emitter.rb +102 -21
- data/lib/datadog/statsd/schema/analyzer.rb +397 -0
- data/lib/datadog/statsd/schema/cli.rb +16 -0
- data/lib/datadog/statsd/schema/commands/analyze.rb +52 -0
- data/lib/datadog/statsd/schema/commands.rb +14 -0
- data/lib/datadog/statsd/schema/errors.rb +54 -1
- data/lib/datadog/statsd/schema/metric_definition.rb +86 -3
- data/lib/datadog/statsd/schema/namespace.rb +91 -5
- data/lib/datadog/statsd/schema/schema_builder.rb +162 -4
- data/lib/datadog/statsd/schema/tag_definition.rb +66 -6
- data/lib/datadog/statsd/schema/version.rb +6 -1
- data/lib/datadog/statsd/schema.rb +91 -13
- metadata +25 -4
- data/exe/datadog-statsd-schema +0 -3
@@ -4,18 +4,50 @@ require_relative "namespace"
|
|
4
4
|
require_relative "tag_definition"
|
5
5
|
require_relative "metric_definition"
|
6
6
|
|
7
|
+
# @author Datadog Team
|
8
|
+
# @since 0.1.0
|
7
9
|
module Datadog
|
8
10
|
class Statsd
|
11
|
+
# Schema definition and validation module for StatsD metrics
|
9
12
|
module Schema
|
13
|
+
# Builder class for constructing metric schemas using a DSL
|
14
|
+
# Provides a fluent interface for defining namespaces, tags, and metrics
|
15
|
+
# @example Basic schema building
|
16
|
+
# builder = SchemaBuilder.new
|
17
|
+
# builder.namespace :web do
|
18
|
+
# tags do
|
19
|
+
# tag :controller, values: %w[users posts]
|
20
|
+
# end
|
21
|
+
# metrics do
|
22
|
+
# counter :page_views, tags: { required: [:controller] }
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
# schema = builder.build
|
26
|
+
# @author Datadog Team
|
27
|
+
# @since 0.1.0
|
10
28
|
class SchemaBuilder
|
11
|
-
|
29
|
+
# Hash of transformer functions available for tag transformations
|
30
|
+
# @return [Hash<Symbol, Proc>] Transformer name to proc mapping
|
31
|
+
attr_reader :transformers
|
12
32
|
|
33
|
+
# The root namespace of the schema being built
|
34
|
+
# @return [Namespace] Root namespace instance
|
35
|
+
attr_reader :root_namespace
|
36
|
+
|
37
|
+
# Initialize a new schema builder
|
13
38
|
def initialize
|
14
39
|
@transformers = {}
|
15
40
|
@root_namespace = Namespace.new(name: :root)
|
16
41
|
end
|
17
42
|
|
18
43
|
# Define transformers that can be used by tag definitions
|
44
|
+
# @yield [TransformerBuilder] Block for defining transformers
|
45
|
+
# @return [Hash<Symbol, Proc>] Hash of defined transformers
|
46
|
+
# @example
|
47
|
+
# builder.transformers do
|
48
|
+
# underscore { |value| value.to_s.underscore }
|
49
|
+
# downcase { |value| value.to_s.downcase }
|
50
|
+
# end
|
19
51
|
def transformers(&)
|
20
52
|
return @transformers unless block_given?
|
21
53
|
|
@@ -23,6 +55,14 @@ module Datadog
|
|
23
55
|
end
|
24
56
|
|
25
57
|
# Define a namespace
|
58
|
+
# @param name [Symbol] Name of the namespace
|
59
|
+
# @yield [NamespaceBuilder] Block for defining namespace contents
|
60
|
+
# @return [void]
|
61
|
+
# @example
|
62
|
+
# builder.namespace :web do
|
63
|
+
# description "Web application metrics"
|
64
|
+
# # ... tags and metrics definitions
|
65
|
+
# end
|
26
66
|
def namespace(name, &)
|
27
67
|
builder = NamespaceBuilder.new(name, @transformers)
|
28
68
|
builder.instance_eval(&) if block_given?
|
@@ -32,35 +72,76 @@ module Datadog
|
|
32
72
|
end
|
33
73
|
|
34
74
|
# Build the final schema (returns the root namespace)
|
75
|
+
# @return [Namespace] The root namespace containing the entire schema
|
35
76
|
def build
|
36
77
|
@root_namespace
|
37
78
|
end
|
38
79
|
|
39
80
|
# Validate the schema for consistency
|
81
|
+
# @raise [SchemaError] If schema validation fails
|
82
|
+
# @return [void]
|
40
83
|
def validate!
|
41
84
|
errors = @root_namespace.validate_tag_references
|
42
85
|
raise SchemaError, "Schema validation failed: #{errors.join(", ")}" unless errors.empty?
|
43
86
|
end
|
44
87
|
|
45
|
-
# Helper class for building transformers
|
88
|
+
# Helper class for building transformers within the DSL
|
89
|
+
# @api private
|
46
90
|
class TransformerBuilder
|
91
|
+
# Initialize with the transformers hash
|
92
|
+
# @param transformers [Hash] Hash to store transformer definitions
|
47
93
|
def initialize(transformers)
|
48
94
|
@transformers = transformers
|
49
95
|
end
|
50
96
|
|
97
|
+
# Dynamic method to define transformers
|
98
|
+
# @param name [Symbol] Name of the transformer
|
99
|
+
# @param proc [Proc] Transformer procedure (alternative to block)
|
100
|
+
# @yield [Object] Value to transform
|
101
|
+
# @return [void]
|
51
102
|
def method_missing(name, proc = nil, &block)
|
52
103
|
@transformers[name.to_sym] = proc || block
|
53
104
|
end
|
54
105
|
|
106
|
+
# Always respond to any method for transformer definition
|
107
|
+
# @param _name [Symbol] Method name
|
108
|
+
# @param _include_private [Boolean] Whether to include private methods
|
109
|
+
# @return [Boolean] Always true
|
55
110
|
def respond_to_missing?(_name, _include_private = false)
|
56
111
|
true
|
57
112
|
end
|
58
113
|
end
|
59
114
|
|
60
|
-
# Helper class for building namespaces
|
115
|
+
# Helper class for building namespaces within the DSL
|
116
|
+
# @api private
|
61
117
|
class NamespaceBuilder
|
62
|
-
|
118
|
+
# Name of the namespace being built
|
119
|
+
# @return [Symbol] Namespace name
|
120
|
+
attr_reader :name
|
121
|
+
|
122
|
+
# Available transformers for tags
|
123
|
+
# @return [Hash<Symbol, Proc>] Transformer definitions
|
124
|
+
attr_reader :transformers
|
125
|
+
|
126
|
+
# Tag definitions for this namespace
|
127
|
+
# @return [Hash<Symbol, TagDefinition>] Tag definitions
|
128
|
+
attr_reader :tags
|
129
|
+
|
130
|
+
# Metric definitions for this namespace
|
131
|
+
# @return [Hash<Symbol, MetricDefinition>] Metric definitions
|
132
|
+
attr_reader :metrics
|
133
|
+
|
134
|
+
# Nested namespaces
|
135
|
+
# @return [Hash<Symbol, Namespace>] Nested namespace definitions
|
136
|
+
attr_reader :namespaces
|
137
|
+
|
138
|
+
# Description of this namespace
|
139
|
+
# @return [String, nil] Description text
|
140
|
+
attr_reader :description
|
63
141
|
|
142
|
+
# Initialize a new namespace builder
|
143
|
+
# @param name [Symbol] Name of the namespace
|
144
|
+
# @param transformers [Hash] Available transformer functions
|
64
145
|
def initialize(name, transformers = {})
|
65
146
|
@name = name.to_sym
|
66
147
|
@transformers = transformers
|
@@ -71,27 +152,48 @@ module Datadog
|
|
71
152
|
end
|
72
153
|
|
73
154
|
# Set description for this namespace
|
155
|
+
# @param desc [String] Description text
|
156
|
+
# @return [void]
|
74
157
|
def description(desc)
|
75
158
|
@description = desc
|
76
159
|
end
|
77
160
|
|
78
161
|
# Define tags for this namespace
|
162
|
+
# @yield [TagsBuilder] Block for defining tags
|
163
|
+
# @return [void]
|
164
|
+
# @example
|
165
|
+
# tags do
|
166
|
+
# tag :controller, values: %w[users posts]
|
167
|
+
# tag :action, values: %w[index show create]
|
168
|
+
# end
|
79
169
|
def tags(&)
|
80
170
|
TagsBuilder.new(@tags, @transformers).instance_eval(&)
|
81
171
|
end
|
82
172
|
|
83
173
|
# Define metrics for this namespace
|
174
|
+
# @yield [MetricsBuilder] Block for defining metrics
|
175
|
+
# @return [void]
|
176
|
+
# @example
|
177
|
+
# metrics do
|
178
|
+
# counter :page_views, tags: { required: [:controller] }
|
179
|
+
# gauge :memory_usage
|
180
|
+
# end
|
84
181
|
def metrics(&)
|
85
182
|
MetricsBuilder.new(@metrics, @transformers).instance_eval(&)
|
86
183
|
end
|
87
184
|
|
88
185
|
# Define nested namespace
|
186
|
+
# @param name [Symbol] Name of the nested namespace
|
187
|
+
# @yield [NamespaceBuilder] Block for defining nested namespace
|
188
|
+
# @return [void]
|
89
189
|
def namespace(name, &)
|
90
190
|
builder = NamespaceBuilder.new(name, @transformers)
|
91
191
|
builder.instance_eval(&) if block_given?
|
92
192
|
@namespaces[name.to_sym] = builder.build
|
93
193
|
end
|
94
194
|
|
195
|
+
# Build the namespace instance
|
196
|
+
# @return [Namespace] The constructed namespace
|
95
197
|
def build
|
96
198
|
Namespace.new(
|
97
199
|
name: @name,
|
@@ -104,12 +206,27 @@ module Datadog
|
|
104
206
|
end
|
105
207
|
|
106
208
|
# Helper class for building tags within a namespace
|
209
|
+
# @api private
|
107
210
|
class TagsBuilder
|
211
|
+
# Initialize with tags hash and transformers
|
212
|
+
# @param tags [Hash] Hash to store tag definitions
|
213
|
+
# @param transformers [Hash] Available transformer functions
|
108
214
|
def initialize(tags, transformers)
|
109
215
|
@tags = tags
|
110
216
|
@transformers = transformers
|
111
217
|
end
|
112
218
|
|
219
|
+
# Define a tag
|
220
|
+
# @param name [Symbol] Name of the tag
|
221
|
+
# @param options [Hash] Tag options
|
222
|
+
# @option options [Array, Regexp, Proc] :values Allowed values
|
223
|
+
# @option options [Symbol] :type Data type (:string, :integer, :symbol)
|
224
|
+
# @option options [Array<Symbol>] :transform Transformation functions to apply
|
225
|
+
# @option options [Proc] :validate Custom validation function
|
226
|
+
# @return [void]
|
227
|
+
# @example
|
228
|
+
# tag :controller, values: %w[users posts], type: :string
|
229
|
+
# tag :status_code, type: :integer, validate: ->(code) { (100..599).include?(code) }
|
113
230
|
def tag(name, **options)
|
114
231
|
tag_def = TagDefinition.new(
|
115
232
|
name: name.to_sym,
|
@@ -124,7 +241,11 @@ module Datadog
|
|
124
241
|
end
|
125
242
|
|
126
243
|
# Helper class for building metrics within a namespace
|
244
|
+
# @api private
|
127
245
|
class MetricsBuilder
|
246
|
+
# Initialize with metrics hash and transformers
|
247
|
+
# @param metrics [Hash] Hash to store metric definitions
|
248
|
+
# @param transformers [Hash] Available transformer functions
|
128
249
|
def initialize(metrics, transformers)
|
129
250
|
@metrics = metrics
|
130
251
|
@transformers = transformers
|
@@ -132,6 +253,9 @@ module Datadog
|
|
132
253
|
end
|
133
254
|
|
134
255
|
# Define a nested namespace for metrics
|
256
|
+
# @param name [Symbol] Namespace name
|
257
|
+
# @yield Block for defining metrics within the namespace
|
258
|
+
# @return [void]
|
135
259
|
def namespace(name, &)
|
136
260
|
@current_namespace = name.to_sym
|
137
261
|
instance_eval(&)
|
@@ -140,6 +264,15 @@ module Datadog
|
|
140
264
|
|
141
265
|
# Define individual metric types
|
142
266
|
%i[counter gauge histogram distribution timing set].each do |metric_type|
|
267
|
+
# Define a metric of the specified type
|
268
|
+
# @param name [Symbol] Metric name
|
269
|
+
# @param options [Hash] Metric options
|
270
|
+
# @option options [String] :description Human-readable description
|
271
|
+
# @option options [Hash, Array] :tags Tag configuration
|
272
|
+
# @option options [String] :inherit_tags Path to metric to inherit from
|
273
|
+
# @option options [String] :units Unit of measurement
|
274
|
+
# @yield [MetricBuilder] Block for additional metric configuration
|
275
|
+
# @return [void]
|
143
276
|
define_method(metric_type) do |name, **options, &block|
|
144
277
|
metric_name = @current_namespace ? :"#{@current_namespace}_#{name}" : name.to_sym
|
145
278
|
|
@@ -166,6 +299,9 @@ module Datadog
|
|
166
299
|
|
167
300
|
private
|
168
301
|
|
302
|
+
# Extract allowed tags from options
|
303
|
+
# @param options [Hash] Metric options
|
304
|
+
# @return [Array<Symbol>] Allowed tag names
|
169
305
|
def extract_allowed_tags(options)
|
170
306
|
tags_option = options[:tags]
|
171
307
|
return [] unless tags_option
|
@@ -177,6 +313,9 @@ module Datadog
|
|
177
313
|
end
|
178
314
|
end
|
179
315
|
|
316
|
+
# Extract required tags from options
|
317
|
+
# @param options [Hash] Metric options
|
318
|
+
# @return [Array<Symbol>] Required tag names
|
180
319
|
def extract_required_tags(options)
|
181
320
|
tags_option = options[:tags]
|
182
321
|
return [] unless tags_option
|
@@ -190,15 +329,26 @@ module Datadog
|
|
190
329
|
end
|
191
330
|
|
192
331
|
# Helper class for building individual metrics with block syntax
|
332
|
+
# @api private
|
193
333
|
class MetricBuilder
|
334
|
+
# Initialize with a metric definition
|
335
|
+
# @param metric_def [MetricDefinition] Initial metric definition
|
194
336
|
def initialize(metric_def)
|
195
337
|
@metric_def = metric_def
|
196
338
|
end
|
197
339
|
|
340
|
+
# Set metric description
|
341
|
+
# @param desc [String] Description text
|
342
|
+
# @return [void]
|
198
343
|
def description(desc)
|
199
344
|
@metric_def = @metric_def.new(description: desc)
|
200
345
|
end
|
201
346
|
|
347
|
+
# Configure metric tags
|
348
|
+
# @param options [Hash] Tag configuration
|
349
|
+
# @option options [Array] :allowed Allowed tag names
|
350
|
+
# @option options [Array] :required Required tag names
|
351
|
+
# @return [void]
|
202
352
|
def tags(**options)
|
203
353
|
allowed = Array(options[:allowed] || []).map(&:to_sym)
|
204
354
|
required = Array(options[:required] || []).map(&:to_sym)
|
@@ -209,14 +359,22 @@ module Datadog
|
|
209
359
|
)
|
210
360
|
end
|
211
361
|
|
362
|
+
# Set metric units
|
363
|
+
# @param unit_name [String] Unit description
|
364
|
+
# @return [void]
|
212
365
|
def units(unit_name)
|
213
366
|
@metric_def = @metric_def.new(units: unit_name)
|
214
367
|
end
|
215
368
|
|
369
|
+
# Set metric inheritance
|
370
|
+
# @param metric_path [String] Path to parent metric
|
371
|
+
# @return [void]
|
216
372
|
def inherit_tags(metric_path)
|
217
373
|
@metric_def = @metric_def.new(inherit_tags: metric_path)
|
218
374
|
end
|
219
375
|
|
376
|
+
# Build the final metric definition
|
377
|
+
# @return [MetricDefinition] The constructed metric definition
|
220
378
|
def build
|
221
379
|
@metric_def
|
222
380
|
end
|
@@ -3,23 +3,65 @@
|
|
3
3
|
require "dry-struct"
|
4
4
|
require "dry-types"
|
5
5
|
|
6
|
+
# @author Datadog Team
|
7
|
+
# @since 0.1.0
|
6
8
|
module Datadog
|
7
9
|
class Statsd
|
10
|
+
# Schema definition and validation module for StatsD metrics
|
8
11
|
module Schema
|
12
|
+
# Represents a tag definition within a schema namespace
|
13
|
+
# Defines validation rules, allowed values, transformations, and type constraints for tags
|
14
|
+
# @example Basic tag definition
|
15
|
+
# tag_def = TagDefinition.new(
|
16
|
+
# name: :environment,
|
17
|
+
# values: [:production, :staging, :development],
|
18
|
+
# type: :symbol
|
19
|
+
# )
|
20
|
+
# @example Tag with custom validation
|
21
|
+
# tag_def = TagDefinition.new(
|
22
|
+
# name: :user_id,
|
23
|
+
# type: :integer,
|
24
|
+
# validate: ->(value) { value > 0 }
|
25
|
+
# )
|
26
|
+
# @author Datadog Team
|
27
|
+
# @since 0.1.0
|
9
28
|
class TagDefinition < Dry::Struct
|
10
|
-
# Include the types module for easier access
|
29
|
+
# Include the types module for easier access to Dry::Types
|
11
30
|
module Types
|
12
31
|
include Dry.Types()
|
13
32
|
end
|
14
33
|
|
34
|
+
# The tag name as a symbol
|
35
|
+
# @return [Symbol] Tag name
|
15
36
|
attribute :name, Types::Strict::Symbol
|
16
|
-
|
37
|
+
|
38
|
+
# Allowed values for this tag (can be Array, Regexp, Proc, or single value)
|
39
|
+
# @return [Array, Regexp, Proc, Object, nil] Allowed values constraint
|
40
|
+
attribute :values, Types::Any.optional.default(nil)
|
41
|
+
|
42
|
+
# The expected data type for tag values
|
43
|
+
# @return [Symbol] Type constraint (:string, :integer, :symbol)
|
17
44
|
attribute :type, Types::Strict::Symbol.default(:string)
|
45
|
+
|
46
|
+
# Array of transformation functions to apply to values
|
47
|
+
# @return [Array<Symbol>] Transformation function names
|
18
48
|
attribute :transform, Types::Array.of(Types::Symbol).default([].freeze)
|
19
|
-
|
49
|
+
|
50
|
+
# Custom validation procedure for tag values
|
51
|
+
# @return [Proc, nil] Custom validation function
|
52
|
+
attribute :validate, Types::Any.optional.default(nil)
|
53
|
+
|
54
|
+
# The namespace this tag belongs to
|
55
|
+
# @return [Symbol, nil] Namespace name
|
20
56
|
attribute :namespace, Types::Strict::Symbol.optional.default(nil)
|
21
57
|
|
22
|
-
# Check if a value is allowed for this tag
|
58
|
+
# Check if a value is allowed for this tag according to the values constraint
|
59
|
+
# @param value [Object] The value to check
|
60
|
+
# @return [Boolean] true if the value is allowed
|
61
|
+
# @example
|
62
|
+
# tag_def = TagDefinition.new(name: :env, values: [:prod, :dev])
|
63
|
+
# tag_def.allows_value?(:prod) # => true
|
64
|
+
# tag_def.allows_value?(:test) # => false
|
23
65
|
def allows_value?(value)
|
24
66
|
return true if values.nil? # No restrictions
|
25
67
|
|
@@ -35,7 +77,14 @@ module Datadog
|
|
35
77
|
end
|
36
78
|
end
|
37
79
|
|
38
|
-
# Apply transformations to a value
|
80
|
+
# Apply transformations to a value using the provided transformer functions
|
81
|
+
# @param value [Object] The value to transform
|
82
|
+
# @param transformers [Hash<Symbol, Proc>] Hash of transformer name to proc mappings
|
83
|
+
# @return [Object] The transformed value
|
84
|
+
# @example
|
85
|
+
# tag_def = TagDefinition.new(name: :service, transform: [:downcase])
|
86
|
+
# transformers = { downcase: ->(val) { val.to_s.downcase } }
|
87
|
+
# tag_def.transform_value("WEB-SERVICE", transformers) # => "web-service"
|
39
88
|
def transform_value(value, transformers = {})
|
40
89
|
return value if transform.empty?
|
41
90
|
|
@@ -45,7 +94,18 @@ module Datadog
|
|
45
94
|
end
|
46
95
|
end
|
47
96
|
|
48
|
-
# Validate a value using custom validation
|
97
|
+
# Validate a value using type checking, transformations, and custom validation
|
98
|
+
# @param value [Object] The value to validate
|
99
|
+
# @param transformers [Hash<Symbol, Proc>] Hash of transformer name to proc mappings
|
100
|
+
# @return [Boolean] true if the value is valid
|
101
|
+
# @example
|
102
|
+
# tag_def = TagDefinition.new(
|
103
|
+
# name: :port,
|
104
|
+
# type: :integer,
|
105
|
+
# validate: ->(val) { val > 0 && val < 65536 }
|
106
|
+
# )
|
107
|
+
# tag_def.valid_value?(8080) # => true
|
108
|
+
# tag_def.valid_value?(-1) # => false
|
49
109
|
def valid_value?(value, transformers = {})
|
50
110
|
transformed_value = transform_value(value, transformers)
|
51
111
|
|
@@ -1,9 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# @author Datadog Team
|
4
|
+
# @since 0.1.0
|
3
5
|
module Datadog
|
4
6
|
class Statsd
|
7
|
+
# Schema definition and validation module for StatsD metrics
|
5
8
|
module Schema
|
6
|
-
|
9
|
+
# Current version of the datadog-statsd-schema gem
|
10
|
+
# @return [String] The semantic version string
|
11
|
+
VERSION = "0.2.0"
|
7
12
|
end
|
8
13
|
end
|
9
14
|
end
|
@@ -9,57 +9,135 @@ require_relative "schema/tag_definition"
|
|
9
9
|
require_relative "schema/metric_definition"
|
10
10
|
require_relative "schema/namespace"
|
11
11
|
require_relative "schema/schema_builder"
|
12
|
+
require_relative "schema/analyzer"
|
13
|
+
require_relative "schema/cli"
|
12
14
|
require_relative "emitter"
|
13
15
|
|
16
|
+
# @author Konstantin Gredeskoul @ https://github.com/kigster
|
17
|
+
# @since 0.1.0
|
18
|
+
# @see https://github.com/DataDog/dogstatsd-ruby
|
14
19
|
module Datadog
|
15
|
-
class
|
16
|
-
|
17
|
-
|
18
|
-
|
20
|
+
# Main StatsD client class that provides factory methods for creating emitters and schemas
|
21
|
+
# @see Datadog::Statsd::Emitter
|
22
|
+
# @see Datadog::Statsd::Schema
|
23
|
+
class Statsd
|
24
|
+
class << self
|
25
|
+
# Factory method to create a new Emitter instance
|
26
|
+
# @param args [Array] Arguments passed to Emitter.new
|
27
|
+
# @return [Datadog::Statsd::Emitter] A new emitter instance
|
28
|
+
# @see Datadog::Statsd::Emitter#initialize
|
29
|
+
def emitter(...)
|
30
|
+
::Datadog::Statsd::Emitter.new(...)
|
31
|
+
end
|
19
32
|
|
20
|
-
|
21
|
-
|
33
|
+
# Factory method to create a new Schema instance
|
34
|
+
# @param block [Proc] Block to define the schema structure
|
35
|
+
# @return [Datadog::Statsd::Schema::Namespace] A new schema namespace
|
36
|
+
# @see Datadog::Statsd::Schema.new
|
37
|
+
def schema(...)
|
38
|
+
::Datadog::Statsd::Schema.new(...)
|
39
|
+
end
|
22
40
|
end
|
23
|
-
end
|
24
41
|
|
25
|
-
|
42
|
+
# Schema definition and validation module for StatsD metrics
|
43
|
+
# Provides a DSL for defining metric schemas with type safety and validation
|
44
|
+
# @example Basic schema definition
|
45
|
+
# schema = Datadog::Statsd::Schema.new do
|
46
|
+
# namespace :web do
|
47
|
+
# tags do
|
48
|
+
# tag :environment, values: [:production, :staging, :development]
|
49
|
+
# tag :service, type: :string
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# metrics do
|
53
|
+
# counter :requests_total, tags: { required: [:environment, :service] }
|
54
|
+
# gauge :memory_usage, tags: { allowed: [:environment] }
|
55
|
+
# end
|
56
|
+
# end
|
57
|
+
# end
|
58
|
+
# @author Datadog Team
|
59
|
+
# @since 0.1.0
|
26
60
|
module Schema
|
61
|
+
# Base error class for all schema-related errors
|
62
|
+
# @see Datadog::Statsd::Schema::SchemaError
|
27
63
|
class Error < StandardError
|
28
64
|
end
|
29
65
|
|
30
66
|
class << self
|
67
|
+
# Controls whether the schema is in test mode
|
68
|
+
# When true, colored output is disabled for test environments
|
69
|
+
# @return [Boolean] Test mode flag
|
31
70
|
attr_accessor :in_test
|
32
71
|
end
|
33
72
|
|
34
73
|
self.in_test = false
|
35
74
|
|
36
|
-
# Create a new schema definition
|
75
|
+
# Create a new schema definition using the provided block
|
76
|
+
# @param block [Proc] Block containing schema definition DSL
|
77
|
+
# @return [Datadog::Statsd::Schema::Namespace] Root namespace of the schema
|
78
|
+
# @example
|
79
|
+
# schema = Datadog::Statsd::Schema.new do
|
80
|
+
# namespace :app do
|
81
|
+
# tags do
|
82
|
+
# tag :env, values: [:prod, :dev]
|
83
|
+
# end
|
84
|
+
# metrics do
|
85
|
+
# counter :requests
|
86
|
+
# end
|
87
|
+
# end
|
88
|
+
# end
|
37
89
|
def self.new(&)
|
38
90
|
builder = SchemaBuilder.new
|
39
91
|
builder.instance_eval(&) if block_given?
|
40
92
|
builder.build
|
41
93
|
end
|
42
94
|
|
43
|
-
# Load schema from a file
|
95
|
+
# Load schema definition from a file
|
96
|
+
# @param path [String] Path to the schema definition file
|
97
|
+
# @return [Datadog::Statsd::Schema::Namespace] Root namespace of the loaded schema
|
98
|
+
# @raise [Errno::ENOENT] If the file doesn't exist
|
99
|
+
# @example
|
100
|
+
# schema = Datadog::Statsd::Schema.load_file("config/metrics_schema.rb")
|
44
101
|
def self.load_file(path)
|
45
102
|
builder = SchemaBuilder.new
|
46
103
|
builder.instance_eval(File.read(path), path)
|
47
104
|
builder.build
|
48
105
|
end
|
49
106
|
|
50
|
-
# Configure
|
107
|
+
# Configure global schema settings
|
108
|
+
# @param block [Proc] Configuration block
|
109
|
+
# @yield [Configuration] Configuration object for setting global options
|
110
|
+
# @example
|
111
|
+
# Datadog::Statsd::Schema.configure do |config|
|
112
|
+
# config.statsd = Datadog::Statsd.new('localhost', 8125)
|
113
|
+
# config.tags = { environment: 'production' }
|
114
|
+
# end
|
51
115
|
def self.configure
|
52
116
|
yield configuration
|
53
117
|
end
|
54
118
|
|
119
|
+
# Get the global configuration object
|
120
|
+
# @return [Datadog::Statsd::Schema::Configuration] Global configuration instance
|
55
121
|
def self.configuration
|
56
122
|
@configuration ||= Configuration.new
|
57
123
|
end
|
58
124
|
|
59
|
-
#
|
125
|
+
# Global configuration class for schema settings
|
126
|
+
# Manages global StatsD client instance, schema, and tags
|
60
127
|
class Configuration
|
61
|
-
|
128
|
+
# Global StatsD client instance
|
129
|
+
# @return [Datadog::Statsd, nil] StatsD client or nil if not configured
|
130
|
+
attr_accessor :statsd
|
131
|
+
|
132
|
+
# Global schema instance
|
133
|
+
# @return [Datadog::Statsd::Schema::Namespace, nil] Schema or nil if not configured
|
134
|
+
attr_accessor :schema
|
135
|
+
|
136
|
+
# Global tags to be applied to all metrics
|
137
|
+
# @return [Hash] Hash of global tags
|
138
|
+
attr_accessor :tags
|
62
139
|
|
140
|
+
# Initialize a new configuration with default values
|
63
141
|
def initialize
|
64
142
|
@statsd = nil
|
65
143
|
@schema = nil
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: datadog-statsd-schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Konstantin Gredeskoul
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-
|
10
|
+
date: 2025-06-07 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: activesupport
|
@@ -51,6 +51,20 @@ dependencies:
|
|
51
51
|
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
53
|
version: '0'
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: dry-cli
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
type: :runtime
|
62
|
+
prerelease: false
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
54
68
|
- !ruby/object:Gem::Dependency
|
55
69
|
name: dry-schema
|
56
70
|
requirement: !ruby/object:Gem::Requirement
|
@@ -144,25 +158,32 @@ description: This gem is an adapter for the dogstatsd-ruby gem. Unlike the Datad
|
|
144
158
|
email:
|
145
159
|
- kigster@gmail.com
|
146
160
|
executables:
|
147
|
-
-
|
161
|
+
- dss
|
148
162
|
extensions: []
|
149
163
|
extra_rdoc_files: []
|
150
164
|
files:
|
165
|
+
- ".envrc"
|
151
166
|
- ".rspec"
|
152
167
|
- ".rubocop.yml"
|
153
168
|
- ".rubocop_todo.yml"
|
154
169
|
- CHANGELOG.md
|
170
|
+
- FUTURE_DIRECTION.md
|
155
171
|
- LICENSE.txt
|
156
172
|
- README.md
|
157
173
|
- Rakefile
|
158
174
|
- examples/README.md
|
175
|
+
- examples/schema/example_marathon.rb
|
159
176
|
- examples/schema_emitter.png
|
160
177
|
- examples/schema_emitter.rb
|
161
178
|
- examples/shared.rb
|
162
179
|
- examples/simple_emitter.rb
|
163
|
-
- exe/
|
180
|
+
- exe/dss
|
164
181
|
- lib/datadog/statsd/emitter.rb
|
165
182
|
- lib/datadog/statsd/schema.rb
|
183
|
+
- lib/datadog/statsd/schema/analyzer.rb
|
184
|
+
- lib/datadog/statsd/schema/cli.rb
|
185
|
+
- lib/datadog/statsd/schema/commands.rb
|
186
|
+
- lib/datadog/statsd/schema/commands/analyze.rb
|
166
187
|
- lib/datadog/statsd/schema/errors.rb
|
167
188
|
- lib/datadog/statsd/schema/metric_definition.rb
|
168
189
|
- lib/datadog/statsd/schema/namespace.rb
|
data/exe/datadog-statsd-schema
DELETED