Skip to content
This repository was archived by the owner on Apr 19, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions exporters/trace/gcp_exporter/internal/recordable.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "exporters/trace/gcp_exporter/recordable.h"
#include <assert.h>


OPENTELEMETRY_BEGIN_NAMESPACE
Expand Down Expand Up @@ -34,31 +33,60 @@ void Recordable::SetIds(trace::TraceId trace_id,


void Recordable::SetAttribute(nostd::string_view key,
const common::AttributeValue &&value) noexcept
const common::AttributeValue &value) noexcept
{
// Get the protobuf span's map
auto map = span_.mutable_attributes()->mutable_attribute_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));
(*map)[key.data()].set_bool_value(nostd::get<bool>(value));
}
else if (nostd::holds_alternative<int>(value))
{
(*map)[key.data()].set_int_value(nostd::get<int>(value));
}
else if (nostd::holds_alternative<int64_t>(value))
{
(*map)[std::string(key)].set_int_value(nostd::get<int64_t>(value));
(*map)[key.data()].set_int_value(nostd::get<int64_t>(value));
}
else if (nostd::holds_alternative<unsigned int>(value))
{
(*map)[key.data()].set_int_value(nostd::get<unsigned int>(value));
}
else if (nostd::holds_alternative<uint64_t>(value))
{
(*map)[key.data()].set_int_value(nostd::get<uint64_t>(value));
}
Comment thread
snehilchopra marked this conversation as resolved.
else if (nostd::holds_alternative<int64_t>(value))
{
(*map)[key.data()].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);
(*map)[key.data()].mutable_string_value()->set_value(value_str);
}
}


void Recordable::AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept
void Recordable::AddEvent(nostd::string_view name,
core::SystemTimestamp timestamp,
const opentelemetry::trace::KeyValueIterable &attributes) noexcept
{
(void)name;
(void)timestamp;
(void)attributes;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I go with unnamed parameters here and below?
Not sure what the specification is under the GCP org for unused parameters.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what is the spec, but I think those parameters are unused for a short term.
Eventually this code AddEvent should eventually map to a TimeEvent (https://cloud.google.com/trace/docs/reference/v2/rpc/google.devtools.cloudtrace.v2#timeevent).

}


void Recordable::AddLink(
opentelemetry::trace::SpanContext span_context,
const opentelemetry::trace::KeyValueIterable &attributes) noexcept
{
(void)span_context;
(void)attributes;
}


Expand Down
32 changes: 24 additions & 8 deletions exporters/trace/gcp_exporter/internal/recordable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace exporter
namespace gcp
{

TEST(Recordable, TestSetAttribute)
TEST(Recordable, TestSetNonIntAttribute)
{
Recordable rec;

Expand All @@ -16,12 +16,6 @@ TEST(Recordable, TestSetAttribute)
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";
Expand All @@ -30,10 +24,32 @@ TEST(Recordable, TestSetAttribute)
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());
}

template <typename T>
struct IntAttributeTest : public testing::Test
{
using IntParamType = T;
};

using IntTypes = testing::Types<int, int64_t, unsigned int, uint64_t>;
TYPED_TEST_CASE(IntAttributeTest, IntTypes);

TYPED_TEST(IntAttributeTest, SetIntSingleAttribute)
{
using IntType = typename TestFixture::IntParamType;
IntType i = 2;
common::AttributeValue int_val(i);

Recordable rec;
rec.SetAttribute("int_key", int_val);

auto attr_map = rec.span().attributes().attribute_map();

EXPECT_EQ(nostd::get<IntType>(int_val), attr_map["int_key"].int_value());
}

TEST(Recordable, TestSetIds)
{
setenv("GOOGLE_CLOUD_PROJECT_ID", "test_project", 1);
Expand Down
20 changes: 14 additions & 6 deletions exporters/trace/gcp_exporter/recordable.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,24 @@ 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 SetIds(opentelemetry::trace::TraceId trace_id,
opentelemetry::trace::SpanId span_id,
opentelemetry::trace::SpanId parent_span_id) noexcept override;

void SetAttribute(nostd::string_view key,
const opentelemetry::common::AttributeValue &&value) noexcept override;
const opentelemetry::common::AttributeValue &value) noexcept override;

void AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept override;
void AddEvent(
nostd::string_view name,
core::SystemTimestamp timestamp,
const opentelemetry::trace::KeyValueIterable &attributes) noexcept override;

void SetStatus(trace::CanonicalCode code, nostd::string_view description) noexcept override;
void AddLink(
opentelemetry::trace::SpanContext span_context,
const opentelemetry::trace::KeyValueIterable &attributes) noexcept override;

void SetStatus(opentelemetry::trace::CanonicalCode code,
nostd::string_view description) noexcept override;

void SetName(nostd::string_view name) noexcept override;

Expand Down