atscppapi  1.0.9
C++ wrapper for Apache Traffic Server API
 All Classes Files Functions Enumerations Enumerator Macros
Stat.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 LinkedIn Corp. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of the license at
5  * http://www.apache.org/licenses/LICENSE-2.0
6  *
7  * Unless required by applicable law or agreed to in writing, software distributed under the
8  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
9  * either express or implied.
10  *
11  */
12 
13 /**
14  * @file Stat.h
15  * @author Brian Geffon
16  * @author Manjesh Nilange
17  */
18 
19 #pragma once
20 #ifndef ATSCPPAPI_STAT_H_
21 #define ATSCPPAPI_STAT_H_
22 
23 #include <atscppapi/noncopyable.h>
24 #include <stdint.h>
25 #include <string>
26 
27 namespace atscppapi {
28 
29 /**
30  * @brief A Stat is an atomic variable that can be used to store counters, averages, time averages, or summations.
31  *
32  * All stats are exposed through the traffic_line program included with Apache Traffic Server. Additionally,
33  * if you've enabled HttpStats all Stats you define will be displayed there. Stats can be read via
34  * traffic_line -r stat_name.
35  *
36  * Stats are very easy to use, here is a simple example of how you can create a counter and increment its
37  * value:
38  * \code
39  * Stat stat;
40  * stat.init("stat_name");
41  stat.inc();
42  * \endcode
43  *
44  * A full example is available in examples/stat_example/.
45  */
46 class Stat : noncopyable {
47 public:
48  /**
49  * The available Stat types.
50  */
51  enum SyncType {
52  SYNC_SUM = 0, /**< The stat will sum all values from a stat.inc(VAL) */
53  SYNC_COUNT, /**< The stat will count all calls to stat.inc(VAL) */
54  SYNC_AVG, /**< The stat will keep an average after call calls to stat.inc(VAL) */
55  SYNC_TIMEAVG /**< The stat will keep a time average of all calls to stat.inc(VAL) */
56  };
57 
58  Stat();
59  ~Stat();
60 
61  /**
62  * You must initialize your Stat with a call to this init() method.
63  *
64  * @param name The string name of the stat, this will be visbible via traffic_line -r, or through http stats.
65  * @param type The SyncType of the Stat, this decides how TrafficServer will treat your inputs. The default
66  * value is SYNC_COUNT.
67  * @param persistent This determines if your Stats will persist, the default value is false.
68  * @return True if the stat was successfully created and false otherwise.
69  *
70  * @see SyncType
71  */
72  bool init(std::string name, Stat::SyncType type = SYNC_COUNT, bool persistent = false);
73 
74  /**
75  * This method allows you to increment a stat by a certain amount.
76  * @param amount the amount to increment the stat by the default value is 1.
77  */
78  void increment(int64_t amount = 1);
79 
80  /**
81  * This method allows you to decrement a stat by a certain amount.
82  * @param amount the amount to decrement the stat by the default value is 1.
83  */
84  void decrement(int64_t amount = 1);
85 
86  /**
87  * This method returns the current value of the stat.
88  * @return The value of the stat.
89  */
90  int64_t get() const;
91 
92  /** This method sets the value of the stat.
93  * @param value the value to set the stat to.
94  */
95  void set(int64_t value);
96 private:
97  int stat_id_; /**< The internal stat ID */
98 };
99 
100 } /* atscppapi */
101 
102 
103 #endif /* ATSCPPAPI_STAT_H_ */