Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 6505dd1

Browse files
authored
Initial stats implementation. (#8)
1 parent 25bd9f5 commit 6505dd1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+4163
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Build Status][travis-image]][travis-url]
44

55
OpenCensus is a toolkit for collecting application performance and behavior data. It currently
6-
includes an API for tracing.
6+
includes an API for tracing and stats.
77

88
This library is currently in alpha: the API is in the process of being
99
finalized; much of the implementation will be replaced with a more optimized
@@ -18,12 +18,16 @@ This is not an officially supported Google product.
1818

1919
Please refer to
2020
[`trace/examples/span_example.cc`](opencensus/trace/examples/span_example.cc)
21+
for tracing and
22+
[`stats/examples/view_and_recording_example.cc`](opencensus/stats/examples/view_and_recording_example.cc)
23+
for stats.
2124

2225
## Directory structure
2326

2427
* [`opencensus/`](opencensus) prefix to get `#include` paths like `opencensus/trace/span.h`
2528
* [`common/`](opencensus/common) - Provides common libraries and components for OpenCensus.
2629
* [`doc/`](opencensus/doc) - Documentation for our APIs, coding style, etc.
30+
* [`stats/`](opencensus/stats) - OpenCensus stats API.
2731
* [`trace/`](opencensus/trace) - OpenCensus tracing API.
2832

2933
## Language support

opencensus/stats/BUILD

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# OpenCensus C++ Stats library.
2+
#
3+
# Copyright 2017, OpenCensus Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
licenses(["notice"]) # Apache License 2.0
18+
19+
package(default_visibility = ["//visibility:private"])
20+
21+
# The public stats API.
22+
cc_library(
23+
name = "stats",
24+
hdrs = ["stats.h"],
25+
visibility = ["//visibility:public"],
26+
deps = [
27+
":core",
28+
":export",
29+
":recording",
30+
],
31+
)
32+
33+
cc_library(
34+
name = "core",
35+
srcs = [
36+
"internal/aggregation.cc",
37+
"internal/aggregation_window.cc",
38+
"internal/bucket_boundaries.cc",
39+
"internal/distribution.cc",
40+
"internal/measure.cc",
41+
"internal/measure_descriptor.cc",
42+
"internal/measure_registry.cc",
43+
"internal/measure_registry_impl.cc",
44+
"internal/stats_manager.cc",
45+
"internal/view_data.cc",
46+
"internal/view_data_impl.cc",
47+
"internal/view_descriptor.cc",
48+
],
49+
hdrs = [
50+
"aggregation.h",
51+
"aggregation_window.h",
52+
"bucket_boundaries.h",
53+
"distribution.h",
54+
"internal/measure_registry_impl.h",
55+
"internal/stats_manager.h",
56+
"internal/view_data_impl.h",
57+
"measure.h",
58+
"measure_descriptor.h",
59+
"measure_registry.h",
60+
"view_data.h",
61+
"view_descriptor.h",
62+
],
63+
deps = [
64+
"@com_google_absl//absl/base:core_headers",
65+
"@com_google_absl//absl/memory",
66+
"@com_google_absl//absl/strings",
67+
"@com_google_absl//absl/synchronization",
68+
"@com_google_absl//absl/time",
69+
"@com_google_absl//absl/types:optional",
70+
"@com_google_absl//absl/types:span",
71+
"//opencensus/common/internal:stats_object",
72+
"//opencensus/common/internal:string_vector_hash",
73+
],
74+
)
75+
76+
cc_library(
77+
name = "recording",
78+
srcs = ["internal/recording.cc"],
79+
hdrs = ["recording.h"],
80+
deps = [
81+
":core",
82+
"@com_google_absl//absl/strings",
83+
],
84+
)
85+
86+
cc_library(
87+
name = "export",
88+
srcs = [
89+
"internal/stats_exporter.cc",
90+
"internal/view.cc",
91+
],
92+
hdrs = [
93+
"stats_exporter.h",
94+
"view.h",
95+
],
96+
deps = [
97+
":core",
98+
"@com_google_absl//absl/base:core_headers",
99+
"@com_google_absl//absl/memory",
100+
"@com_google_absl//absl/strings",
101+
"@com_google_absl//absl/synchronization",
102+
"@com_google_absl//absl/time",
103+
],
104+
)
105+
106+
# Tests
107+
# ========================================================================= #
108+
109+
cc_test(
110+
name = "debug_string_test",
111+
srcs = ["internal/debug_string_test.cc"],
112+
deps = [
113+
":core",
114+
"@com_google_absl//absl/time",
115+
"@com_google_googletest//:gtest_main",
116+
],
117+
)
118+
119+
cc_test(
120+
name = "distribution_test",
121+
srcs = ["internal/distribution_test.cc"],
122+
deps = [
123+
":core",
124+
"@com_google_googletest//:gtest_main",
125+
],
126+
)
127+
128+
cc_test(
129+
name = "bucket_boundaries_test",
130+
srcs = ["internal/bucket_boundaries_test.cc"],
131+
deps = [
132+
":core",
133+
"@com_google_googletest//:gtest_main",
134+
],
135+
)
136+
137+
cc_test(
138+
name = "measure_registry_test",
139+
srcs = ["internal/measure_registry_test.cc"],
140+
deps = [
141+
":core",
142+
"@com_google_absl//absl/strings",
143+
"@com_google_googletest//:gtest_main",
144+
],
145+
)
146+
147+
cc_test(
148+
name = "stats_exporter_test",
149+
srcs = ["internal/stats_exporter_test.cc"],
150+
deps = [
151+
":core",
152+
":export",
153+
"@com_google_absl//absl/memory",
154+
"@com_google_absl//absl/time",
155+
"@com_google_googletest//:gtest_main",
156+
],
157+
)
158+
159+
cc_test(
160+
name = "stats_manager_test",
161+
srcs = ["internal/stats_manager_test.cc"],
162+
deps = [
163+
":core",
164+
":export",
165+
":recording",
166+
"@com_google_absl//absl/types:optional",
167+
"@com_google_googletest//:gtest_main",
168+
],
169+
)
170+
171+
cc_test(
172+
name = "view_data_impl_test",
173+
srcs = ["internal/view_data_impl_test.cc"],
174+
deps = [
175+
":core",
176+
"@com_google_absl//absl/time",
177+
"@com_google_googletest//:gtest_main",
178+
],
179+
)

