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;
3940constexpr size_t kAnnotationStringLen = 256 ;
4041constexpr 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-
6243void 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