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
// Copyright (C) 2015-2023 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef STATSMGR_H
#define STATSMGR_H

#include <stats/observation.h>
#include <stats/context.h>
#include <util/bigints.h>

#include <boost/noncopyable.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/scoped_ptr.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <map><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <mutex><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace stats {

/// @brief Statistics Manager class
///
/// StatsMgr is a singleton class that represents a subsystem that manages
/// collection, storage and reporting of various types of statistics.
/// It is also the intended API for both core code and hooks.
///
/// As of May 2015, Tomek ran performance benchmarks (see unit-tests in
/// stats_mgr_unittest.cc with performance in their names) and it seems
/// the code is able to register ~2.5-3 million observations per second, even
/// with 1000 different statistics recorded. That seems sufficient for now,
/// so there is no immediate need to develop any multi-threading solutions
/// for now. However, should this decision be revised in the future, the
/// best place for it would to be modify @ref addObservation method here.
/// It's the common code point that all new observations must pass through.
/// One possible way to enable multi-threading would be to run a separate
/// thread handling collection. The main thread would call @ref addValue and
/// @ref setValue methods that would end up calling @ref addObservation.
/// That method would pass the data to separate thread to be collected and
/// would immediately return. Further processing would be mostly as it
/// is today, except happening in a separate thread. One unsolved issue in
/// this approach is how to extract data, but that will remain unsolvable
/// until we get the control socket implementation.
///
/// Statistics Manager does not use logging by design. The reasons are:
/// - performance impact (logging every observation would degrade performance
///   significantly. While it's possible to log on sufficiently high debug
///   level, such a log would be not that useful)
/// - dependency (statistics are intended to be a lightweight library, adding
///   dependency on libkea-log, which has its own dependencies, including
///   external log4cplus, is against 'lightweight' design)
/// - if logging of specific statistics is warranted, it is recommended to
///   add log entries in the code that calls StatsMgr.
/// - enabling logging in StatsMgr does not offer fine tuning. It would be
///   either all or nothing. Adding logging entries only when necessary
///   in the code that uses StatsMgr gives better granularity.
///
/// If this decision is revisited in the future, the most universal places
/// for adding logging have been marked in @ref addValueInternal and
/// @ref setValueInternal.
class StatsMgr : public boost::noncopyable {
public:

    /// @brief Statistics Manager accessor method.
    static StatsMgr& instance();

    /// @defgroup producer_methods Methods are used by data producers.
    ///
    /// @brief The following methods are used by data producers:
    ///
    /// @{

    /// @brief Records absolute integer observation.
    ///
    /// @param name name of the observation
    /// @param value integer value observed
    /// @throw InvalidStatType if statistic is not integer
    void setValue(const std::string& name, const int64_t value);

    /// @brief Records an absolute big integer observation.
    ///
    /// @param name name of the observation
    /// @param value integer value observed
    /// @throw InvalidStatType if statistic is not integer
    void setValue(const std::string& name, const isc::util::int128_t& value);

    /// @brief Records absolute floating point observation.
    ///
    /// @param name name of the observation
    /// @param value floating point value observed
    /// @throw InvalidStatType if statistic is not fp
    void setValue(const std::string& name, const double value);

    /// @brief Records absolute duration observation.
    ///
    /// @param name name of the observation
    /// @param value duration value observed
    /// @throw InvalidStatType if statistic is not time duration
    void setValue(const std::string& name, const StatsDuration& value);

    /// @brief Records absolute string observation.
    ///
    /// @param name name of the observation
    /// @param value string value observed
    /// @throw InvalidStatType if statistic is not a string
    void setValue(const std::string& name, const std::string& value);

    /// @brief Records incremental integer observation.
    ///
    /// @param name name of the observation
    /// @param value integer value observed
    /// @throw InvalidStatType if statistic is not integer
    void addValue(const std::string& name, const int64_t value);

    /// @brief Records an incremental big integer observation.
    ///
    /// @param name name of the observation
    /// @param value integer value observed
    /// @throw InvalidStatType if statistic is not integer
    void addValue(const std::string& name, const isc::util::int128_t& value);