opencensus/stats/aggregation.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2017, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef OPENCENSUS_STATS_AGGREGATION_H_
16+
#define OPENCENSUS_STATS_AGGREGATION_H_
17+
18+
#include <string>
19+
#include <utility>
20+
21+
#include "opencensus/stats/bucket_boundaries.h"
22+
23+
namespace opencensus {
24+
namespace stats {
25+
26+
// Aggregation defines how to aggregate data for each view. See the static
27+
// constructors for details of the various options.
28+
// Aggregation is immutable.
29+
class Aggregation final {
30+
public:
31+
// Count aggregation counts the number of records, ignoring their individual
32+
// values.
33+
static Aggregation Count() {
34+
return Aggregation(Type::kCount, BucketBoundaries::Explicit({}));
35+
}
36+
37+
// Sum aggregation sums all records.
38+
static Aggregation Sum() {
39+
return Aggregation(Type::kSum, BucketBoundaries::Explicit({}));
40+
}
41+
42+
// Distribution aggregation records the number of records in each bucket
43+
// defined by 'buckets', and calculates distribution stats from that.
44+
static Aggregation Distribution(BucketBoundaries buckets) {
45+
return Aggregation(Type::kDistribution, std::move(buckets));
46+
}
47+
48+
enum class Type {
49+
kCount,
50+
kSum,
51+
kDistribution,
52+
};
53+
54+
Type type() const { return type_; }
55+
const BucketBoundaries& bucket_boundaries() const {
56+
return bucket_boundaries_;
57+
}
58+
59+
std::string DebugString() const;
60+
61+
bool operator==(const Aggregation& other) const {
62+
return type_ == other.type_ &&
63+
bucket_boundaries_ == other.bucket_boundaries_;
64+
}
65+
bool operator!=(const Aggregation& other) const { return !(*this == other); }
66+
67+
private:
68+
Aggregation(Type type, BucketBoundaries buckets)
69+
: type_(type), bucket_boundaries_(std::move(buckets)) {}
70+
71+
Type type_;
72+
// Ignored except if type_ == kDistribution.
73+
BucketBoundaries bucket_boundaries_;
74+
};
75+
76+
} // namespace stats
77+
} // namespace opencensus
78+
79+
#endif // OPENCENSUS_STATS_AGGREGATION_H_
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2017, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef OPENCENSUS_STATS_AGGREGATION_WINDOW_H_
16+
#define OPENCENSUS_STATS_AGGREGATION_WINDOW_H_
17+
18+
#include <string>
19+
20+
#include "absl/time/time.h"
21+
22+
namespace opencensus {
23+
namespace stats {
24+
25+
// AggregationWindow defines the time range over which recorded data is
26+
// aggregated for each view.
27+
// AggregationWindow is immutable.
28+
class AggregationWindow final {
29+
public:
30+
// Cumulative aggregation accumulates data over the lifetime of the process.
31+
static AggregationWindow Cumulative() {
32+
return AggregationWindow(Type::kCumulative, absl::InfiniteDuration());
33+
}
34+
35+
// Interval aggregation keeps a rolling total of usage over the previous
36+
// 'interval' of time.
37+
static AggregationWindow Interval(absl::Duration interval) {
38+
return AggregationWindow(Type::kInterval, interval);
39+
}
40+
41+
enum class Type {
42+
kCumulative,
43+
kInterval,
44+
};
45+
46+
Type type() const { return type_; }
47+
absl::Duration duration() const { return duration_; }
48+
49+
std::string DebugString() const;
50+
51+
bool operator==(const AggregationWindow& other) const {
52+
return type_ == other.type_ && duration_ == other.duration_;
53+
}
54+
bool operator!=(const AggregationWindow& other) const {
55+
return !(*this == other);
56+
}
57+
58+
private:
59+
AggregationWindow(Type type, absl::Duration duration)
60+
: type_(type), duration_(duration) {}
61+
62+
Type type_;
63+
// Should always be InfiniteDuration if type_ == kCumulative, to simplify
64+
// equality checking.
65+
absl::Duration duration_;
66+
};
67+
68+
} // namespace stats
69+
} // namespace opencensus
70+
71+
#endif // OPENCENSUS_STATS_AGGREGATION_WINDOW_H_

0 commit comments

Comments
 (0)