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

Commit f6151ba

Browse files
authored
Factor out SetTimestamp code. (#393)
Use opencensus::common::SetTimestamp() everywhere.
1 parent 83d38e1 commit f6151ba

12 files changed

Lines changed: 203 additions & 70 deletions

File tree

opencensus/common/internal/BUILD

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ cc_library(
5959
deps = ["@com_google_absl//absl/hash"],
6060
)
6161

62+
cc_library(
63+
name = "timestamp",
64+
srcs = ["timestamp.cc"],
65+
hdrs = ["timestamp.h"],
66+
copts = DEFAULT_COPTS,
67+
deps = [
68+
"@com_google_absl//absl/time",
69+
"@com_google_protobuf//:protobuf",
70+
],
71+
)
72+
6273
# Tests
6374
# ========================================================================= #
6475

@@ -106,3 +117,30 @@ cc_test(
106117
"@com_google_googletest//:gtest_main",
107118
],
108119
)
120+
121+
cc_binary(
122+
name = "timestamp_benchmark",
123+
testonly = 1,
124+
srcs = ["timestamp_benchmark.cc"],
125+
copts = TEST_COPTS,
126+
#linkopts = ["-pthread"], # Required for absl/synchronization bits.
127+
linkstatic = 1,
128+
deps = [
129+
":timestamp",
130+
"@com_github_google_benchmark//:benchmark",
131+
"@com_google_absl//absl/time",
132+
"@com_google_protobuf//:protobuf",
133+
],
134+
)
135+
136+
cc_test(
137+
name = "timestamp_test",
138+
srcs = ["timestamp_test.cc"],
139+
copts = TEST_COPTS,
140+
deps = [
141+
":timestamp",
142+
"@com_google_absl//absl/time",
143+
"@com_google_googletest//:gtest_main",
144+
"@com_google_protobuf//:protobuf",
145+
],
146+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2019, 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+
#include "opencensus/common/internal/timestamp.h"
16+
17+
#include <cstdint>
18+
19+
#include "absl/time/time.h"
20+
#include "google/protobuf/timestamp.pb.h"
21+
22+
namespace opencensus {
23+
namespace common {
24+
25+
void SetTimestamp(absl::Time time, google::protobuf::Timestamp* proto) {
26+
const int64_t seconds = absl::ToUnixSeconds(time);
27+
proto->set_seconds(seconds);
28+
proto->set_nanos(
29+
absl::ToInt64Nanoseconds(time - absl::FromUnixSeconds(seconds)));
30+
}
31+
32+
} // namespace common
33+
} // namespace opencensus
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2019, 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_COMMON_INTERNAL_TIMESTAMP_H_
16+
#define OPENCENSUS_COMMON_INTERNAL_TIMESTAMP_H_
17+
18+
#include "absl/time/time.h"
19+
#include "google/protobuf/timestamp.pb.h"
20+
21+
namespace opencensus {
22+
namespace common {
23+
24+
// Converts time to proto.
25+
void SetTimestamp(absl::Time time, google::protobuf::Timestamp* proto);
26+
27+
} // namespace common
28+
} // namespace opencensus
29+
30+
#endif // OPENCENSUS_COMMON_INTERNAL_TIMESTAMP_H_
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2019, 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+
#include "absl/time/clock.h"
16+
#include "absl/time/time.h"
17+
#include "benchmark/benchmark.h"
18+
#include "google/protobuf/timestamp.pb.h"
19+
#include "opencensus/common/internal/timestamp.h"
20+
21+
namespace {
22+
23+
void BM_SetTimestamp(benchmark::State& state) {
24+
absl::Time t = absl::Now();
25+
for (auto _ : state) {
26+
google::protobuf::Timestamp proto;
27+
::opencensus::common::SetTimestamp(t, &proto);
28+
}
29+
}
30+
BENCHMARK(BM_SetTimestamp);
31+
32+
} // namespace
33+
34+
BENCHMARK_MAIN();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2019, 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+
#include "opencensus/common/internal/timestamp.h"
16+
17+
#include "absl/time/time.h"
18+
#include "google/protobuf/timestamp.pb.h"
19+
#include "gtest/gtest.h"
20+
21+
namespace {
22+
23+
TEST(TimestampTest, Zero) {
24+
absl::Time t;
25+
google::protobuf::Timestamp proto;
26+
::opencensus::common::SetTimestamp(t, &proto);
27+
EXPECT_EQ(0, proto.seconds());
28+
EXPECT_EQ(0, proto.nanos());
29+
}
30+
31+
TEST(TimestampTest, NonZero) {
32+
absl::Time t =
33+
absl::FromUnixSeconds(1561234567) + absl::Nanoseconds(123456789);
34+
google::protobuf::Timestamp proto;
35+
::opencensus::common::SetTimestamp(t, &proto);
36+
EXPECT_EQ(1561234567, proto.seconds());
37+
EXPECT_EQ(123456789, proto.nanos());
38+
}
39+
40+
} // namespace

opencensus/exporters/stats/stackdriver/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ cc_library(
5050
hdrs = ["internal/stackdriver_utils.h"],
5151
copts = DEFAULT_COPTS,
5252
deps = [
53+
"//opencensus/common/internal:timestamp",
5354
"//opencensus/stats",
5455
"@com_google_absl//absl/base",
5556
"@com_google_absl//absl/base:core_headers",

opencensus/exporters/stats/stackdriver/internal/stackdriver_utils.cc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "google/monitoring/v3/common.pb.h"
3030
#include "google/monitoring/v3/metric.pb.h"
3131
#include "google/protobuf/timestamp.pb.h"
32+
#include "opencensus/common/internal/timestamp.h"
3233
#include "opencensus/stats/stats.h"
3334

3435
namespace opencensus {
@@ -226,9 +227,11 @@ std::vector<google::monitoring::v3::TimeSeries> MakeTimeSeries(
226227
// metrics.
227228
if (view_descriptor.aggregation().type() !=
228229
opencensus::stats::Aggregation::Type::kLastValue) {
229-
SetTimestamp(data.start_time(), interval->mutable_start_time());
230+
opencensus::common::SetTimestamp(data.start_time(),
231+
interval->mutable_start_time());
230232
}
231-
SetTimestamp(data.end_time(), interval->mutable_end_time());
233+
opencensus::common::SetTimestamp(data.end_time(),
234+
interval->mutable_end_time());
232235
if (add_task_label) {
233236
(*base_time_series.mutable_metric()->mutable_labels())[kOpenCensusTaskKey] =
234237
std::string(opencensus_task);
@@ -248,13 +251,6 @@ std::vector<google::monitoring::v3::TimeSeries> MakeTimeSeries(
248251
return {};
249252
}
250253

251-
void SetTimestamp(absl::Time time, google::protobuf::Timestamp* proto) {
252-
const int64_t seconds = absl::ToUnixSeconds(time);
253-
proto->set_seconds(seconds);
254-
proto->set_nanos(
255-
absl::ToInt64Nanoseconds(time - absl::FromUnixSeconds(seconds)));
256-
}
257-
258254
} // namespace stats
259255
} // namespace exporters
260256
} // namespace opencensus

opencensus/exporters/stats/stackdriver/internal/stackdriver_utils.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ std::vector<google::monitoring::v3::TimeSeries> MakeTimeSeries(
6262
const opencensus::stats::ViewData& data, bool add_task_label,
6363
absl::string_view opencensus_task);
6464

65-
// Populates proto based on the given time.
66-
void SetTimestamp(absl::Time time, google::protobuf::Timestamp* proto);
67-
6865
} // namespace stats
6966
} // namespace exporters
7067
} // namespace opencensus

