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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

use std::collections::HashMap;
use std::sync::atomic::Ordering;

use proc_macro2::{Span, TokenStream as Tokens};
use quote::{ToTokens, TokenStreamExt};
use syn::{Ident, Visibility};

use super::parser::*;
use super::util;
use crate::builder::TokensBuilder;
use crate::builder::SCOPE_ID;

pub struct AutoFlushTokensBuilder;

impl AutoFlushTokensBuilder {
    pub fn build(macro_body: StaticMetricMacroBody) -> Tokens {
        let mut enums_definitions = HashMap::new();
        let mut tokens = Tokens::new();
        for item in macro_body.items {
            match item {
                StaticMetricMacroBodyItem::Metric(m) => {
                    // If this is a metric definition, expand to a `struct`.
                    tokens.append_all(Self::build_metric_struct(&m, &enums_definitions));
                }
                StaticMetricMacroBodyItem::Enum(e) => {
                    // If this is a label enum definition, expand to an `enum` and
                    // add to the collection.
                    tokens.append_all(TokensBuilder::build_label_enum(&e));
                    enums_definitions.insert(e.enum_name.clone(), e);
                }
            }
        }
        tokens
    }

    fn build_metric_struct(
        metric: &MetricDef,
        enum_definitions: &HashMap<Ident, MetricEnumDef>,
    ) -> Tokens {
        // Check `label_enum` references.
        for label in &metric.labels {
            let enum_ident = label.get_enum_ident();
            if let Some(e) = enum_ident {
                // If metric is using a `label_enum`, it must exist before the metric definition.
                let enum_def = enum_definitions.get(e);
                if enum_def.is_none() {
                    panic!("Label enum `{}` is undefined.", e)
                }

                // If metric has `pub` visibility, then `label_enum` should also be `pub`.
                // TODO: Support other visibility, like `pub(xx)`.
                if let Visibility::Public(_) = metric.visibility {
                    if let Visibility::Public(_) = enum_def.unwrap().visibility {
                        // `pub` is ok.
                    } else {
                        // others are unexpected.
                        panic!(
                            "Label enum `{}` does not have enough visibility because it is \
                             used in metric `{}` which has `pub` visibility.",
                            e, metric.struct_name
                        );
                    }
                }
            }
        }

        let label_struct: Vec<_> = metric
            .labels
            .iter()
            .enumerate()
            .map(|(i, _)| {
                let builder_context = MetricBuilderContext::new(metric, enum_definitions, i);
                let inner_struct = builder_context.build_inner_struct();
                let inner_impl = builder_context.build_inner_impl();
                let inner_trait_impl = builder_context.build_inner_trait_impl();
                let delegator_struct = builder_context.build_delegator_struct();
                let delegator_impl = builder_context.build_delegator_impl();
                quote! {
                    #inner_struct
                    #inner_impl
                    #inner_trait_impl
                    #delegator_struct
                    #delegator_impl
                }
            })
            .collect();

        let builder_contexts: Vec<MetricBuilderContext> = metric
            .labels
            .iter()
            .enumerate()
            .map(|(i, _)| MetricBuilderContext::new(metric, enum_definitions, i))
            .collect();

        let auto_flush_delegator: Tokens =
            Self::build_auto_flush_delegator(metric, &builder_contexts);
        let outer_struct: Tokens = Self::build_outer_struct(&builder_contexts);
        let outer_impl: Tokens = Self::build_outer_impl(&builder_contexts);
        let scope_id = SCOPE_ID.fetch_add(1, Ordering::Relaxed);
        let scope_name = Ident::new(
            &format!("prometheus_static_scope_{}", scope_id),
            Span::call_site(),
        );

        let visibility = &metric.visibility;
        let inner_struct_name =
            Ident::new(&format!("{}Inner", &metric.struct_name), Span::call_site());
        let outer_struct_name = metric.struct_name.clone();

        quote! {
            #visibility use self::#scope_name::#inner_struct_name;
            #visibility use self::#scope_name::#outer_struct_name;

            #[allow(dead_code)]
            mod #scope_name {
                use ::std::collections::HashMap;
                use ::prometheus::*;
                use ::prometheus::local::*;
                use ::std::cell::Cell;
                use ::std::thread::LocalKey;
                use std::mem;
                use std::mem::MaybeUninit;


                #[allow(unused_imports)]
                use super::*;

                #(
                    #label_struct
                )*

                #auto_flush_delegator
                #outer_struct
                #outer_impl
            }
        }
    }

    fn build_auto_flush_delegator(
        metric: &MetricDef,
        builder_contexts: &[MetricBuilderContext],
    ) -> Tokens {
        let inner_struct = &builder_contexts[0].inner_struct_name();
        let last_builder_contexts = &builder_contexts
            .last()
            .expect("builder contexts should not be empty");
        let last_delegator = last_builder_contexts.delegator_struct_name();
        let metric_type = metric.metric_type.clone();

        fn offset_fetcher(builder_context: &MetricBuilderContext) -> Tokens {
            let struct_type = builder_context.inner_struct_name();
            let struct_var_name = Ident::new(
                struct_type.to_string().to_lowercase().as_str(),
                Span::call_site(),
            );

            let member_type = builder_context.inner_member_type.clone();
            let member_var_name = Ident::new(
                member_type.to_string().to_lowercase().as_str(),
                Span::call_site(),
            );
            let offset = Ident::new(
                &format!("offset{}", builder_context.label_index + 1),
                Span::call_site(),
            );
            let head = if builder_context.label_index == 0 {
                quote! {
                    let #struct_var_name = root_metric as *const #struct_type;
                }
            } else {
                Tokens::new()
            };

            let body = quote! {
                let #member_var_name = (#struct_var_name as usize + self.#offset) as *const #member_type;
            };

            let tail = if builder_context.is_last_label {
                quote! {
                    &*#member_var_name
                }
            } else {
                Tokens::new()
            };

            quote! {
                #head
                #body
                #tail
            }
        }

        let offset_fetchers = builder_contexts
            .iter()
            .map(|m| offset_fetcher(m))
            .collect::<Vec<Tokens>>();

        let delegator_tokens = if metric_type.to_string().contains("Counter") {
            quote! {
                CounterDelegator<#inner_struct, #metric_type>
            }
        } else if metric_type.to_string().contains("Histogram") {
            quote! {
                HistogramDelegator<#inner_struct>
            }
        } else {
            panic!("Metric type must be one of LocalCounter or LocalHistogram")
        };

        quote! {
            impl #delegator_tokens for #last_delegator {
                fn get_root_metric(&self) -> &'static LocalKey<#inner_struct> {
                    self.root
                }
                fn get_local<'a>(&self, root_metric: &'a #inner_struct) -> &'a #metric_type {
                   unsafe {
                       #(
                            #offset_fetchers
                       )*
                   }
                }
            }
        }
    }

    fn build_outer_struct(builder_contexts: &[MetricBuilderContext]) -> Tokens {
        builder_contexts[0].build_outer_struct()
    }

    fn build_outer_impl(builder_contexts: &[MetricBuilderContext]) -> Tokens {
        builder_contexts[0].build_outer_impl()
    }
}

