-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtemplates.rs
More file actions
568 lines (519 loc) · 17.9 KB
/
templates.rs
File metadata and controls
568 lines (519 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
use std::{cmp, collections::BTreeMap, rc::Rc};
use opentelemetry_proto::tonic::{
common::v1::InstrumentationScope,
metrics::{
self,
v1::{Metric, NumberDataPoint, ResourceMetrics, ScopeMetrics, metric::Data},
},
resource,
};
use prost::Message;
use rand::{
Rng,
distr::{Distribution, StandardUniform, weighted::WeightedIndex},
seq::{IndexedRandom, IteratorRandom},
};
use tracing::{debug, error};
use super::{Config, UnitGenerator, tags::TagGenerator};
use crate::{Error, Generator, SizedGenerator, common::config::ConfRange, common::strings};
const UNIQUE_TAG_RATIO: f32 = 0.75;
/// Errors related to template generators
#[derive(thiserror::Error, Debug, Clone, Copy)]
pub enum PoolError {
#[error("Choice could not be made on empty container.")]
EmptyChoice,
#[error("Generation error: {0}")]
Generator(#[from] GeneratorError),
}
#[derive(Debug, Clone)]
pub(crate) struct Pool {
context_cap: u32,
/// key: encoded size; val: templates with that size
by_size: BTreeMap<usize, Vec<ResourceMetrics>>,
generator: ResourceTemplateGenerator,
len: u32,
}
impl Pool {
/// Build an empty pool that can hold at most `context_cap` templates.
pub(crate) fn new(context_cap: u32, generator: ResourceTemplateGenerator) -> Self {
Self {
context_cap,
by_size: BTreeMap::new(),
generator,
len: 0,
}
}
/// Return a `ResourceMetrics` from the pool.
///
/// Instances of `ResourceMetrics` returned by this function are guaranteed
/// to be of an encoded size no greater than budget. No greater than
/// `context_cap` instances of `ResourceMetrics` will ever be stored in this
/// structure.
pub(crate) fn fetch<R>(
&mut self,
rng: &mut R,
budget: &mut usize,
) -> Result<&ResourceMetrics, PoolError>
where
R: rand::Rng + ?Sized,
{
// If we are at context cap, search by_size for templates <= budget and
// return a random choice. If we are not at context cap, call
// ResourceMetrics::generator with the budget and then store the result
// for future use in `by_size`.
//
// Size search is in the interval (0, budget].
let upper = *budget;
let mut limit = *budget;
// Generate new instances until either context_cap is hit or the
// remaining space drops below our lookup interval.
if self.len < self.context_cap {
match self.generator.generate(rng, &mut limit) {
Ok(rm) => {
let sz = rm.encoded_len();
self.by_size.entry(sz).or_default().push(rm);
self.len += 1;
}
Err(e) => return Err(PoolError::Generator(e)),
}
}
let (choice_sz, choices) = self
.by_size
.range(..=upper)
.choose(rng)
.ok_or(PoolError::EmptyChoice)?;
let choice = choices.choose(rng).ok_or(PoolError::EmptyChoice)?;
*budget = budget.saturating_sub(*choice_sz);
Ok(choice)
}
}
/// Errors related to template generators
#[derive(thiserror::Error, Debug, Clone, Copy)]
pub enum GeneratorError {
#[error("Generator exhausted bytes budget prematurely")]
SizeExhausted,
/// failed to generate string
#[error("Failed to generate string")]
StringGenerate,
}
struct Ndp(NumberDataPoint);
impl Distribution<Ndp> for StandardUniform {
fn sample<R>(&self, rng: &mut R) -> Ndp
where
R: Rng + ?Sized,
{
let value = match rng.random_range(0..=1) {
0 => metrics::v1::number_data_point::Value::AsDouble(0.0),
1 => metrics::v1::number_data_point::Value::AsInt(0),
_ => unreachable!(),
};
Ndp(NumberDataPoint {
// NOTE absent a reason to set attributes to not-empty, it's unclear
// that we should.
attributes: Vec::new(),
start_time_unix_nano: 0, // epoch instant
time_unix_nano: rng.random(),
// Unclear that this needs to be set.
exemplars: Vec::new(),
// Equivalent to DoNotUse, the flag is ignored. This is discussed in
// the upstream OTLP protobuf definition, which we inherit from the
// SDK. If we ever set `value` to None this must be set to
// DATA_POINT_FLAGS_NO_RECORDED_VALUE_MASK
flags: 0,
value: Some(value),
})
}
}
#[derive(Debug, Clone)]
pub(crate) struct MetricTemplateGenerator {
kind_dist: WeightedIndex<u16>,
unit_gen: UnitGenerator,
str_pool: Rc<strings::Pool>,
tags: TagGenerator,
}
impl MetricTemplateGenerator {
pub(crate) fn new<R>(
config: &Config,
str_pool: &Rc<strings::Pool>,
rng: &mut R,
) -> Result<Self, Error>
where
R: Rng + ?Sized,
{
let tags = TagGenerator::new(
rng.random(),
config.contexts.attributes_per_metric,
ConfRange::Inclusive { min: 3, max: 32 },
config.contexts.total_contexts.end() as usize,
Rc::clone(str_pool),
UNIQUE_TAG_RATIO,
)?;
Ok(Self {
kind_dist: WeightedIndex::new([
u16::from(config.metric_weights.gauge),
u16::from(config.metric_weights.sum),
])?,
unit_gen: UnitGenerator::new(),
str_pool: Rc::clone(str_pool),
tags,
})
}
}
impl<'a> crate::SizedGenerator<'a> for MetricTemplateGenerator {
type Output = Metric;
type Error = GeneratorError;
fn generate<R>(
&'a mut self,
rng: &mut R,
budget: &mut usize,
) -> Result<Self::Output, Self::Error>
where
R: Rng + ?Sized,
{
// We record the original budget because if we bail out on generation we
// are obligated by trait semantics to NOT alter the passed budget.
let original_budget: usize = *budget;
let mut inner_budget: usize = *budget;
let metadata = match self.tags.generate(rng, &mut inner_budget) {
Ok(md) => md,
Err(GeneratorError::SizeExhausted) => {
debug!("Tag generator unable to satify request for {inner_budget} size");
Vec::new()
}
Err(e) => Err(e)?,
};
let name = self
.str_pool
.of_size_range(rng, 1_u8..16)
.ok_or(Self::Error::StringGenerate)?
.to_owned();
let description = if rng.random_bool(0.1) {
self.str_pool
.of_size_range(rng, 1_u8..16)
.ok_or(Self::Error::StringGenerate)?
.to_owned()
} else {
String::new()
};
let unit = if rng.random_bool(0.1) {
self.unit_gen.generate(rng)?.to_owned()
} else {
String::new()
};
let kind = match self.kind_dist.sample(rng) {
0 => Kind::Gauge,
1 => Kind::Sum {
aggregation_temporality: *[1, 2].choose(rng).expect("cannot fail"),
is_monotonic: rng.random_bool(0.5),
},
_ => unreachable!(),
};
let total_data_points = rng.random_range(1..60);
let data_points = (0..total_data_points)
.map(|_| rng.random::<Ndp>().0)
.collect();
let data = match kind {
Kind::Gauge => Data::Gauge(metrics::v1::Gauge { data_points }),
Kind::Sum {
aggregation_temporality,
is_monotonic,
} => Data::Sum(metrics::v1::Sum {
data_points,
aggregation_temporality,
is_monotonic,
}),
};
let mut metric = Metric {
name,
description,
unit,
data: Some(data),
metadata,
};
while data_points_total(&metric) > 0 {
let required_bytes = metric.encoded_len();
assert_eq!(original_budget, *budget);
match original_budget.cmp(&required_bytes) {
cmp::Ordering::Equal | cmp::Ordering::Greater => {
*budget -= required_bytes;
return Ok(metric);
}
cmp::Ordering::Less => {
// Too many metric points, go around the loop again and try
// again.
metric = cut_data_points(metric);
}
}
}
debug!("MetricTemplateGenerator unable to satisfy request for {original_budget} bytes.");
Err(Self::Error::SizeExhausted)
}
}
fn data_points_total(metric: &Metric) -> usize {
let data = &metric.data;
match data {
Some(
Data::Gauge(metrics::v1::Gauge { data_points })
| Data::Sum(metrics::v1::Sum { data_points, .. }),
) => data_points.len(),
None => 0,
_ => unimplemented!("only gauge/sum metrics supported"),
}
}
fn cut_data_points(metric: Metric) -> Metric {
let name = metric.name;
let description = metric.description;
let unit = metric.unit;
let metadata = metric.metadata;
let data = metric.data;
let new_data = match data {
Some(Data::Gauge(metrics::v1::Gauge { mut data_points })) => {
let new_len = data_points.len() / 2;
data_points.truncate(new_len);
Some(Data::Gauge(metrics::v1::Gauge { data_points }))
}
Some(Data::Sum(metrics::v1::Sum {
mut data_points,
aggregation_temporality,
is_monotonic,
})) => {
let new_len = data_points.len() / 2;
data_points.truncate(new_len);
Some(Data::Sum(metrics::v1::Sum {
data_points,
aggregation_temporality,
is_monotonic,
}))
}
None => None,
_ => unimplemented!("only gauge/sum metrics supported"),
};
Metric {
name,
description,
unit,
metadata,
data: new_data,
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum Kind {
Gauge,
Sum {
aggregation_temporality: i32,
is_monotonic: bool,
},
}
#[derive(Clone, Debug)]
pub(crate) struct ScopeTemplateGenerator {
metrics_per_scope: ConfRange<u8>,
metric_generator: MetricTemplateGenerator,
str_pool: Rc<strings::Pool>,
tags: TagGenerator,
attributes_per_scope: ConfRange<u8>,
}
impl ScopeTemplateGenerator {
pub(crate) fn new<R>(
config: &Config,
str_pool: &Rc<strings::Pool>,
rng: &mut R,
) -> Result<Self, Error>
where
R: Rng + ?Sized,
{
let tags = TagGenerator::new(
rng.random(),
config.contexts.attributes_per_scope,
ConfRange::Inclusive { min: 3, max: 32 },
config.contexts.total_contexts.end() as usize,
Rc::clone(str_pool),
UNIQUE_TAG_RATIO,
)?;
Ok(Self {
metrics_per_scope: config.contexts.metrics_per_scope,
metric_generator: MetricTemplateGenerator::new(config, str_pool, rng)?,
str_pool: Rc::clone(str_pool),
tags,
attributes_per_scope: config.contexts.attributes_per_scope,
})
}
}
impl<'a> crate::SizedGenerator<'a> for ScopeTemplateGenerator {
type Output = ScopeMetrics;
type Error = GeneratorError;
fn generate<R>(
&'a mut self,
rng: &mut R,
budget: &mut usize,
) -> Result<Self::Output, Self::Error>
where
R: Rng + ?Sized,
{
// We record the original budget because if we bail out on generation we
// are obligated by trait semantics to NOT alter the passed budget.
let original_budget = *budget;
let mut inner_budget = *budget;
let scope = if self.attributes_per_scope.start() == 0 {
None
} else {
let attributes = match self.tags.generate(rng, &mut inner_budget) {
Ok(md) => md,
Err(GeneratorError::SizeExhausted) => {
debug!("Tag generator unable to satify request for {inner_budget} size");
Vec::new()
}
Err(e) => Err(e)?,
};
Some(InstrumentationScope {
name: self
.str_pool
.of_size_range(rng, 1_u8..16)
.ok_or(Self::Error::StringGenerate)?
.to_owned(),
version: String::new(),
attributes: attributes.as_slice().to_owned(),
dropped_attributes_count: 0,
})
};
let total_metrics = self.metrics_per_scope.sample(rng);
let mut metrics: Vec<Metric> = Vec::with_capacity(total_metrics as usize);
// Search for the most metrics we can fit. If the metric_generator
// returns SizeExhausted we check to see if metrics was populated at all
// and if it was not we signal SizeExhausted.
for _ in 0..total_metrics {
match self.metric_generator.generate(rng, &mut inner_budget) {
Ok(m) => metrics.push(m),
Err(GeneratorError::SizeExhausted) => break,
Err(e) => return Err(e),
}
}
if metrics.is_empty() {
debug!(
"ScopeTemplateGenerator unable to populate metrics with budget {original_budget}"
);
return Err(GeneratorError::SizeExhausted);
}
let mut scope_metrics = ScopeMetrics {
scope,
metrics,
schema_url: String::new(),
};
loop {
let required_bytes = scope_metrics.encoded_len();
match original_budget.cmp(&required_bytes) {
cmp::Ordering::Equal | cmp::Ordering::Greater => {
*budget -= required_bytes;
return Ok(scope_metrics);
}
cmp::Ordering::Less => {
if scope_metrics.metrics.pop().is_some() {
continue;
}
return Err(GeneratorError::SizeExhausted);
}
}
}
}
}
#[derive(Clone, Debug)]
pub(crate) struct ResourceTemplateGenerator {
scopes_per_resource: ConfRange<u8>,
attributes_per_resource: ConfRange<u8>,
scope_generator: ScopeTemplateGenerator,
tags: TagGenerator,
}
impl ResourceTemplateGenerator {
pub(crate) fn new<R>(
config: &Config,
str_pool: &Rc<strings::Pool>,
rng: &mut R,
) -> Result<Self, Error>
where
R: Rng + ?Sized,
{
let tags = TagGenerator::new(
rng.random(),
config.contexts.attributes_per_resource,
ConfRange::Inclusive { min: 3, max: 32 },
config.contexts.total_contexts.end() as usize,
Rc::clone(str_pool),
UNIQUE_TAG_RATIO,
)?;
Ok(Self {
scopes_per_resource: config.contexts.scopes_per_resource,
attributes_per_resource: config.contexts.attributes_per_resource,
scope_generator: ScopeTemplateGenerator::new(config, str_pool, rng)?,
tags,
})
}
}
impl<'a> crate::SizedGenerator<'a> for ResourceTemplateGenerator {
type Output = ResourceMetrics;
type Error = GeneratorError;
fn generate<R>(
&'a mut self,
rng: &mut R,
budget: &mut usize,
) -> Result<Self::Output, Self::Error>
where
R: Rng + ?Sized,
{
// We record the original budget because if we bail out on generation we
// are obligated by trait semantics to NOT alter the passed budget.
let original_budget = *budget;
let mut inner_budget = *budget;
let resource = if self.attributes_per_resource.end() == 0 {
None
} else {
match self.tags.generate(rng, &mut inner_budget) {
Ok(attributes) => {
let res = resource::v1::Resource {
attributes: attributes.as_slice().to_owned(),
dropped_attributes_count: 0,
};
Some(res)
}
Err(GeneratorError::SizeExhausted) => None,
Err(e) => return Err(e),
}
};
// Search for the most scopes we can fit. If the scope_generator
// returns SizeExhausted we check to see if metrics was populated at all
// and if it was not we signal SizeExhausted.
let total_scopes = self.scopes_per_resource.sample(rng);
let mut scopes = Vec::with_capacity(total_scopes as usize);
for _ in 0..total_scopes {
match self.scope_generator.generate(rng, &mut inner_budget) {
Ok(s) => scopes.push(s),
Err(GeneratorError::SizeExhausted) => break,
Err(e) => return Err(e),
}
}
if scopes.is_empty() {
debug!(
"ResourceTemplateGenerator unable to populate metrics with budget {original_budget}"
);
return Err(GeneratorError::SizeExhausted);
}
let mut resource_metrics = ResourceMetrics {
resource,
scope_metrics: scopes,
schema_url: String::new(),
};
loop {
let required_bytes = resource_metrics.encoded_len();
match original_budget.cmp(&required_bytes) {
cmp::Ordering::Equal | cmp::Ordering::Greater => {
*budget -= required_bytes;
return Ok(resource_metrics);
}
cmp::Ordering::Less => {
if resource_metrics.scope_metrics.pop().is_some() {
continue;
}
return Err(Self::Error::SizeExhausted);
}
}
}
}
}