opencensus/exporters/trace/ocagent/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ cc_library(
3030
deps = [
3131
"//opencensus/common:version",
3232
"//opencensus/common/internal:hostname",
33+
"//opencensus/common/internal:timestamp",
3334
"//opencensus/common/internal/grpc:status",
3435
"//opencensus/common/internal/grpc:with_user_agent",
3536
"//opencensus/trace",

opencensus/exporters/trace/ocagent/internal/ocagent_exporter.cc

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "opencensus/common/internal/grpc/status.h"
2727
#include "opencensus/common/internal/grpc/with_user_agent.h"
2828
#include "opencensus/common/internal/hostname.h"
29+
#include "opencensus/common/internal/timestamp.h"
2930
#include "opencensus/common/version.h"
3031
#include "opencensus/trace/exporter/span_data.h"
3132
#include "opencensus/trace/exporter/span_exporter.h"
@@ -39,26 +40,6 @@ constexpr size_t kAttributeStringLen = 256;
3940
constexpr size_t kAnnotationStringLen = 256;
4041
constexpr size_t kDisplayNameStringLen = 128;
4142

42-
bool Validate(const google::protobuf::Timestamp &t) {
43-
const auto sec = t.seconds();
44-
const auto ns = t.nanos();
45-
// sec must be [0001-01-01T00:00:00Z, 9999-12-31T23:59:59.999999999Z]
46-
if (sec < -62135596800 || sec > 253402300799) {
47-
return false;
48-
}
49-
if (ns < 0 || ns > 999999999) {
50-
return false;
51-
}
52-
return true;
53-
}
54-
55-
bool EncodeTimestampProto(absl::Time t, google::protobuf::Timestamp *proto) {
56-
const int64_t s = absl::ToUnixSeconds(t);
57-
proto->set_seconds(s);
58-
proto->set_nanos((t - absl::FromUnixSeconds(s)) / absl::Nanoseconds(1));
59-
return Validate(*proto);
60-
}
61-
6243
void SetTruncatableString(
6344
absl::string_view str, size_t max_len,
6445
::opencensus::proto::trace::v1::TruncatableString *t_str) {
@@ -135,9 +116,8 @@ void ConvertTimeEvents(const ::opencensus::trace::exporter::SpanData &span,
135116
::opencensus::proto::trace::v1::Span *proto_span) {
136117
for (const auto &annotation : span.annotations().events()) {
137118
auto event = proto_span->mutable_time_events()->add_time_event();
138-
139-
// Encode Timestamp
140-
EncodeTimestampProto(annotation.timestamp(), event->mutable_time());
119+
opencensus::common::SetTimestamp(annotation.timestamp(),
120+
event->mutable_time());
141121

142122
// Populate annotation.
143123
SetTruncatableString(annotation.event().description(), kAnnotationStringLen,
@@ -150,9 +130,8 @@ void ConvertTimeEvents(const ::opencensus::trace::exporter::SpanData &span,
150130

151131
for (const auto &message : span.message_events().events()) {
152132
auto event = proto_span->mutable_time_events()->add_time_event();
153-
154-
// Encode Timestamp
155-
EncodeTimestampProto(message.timestamp(), event->mutable_time());
133+
opencensus::common::SetTimestamp(message.timestamp(),
134+
event->mutable_time());
156135

157136
// Populate message event.
158137
event->mutable_message_event()->set_type(
@@ -222,9 +201,11 @@ void ConvertSpans(
222201
SetTruncatableString(from_span.name(), kDisplayNameStringLen,
223202
to_span->mutable_name());
224203
// 5. The start time of the span.
225-
EncodeTimestampProto(from_span.start_time(), to_span->mutable_start_time());
204+
opencensus::common::SetTimestamp(from_span.start_time(),
205+
to_span->mutable_start_time());
226206
// 6. The end time of the span.
227-
EncodeTimestampProto(from_span.end_time(), to_span->mutable_end_time());
207+
opencensus::common::SetTimestamp(from_span.end_time(),
208+
to_span->mutable_end_time());
228209

229210
// 7. Export Attributes
230211
ConvertAttributes(from_span, to_span);
@@ -300,8 +281,8 @@ void Handler::InitNode() {
300281

301282
identifier->set_host_name(::opencensus::common::Hostname());
302283
identifier->set_pid(getpid());
303-
304-
EncodeTimestampProto(absl::Now(), identifier->mutable_start_timestamp());
284+
opencensus::common::SetTimestamp(absl::Now(),
285+
identifier->mutable_start_timestamp());
305286

306287
auto library_info = nodeInfo_.mutable_library_info();
307288
library_info->set_language(

0 commit comments

Comments
 (0)