    /// @brief Records incremental floating point observation.
    ///
    /// @param name name of the observation
    /// @param value floating point value observed
    /// @throw InvalidStatType if statistic is not fp
    void addValue(const std::string& name, const double value);

    /// @brief Records incremental duration observation.
    ///
    /// @param name name of the observation
    /// @param value duration value observed
    /// @throw InvalidStatType if statistic is not time duration
    void addValue(const std::string& name, const StatsDuration& value);

    /// @brief Records incremental string observation.
    ///
    /// @param name name of the observation
    /// @param value string value observed
    /// @throw InvalidStatType if statistic is not a string
    void addValue(const std::string& name, const std::string& value);

    /// @brief Determines maximum age of samples.
    ///
    /// Specifies that statistic name should be stored not as a single value,
    /// but rather as a set of values. duration determines the timespan.
    /// Samples older than duration will be discarded. This is time-constrained
    /// approach. For sample count constrained approach, see @ref
    /// setMaxSampleCount() below.
    /// Example:
    /// To set a statistic to keep observations for the last 5 minutes, call:
    /// setMaxSampleAge("incoming-packets", StatsDuration::minutes(5));
    /// to revert statistic to a single value, call:
    /// setMaxSampleAge("incoming-packets", StatsDuration:zero());
    ///
    /// @param name name of the observation
    /// @param duration determines maximum age of samples
    /// @return true if successful, false if there's no such statistic
    bool setMaxSampleAge(const std::string& name, const StatsDuration& duration);

    /// @brief Determines how many samples of a given statistic should be kept.
    ///
    /// Specifies that statistic name should be stored not as single value, but
    /// rather as a set of values. In this form, at most max_samples will be kept.
    /// When adding max_samples + 1 sample, the oldest sample will be discarded.
    /// Example:
    /// To set a statistic to keep the last 100 observations, call:
    /// setMaxSampleCount("incoming-packets", 100);
    ///
    /// @param name name of the observation
    /// @param max_samples how many samples of a given statistic should be kept
    /// @return true if successful, false if there's no such statistic
    bool setMaxSampleCount(const std::string& name, uint32_t max_samples);

    /// @brief Set duration limit for all collected statistics.
    ///
    /// @param duration determines maximum age of samples
    void setMaxSampleAgeAll(const StatsDuration& duration);

    /// @brief Set count limit for all collected statistics.
    ///
    /// @param max_samples how many samples of a given statistic should be kept
    void setMaxSampleCountAll(uint32_t max_samples);

    /// @brief Set default duration limit.
    ///
    /// @param duration default maximum age of samples to keep
    void setMaxSampleAgeDefault(const StatsDuration& duration);

    /// @brief Set default count limit.
    ///
    /// @param max_samples default maximum number of samples to keep
    /// (0 means to disable count limit and enable age limit)
    void setMaxSampleCountDefault(uint32_t max_samples);

    /// @brief Get default duration limit.
    ///
    /// @return default maximum age of samples to keep.
    const StatsDuration& getMaxSampleAgeDefault() const;

    /// @brief Get default count limit.
    ///
    /// @return default maximum number of samples to keep.
    /// (0 means that count limit was disabled)
    uint32_t getMaxSampleCountDefault() const;

    /// @}

    /// @defgroup consumer_methods Methods are used by data consumers.
    ///
    /// @brief The following methods are used by data consumers:
    ///
    /// @{

    /// @brief Resets specified statistic.
    ///
    /// This is a convenience function and is equivalent to setValue(name,
    /// neutral_value), where neutral_value is 0, 0.0 or "".
    ///
    /// @param name name of the statistic to be reset.
    /// @return true if successful, false if there's no such statistic
    bool reset(const std::string& name);

    /// @brief Removes specified statistic.
    ///
    /// @param name name of the statistic to be removed.
    /// @return true if successful, false if there's no such statistic
    bool del(const std::string& name);

    /// @brief Resets all collected statistics back to zero.
    void resetAll();

    /// @brief Removes all collected statistics.
    /// @note This command was deprecated.
    void removeAll();

    /// @brief Returns size of specified statistic.
    ///
    /// @param name name of the statistic which size should be return.
    /// @return size of specified statistic, 0 means lack of given statistic.
    size_t getSize(const std::string& name) const;

