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)
master
gaschler 2018-02-19 14:02:54 +01:00 committed by Wally B. Feed
parent 8e27db0f0e
commit 64bc194609
9 changed files with 385 additions and 0 deletions

View File

@ -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

View File

@ -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 <map>
#include <vector>
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_

View File

@ -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 <memory>
#include <string>
#include "cartographer/metrics/counter.h"
#include "cartographer/metrics/gauge.h"
#include "cartographer/metrics/histogram.h"
namespace cartographer {
namespace metrics {
template <typename MetricType>
class Family {
public:
virtual ~Family() = default;
virtual MetricType* Add(const std::map<std::string, std::string>& labels) = 0;
};
class FamilyFactory {
public:
virtual ~FamilyFactory() = default;
virtual Family<Counter>* NewCounterFamily(const std::string& name,
const std::string& description) = 0;
virtual Family<Gauge>* NewGaugeFamily(const std::string& name,
const std::string& description) = 0;
virtual Family<Histogram>* NewHistogramFamily(
const std::string& name, const std::string& description,
const Histogram::BucketBoundaries& boundaries) = 0;
};
} // namespace metrics
} // namespace cartographer
#endif // CARTOGRAPHER_METRICS_FAMILY_FACTORY_H_

View File

@ -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

View File

@ -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 <map>
#include <vector>
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_

View File

@ -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

View File

@ -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 <map>
#include <vector>
namespace cartographer {
namespace metrics {
class Histogram {
public:
using BucketBoundaries = std::vector<double>;
// 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_

View File

@ -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

View File

@ -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_