• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Stat.h

Go to the documentation of this file.
00001 /**
00002   Licensed to the Apache Software Foundation (ASF) under one
00003   or more contributor license agreements.  See the NOTICE file
00004   distributed with this work for additional information
00005   regarding copyright ownership.  The ASF licenses this file
00006   to you under the Apache License, Version 2.0 (the
00007   "License"); you may not use this file except in compliance
00008   with the License.  You may obtain a copy of the License at
00009 
00010       http://www.apache.org/licenses/LICENSE-2.0
00011 
00012   Unless required by applicable law or agreed to in writing, software
00013   distributed under the License is distributed on an "AS IS" BASIS,
00014   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015   See the License for the specific language governing permissions and
00016   limitations under the License.
00017  */
00018 /**
00019  * @file Stat.h
00020  */
00021 
00022 #pragma once
00023 #ifndef ATSCPPAPI_STAT_H_
00024 #define ATSCPPAPI_STAT_H_
00025 
00026 #include <atscppapi/noncopyable.h>
00027 #include <stdint.h>
00028 #include <string>
00029 
00030 namespace atscppapi {
00031 
00032 /**
00033  * @brief A Stat is an atomic variable that can be used to store counters, averages, time averages, or summations.
00034  *
00035  * All stats are exposed through the traffic_line program included with Apache Traffic Server. Additionally,
00036  * if you've enabled HttpStats all Stats you define will be displayed there. Stats can be read via
00037  * traffic_line -r stat_name.
00038  *
00039  * Stats are very easy to use, here is a simple example of how you can create a counter and increment its
00040  * value:
00041  * \code
00042  *  Stat stat;
00043  *  stat.init("stat_name");
00044     stat.inc();
00045  * \endcode
00046  *
00047  * A full example is available in examples/stat_example/.
00048  */
00049 class Stat : noncopyable {
00050 public:
00051   /**
00052    * The available Stat types.
00053    */
00054   enum SyncType {
00055     SYNC_SUM = 0, /**< The stat will sum all values from a stat.inc(VAL) */
00056     SYNC_COUNT, /**< The stat will count all calls to stat.inc(VAL) */
00057     SYNC_AVG, /**< The stat will keep an average after call calls to stat.inc(VAL) */
00058     SYNC_TIMEAVG /**< The stat will keep a time average of all calls to stat.inc(VAL) */
00059   };
00060 
00061   Stat();
00062   ~Stat();
00063 
00064   /**
00065    * You must initialize your Stat with a call to this init() method.
00066    *
00067    * @param name The string name of the stat, this will be visbible via traffic_line -r, or through http stats.
00068    * @param type The SyncType of the Stat, this decides how TrafficServer will treat your inputs. The default
00069    *   value is SYNC_COUNT.
00070    * @param persistent This determines if your Stats will persist, the default value is false.
00071    * @return True if the stat was successfully created and false otherwise.
00072    *
00073    * @see SyncType
00074    */
00075   bool init(std::string name, Stat::SyncType type = SYNC_COUNT, bool persistent = false);
00076 
00077   /**
00078    * This method allows you to increment a stat by a certain amount.
00079    * @param amount the amount to increment the stat by the default value is 1.
00080    */
00081   void increment(int64_t amount = 1);
00082 
00083   /**
00084    * This method allows you to decrement a stat by a certain amount.
00085    * @param amount the amount to decrement the stat by the default value is 1.
00086    */
00087   void decrement(int64_t amount = 1);
00088 
00089   /**
00090    * This method returns the current value of the stat.
00091    * @return The value of the stat.
00092    */
00093   int64_t get() const;
00094 
00095   /** This method sets the value of the stat.
00096    * @param value the value to set the stat to.
00097    */
00098   void set(int64_t value);
00099 private:
00100   int stat_id_; /**< The internal stat ID */
00101 };
00102 
00103 } /* atscppapi */
00104 
00105 
00106 #endif /* ATSCPPAPI_STAT_H_ */

Generated by  doxygen 1.7.1