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
data/README.md
CHANGED
@@ -1,359 +1,410 @@
|
|
1
1
|
[](https://github.com/kigster/datadog-statsd-schema/actions/workflows/ruby.yml)
|
2
2
|
|
3
|
-
# Datadog
|
3
|
+
# Datadog StatsD Schema
|
4
4
|
|
5
|
-
|
5
|
+
A Ruby gem that provides comprehensive schema definition, validation, and cost analysis for Datadog StatsD metrics. This library helps teams prevent metric explosion, control costs, and maintain consistent metric naming conventions.
|
6
6
|
|
7
|
-
|
7
|
+
## Features
|
8
8
|
|
9
|
-
|
9
|
+
- **Schema Definition**: Define metric schemas with type safety and validation
|
10
|
+
- **Tag Management**: Centralized tag definitions with inheritance and validation
|
11
|
+
- **Cost Analysis**: Analyze potential custom metric costs before deployment
|
12
|
+
- **Metric Validation**: Runtime validation of metrics against defined schemas
|
13
|
+
- **CLI Tools**: Command-line interface for schema analysis and validation
|
14
|
+
- **Global Configuration**: Centralized configuration for tags and StatsD clients
|
10
15
|
|
11
|
-
|
16
|
+
## Installation
|
12
17
|
|
13
|
-
|
18
|
+
Add this line to your application's Gemfile:
|
14
19
|
|
15
|
-
|
20
|
+
```ruby
|
21
|
+
gem 'datadog-statsd-schema'
|
22
|
+
```
|
16
23
|
|
17
|
-
|
18
|
-
|
19
|
-
## Metric Types
|
24
|
+
And then execute:
|
20
25
|
|
21
|
-
|
26
|
+
```bash
|
27
|
+
bundle install
|
28
|
+
```
|
22
29
|
|
23
|
-
|
30
|
+
Or install it yourself as:
|
24
31
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
* `DISTRIBUTION` (eg,`Datadog::Statsd::Emitter.distribution('page.load.time', 100)`)
|
29
|
-
* `SET` (eg, `Datadog::Statsd::Emitter.set('users.unique', '12345')`)
|
32
|
+
```bash
|
33
|
+
gem install datadog-statsd-schema
|
34
|
+
```
|
30
35
|
|
31
|
-
|
36
|
+
## Quick Start
|
32
37
|
|
33
|
-
|
38
|
+
### Basic Schema Definition
|
34
39
|
|
35
|
-
|
40
|
+
```ruby
|
41
|
+
require 'datadog/statsd/schema'
|
42
|
+
|
43
|
+
# Define your metrics schema
|
44
|
+
schema = Datadog::Statsd::Schema.new do
|
45
|
+
namespace :web do
|
46
|
+
tags do
|
47
|
+
tag :environment, values: %w[production staging development]
|
48
|
+
tag :service, values: %w[api web worker]
|
49
|
+
tag :region, values: %w[us-east-1 us-west-2]
|
50
|
+
end
|
36
51
|
|
37
|
-
|
52
|
+
metrics do
|
53
|
+
counter :requests_total do
|
54
|
+
description "Total HTTP requests"
|
55
|
+
tags required: [:environment, :service], allowed: [:region]
|
56
|
+
end
|
38
57
|
|
39
|
-
|
58
|
+
gauge :memory_usage do
|
59
|
+
description "Memory usage in bytes"
|
60
|
+
tags required: [:environment], allowed: [:service, :region]
|
61
|
+
end
|
40
62
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
tags: {
|
49
|
-
course: "sf-marathon",
|
50
|
-
length: 26.212,
|
51
|
-
units: "miles"
|
52
|
-
},
|
53
|
-
schema: ....
|
54
|
-
)
|
63
|
+
distribution :request_duration do
|
64
|
+
description "Request processing time in milliseconds"
|
65
|
+
tags required: [:environment, :service]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
55
70
|
```
|
56
71
|
|
57
|
-
|
72
|
+
### Using the Emitter with Schema Validation
|
58
73
|
|
59
|
-
|
74
|
+
```ruby
|
75
|
+
# Configure global settings
|
76
|
+
Datadog::Statsd::Schema.configure do |config|
|
77
|
+
config.statsd = Datadog::Statsd.new('localhost', 8125)
|
78
|
+
config.schema = schema
|
79
|
+
config.tags = { environment: 'production' }
|
80
|
+
end
|
81
|
+
|
82
|
+
# Create an emitter with validation
|
83
|
+
emitter = Datadog::Statsd::Emitter.new(
|
84
|
+
schema: schema,
|
85
|
+
validation_mode: :strict # :strict, :warn, or :disabled
|
86
|
+
)
|
60
87
|
|
61
|
-
|
88
|
+
# Send metrics with automatic validation
|
89
|
+
emitter.increment('web.requests_total', tags: { service: 'api', region: 'us-east-1' })
|
90
|
+
emitter.gauge('web.memory_usage', 512_000_000, tags: { service: 'api' })
|
91
|
+
emitter.distribution('web.request_duration', 45.2, tags: { service: 'api' })
|
92
|
+
```
|
62
93
|
|
63
|
-
|
94
|
+
## CLI Usage
|
64
95
|
|
65
|
-
|
66
|
-
require 'etc'
|
67
|
-
require 'git'
|
96
|
+
The gem provides a command-line interface for analyzing schemas and understanding their cost implications.
|
68
97
|
|
69
|
-
|
70
|
-
require 'datadog/statsd/schema'
|
98
|
+
### Installation
|
71
99
|
|
72
|
-
|
73
|
-
$statsd = ::Datadog::Statsd.new(
|
74
|
-
'localhost', 8125,
|
75
|
-
delay_serialization: true
|
76
|
-
)
|
100
|
+
After installing the gem, the `dss` (Datadog StatsD Schema) command will be available:
|
77
101
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
config.tags = {
|
82
|
-
env: "development",
|
83
|
-
arch: Etc.uname[:machine],
|
84
|
-
version: Git.open('.').object('HEAD').sha
|
85
|
-
}
|
86
|
-
|
87
|
-
config.statsd = $statsd
|
88
|
-
end
|
102
|
+
```bash
|
103
|
+
dss --help
|
104
|
+
```
|
89
105
|
|
90
|
-
|
91
|
-
schema = Datadog.schema do
|
92
|
-
# Transformers can be attached to the tags, and applied before the tags are submitted
|
93
|
-
# or validated.
|
94
|
-
transformers do
|
95
|
-
underscore: ->(text) { text.underscore },
|
96
|
-
downcase: ->(text) { text.downcase }
|
97
|
-
end
|
106
|
+
### Schema Analysis
|
98
107
|
|
99
|
-
|
100
|
-
tags do
|
101
|
-
tag :course,
|
102
|
-
values: ["san francisco", "boston", "new york"],
|
103
|
-
transform: %i[downcase underscore],
|
108
|
+
Create a schema file (e.g., `metrics_schema.rb`):
|
104
109
|
|
105
|
-
|
106
|
-
|
107
|
-
|
110
|
+
```ruby
|
111
|
+
namespace :web do
|
112
|
+
tags do
|
113
|
+
tag :environment, values: %w[production staging development]
|
114
|
+
tag :service, values: %w[api web worker]
|
115
|
+
tag :region, values: %w[us-east-1 us-west-2 eu-west-1]
|
116
|
+
end
|
117
|
+
|
118
|
+
namespace :requests do
|
119
|
+
metrics do
|
120
|
+
counter :total do
|
121
|
+
description "Total HTTP requests"
|
122
|
+
tags required: [:environment, :service], allowed: [:region]
|
108
123
|
end
|
109
124
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
counter :total do
|
114
|
-
description "Incrementing - the total number of people who were registered for this marathon"
|
115
|
-
tags required: %i[ course marathon_type ],
|
116
|
-
allowed: %i[ sponsorship ]
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
# defines two metrics: a counter metric named "marathon.finished.total" and
|
121
|
-
# a distribution metric "marathon.finished.duration"
|
122
|
-
namespace :finished do
|
123
|
-
counter :total, inherit_tags: "marathon.started.total",
|
124
|
-
description "The number of people who finished a given marathon"
|
125
|
-
tags required: %i[ status ]
|
126
|
-
end
|
127
|
-
|
128
|
-
distribution :duration, units: "minutes", inherit_tags: "marathon.finished.count" do
|
129
|
-
description "The distribution of all finish times registered."
|
130
|
-
end
|
131
|
-
end
|
125
|
+
distribution :duration do
|
126
|
+
description "Request processing time in milliseconds"
|
127
|
+
inherit_tags "web.requests.total"
|
132
128
|
end
|
133
129
|
end
|
134
130
|
end
|
135
131
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
my_sender.increment('started.total', by: 43579) # register all participants at start
|
144
|
-
# time passes, first runners start to arrive
|
145
|
-
my_sender.increment('finished.total') # register one at a time
|
146
|
-
my_sender.distribution('finished.duration', 33.21, tags: { sponsorship: 'nike' })
|
147
|
-
...
|
148
|
-
my_sender.increment('finished.total')
|
149
|
-
my_sender.distribution('finished.duration', 35.09, tags: { sponsorship: "redbull" })
|
132
|
+
metrics do
|
133
|
+
gauge :memory_usage do
|
134
|
+
description "Memory usage in bytes"
|
135
|
+
tags required: [:environment], allowed: [:service]
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
150
139
|
```
|
151
140
|
|
152
|
-
|
141
|
+
Analyze the schema to understand metric costs:
|
153
142
|
|
154
|
-
```
|
155
|
-
|
156
|
-
schema: schema,
|
157
|
-
validation_mode: :warn,
|
158
|
-
metric: "marathon.finished",
|
159
|
-
tags: { marathon_type: :full, course: "san-francisco" }
|
160
|
-
)
|
161
|
-
finish.increment("total")
|
162
|
-
finish.distribution("duration", 34)
|
143
|
+
```bash
|
144
|
+
dss analyze --file metrics_schema.rb
|
163
145
|
```
|
164
146
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
)
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
147
|
+
**Output:**
|
148
|
+
```
|
149
|
+
┌──────────────────────────────────────────────────────────────────────────────────────────────┐
|
150
|
+
│ Detailed Metric Analysis: │
|
151
|
+
└──────────────────────────────────────────────────────────────────────────────────────────────┘
|
152
|
+
|
153
|
+
• gauge('web.memory_usage')
|
154
|
+
Expanded names:
|
155
|
+
• web.memory_usage.count
|
156
|
+
• web.memory_usage.min
|
157
|
+
• web.memory_usage.max
|
158
|
+
• web.memory_usage.sum
|
159
|
+
• web.memory_usage.avg
|
160
|
+
|
161
|
+
Unique tags: 2
|
162
|
+
Total tag values: 6
|
163
|
+
Possible combinations: 45
|
164
|
+
|
165
|
+
──────────────────────────────────────────────────────────────────────────────────────────────
|
166
|
+
|
167
|
+
• counter('web.requests.total')
|
168
|
+
|
169
|
+
Unique tags: 3
|
170
|
+
Total tag values: 9
|
171
|
+
Possible combinations: 27
|
172
|
+
|
173
|
+
──────────────────────────────────────────────────────────────────────────────────────────────
|
174
|
+
|
175
|
+
• distribution('web.requests.duration')
|
176
|
+
Expanded names:
|
177
|
+
• web.requests.duration.count
|
178
|
+
• web.requests.duration.min
|
179
|
+
• web.requests.duration.max
|
180
|
+
• web.requests.duration.sum
|
181
|
+
• web.requests.duration.avg
|
182
|
+
• web.requests.duration.p50
|
183
|
+
• web.requests.duration.p75
|
184
|
+
• web.requests.duration.p90
|
185
|
+
• web.requests.duration.p95
|
186
|
+
• web.requests.duration.p99
|
187
|
+
|
188
|
+
Unique tags: 3
|
189
|
+
Total tag values: 9
|
190
|
+
Possible combinations: 270
|
191
|
+
|
192
|
+
──────────────────────────────────────────────────────────────────────────────────────────────
|
193
|
+
┌──────────────────────────────────────────────────────────────────────────────────────────────┐
|
194
|
+
│ Schema Analysis Results: │
|
195
|
+
│ SUMMARY │
|
196
|
+
└──────────────────────────────────────────────────────────────────────────────────────────────┘
|
197
|
+
|
198
|
+
Total unique metrics: 16
|
199
|
+
Total possible custom metric combinations: 342
|
177
200
|
```
|
178
201
|
|
179
|
-
|
202
|
+
This analysis shows that your schema will generate **342 custom metrics** across **16 unique metric names**. Understanding this before deployment helps prevent unexpected Datadog billing surprises.
|
180
203
|
|
181
|
-
|
204
|
+
## Advanced Features
|
182
205
|
|
183
|
-
|
184
|
-
2. `:warn` — print to stderr and continue
|
185
|
-
3. `:drop` — drop this metric
|
186
|
-
4. `:off` — no validation, as if schema was not even passed.
|
206
|
+
### Tag Inheritance
|
187
207
|
|
188
|
-
|
208
|
+
Metrics can inherit tag configurations from other metrics to reduce duplication:
|
189
209
|
|
190
210
|
```ruby
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
values: %w[logged_in logged_out]
|
202
|
-
|
203
|
-
tag :billing_plan,
|
204
|
-
values: %w[premium trial free]
|
205
|
-
|
206
|
-
tag :controller,
|
207
|
-
values: %r{[a-z.]*},
|
208
|
-
transform: [ :underscore, :downcase ]
|
209
|
-
|
210
|
-
tag :action,
|
211
|
-
values: %r{[a-z.]*},
|
212
|
-
transform: [ :underscore, :downcase ]
|
213
|
-
|
214
|
-
tag :method,
|
215
|
-
values: %i[get post put patch delete head options trace connect],
|
216
|
-
transform: [ :downcase ]
|
217
|
-
|
218
|
-
tag :status_code,
|
219
|
-
type: :integer,
|
220
|
-
validate: ->(code) { (100..599).include?(code) }
|
221
|
-
end
|
222
|
-
|
223
|
-
metrics do
|
224
|
-
# This distribution allows tracking of the latency of the request.
|
225
|
-
distribution :duration do
|
226
|
-
description "HTTP request processing time in milliseconds"
|
227
|
-
tags allowed: %w[controller action method status_code region]
|
228
|
-
required: %w[controller]
|
229
|
-
end
|
230
|
-
|
231
|
-
# This counter allows tracking the frequency of each controller/action
|
232
|
-
counter :total, inherit_tags: :duration do
|
233
|
-
description "Total number of requests received"
|
234
|
-
end
|
235
|
-
end
|
236
|
-
end
|
237
|
-
end
|
211
|
+
namespace :api do
|
212
|
+
metrics do
|
213
|
+
counter :requests_total do
|
214
|
+
tags required: [:environment, :service], allowed: [:region]
|
215
|
+
end
|
216
|
+
|
217
|
+
# Inherits environment, service (required) and region (allowed) from requests_total
|
218
|
+
distribution :request_duration do
|
219
|
+
inherit_tags "api.requests_total"
|
220
|
+
tags required: [:endpoint] # Adds endpoint as additional required tag
|
238
221
|
end
|
239
222
|
end
|
223
|
+
end
|
240
224
|
```
|
241
225
|
|
226
|
+
### Nested Namespaces
|
242
227
|
|
243
|
-
|
228
|
+
Organize metrics hierarchically with nested namespaces:
|
244
229
|
|
245
230
|
```ruby
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
metric: "web.request",
|
251
|
-
tags: { billing_plan: :premium, logged_in: :logged_in }
|
252
|
-
)
|
253
|
-
|
254
|
-
my_sender.increment('total', tags: { uri: '/home/settings', method: :get } )
|
255
|
-
my_sender.distribution('duration', tags: { uri: '/home/settings', method: :get } )
|
231
|
+
namespace :application do
|
232
|
+
tags do
|
233
|
+
tag :environment, values: %w[prod staging dev]
|
234
|
+
end
|
256
235
|
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
The above code will send two metrics: `web.request.total` as a counter, tagged with: `{ billing_plan: :premium, logged_in: :logged_in, uri: '/home/settings' }` and the second time for the `uri: '/app/calendar'`.
|
236
|
+
namespace :database do
|
237
|
+
tags do
|
238
|
+
tag :table_name, values: %w[users orders products]
|
239
|
+
end
|
262
240
|
|
263
|
-
|
241
|
+
metrics do
|
242
|
+
counter :queries_total
|
243
|
+
distribution :query_duration
|
244
|
+
end
|
245
|
+
end
|
264
246
|
|
265
|
-
|
247
|
+
namespace :cache do
|
248
|
+
tags do
|
249
|
+
tag :cache_type, values: %w[redis memcached]
|
250
|
+
end
|
266
251
|
|
267
|
-
|
252
|
+
metrics do
|
253
|
+
counter :hits_total
|
254
|
+
counter :misses_total
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
258
|
+
```
|
268
259
|
|
269
|
-
|
260
|
+
### Validation Modes
|
270
261
|
|
271
|
-
|
262
|
+
Control how validation errors are handled:
|
272
263
|
|
273
|
-
|
264
|
+
```ruby
|
265
|
+
# Strict mode: Raises exceptions on validation failures
|
266
|
+
emitter = Datadog::Statsd::Emitter.new(schema: schema, validation_mode: :strict)
|
274
267
|
|
275
|
-
|
268
|
+
# Warn mode: Logs warnings but continues execution
|
269
|
+
emitter = Datadog::Statsd::Emitter.new(schema: schema, validation_mode: :warn)
|
276
270
|
|
277
|
-
|
271
|
+
# Disabled: No validation (production default)
|
272
|
+
emitter = Datadog::Statsd::Emitter.new(schema: schema, validation_mode: :disabled)
|
273
|
+
```
|
278
274
|
|
279
|
-
|
275
|
+
### Global Configuration
|
280
276
|
|
281
|
-
|
277
|
+
Set up global defaults for your application:
|
282
278
|
|
283
279
|
```ruby
|
280
|
+
Datadog::Statsd::Schema.configure do |config|
|
281
|
+
config.statsd = Datadog::Statsd.new(
|
282
|
+
ENV['DATADOG_AGENT_HOST'] || 'localhost',
|
283
|
+
ENV['DATADOG_AGENT_PORT'] || 8125
|
284
|
+
)
|
285
|
+
config.schema = schema
|
286
|
+
config.tags = {
|
287
|
+
environment: ENV['RAILS_ENV'],
|
288
|
+
service: 'my-application',
|
289
|
+
version: ENV['APP_VERSION']
|
290
|
+
}
|
291
|
+
end
|
292
|
+
|
293
|
+
# These global tags are automatically added to all metrics
|
294
|
+
emitter = Datadog::Statsd::Emitter.new
|
295
|
+
emitter.increment('user.signup') # Automatically includes global tags
|
296
|
+
```
|
284
297
|
|
285
|
-
|
286
|
-
self,
|
287
|
-
metric: 'emails'
|
288
|
-
)
|
298
|
+
## Cost Control and Best Practices
|
289
299
|
|
290
|
-
|
291
|
-
emails_emitter.increment('delivered.total', by: count)
|
292
|
-
emails_emitter.gauge('queue.size', EmailQueue.size)
|
293
|
-
```
|
300
|
+
### Understanding Metric Expansion
|
294
301
|
|
295
|
-
|
302
|
+
Different metric types create different numbers of time series:
|
296
303
|
|
297
|
-
|
304
|
+
- **Counter/Set**: 1 time series per unique tag combination
|
305
|
+
- **Gauge**: 5 time series (count, min, max, sum, avg)
|
306
|
+
- **Distribution/Histogram**: 10 time series (count, min, max, sum, avg, p50, p75, p90, p95, p99)
|
298
307
|
|
299
|
-
|
308
|
+
### Tag Value Limits
|
300
309
|
|
301
|
-
|
310
|
+
Be mindful of tag cardinality:
|
302
311
|
|
303
312
|
```ruby
|
304
|
-
|
313
|
+
# High cardinality - avoid
|
314
|
+
tag :user_id, type: :string # Could be millions of values
|
315
|
+
|
316
|
+
# Better approach - use bucketing
|
317
|
+
tag :user_tier, values: %w[free premium enterprise]
|
318
|
+
tag :user_cohort, values: %w[new_user returning_user power_user]
|
305
319
|
```
|
306
320
|
|
307
|
-
|
321
|
+
### Schema Validation
|
308
322
|
|
309
|
-
|
323
|
+
Always validate your schema before deployment:
|
310
324
|
|
311
325
|
```ruby
|
326
|
+
# Check for common issues
|
327
|
+
errors = schema.validate
|
328
|
+
if errors.any?
|
329
|
+
puts "Schema validation errors:"
|
330
|
+
errors.each { |error| puts " - #{error}" }
|
331
|
+
end
|
332
|
+
```
|
312
333
|
|
313
|
-
|
314
|
-
.increment('emails.sent', by: 2)
|
334
|
+
## Integration Examples
|
315
335
|
|
316
|
-
|
317
|
-
.increment('users.logged_in')
|
318
|
-
# => tags: { ab_test_name: 'login_test_2025',
|
319
|
-
# ab_test_group: 'control' }
|
336
|
+
### Rails Integration
|
320
337
|
|
321
|
-
|
322
|
-
|
338
|
+
```ruby
|
339
|
+
# config/initializers/datadog_statsd.rb
|
340
|
+
schema = Datadog::Statsd::Schema.load_file(Rails.root.join('config/metrics_schema.rb'))
|
341
|
+
|
342
|
+
Datadog::Statsd::Schema.configure do |config|
|
343
|
+
config.statsd = Datadog::Statsd.new
|
344
|
+
config.schema = schema
|
345
|
+
config.tags = {
|
346
|
+
environment: Rails.env,
|
347
|
+
service: 'my-rails-app'
|
348
|
+
}
|
349
|
+
end
|
350
|
+
|
351
|
+
# app/controllers/application_controller.rb
|
352
|
+
class ApplicationController < ActionController::Base
|
353
|
+
before_action :setup_metrics
|
354
|
+
|
355
|
+
private
|
356
|
+
|
357
|
+
def setup_metrics
|
358
|
+
@metrics = Datadog::Statsd::Emitter.new(
|
359
|
+
validation_mode: Rails.env.production? ? :disabled : :warn
|
360
|
+
)
|
361
|
+
end
|
362
|
+
end
|
363
|
+
```
|
323
364
|
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
365
|
+
### Background Job Monitoring
|
366
|
+
|
367
|
+
```ruby
|
368
|
+
class OrderProcessingJob
|
369
|
+
def perform(order_id)
|
370
|
+
metrics = Datadog::Statsd::Emitter.new
|
371
|
+
|
372
|
+
start_time = Time.current
|
373
|
+
|
374
|
+
begin
|
375
|
+
process_order(order_id)
|
376
|
+
metrics.increment('jobs.order_processing.success', tags: { queue: 'orders' })
|
377
|
+
rescue => error
|
378
|
+
metrics.increment('jobs.order_processing.failure',
|
379
|
+
tags: { queue: 'orders', error_type: error.class.name })
|
380
|
+
raise
|
381
|
+
ensure
|
382
|
+
duration = Time.current - start_time
|
383
|
+
metrics.distribution('jobs.order_processing.duration', duration * 1000,
|
384
|
+
tags: { queue: 'orders' })
|
385
|
+
end
|
386
|
+
end
|
387
|
+
end
|
328
388
|
```
|
329
389
|
|
330
|
-
##
|
390
|
+
## Development
|
331
391
|
|
392
|
+
After checking out the repo, run:
|
332
393
|
|
333
394
|
```bash
|
334
|
-
|
395
|
+
bin/setup # Install dependencies
|
396
|
+
bundle exec rake spec # Run tests
|
335
397
|
```
|
336
398
|
|
337
|
-
|
399
|
+
To install this gem onto your local machine:
|
338
400
|
|
339
401
|
```bash
|
340
|
-
|
402
|
+
bundle exec rake install
|
341
403
|
```
|
342
404
|
|
343
|
-
## Usage
|
344
|
-
|
345
|
-
1. Define your metrics and tagging schema
|
346
|
-
2. Create as many "emitters" as necessary and start sending!
|
347
|
-
|
348
|
-
## Development
|
349
|
-
|
350
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
351
|
-
|
352
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
353
|
-
|
354
405
|
## Contributing
|
355
406
|
|
356
|
-
Bug reports and pull requests are welcome on GitHub at
|
407
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kigster/datadog-statsd-schema.
|
357
408
|
|
358
409
|
## License
|
359
410
|
|