From 64bc194609b348caf73feaaeeef5188ab042d886 Mon Sep 17 00:00:00 2001 From: gaschler Date: Mon, 19 Feb 2018 14:02:54 +0100 Subject: [PATCH] Introduce interfaces for metrics (#907) Adds interfaces for metrics and metrics families. Adds creation of null (noop) metrics. Declares global register function. RFC=[0014](https://github.com/googlecartographer/rfcs/blob/master/text/0014-monitoring.md) --- cartographer/metrics/counter.cc | 39 ++++++++++++++++ cartographer/metrics/counter.h | 39 ++++++++++++++++ cartographer/metrics/family_factory.h | 54 ++++++++++++++++++++++ cartographer/metrics/gauge.cc | 42 +++++++++++++++++ cartographer/metrics/gauge.h | 42 +++++++++++++++++ cartographer/metrics/histogram.cc | 65 +++++++++++++++++++++++++++ cartographer/metrics/histogram.h | 44 ++++++++++++++++++ cartographer/metrics/register.cc | 30 +++++++++++++ cartographer/metrics/register.h | 30 +++++++++++++ 9 files changed, 385 insertions(+) create mode 100644 cartographer/metrics/counter.cc create mode 100644 cartographer/metrics/counter.h create mode 100644 cartographer/metrics/family_factory.h create mode 100644 cartographer/metrics/gauge.cc create mode 100644 cartographer/metrics/gauge.h create mode 100644 cartographer/metrics/histogram.cc create mode 100644 cartographer/metrics/histogram.h create mode 100644 cartographer/metrics/register.cc create mode 100644 cartographer/metrics/register.h diff --git a/cartographer/metrics/counter.cc b/cartographer/metrics/counter.cc new file mode 100644 index 0000000..8fae37d --- /dev/null +++ b/cartographer/metrics/counter.cc @@ -0,0 +1,39 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed 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. + */ + +#include "cartographer/metrics/counter.h" + +namespace cartographer { +namespace metrics { + +namespace { + +// Implementation of counter that does nothing. +class NullCounter : public Counter { + public: + void Increment() override{}; + void Increment(double) override{}; +}; + +} // namespace + +Counter* Counter::Null() { + static NullCounter null_counter; + return &null_counter; +} + +} // namespace metrics +} // namespace cartographer diff --git a/cartographer/metrics/counter.h b/cartographer/metrics/counter.h new file mode 100644 index 0000000..781d5e4 --- /dev/null +++ b/cartographer/metrics/counter.h @@ -0,0 +1,39 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed 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. + */ + +#ifndef CARTOGRAPHER_METRICS_COUNTER_H_ +#define CARTOGRAPHER_METRICS_COUNTER_H_ + +#include +#include + +namespace cartographer { +namespace metrics { + +class Counter { + public: + // Counter instance that does nothing. Safe for use in static initializers. + static Counter* Null(); + + virtual ~Counter() = default; + virtual void Increment() = 0; + virtual void Increment(double by_value) = 0; +}; + +} // namespace metrics +} // namespace cartographer + +#endif // CARTOGRAPHER_METRICS_COUNTER_H_ diff --git a/cartographer/metrics/family_factory.h b/cartographer/metrics/family_factory.h new file mode 100644 index 0000000..ca7be8d --- /dev/null +++ b/cartographer/metrics/family_factory.h @@ -0,0 +1,54 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed 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. + */ + +#ifndef CARTOGRAPHER_METRICS_FAMILY_FACTORY_H_ +#define CARTOGRAPHER_METRICS_FAMILY_FACTORY_H_ + +#include +#include + +#include "cartographer/metrics/counter.h" +#include "cartographer/metrics/gauge.h" +#include "cartographer/metrics/histogram.h" + +namespace cartographer { +namespace metrics { + +template +class Family { + public: + virtual ~Family() = default; + + virtual MetricType* Add(const std::map& labels) = 0; +}; + +class FamilyFactory { + public: + virtual ~FamilyFactory() = default; + + virtual Family* NewCounterFamily(const std::string& name, + const std::string& description) = 0; + virtual Family* NewGaugeFamily(const std::string& name, + const std::string& description) = 0; + virtual Family* NewHistogramFamily( + const std::string& name, const std::string& description, + const Histogram::BucketBoundaries& boundaries) = 0; +}; + +} // namespace metrics +} // namespace cartographer + +#endif // CARTOGRAPHER_METRICS_FAMILY_FACTORY_H_ diff --git a/cartographer/metrics/gauge.cc b/cartographer/metrics/gauge.cc new file mode 100644 index 0000000..6af2b0a --- /dev/null +++ b/cartographer/metrics/gauge.cc @@ -0,0 +1,42 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed 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. + */ + +#include "cartographer/metrics/gauge.h" + +namespace cartographer { +namespace metrics { + +namespace { + +// Implementation of gauge that does nothing. +class NullGauge : public Gauge { + public: + void Increment() override{}; + void Increment(double) override{}; + void Decrement() override{}; + void Decrement(double) override{}; + void Set(double) override{}; +}; + +} // namespace + +Gauge* Gauge::Null() { + static NullGauge null_gauge; + return &null_gauge; +} + +} // namespace metrics +} // namespace cartographer diff --git a/cartographer/metrics/gauge.h b/cartographer/metrics/gauge.h new file mode 100644 index 0000000..0b3c225 --- /dev/null +++ b/cartographer/metrics/gauge.h @@ -0,0 +1,42 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed 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. + */ + +#ifndef CARTOGRAPHER_METRICS_GAUGE_H_ +#define CARTOGRAPHER_METRICS_GAUGE_H_ + +#include +#include + +namespace cartographer { +namespace metrics { + +class Gauge { + public: + // Gauge instance that does nothing. Safe for use in static initializers. + static Gauge* Null(); + + virtual ~Gauge() = default; + virtual void Increment() = 0; + virtual void Increment(double by_value) = 0; + virtual void Decrement() = 0; + virtual void Decrement(double by_value) = 0; + virtual void Set(double value) = 0; +}; + +} // namespace metrics +} // namespace cartographer + +#endif // CARTOGRAPHER_METRICS_GAUGE_H_ diff --git a/cartographer/metrics/histogram.cc b/cartographer/metrics/histogram.cc new file mode 100644 index 0000000..d7d0cf2 --- /dev/null +++ b/cartographer/metrics/histogram.cc @@ -0,0 +1,65 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed 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. + */ + +#include "cartographer/metrics/histogram.h" + +#include "glog/logging.h" + +namespace cartographer { +namespace metrics { + +namespace { + +// Implementation of histogram that does nothing. +class NullHistogram : public Histogram { + public: + void Observe(double) override {} +}; + +} // namespace + +Histogram* Histogram::Null() { + static NullHistogram null_histogram; + return &null_histogram; +} + +Histogram::BucketBoundaries Histogram::FixedWidth(double width, + int num_finite_buckets) { + BucketBoundaries result; + double boundary = 0; + for (int i = 0; i < num_finite_buckets; ++i) { + boundary += width; + result.push_back(boundary); + } + return result; +} + +Histogram::BucketBoundaries Histogram::ScaledPowersOf(double base, + double scale_factor, + double max_value) { + CHECK_GT(base, 1); + CHECK_GT(scale_factor, 0); + BucketBoundaries result; + double boundary = scale_factor; + while (boundary < max_value) { + result.push_back(boundary); + boundary *= base; + } + return result; +} + +} // namespace metrics +} // namespace cartographer diff --git a/cartographer/metrics/histogram.h b/cartographer/metrics/histogram.h new file mode 100644 index 0000000..51978f8 --- /dev/null +++ b/cartographer/metrics/histogram.h @@ -0,0 +1,44 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed 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. + */ + +#ifndef CARTOGRAPHER_METRICS_HISTOGRAM_H_ +#define CARTOGRAPHER_METRICS_HISTOGRAM_H_ + +#include +#include + +namespace cartographer { +namespace metrics { + +class Histogram { + public: + using BucketBoundaries = std::vector; + + // Histogram instance that does nothing. Safe for use in static initializers. + static Histogram* Null(); + + static BucketBoundaries FixedWidth(double width, int num_finite_buckets); + static BucketBoundaries ScaledPowersOf(double base, double scale_factor, + double max_value); + + virtual ~Histogram() = default; + virtual void Observe(double value) = 0; +}; + +} // namespace metrics +} // namespace cartographer + +#endif // CARTOGRAPHER_METRICS_HISTOGRAM_H_ diff --git a/cartographer/metrics/register.cc b/cartographer/metrics/register.cc new file mode 100644 index 0000000..3b23e1e --- /dev/null +++ b/cartographer/metrics/register.cc @@ -0,0 +1,30 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed 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. + */ + +#include "cartographer/metrics/register.h" + +#include "cartographer/mapping_2d/pose_graph/constraint_builder.h" +#include "cartographer/mapping_3d/pose_graph/constraint_builder.h" + +namespace cartographer { +namespace metrics { + +void RegisterAllMetrics(FamilyFactory *registry) { + // TODO(gaschler): Register classes that instrument metrics. +} + +} // namespace metrics +} // namespace cartographer diff --git a/cartographer/metrics/register.h b/cartographer/metrics/register.h new file mode 100644 index 0000000..565da23 --- /dev/null +++ b/cartographer/metrics/register.h @@ -0,0 +1,30 @@ +/* + * Copyright 2018 The Cartographer Authors + * + * Licensed 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. + */ + +#ifndef CARTOGRAPHER_METRICS_REGISTER_H_ +#define CARTOGRAPHER_METRICS_REGISTER_H_ + +#include "cartographer/metrics/family_factory.h" + +namespace cartographer { +namespace metrics { + +void RegisterAllMetrics(FamilyFactory *registry); + +} // namespace metrics +} // namespace cartographer + +#endif // CARTOGRAPHER_METRICS_REGISTER_H_