    /// @brief Returns number of available statistics.
    ///
    /// @return number of recorded statistics.
    size_t count() const;

    /// @brief Returns a single statistic as a JSON structure.
    ///
    /// @return JSON structures representing a single statistic
    isc::data::ConstElementPtr get(const std::string& name) const;

    /// @brief Returns all statistics as a JSON structure.
    ///
    /// @return JSON structures representing all statistics
    isc::data::ConstElementPtr getAll() const;

    /// @}

    /// @brief Returns an observation.
    ///
    /// Used in testing only. Production code should use @ref get() method
    /// when the value is dereferenced.
    /// Calls @ref getObservationInternal() method in a thread safe context.
    ///
    /// @param name name of the statistic
    /// @return Pointer to the Observation object
    ObservationPtr getObservation(const std::string& name) const;

    /// @brief Returns an observation in a thread safe context.
    ///
    /// Used in testing only. Production code should use @ref get() method
    /// when the value is dereferenced. Should be called in a thread safe context.
    ///
    /// @param name name of the statistic
    /// @return Pointer to the Observation object
    ObservationPtr getObservationInternal(const std::string& name) const;

    /// @brief Generates statistic name in a given context
    ///
    /// Example:
    /// @code
    /// generateName("subnet", 123, "received-packets");
    /// @endcode
    /// will return subnet[123].received-packets. Any printable type
    /// can be used as index.
    ///
    /// @tparam Type any type that can be used to index contexts
    /// @param context name of the context (e.g. 'subnet')
    /// @param index value used for indexing contexts (e.g. subnet_id)
    /// @param stat_name name of the statistic
    /// @return returns full statistic name in form context[index].stat_name
    template<typename Type>
    static std::string generateName(const std::string& context, Type index,
                                    const std::string& stat_name) {
        std::stringstream name;
        name << context << "[" << index << "]." << stat_name;
        return (name.str());
    }

    /// @defgroup command_methods Methods are used to handle commands.
    ///
    /// @brief The following methods are used to handle commands:
    ///
    /// @{

    /// @brief Handles statistic-get command
    ///
    /// This method handles statistic-get command, which returns value
    /// of a given statistic). It expects one parameter stored in params map:
    /// name: name of the statistic
    ///
    /// Example params structure:
    /// {
    ///     "name": "packets-received"
    /// }
    ///
    /// @param name name of the command (ignored, should be "statistic-get")
    /// @param params structure containing a map that contains "name"
    /// @return answer containing details of specified statistic
    static isc::data::ConstElementPtr
    statisticGetHandler(const std::string& name,
                        const isc::data::ConstElementPtr& params);

    /// @brief Handles statistic-reset command
    ///
    /// This method handles statistic-reset command, which resets value
    /// of a given statistic. It expects one parameter stored in params map:
    /// name: name of the statistic
    ///
    /// Example params structure:
    /// {
    ///     "name": "packets-received"
    /// }
    ///
    /// @param name name of the command (ignored, should be "statistic-reset")
    /// @param params structure containing a map that contains "name"
    /// @return answer containing confirmation
    static isc::data::ConstElementPtr
    statisticResetHandler(const std::string& name,
                          const isc::data::ConstElementPtr& params);

    /// @brief Handles statistic-remove command
    ///
    /// This method handles statistic-reset command, which removes a given
    /// statistic completely. It expects one parameter stored in params map:
    /// name: name of the statistic
    ///
    /// Example params structure:
    /// {
    ///     "name": "packets-received"
    /// }
    ///
    /// @param name name of the command (ignored, should be "statistic-remove")
    /// @param params structure containing a map that contains "name" element
    /// @return answer containing confirmation
    static isc::data::ConstElementPtr
    statisticRemoveHandler(const std::string& name,
                           const isc::data::ConstElementPtr& params);

    /// @brief Handles statistic-sample-age-set command
    ///
    /// This method handles statistic-sample-age-set command,
    /// which sets max_sample_age_ limit of a given statistic
    /// and leaves max_sample_count_ disabled.
    /// It expects two parameters stored in params map:
    /// name: name of the statistic
    /// duration: time limit expressed as a number of seconds
    ///
    /// Example params structure:
    /// {
    ///     "name": "packets-received",
    ///     "duration": 1245
    /// }
    ///
    /// @param name name of the command (ignored, should be "statistic-sample-age-set")
    /// @param params structure containing a map that contains "name" and "duration"
    /// @return answer containing information about successfully setup limit of statistic
    static isc::data::ConstElementPtr
    statisticSetMaxSampleAgeHandler(const std::string& name,
                                    const isc::data::ConstElementPtr& params);

