This repository was archived by the owner on Apr 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Added Custom Recordable Implementation and Unit Tests #2
Merged
IlyaKobelevskiy
merged 4 commits into
GoogleCloudPlatform:master
from
snehilchopra:gcp_recordable
Jul 2, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2ebc105
Added Custom Recordable Implementation and Unit Tests
snehilchopra cd7fa32
Addressed reviews and assertions for SetAttribute() Method
snehilchopra c3a10f2
Modified SetIds() and the corresponding test
snehilchopra 8474cdf
Addressed further changes
snehilchopra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| #include "exporters/trace/gcp_exporter/recordable.h" | ||
| #include <assert.h> | ||
|
|
||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace exporter | ||
| { | ||
| namespace gcp | ||
| { | ||
|
|
||
| void Recordable::SetIds(trace::TraceId trace_id, | ||
| trace::SpanId span_id, | ||
| trace::SpanId parent_span_id) noexcept | ||
| { | ||
| std::array<char, 2*trace::TraceId::kSize> hex_trace_buf; | ||
| trace_id.ToLowerBase16(hex_trace_buf); | ||
| const std::string hex_trace(hex_trace_buf.data(), 2*trace::TraceId::kSize); | ||
|
|
||
| std::array<char, 2*trace::SpanId::kSize> hex_span_buf; | ||
| span_id.ToLowerBase16(hex_span_buf); | ||
| const std::string hex_span(hex_span_buf.data(), 2*trace::SpanId::kSize); | ||
|
|
||
| std::array<char, 2*trace::SpanId::kSize> hex_parent_span_buf; | ||
| parent_span_id.ToLowerBase16(hex_parent_span_buf); | ||
| const std::string hex_parent_span(hex_parent_span_buf.data(), 2*trace::SpanId::kSize); | ||
|
|
||
| // Get Project Id | ||
| const std::string project_id(getenv(kGCPEnvVar)); | ||
|
|
||
| span_.set_name(kProjectsPathStr + project_id + kTracesPathStr + hex_trace + kSpansPathStr + hex_span); | ||
| span_.set_span_id(hex_span); | ||
| span_.set_parent_span_id(hex_parent_span); | ||
| } | ||
|
|
||
|
|
||
| void Recordable::SetAttribute(nostd::string_view key, | ||
| const common::AttributeValue &&value) noexcept | ||
| { | ||
| // Get the protobuf span's map | ||
| auto map = span_.mutable_attributes()->mutable_attribute_map(); | ||
|
|
||
| if(nostd::holds_alternative<bool>(value)) | ||
| { | ||
| (*map)[std::string(key)].set_bool_value(nostd::get<bool>(value)); | ||
| } | ||
| else if (nostd::holds_alternative<int64_t>(value)) | ||
| { | ||
| (*map)[std::string(key)].set_int_value(nostd::get<int64_t>(value)); | ||
| } | ||
| else if (nostd::holds_alternative<nostd::string_view>(value)) | ||
| { | ||
| // TODO: Truncate string to 128 bytes | ||
| std::string value_str = std::string(nostd::get<nostd::string_view>(value)); | ||
| (*map)[std::string(key)].mutable_string_value()->set_value(value_str); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void Recordable::AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept | ||
| { | ||
| (void)name; | ||
| } | ||
|
|
||
|
|
||
| void Recordable::SetStatus(trace::CanonicalCode code, nostd::string_view description) noexcept | ||
| { | ||
| (void)code; | ||
| (void)description; | ||
| } | ||
|
|
||
|
|
||
| void Recordable::SetName(nostd::string_view name) noexcept | ||
| { | ||
| // TODO: Truncate string to 128 bytes | ||
| span_.mutable_display_name()->set_value(std::string(name)); | ||
| } | ||
|
|
||
|
|
||
| void Recordable::SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept | ||
| { | ||
| const std::chrono::nanoseconds unix_time_nanoseconds(start_time.time_since_epoch().count()); | ||
| const auto seconds = std::chrono::duration_cast<std::chrono::seconds>(unix_time_nanoseconds); | ||
| span_.mutable_start_time()->set_seconds(seconds.count()); | ||
| span_.mutable_start_time()->set_nanos(unix_time_nanoseconds.count()- | ||
| std::chrono::duration_cast<std::chrono::nanoseconds>(seconds).count()); | ||
| } | ||
|
|
||
|
|
||
| void Recordable::SetDuration(std::chrono::nanoseconds duration) noexcept | ||
| { | ||
| const std::chrono::nanoseconds start_time_nanos(span_.start_time().nanos()); | ||
| const std::chrono::seconds start_time_seconds(span_.start_time().seconds()); | ||
| const std::chrono::nanoseconds unix_end_time( | ||
| std::chrono::duration_cast<std::chrono::nanoseconds>(start_time_seconds).count() | ||
| + start_time_nanos.count() | ||
| + duration.count()); | ||
| const auto seconds = std::chrono::duration_cast<std::chrono::seconds>(unix_end_time); | ||
| span_.mutable_end_time()->set_seconds(seconds.count()); | ||
| span_.mutable_end_time()->set_nanos( | ||
| unix_end_time.count()- | ||
| std::chrono::duration_cast<std::chrono::nanoseconds>(seconds).count()); | ||
| } | ||
|
|
||
| } // namespace gcp | ||
| } // namespace exporter | ||
| OPENTELEMETRY_END_NAMESPACE | ||
119 changes: 119 additions & 0 deletions
119
exporters/trace/gcp_exporter/internal/recordable_test.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| #include "exporters/trace/gcp_exporter/recordable.h" | ||
| #include <gtest/gtest.h> | ||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace exporter | ||
| { | ||
| namespace gcp | ||
| { | ||
|
|
||
| TEST(Recordable, TestSetAttribute) | ||
| { | ||
| Recordable rec; | ||
|
|
||
| // Set 'bool' type | ||
| const nostd::string_view bool_key = "bool_key"; | ||
| const common::AttributeValue bool_value = true; | ||
| rec.SetAttribute(bool_key, std::move(bool_value)); | ||
|
|
||
| // Set 'integer' type | ||
| const nostd::string_view int_key = "int_key"; | ||
| const int64_t seven = 7; | ||
| const common::AttributeValue int_value = seven; | ||
| rec.SetAttribute(int_key, std::move(int_value)); | ||
|
|
||
| // Set 'string' type | ||
| const nostd::string_view string_key = "string_key"; | ||
| const common::AttributeValue string_value = "test"; | ||
| rec.SetAttribute(string_key, std::move(string_value)); | ||
|
|
||
| auto attr_map = rec.span().attributes().attribute_map(); | ||
|
|
||
| EXPECT_TRUE(attr_map["bool_key"].bool_value()); | ||
| EXPECT_EQ(seven, attr_map["int_key"].int_value()); | ||
| EXPECT_EQ("test", attr_map["string_key"].string_value().value()); | ||
| } | ||
|
|
||
| TEST(Recordable, TestSetIds) | ||
| { | ||
| setenv("GOOGLE_CLOUD_PROJECT_ID", "test_project", 1); | ||
|
|
||
| const opentelemetry::trace::TraceId trace_id( | ||
| std::array<const uint8_t, opentelemetry::trace::TraceId::kSize>( | ||
| {0, 1, 0, 2, 1, 3, 1, 4, 1, 5, 1, 6, 3, 7, 0, 0})); | ||
|
|
||
| const opentelemetry::trace::SpanId span_id( | ||
| std::array<const uint8_t, opentelemetry::trace::SpanId::kSize>( | ||
| {1, 2, 3, 4, 5, 6, 7, 8})); | ||
|
|
||
| const opentelemetry::trace::SpanId parent_span_id( | ||
| std::array<const uint8_t, opentelemetry::trace::SpanId::kSize>( | ||
| {4, 5, 0, 1, 1, 1, 1, 3})); | ||
|
|
||
| Recordable rec; | ||
|
|
||
| rec.SetIds(trace_id, span_id, parent_span_id); | ||
|
|
||
| EXPECT_EQ("projects/test_project/traces/00010002010301040105010603070000/spans/0102030405060708", | ||
| rec.span().name()); | ||
| EXPECT_EQ("0102030405060708", rec.span().span_id()); | ||
| EXPECT_EQ("0405000101010103", rec.span().parent_span_id()); | ||
| } | ||
|
|
||
|
|
||
| TEST(Recordable, TestSetName) | ||
| { | ||
| Recordable rec; | ||
| const nostd::string_view expected_name = "Test Span"; | ||
| rec.SetName(expected_name); | ||
| EXPECT_EQ(expected_name, rec.span().display_name().value()); | ||
| } | ||
|
|
||
|
|
||
| TEST(Recordable, TestSetStartTime) | ||
| { | ||
| Recordable rec; | ||
|
|
||
| const std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now(); | ||
| const core::SystemTimestamp start_timestamp(start_time); | ||
|
|
||
| const int64_t expected_unix_start_time = | ||
| std::chrono::duration_cast<std::chrono::nanoseconds>(start_time.time_since_epoch()).count(); | ||
|
|
||
| rec.SetStartTime(start_timestamp); | ||
|
|
||
| const std::chrono::nanoseconds start_time_nanos(rec.span().start_time().nanos()); | ||
| const std::chrono::seconds start_time_seconds(rec.span().start_time().seconds()); | ||
| const std::chrono::nanoseconds unix_start_time( | ||
| std::chrono::duration_cast<std::chrono::nanoseconds>(start_time_seconds).count() | ||
| + start_time_nanos.count()); | ||
|
|
||
| EXPECT_EQ(expected_unix_start_time, unix_start_time.count()); | ||
| } | ||
|
|
||
|
|
||
| TEST(Recordable, TestSetDuration) | ||
| { | ||
| Recordable rec; | ||
|
|
||
| const std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now(); | ||
| const core::SystemTimestamp start_timestamp(start_time); | ||
| const std::chrono::nanoseconds duration(10); | ||
|
|
||
| const int64_t expected_unix_end_time = start_timestamp.time_since_epoch().count() + duration.count(); | ||
|
|
||
| rec.SetStartTime(start_timestamp); | ||
| rec.SetDuration(duration); | ||
|
|
||
| const std::chrono::nanoseconds end_time_nanos(rec.span().end_time().nanos()); | ||
| const std::chrono::seconds end_time_seconds(rec.span().end_time().seconds()); | ||
| const std::chrono::nanoseconds unix_end_time( | ||
| std::chrono::duration_cast<std::chrono::nanoseconds>(end_time_seconds).count() | ||
| + end_time_nanos.count()); | ||
|
|
||
| EXPECT_EQ(expected_unix_end_time, unix_end_time.count()); | ||
| } | ||
|
|
||
| } // namespace gcp | ||
| } // namespace exporter | ||
| OPENTELEMETRY_END_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| #pragma once | ||
|
|
||
| #include "google/devtools/cloudtrace/v2/tracing.grpc.pb.h" | ||
| #include "opentelemetry/sdk/trace/recordable.h" | ||
| #include "opentelemetry/version.h" | ||
| #include "opentelemetry/nostd/variant.h" | ||
|
|
||
|
|
||
| constexpr char kProjectsPathStr[] = "projects/"; | ||
| constexpr char kTracesPathStr[] = "/traces/"; | ||
| constexpr char kSpansPathStr[] = "/spans/"; | ||
| constexpr char kGCPEnvVar[] = "GOOGLE_CLOUD_PROJECT_ID"; | ||
|
|
||
|
|
||
| OPENTELEMETRY_BEGIN_NAMESPACE | ||
| namespace exporter | ||
| { | ||
| namespace gcp | ||
| { | ||
| class Recordable final : public sdk::trace::Recordable | ||
| { | ||
| public: | ||
| const google::devtools::cloudtrace::v2::Span &span() const noexcept { return span_; } | ||
|
|
||
| void SetIds(trace::TraceId trace_id, | ||
| trace::SpanId span_id, | ||
| trace::SpanId parent_span_id) noexcept override; | ||
|
|
||
| void SetAttribute(nostd::string_view key, | ||
| const opentelemetry::common::AttributeValue &&value) noexcept override; | ||
|
|
||
| void AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept override; | ||
|
|
||
| void SetStatus(trace::CanonicalCode code, nostd::string_view description) noexcept override; | ||
|
|
||
| void SetName(nostd::string_view name) noexcept override; | ||
|
|
||
| void SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept override; | ||
|
|
||
| void SetDuration(std::chrono::nanoseconds duration) noexcept override; | ||
|
|
||
| private: | ||
| google::devtools::cloudtrace::v2::Span span_; | ||
| }; | ||
|
|
||
| } // gcp | ||
| } // exporter | ||
| OPENTELEMETRY_END_NAMESPACE |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.