1 /** @file
2 
3   Statistics example plugin.
4 
5   This plugin demonstrates the statistics API and also serves as a
6   regression test for TS-4840. If traffic_server is restarted, a
7   plugin ought to be able to safely reattach to its statistics.
8 
9   This source is included as an example in the developers so if you
10   change it, you may have to update the line numbers on
11   doc/developer-guide/plugins/adding-statistics.en.rst
12 
13   @section license License
14 
15   Licensed to the Apache Software Foundation (ASF) under one
16   or more contributor license agreements.  See the NOTICE file
17   distributed with this work for additional information
18   regarding copyright ownership.  The ASF licenses this file
19   to you under the Apache License, Version 2.0 (the
20   "License"); you may not use this file except in compliance
21   with the License.  You may obtain a copy of the License at
22 
23       http://www.apache.org/licenses/LICENSE-2.0
24 
25   Unless required by applicable law or agreed to in writing, software
26   distributed under the License is distributed on an "AS IS" BASIS,
27   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28   See the License for the specific language governing permissions and
29   limitations under the License.
30  */
31 
32 #include <ts/ts.h>
33 #include <cinttypes>
34 #include <ctime>
35 
36 #define PLUGIN_NAME "statistics"
37 
38 void
TSPluginInit(int,const char * [])39 TSPluginInit(int /* argc */, const char * /* argv */[])
40 {
41   TSPluginRegistrationInfo info;
42 
43   info.plugin_name   = PLUGIN_NAME;
44   info.vendor_name   = "Apache Software Foundation";
45   info.support_email = "dev@trafficserver.apache.org";
46 
47   int id;
48   const char name[] = "plugin." PLUGIN_NAME ".now";
49 
50   if (TSStatFindName(name, &id) == TS_ERROR) {
51     id = TSStatCreate(name, TS_RECORDDATATYPE_INT, TS_STAT_NON_PERSISTENT, TS_STAT_SYNC_SUM);
52     if (id == TS_ERROR) {
53       TSError("[%s] failed to register '%s'", PLUGIN_NAME, name);
54       return;
55     }
56   }
57 
58   TSError("[%s] %s registered with id %d", PLUGIN_NAME, name, id);
59 
60 #if DEBUG
61   TSReleaseAssert(id != TS_ERROR);
62 #endif
63 
64   // Set an initial value for our statistic.
65   TSStatIntSet(id, time(nullptr));
66 
67   // Increment the statistic as time passes.
68   TSStatIntIncrement(id, 1);
69 
70   TSDebug(PLUGIN_NAME, "%s is set to %" PRId64, name, TSStatIntGet(id));
71   TSReleaseAssert(TSPluginRegister(&info) == TS_SUCCESS);
72 }
73