    /// @brief Handles statistic-sample-count-set command
    ///
    /// This method handles statistic-sample-count-set command,
    /// which sets max_sample_count_ limit of a given statistic
    /// and leaves max_sample_age_ disabled.
    /// It expects two parameters stored in params map:
    /// name: name of the statistic
    /// max-samples: count limit
    ///
    /// Example params structure:
    /// {
    ///     "name": "packets-received",
    ///     "max-samples": 15
    /// }
    ///
    /// @param name name of the command (ignored, should be "statistic-sample-count-set")
    /// @param params structure containing a map that contains "name" and "max-samples"
    /// @return answer containing information about successfully setup limit of statistic
    static isc::data::ConstElementPtr
    statisticSetMaxSampleCountHandler(const std::string& name,
                                      const isc::data::ConstElementPtr& params);

    /// @brief Handles statistic-get-all command
    ///
    /// This method handles statistic-get-all command, which returns values
    /// of all statistics. Params parameter is ignored.
    ///
    /// @param name name of the command (ignored, should be "statistic-get-all")
    /// @param params ignored
    /// @return answer containing values of all statistic
    static isc::data::ConstElementPtr
    statisticGetAllHandler(const std::string& name,
                           const isc::data::ConstElementPtr& params);

    /// @brief Handles statistic-reset-all command
    ///
    /// This method handles statistic-reset-all command, which sets values of
    /// all statistics back to zero. Params parameter is ignored.
    ///
    /// @param name name of the command (ignored, should be "statistic-reset-all")
    /// @param params ignored
    /// @return answer confirming success of this operation
    static isc::data::ConstElementPtr
    statisticResetAllHandler(const std::string& name,
                             const isc::data::ConstElementPtr& params);

    /// @brief Handles statistic-remove-all command
    ///
    /// @note The statistic-remove-all command was deprecated.
    ///
    /// This method handles statistic-remove-all command, which removes all
    /// statistics. Params parameter is ignored.
    ///
    /// @param name name of the command (ignored, should be "statistic-remove-all")
    /// @param params ignored
    /// @return answer confirming success of this operation
    static isc::data::ConstElementPtr
    statisticRemoveAllHandler(const std::string& name,
                              const isc::data::ConstElementPtr& params);

    /// @brief Handles statistic-sample-age-set-all command
    ///
    /// This method handles statistic-sample-age-set-all command,
    /// which sets max_sample_age_ limit to all statistics and the default.
    /// It expects one parameter stored in params map:
    /// duration: limit expressed as a number of seconds
    ///
    /// Example params structure:
    /// {
    ///     "duration": 1245
    /// }
    ///
    /// @param params structure containing a map that contains "duration"
    /// @return answer confirming success of this operation
    isc::data::ConstElementPtr
    statisticSetMaxSampleAgeAllHandler(const isc::data::ConstElementPtr& params);

    /// @brief Handles statistic-sample-count-set-all command
    ///
    /// This method handles statistic-sample-count-set-all command,
    /// which sets max_sample_count_ limit of all statistics and the default.
    /// It expects one parameter stored in params map:
    /// max-samples: count limit
    /// The value 0 is out of range.
    ///
    /// Example params structure:
    /// {
    ///     "max-samples": 15
    /// }
    ///
    /// @param params structure containing a map that contains "max-samples"
    /// @return answer confirming success of this operation
    isc::data::ConstElementPtr
    statisticSetMaxSampleCountAllHandler(const isc::data::ConstElementPtr& params);

    /// @}

private:

    /// @private

    /// @brief Private constructor.
    ///
    /// StatsMgr is a singleton. It should be accessed using @ref instance
    /// method.
    StatsMgr();

    /// @public

