| --- |
| title: Creating New Statistics |
| --- |
| |
| <!-- |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| This example provides a programmatic code sample for creating and registering new statistics. |
| |
| For information about the `geode_statistics` API, see [Statistics API](gfe-cpp-api.html#concept_AB12290E89CA4724AC9F51EFF7B8B8EA__section_42764C6327944514AE4A26684D7EBCB0). |
| |
| ## Creating New Statistics Programmatically |
| |
| ``` pre |
| //Get StatisticsFactory |
| StatisticsFactory* factory = StatisticsFactory::getExistingInstance(); |
| |
| |
| //Define each StatisticDescriptor and put each in an array |
| StatisticDescriptor** statDescriptorArr = new StatisticDescriptor*[6]; |
| statDescriptorArr[0] = statFactory->createIntCounter("IntCounter", |
| "Test Statistic Descriptor Int Counter.","TestUnit"); |
| |
| statDescriptorArr[1] = statFactory->createIntGauge("IntGauge", |
| "Test Statistic Descriptor Int Gauge.","TestUnit"); |
| |
| statDescriptorArr[2] = statFactory->createLongCounter("LongCounter", |
| "Test Statistic Descriptor Long Counter.","TestUnit"); |
| |
| statDescriptorArr[3] = statFactory->createLongGauge("LongGauge", |
| "Test Statistic Descriptor Long Gauge.","TestUnit"); |
| |
| statDescriptorArr[4] = statFactory->createDoubleCounter("DoubleCounter", |
| "Test Statistic Descriptor Double Counter.","TestUnit"); |
| |
| statDescriptorArr[5] = statFactory->createDoubleGauge("DoubleGauge", |
| "Test Statistic Descriptor Double Gauge.","TestUnit"); |
| |
| |
| //Create a StatisticsType |
| StatisticsType* statsType = statFactory->createType("TestStatsType", |
| "Statistics for Unit Test.",statDescriptorArr, 6); |
| |
| //Create Statistics of a given type |
| Statistics* testStat = |
| factory->createStatistics(statsType,"TestStatistics"); |
| |
| |
| //Statistics are created and registered. Set and increment individual values |
| Int statIdIntCounter = statsType->nameToId("IntCounter"); |
| testStat->setInt(statIdIntCounter, 10 ); |
| testStat->incInt(statIdIntCounter, 1 ); |
| int currentValue = testStat->getInt(statIdIntCounter); |
| ``` |
| |
| |