struct MetricBuilderContext<'a> {
    metric: &'a MetricDef,
    enum_definitions: &'a HashMap<Ident, MetricEnumDef>,
    label: &'a MetricLabelDef,
    next_label: Option<&'a MetricLabelDef>,
    label_index: usize,
    is_last_label: bool,
    is_secondary_last_label: bool,
    root_struct_name: Ident,
    struct_name: Ident,
    delegator_member_type: Ident,
    inner_member_type: Ident,
    inner_next_member_type: Ident,
}

impl<'a> MetricBuilderContext<'a> {
    fn new(
        metric: &'a MetricDef,
        enum_definitions: &'a HashMap<Ident, MetricEnumDef>,
        label_index: usize,
    ) -> MetricBuilderContext<'a> {
        let is_last_label = label_index == metric.labels.len() - 1;
        let is_secondary_last_label =
            metric.labels.len() >= 2 && label_index == metric.labels.len() - 2;

        MetricBuilderContext {
            metric,
            enum_definitions,
            label: &metric.labels[label_index],
            next_label: metric.labels.get(label_index + 1),
            label_index,
            is_last_label,
            is_secondary_last_label,
            root_struct_name: util::get_label_struct_name(metric.struct_name.clone(), 0),
            struct_name: util::get_label_struct_name(metric.struct_name.clone(), label_index),
            delegator_member_type: util::get_delegator_member_type(
                metric.struct_name.clone(),
                label_index,
                is_last_label,
            ),
            inner_member_type: util::get_inner_member_type(
                metric.struct_name.clone(),
                label_index,
                metric.metric_type.clone(),
                is_last_label,
            ),
            inner_next_member_type: util::get_inner_member_type(
                metric.struct_name.clone(),
                label_index + 1,
                metric.metric_type.clone(),
                is_secondary_last_label,
            ),
        }
    }

    fn inner_struct_name(&self) -> Ident {
        Ident::new(&format!("{}Inner", &self.struct_name), Span::call_site())
    }

    fn delegator_struct_name(&self) -> Ident {
        Ident::new(
            &format!("{}Delegator", &self.struct_name),
            Span::call_site(),
        )
    }

    fn build_inner_struct(&self) -> Tokens {
        let struct_name = self.inner_struct_name();

        let field_names = self
            .label
            .get_value_def_list(self.enum_definitions)
            .get_names();
        let member_types: Vec<_> = field_names
            .iter()
            .map(|_| &self.inner_member_type)
            .collect();
        let last_flush = if self.label_index == 0 {
            quote! {
                last_flush: Cell<u64>,
                flush_millis: u64,
            }
        } else {
            Tokens::new()
        };

        quote! {
            #[allow(missing_copy_implementations)]
            pub struct #struct_name {
                #(
                    pub #field_names: #member_types,
                )*
                #last_flush
            }
        }
    }

    fn build_outer_struct(&self) -> Tokens {
        let metric_type = self.metric.metric_type.clone();
        let outer_struct_name = self.struct_name.clone();
        let inner_struct_name = self.inner_struct_name();
        let delegator_name = self.delegator_struct_name();
        let auto_flush_local = if metric_type.to_string().contains("Counter") {
            quote! {
                AFLocalCounter<#inner_struct_name,#metric_type,#delegator_name>
            }
        } else {
            quote! {
                AFLocalHistogram<#inner_struct_name,#delegator_name>
            }
        };

        let member_type = if self.metric.labels.len() == 1 {
            auto_flush_local
        } else {
            delegator_name.into_token_stream()
        };
        let field_names = self
            .label
            .get_value_def_list(self.enum_definitions)
            .get_names();

        quote! {
            pub struct #outer_struct_name {
                inner: &'static LocalKey<#inner_struct_name>,
                #(
                  pub #field_names: #member_type,
                )*
            }
        }
    }
    fn build_inner_impl(&self) -> Tokens {
        let struct_name = self.inner_struct_name();
        let impl_from = self.build_inner_impl_from();
        let impl_flush = self.build_inner_impl_flush();
        let update_flush_duration = self.build_with_flush_duration();

        quote! {
            impl #struct_name {
                #impl_from
                #impl_flush
                #update_flush_duration
            }
        }
    }

    fn build_delegator_impl(&self) -> Tokens {
        let struct_name = self.delegator_struct_name();
        let impl_new = self.build_delegator_impl_new();
        let impl_get = self.build_delegator_impl_get();

        quote! {
            impl #struct_name {
                #impl_new
                #impl_get
            }
        }
    }

    fn build_outer_impl(&self) -> Tokens {
        let outer_struct_name = self.struct_name.clone();

        let impl_from = self.build_outer_impl_from();
        let impl_get = self.build_outer_impl_get();
        quote! {
            impl #outer_struct_name {
                #impl_from
                #impl_get

                pub fn flush(&self) {
                    self.inner.with(|m| m.flush())
                }
            }
        }
    }

    fn build_inner_trait_impl(&self) -> Tokens {
        let struct_name = self.inner_struct_name();
        if self.label_index == 0 {
            quote! {
                impl ::prometheus::local::LocalMetric for #struct_name {
                    fn flush(&self) {
                        #struct_name::flush(self);
                    }
                }

                impl ::prometheus::local::MayFlush for #struct_name {
                    fn may_flush(&self) {
                        MayFlush::try_flush(self, &self.last_flush, self.flush_millis)
                    }
                }
            }
        } else {
            Tokens::new()
        }
    }

    fn build_inner_impl_from(&self) -> Tokens {
        let struct_name = self.inner_struct_name();
        let metric_vec_type = util::to_non_local_metric_type(util::get_metric_vec_type(
            self.metric.metric_type.clone(),
        ));

        let prev_labels_ident: Vec<_> = (0..self.label_index)
            .map(|i| Ident::new(&format!("label_{}", i), Span::call_site()))
            .collect();
        let body = self.build_inner_impl_from_body(&prev_labels_ident);

        quote! {
            pub fn from(
                #(
                    #prev_labels_ident: &str,
                )*
                m: &#metric_vec_type
            ) -> #struct_name {
                #struct_name {
                    #body
                }
            }
        }
    }

    fn build_delegator_impl_new(&self) -> Tokens {
        let inner_name = Ident::new(
            &format!("{}Inner", self.metric.struct_name),
            Span::call_site(),
        );
        let delegator_name = self.delegator_struct_name();
        let delegator_member = self.delegator_member_type.clone();
        let member_type = self.inner_member_type.clone();
        let next_member_type = self.inner_next_member_type.clone();
        let metric_type = self.metric.metric_type.clone();
        let known_offsets = (1..=(self.label_index + 1))
            .map(|m| Ident::new(&format!("offset{}", m), Span::call_site()))
            .collect::<Vec<Ident>>();
        let known_offsets_tokens = quote! {
          #(
          #known_offsets,
          )*
        };
        if self.is_last_label {
            let local_id = if metric_type.to_string().contains("Counter") {
                quote! {
                    AFLocalCounter
                }
            } else {
                quote! {
                    AFLocalHistogram
                }
            };

            let local_tokens = if metric_type.to_string().contains("Counter") {
                quote! {
                    #local_id<#inner_name,#metric_type,#delegator_name>
                }
            } else {
                quote! {
                    #local_id<#inner_name, #delegator_name>
                }
            };

            quote! {
            pub fn new(
                        root: &'static LocalKey<#inner_name>,
                            #(
                              #known_offsets : usize,
                            )*
                        ) -> #local_tokens {
                            let delegator = #delegator_name {
                                root,
                                #known_offsets_tokens
                            };
                           #local_id::new(delegator)
                        }
            }
        } else {
            let delegator_field_names = &self.delegator_field_names();

            quote! {
                pub fn new(
                    root: &'static LocalKey<#inner_name>,
                    #(
                      #known_offsets : usize,
                    )*
                ) -> #delegator_name {
                    let x = unsafe { MaybeUninit::<#member_type>::uninit().assume_init() };
                    let branch_offset = (&x as *const #member_type) as usize;
                    #(
                      let #delegator_field_names = #delegator_member::new(
                      root,
                      #known_offsets_tokens
                      &(x.#delegator_field_names) as *const #next_member_type as usize - branch_offset,
                      );
                    )*
                    mem::forget(x);
                    #delegator_name {
                        #(
                         #delegator_field_names,
                        )*
                    }
                }
            }
        }
    }

    fn build_outer_impl_from(&self) -> Tokens {
        let outer_struct_name = self.struct_name.clone();
        let inner_struct_name = self.inner_struct_name();
        let delegator_name = self.delegator_struct_name();
        let inner_member_type = self.inner_member_type.clone();
        let field_names = self
            .label
            .get_value_def_list(self.enum_definitions)
            .get_names();

        quote! {
            pub fn from(inner: &'static LocalKey<#inner_struct_name>) -> #outer_struct_name {
                let x = unsafe { MaybeUninit::<#inner_struct_name>::uninit().assume_init() };
                let branch_offset = &x as *const #inner_struct_name as usize;

                #(
                  let #field_names = #delegator_name::new(
                  &inner,
                  &(x.#field_names) as *const #inner_member_type as usize - branch_offset,
                  );
                )*
                mem::forget(x);

            #outer_struct_name {
             inner,
             #(
                #field_names,
             )*
            }
           }
        }
    }

    /// `fn get()` is only available when label is defined by `label_enum`.
    fn build_delegator_impl_get(&self) -> Tokens {
        let enum_ident = self.next_label.and_then(|m| m.get_enum_ident());
        let inner_root_name = Ident::new(
            &format!("{}Inner", &self.root_struct_name),
            Span::call_site(),
        );
        let metric_type = self.metric.metric_type.clone();

        if let Some(e) = enum_ident {
            let delegator_member_type = &self.delegator_member_type;
            let member_type = if self.is_secondary_last_label {
                if metric_type.to_string().contains("Counter") {
                    quote! {
                        AFLocalCounter<#inner_root_name,#metric_type,#delegator_member_type>
                    }
                } else {
                    quote! {
                        AFLocalHistogram<#inner_root_name,#delegator_member_type>
                    }
                }
            } else {
                delegator_member_type.into_token_stream()
            };

            let match_patterns = self
                .enum_definitions
                .get(e)
                .unwrap()
                .build_fields_with_path();
            let fields = self
                .next_label
                .expect("should have next label here")
                .get_value_def_list(self.enum_definitions)
                .get_names();
            quote! {
                pub fn get(&self, enum_value: #e) -> &#member_type {
                    match enum_value {
                        #(
                            #match_patterns => &self.#fields,
                        )*
                    }
                }
            }
        } else {
            Tokens::new()
        }
    }

    /// `fn get()` is only available when label is defined by `label_enum`.
    fn build_outer_impl_get(&self) -> Tokens {
        let enum_ident = self.label.get_enum_ident();
        if let Some(e) = enum_ident {
            let metric_type = self.metric.metric_type.clone();
            let inner_struct_name = self.inner_struct_name();
            let delegator_name = self.delegator_struct_name();

            let auto_flush_local = if metric_type.to_string().contains("Counter") {
                quote! {
                    AFLocalCounter<#inner_struct_name,#metric_type,#delegator_name>
                }
            } else {
                quote! {
                    AFLocalHistogram<#inner_struct_name,#delegator_name>
                }
            };

            let member_type = if self.metric.labels.len() == 1 {
                auto_flush_local
            } else {
                delegator_name.into_token_stream()
            };

            let match_patterns = self
                .enum_definitions
                .get(e)
                .unwrap()
                .build_fields_with_path();
            let fields = self
                .label
                .get_value_def_list(self.enum_definitions)
                .get_names();
            quote! {
                pub fn get(&self, enum_value: #e) -> &#member_type {
                    match enum_value {
                        #(
                            #match_patterns => &self.#fields,
                        )*
                    }
                }
            }
        } else {
            Tokens::new()
        }
    }

    fn build_inner_impl_from_body(&self, prev_labels_ident: &[Ident]) -> Tokens {
        let member_type = &self.inner_member_type;

        let init_instant = if self.label_index == 0 {
            quote! {
            last_flush: Cell::new(prometheus::timer::now_millis()),
            flush_millis: 1000,
            }
        } else {
            Tokens::new()
        };

        let bodies: Vec<_> = self
            .label
            .get_value_def_list(self.enum_definitions)
            .get()
            .iter()
            .map(|value| {
                let name = &value.name;
                let value = &value.value;
                if self.is_last_label {
                    let current_label = &self.label.label_key;
                    let prev_labels_str: Vec<_> = prev_labels_ident
                        .iter()
                        .enumerate()
                        .map(|(i, _)| &self.metric.labels[i].label_key)
                        .collect();
                    let local_suffix_call =
                        if util::is_local_metric(self.metric.metric_type.clone()) {
                            quote! { .local() }
                        } else {
                            Tokens::new()
                        };
                    quote! {
                        #name: m.with(&{
                            let mut coll = HashMap::new();
                            #(
                                coll.insert(#prev_labels_str, #prev_labels_ident);
                            )*
                            coll.insert(#current_label, #value);
                            coll
                        })#local_suffix_call,
                    }
                } else {
                    let prev_labels_ident = prev_labels_ident;
                    quote! {
                        #name: #member_type::from(
                            #(
                                #prev_labels_ident,
                            )*
                            #value,
                            m,
                        ),
                    }
                }
            })
            .collect();
        quote! {
            #(
                #bodies
            )*
            #init_instant
        }
    }

    fn build_inner_impl_flush(&self) -> Tokens {
        let value_def_list = self.label.get_value_def_list(self.enum_definitions);
        let names = value_def_list.get_names();
        quote! {
            pub fn flush(&self) {
                #(self.#names.flush();)*
            }
        }
    }

    fn build_with_flush_duration(&self) -> Tokens {
        if self.label_index == 0 {
            quote! {
                pub fn with_flush_duration(mut self, duration: std::time::Duration) -> Self {
                    self.flush_millis = prometheus::timer::duration_to_millis(duration);
                    self
                }
            }
        } else {
            Tokens::new()
        }
    }

    fn delegator_field_names(&self) -> Vec<Ident> {
        self.metric.labels[self.label_index + 1]
            .get_value_def_list(self.enum_definitions)
            .get_names()
            .iter()
            .map(|x| Ident::new(&x.to_string(), Span::call_site()))
            .collect()
    }

    fn build_delegator_struct(&self) -> Tokens {
        let struct_name = self.delegator_struct_name();
        let inner_root_name = Ident::new(
            &format!("{}Inner", &self.root_struct_name),
            Span::call_site(),
        );
        let metric_type = self.metric.metric_type.clone();
        let field_names = if self.is_last_label {
            (1..=self.metric.labels.len())
                .map(|suffix| Ident::new(&format!("offset{}", suffix), Span::call_site()))
                .collect::<Vec<Ident>>()
        } else {
            self.delegator_field_names()
        };

        let member_types = if self.is_last_label {
            (1..=self.metric.labels.len())
                .map(|_| self.delegator_member_type.clone().to_token_stream())
                .collect::<Vec<Tokens>>()
        } else if self.is_secondary_last_label {
            self.delegator_field_names()
                .iter()
                .map(|_| {
                    let delegator_member_type = self.delegator_member_type.clone();

                    if metric_type.to_string().contains("Counter") {
                        quote! {
                            AFLocalCounter<#inner_root_name,#metric_type,#delegator_member_type>
                        }
                    } else {
                        quote! {
                            AFLocalHistogram<#inner_root_name,#delegator_member_type>
                        }
                    }
                })
                .collect::<Vec<_>>()
        } else {
            self.delegator_field_names()
                .iter()
                .map(|_| {
                    let member_type = self.delegator_member_type.clone();
                    member_type.to_token_stream()
                })
                .collect::<Vec<_>>()
        };
        let root = if self.is_last_label {
            quote! {
                root: &'static LocalKey<#inner_root_name>,
            }
        } else {
            Tokens::new()
        };

        quote! {
            #[allow(missing_copy_implementations)]
            pub struct #struct_name {
                #root
                #(
                    pub #field_names: #member_types,
                )*
            }
        }
    }
}