    /// @brief Sets a given statistic to specified value (internal version).
    ///
    /// This template method sets statistic identified by name to a value
    /// specified by value. This internal method is used by public @ref setValue
    /// methods.
    ///
    /// @tparam DataType one of int64_t, double, StatsDuration or string
    /// @param name name of the statistic
    /// @param value specified statistic will be set to this value
    /// @throw InvalidStatType is statistic exists and has a different type.
    template<typename DataType>
    void setValueInternal(const std::string& name, DataType value) {
        // If we want to log each observation, here would be the best place for it.
        ObservationPtr stat = getObservationInternal(name);
        if (stat) {
            stat->setValue(value);
        } else {
            stat.reset(new Observation(name, value));
            addObservationInternal(stat);
        }
    }

    /// @public

    /// @brief Adds specified value to a given statistic (internal version).
    ///
    /// This template method adds specified value to a given statistic (identified
    /// by name to a value). This internal method is used by public @ref setValue
    /// methods.
    ///
    /// @tparam DataType one of int64_t, double, StatsDuration or string
    /// @param name name of the statistic
    /// @param value specified statistic will be set to this value
    /// @throw InvalidStatType is statistic exists and has a different type.
    template<typename DataType>
    void addValueInternal(const std::string& name, DataType value) {
        // If we want to log each observation, here would be the best place for it.
        ObservationPtr existing = getObservationInternal(name);
        if (!existing) {
            // We tried to add to a non-existing statistic. We can recover from
            // that. Simply add the new incremental value as a new statistic and
            // we're done.
            setValueInternal(name, value);
            return;
        } else {
            // Let's hope it is of correct type. If not, the underlying
            // addValue() method will throw.
            existing->addValue(value);
        }
    }

    /// @public

    /// @brief Adds a new observation.
    ///
    /// That's an utility method used by public @ref setValue() and
    /// @ref addValue() methods.
    /// Calls @ref addObservationInternal() method in a thread safe context.
    ///
    /// @param stat observation
    void addObservation(const ObservationPtr& stat);

    /// @public

    /// @brief Adds a new observation in a thread safe context.
    ///
    /// That's an utility method used by public @ref setValue() and
    /// @ref addValue() methods.
    /// Should be called in a thread safe context.
    ///
    /// @param stat observation
    void addObservationInternal(const ObservationPtr& stat);

    /// @private

    /// @brief Tries to delete an observation.
    ///
    /// Calls @ref deleteObservationInternal() method in a thread safe context.
    ///
    /// @param name of the statistic to be deleted
    /// @return true if deleted, false if not found
    bool deleteObservation(const std::string& name);

    /// @private

    /// @brief Tries to delete an observation in a thread safe context.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @param name of the statistic to be deleted
    /// @return true if deleted, false if not found
    bool deleteObservationInternal(const std::string& name);

    /// @private

    /// @brief Determines maximum age of samples.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @param name name of the observation
    /// @param duration determines maximum age of samples
    /// @return true if successful, false if there's no such statistic
    bool setMaxSampleAgeInternal(const std::string& name, const StatsDuration& duration);

    /// @private

    /// @brief Determines how many samples of a given statistic should be kept.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @param name name of the observation
    /// @param max_samples how many samples of a given statistic should be kept
    /// @return true if successful, false if there's no such statistic
    bool setMaxSampleCountInternal(const std::string& name, uint32_t max_samples);

    /// @private

    /// @brief Set duration limit for all collected statistics.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @param duration determines maximum age of samples
    void setMaxSampleAgeAllInternal(const StatsDuration& duration);

    /// @private

    /// @brief Set count limit for all collected statistics.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @param max_samples how many samples of a given statistic should be kept
    void setMaxSampleCountAllInternal(uint32_t max_samples);

    /// @private

    /// @brief Set default duration limit.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @param duration default maximum age of samples to keep.
    void setMaxSampleAgeDefaultInternal(const StatsDuration& duration);

    /// @brief Set default count limit.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @param max_samples default maximum number of samples to keep.
    /// (0 means to disable count limit and enable age limit)
    void setMaxSampleCountDefaultInternal(uint32_t max_samples);

    /// @private

    /// @brief Get default duration limit.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @return default maximum age of samples to keep.
    const StatsDuration& getMaxSampleAgeDefaultInternal() const;

    /// @brief Get default count limit.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @return default maximum number of samples to keep.
    /// (0 means that count limit was disabled)
    uint32_t getMaxSampleCountDefaultInternal() const;

    /// @private

    /// @brief Resets specified statistic.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @param name name of the statistic to be reset.
    /// @return true if successful, false if there's no such statistic
    bool resetInternal(const std::string& name);

    /// @private

    /// @brief Removes specified statistic.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @param name name of the statistic to be removed.
    /// @return true if successful, false if there's no such statistic
    bool delInternal(const std::string& name);

    /// @private

    /// @brief Resets all collected statistics back to zero.
    ///
    /// Should be called in a thread safe context.
    void resetAllInternal();

    /// @private

    /// @brief Removes all collected statistics.
    ///
    /// Should be called in a thread safe context.
    void removeAllInternal();

    /// @private

    /// @brief Returns size of specified statistic.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @param name name of the statistic which size should be return.
    /// @return size of specified statistic, 0 means lack of given statistic.
    size_t getSizeInternal(const std::string& name) const;

    /// @private

    /// @brief Returns number of available statistics.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @return number of recorded statistics.
    size_t countInternal() const;

    /// @private

    /// @brief Returns a single statistic as a JSON structure.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @return JSON structures representing a single statistic
    isc::data::ConstElementPtr getInternal(const std::string& name) const;

    /// @private

    /// @brief Returns all statistics as a JSON structure.
    ///
    /// Should be called in a thread safe context.
    ///
    /// @return JSON structures representing all statistics
    isc::data::ConstElementPtr getAllInternal() const;

    /// @private

    /// @brief Utility method that attempts to extract statistic name
    ///
    /// This method attempts to extract statistic name from the params
    /// structure. It is expected to be a map that contains 'name' element,
    /// that is of type string. If present as expected, statistic name
    /// set and true is returned. If missing or is of incorrect type, the reason
    /// is specified in reason parameter and false is returned.
    ///
    /// @param params parameters structure received in command
    /// @param name [out] name of the statistic (if no error detected)
    /// @param reason [out] failure reason (if error is detected)
    /// @return true (if everything is ok), false otherwise
    static bool getStatName(const isc::data::ConstElementPtr& params,
                            std::string& name,
                            std::string& reason);

    /// @private

    /// @brief Utility method that attempts to extract duration limit for
    /// a given statistic
    ///
    /// This method attempts to extract duration limit for a given statistic
    /// from the params structure.
    /// It is expected to be a map that contains four 'duration' elements: 'hours',
    /// 'minutes', 'seconds' and 'milliseconds'
    /// all are of type int. If present as expected, statistic duration
    /// limit is set and true is returned.
    /// If any of these four parameters is missing or is of incorrect type,
    /// the reason is specified in reason parameter and false is returned.
    ///
    /// @param params parameters structure received in command
    /// @param duration [out] duration limit for the statistic (if no error detected)
    /// @param reason [out] failure reason (if error is detected)
    /// @return true (if everything is ok), false otherwise
    static bool getStatDuration(const isc::data::ConstElementPtr& params,
                                StatsDuration& duration,
                                std::string& reason);

    /// @private

    /// @brief Utility method that attempts to extract count limit for
    /// a given statistic
    ///
    /// This method attempts to extract count limit for a given statistic
    /// from the params structure.
    /// It is expected to be a map that contains 'max-samples' element,
    /// that is of type int. If present as expected, statistic count
    /// limit (max_samples) is set and true is returned.
    /// If missing or is of incorrect type, the reason is specified in reason
    /// parameter and false is returned.
    ///
    /// @param params parameters structure received in command
    /// @param max_samples [out] count limit for the statistic (if no error detected)
    /// @param reason [out] failure reason (if error is detected)
    /// @return true (if everything is ok), false otherwise
    static bool getStatMaxSamples(const isc::data::ConstElementPtr& params,
                                  uint32_t& max_samples,
                                  std::string& reason);

    /// @brief This is a global context. All statistics will initially be stored here.
    StatContextPtr global_;

    /// @brief The mutex used to protect internal state.
    const boost::scoped_ptr<std::mutex> mutex_;
};

}  // namespace stats
}  // namespace isc

#endif // STATS_MGR