Skip to content

Commit

Permalink
rename getTraceIdAsHex to getTraceId (#34314)
Browse files Browse the repository at this point in the history
* rename getTraceId

Signed-off-by: zirain <zirain2009@gmail.com>

* implement getTraceId

Signed-off-by: zirain <zirain2009@gmail.com>

* fix test

Signed-off-by: zirain <zirain2009@gmail.com>

* fix test

Signed-off-by: zirain <zirain2009@gmail.com>

---------

Signed-off-by: zirain <zirain2009@gmail.com>
  • Loading branch information
zirain committed May 27, 2024
1 parent d69544e commit d19462e
Show file tree
Hide file tree
Showing 25 changed files with 39 additions and 40 deletions.
6 changes: 3 additions & 3 deletions envoy/tracing/trace_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Span;
using SpanPtr = std::unique_ptr<Span>;

/**
* The upstream sevice type.
* The upstream service type.
*/
enum class ServiceType {
// Service type is unknown.
Expand Down Expand Up @@ -133,9 +133,9 @@ class Span {
* Retrieve the trace ID associated with this span.
* The trace id may be generated for this span, propagated by parent spans, or
* not created yet.
* @return trace ID as a hex string
* @return trace ID
*/
virtual std::string getTraceIdAsHex() const PURE;
virtual std::string getTraceId() const PURE;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions source/common/formatter/http_specific_formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ HeadersByteSizeFormatter::formatValueWithContext(const HttpFormatterContext& con

ProtobufWkt::Value TraceIDFormatter::formatValueWithContext(const HttpFormatterContext& context,
const StreamInfo::StreamInfo&) const {
auto trace_id = context.activeSpan().getTraceIdAsHex();
auto trace_id = context.activeSpan().getTraceId();
if (trace_id.empty()) {
return SubstitutionFormatUtils::unspecifiedValue();
}
Expand All @@ -209,7 +209,7 @@ ProtobufWkt::Value TraceIDFormatter::formatValueWithContext(const HttpFormatterC
absl::optional<std::string>
TraceIDFormatter::formatWithContext(const HttpFormatterContext& context,
const StreamInfo::StreamInfo&) const {
auto trace_id = context.activeSpan().getTraceIdAsHex();
auto trace_id = context.activeSpan().getTraceId();
if (trace_id.empty()) {
return absl::nullopt;
}
Expand Down
2 changes: 1 addition & 1 deletion source/common/tracing/null_span_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class NullSpan : public Span {
void injectContext(Tracing::TraceContext&, const UpstreamContext&) override {}
void setBaggage(absl::string_view, absl::string_view) override {}
std::string getBaggage(absl::string_view) override { return EMPTY_STRING; }
std::string getTraceIdAsHex() const override { return EMPTY_STRING; }
std::string getTraceId() const override { return EMPTY_STRING; }
SpanPtr spawnChild(const Config&, const std::string&, SystemTime) override {
return SpanPtr{new NullSpan()};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void AccessLog::emitLog(const Formatter::HttpFormatterContext& log_context,
// OpenTelemetry trace id is a [16]byte array, backend(e.g. OTel-collector) will reject the
// request if the length is not 16. Some trace provider(e.g. zipkin) may return it as a 64-bit hex
// string. In this case, we need to convert it to a 128-bit hex string, padding left with zeros.
std::string trace_id_hex = log_context.activeSpan().getTraceIdAsHex();
std::string trace_id_hex = log_context.activeSpan().getTraceId();
if (trace_id_hex.size() == 32) {
*log_entry.mutable_trace_id() = absl::HexStringToBytes(trace_id_hex);
} else if (trace_id_hex.size() == 16) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ class OpenTracingSpan : public Tracing::Span, Logger::Loggable<Logger::Id::traci
void setBaggage(absl::string_view key, absl::string_view value) override;

// TODO: This method is unimplemented for OpenTracing.
std::string getTraceIdAsHex() const override { return EMPTY_STRING; };
// This won't be implemented because OpenTracing was deprecated.
// We should remove this method in the future?
std::string getTraceId() const override { return EMPTY_STRING; };

private:
OpenTracingDriver& driver_;
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/tracers/datadog/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void Span::setBaggage(absl::string_view, absl::string_view) {
// not implemented
}

std::string Span::getTraceIdAsHex() const {
std::string Span::getTraceId() const {
if (!span_) {
return std::string{};
}
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/tracers/datadog/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Span : public Tracing::Span {
void setSampled(bool sampled) override;
std::string getBaggage(absl::string_view key) override;
void setBaggage(absl::string_view key, absl::string_view value) override;
std::string getTraceIdAsHex() const override;
std::string getTraceId() const override;

private:
datadog::tracing::Optional<datadog::tracing::Span> span_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Span : public Tracing::Span {
void setBaggage(absl::string_view, absl::string_view) override{};
std::string getBaggage(absl::string_view) override { return EMPTY_STRING; };

std::string getTraceIdAsHex() const override;
std::string getTraceId() const override;

private:
::opencensus::trace::Span span_;
Expand Down Expand Up @@ -236,7 +236,7 @@ void Span::injectContext(Tracing::TraceContext& trace_context, const Tracing::Up
}
}

std::string Span::getTraceIdAsHex() const {
std::string Span::getTraceId() const {
const auto& ctx = span_.context();
return ctx.trace_id().ToHex();
}
Expand Down
7 changes: 3 additions & 4 deletions source/extensions/tracers/opentelemetry/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ void callSampler(SamplerSharedPtr sampler, const absl::optional<SpanContext> spa
if (!sampler) {
return;
}
const auto sampling_result =
sampler->shouldSample(span_context, new_span.getTraceIdAsHex(), operation_name,
new_span.spankind(), trace_context, {});
const auto sampling_result = sampler->shouldSample(
span_context, new_span.getTraceId(), operation_name, new_span.spankind(), trace_context, {});
new_span.setSampled(sampling_result.isSampled());

if (sampling_result.attributes) {
Expand Down Expand Up @@ -70,7 +69,7 @@ Span::Span(const std::string& name, SystemTime start_time, Envoy::TimeSource& ti
Tracing::SpanPtr Span::spawnChild(const Tracing::Config&, const std::string& name,
SystemTime start_time) {
// Build span_context from the current span, then generate the child span from that context.
SpanContext span_context(kDefaultVersion, getTraceIdAsHex(), spanId(), sampled(), tracestate());
SpanContext span_context(kDefaultVersion, getTraceId(), spanId(), sampled(), tracestate());
return parent_tracer_.startSpan(name, start_time, span_context, {},
::opentelemetry::proto::trace::v1::Span::SPAN_KIND_CLIENT);
}
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/tracers/opentelemetry/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Span : Logger::Loggable<Logger::Id::tracing>, public Tracing::Span {
span_.set_trace_id(absl::HexStringToBytes(trace_id_hex));
}

std::string getTraceIdAsHex() const override { return absl::BytesToHexString(span_.trace_id()); };
std::string getTraceId() const override { return absl::BytesToHexString(span_.trace_id()); };

OTelSpanKind spankind() const { return span_.kind(); }

Expand Down
2 changes: 1 addition & 1 deletion source/extensions/tracers/skywalking/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Span : public Tracing::Span {
void setSampled(bool do_sample) override;
std::string getBaggage(absl::string_view) override { return EMPTY_STRING; }
void setBaggage(absl::string_view, absl::string_view) override {}
std::string getTraceIdAsHex() const override { return EMPTY_STRING; }
std::string getTraceId() const override { return tracing_context_->traceId(); }

const TracingContextPtr tracingContext() { return tracing_context_; }
const TracingSpanPtr spanEntity() { return span_entity_; }
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/tracers/xray/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class Span : public Tracing::Span, Logger::Loggable<Logger::Id::config> {
std::string getBaggage(absl::string_view) override { return EMPTY_STRING; }

// TODO: This method is unimplemented for X-Ray.
std::string getTraceIdAsHex() const override { return EMPTY_STRING; };
std::string getTraceId() const override { return trace_id_; };

/**
* Creates a child span.
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/tracers/zipkin/zipkin_tracer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class ZipkinSpan : public Tracing::Span {
void setBaggage(absl::string_view, absl::string_view) override;
std::string getBaggage(absl::string_view) override;

std::string getTraceIdAsHex() const override { return span_.traceIdAsHexString(); };
std::string getTraceId() const override { return span_.traceIdAsHexString(); };

/**
* @return a reference to the Zipkin::Span object.
Expand Down
3 changes: 1 addition & 2 deletions test/common/formatter/substitution_formatter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2153,8 +2153,7 @@ TEST(SubstitutionFormatterTest, TraceIDFormatter) {
std::string body;

Tracing::MockSpan active_span;
EXPECT_CALL(active_span, getTraceIdAsHex())
.WillRepeatedly(Return("ae0046f9075194306d7de2931bd38ce3"));
EXPECT_CALL(active_span, getTraceId()).WillRepeatedly(Return("ae0046f9075194306d7de2931bd38ce3"));

{
HttpFormatterContext formatter_context(&request_header, &response_header, &response_trailer,
Expand Down
2 changes: 1 addition & 1 deletion test/common/tracing/tracer_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ TEST(NullTracerTest, BasicFunctionality) {
span_ptr->setTag("foo", "bar");
span_ptr->setBaggage("key", "value");
ASSERT_EQ("", span_ptr->getBaggage("baggage_key"));
ASSERT_EQ(span_ptr->getTraceIdAsHex(), "");
ASSERT_EQ(span_ptr->getTraceId(), "");
span_ptr->injectContext(trace_context, upstream_context);
span_ptr->log(SystemTime(), "fake_event");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ TEST_F(AccessLogTest, TraceId) {

NiceMock<Tracing::MockSpan> active_span;

EXPECT_CALL(active_span, getTraceIdAsHex()).WillOnce(Return("404142434445464748494a4b4c4d4e4f"));
EXPECT_CALL(active_span, getTraceId()).WillOnce(Return("404142434445464748494a4b4c4d4e4f"));
expectLog(R"EOF(
trace_id: "QEFCQ0RFRkdISUpLTE1OTw=="
time_unix_nano: 3600000000000
Expand All @@ -195,7 +195,7 @@ TEST_F(AccessLogTest, ZipkinTraceId) {

NiceMock<Tracing::MockSpan> active_span;

EXPECT_CALL(active_span, getTraceIdAsHex()).WillOnce(Return("0ccce09bf12e94df"));
EXPECT_CALL(active_span, getTraceId()).WillOnce(Return("0ccce09bf12e94df"));
expectLog(R"EOF(
trace_id: "AAAAAAAAAAAMzOCb8S6U3w=="
time_unix_nano: 3600000000000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Span : public Tracing::Span {
/* not implemented */
return EMPTY_STRING;
};
std::string getTraceIdAsHex() const {
std::string getTraceId() const {
/* not implemented */
return EMPTY_STRING;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ TEST_F(OpenTracingDriverTest, GetTraceId) {
first_span->finishSpan();

// This method is unimplemented and a noop.
ASSERT_EQ(first_span->getTraceIdAsHex(), "");
ASSERT_EQ(first_span->getTraceId(), "");
}

TEST_F(OpenTracingDriverTest, ExtractUsingForeach) {
Expand Down
6 changes: 3 additions & 3 deletions test/extensions/tracers/datadog/span_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,9 @@ TEST_F(DatadogTracerSpanTest, Baggage) {
EXPECT_EQ("", span.getBaggage("foo"));
}

TEST_F(DatadogTracerSpanTest, GetTraceIdAsHex) {
TEST_F(DatadogTracerSpanTest, GetTraceId) {
Span span{std::move(span_)};
EXPECT_EQ("cafebabe", span.getTraceIdAsHex());
EXPECT_EQ("cafebabe", span.getTraceId());
}

TEST_F(DatadogTracerSpanTest, NoOpMode) {
Expand Down Expand Up @@ -429,7 +429,7 @@ TEST_F(DatadogTracerSpanTest, NoOpMode) {
span.setSampled(false);
EXPECT_EQ("", span.getBaggage("foo"));
span.setBaggage("foo", "bar");
EXPECT_EQ("", span.getTraceIdAsHex());
EXPECT_EQ("", span.getTraceId());
}

} // namespace
Expand Down
4 changes: 2 additions & 2 deletions test/extensions/tracers/opencensus/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ TEST(OpenCensusTracerTest, Span) {
ASSERT_EQ("", span->getBaggage("baggage_key"));

// Trace id is automatically created when no parent context exists.
ASSERT_NE(span->getTraceIdAsHex(), "");
ASSERT_NE(span->getTraceId(), "");
}

// Retrieve SpanData from the OpenCensus trace exporter.
Expand Down Expand Up @@ -221,7 +221,7 @@ void testIncomingHeaders(

// Check contents via public API.
// Trace id is set via context propagation headers.
EXPECT_EQ(span->getTraceIdAsHex(), "404142434445464748494a4b4c4d4e4f");
EXPECT_EQ(span->getTraceId(), "404142434445464748494a4b4c4d4e4f");
}

// Retrieve SpanData from the OpenCensus trace exporter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ TEST_F(OpenTelemetryDriverTest, ParseSpanContextFromHeadersTest) {
Tracing::SpanPtr span = driver_->startSpan(mock_tracing_config_, request_headers, stream_info_,
operation_name_, {Tracing::Reason::Sampling, true});

EXPECT_EQ(span->getTraceIdAsHex(), trace_id_hex);
EXPECT_EQ(span->getTraceId(), trace_id_hex);

// Remove headers, then inject context into header from the span.
request_headers.remove(OpenTelemetryConstants::get().TRACE_PARENT.key());
Expand Down
3 changes: 1 addition & 2 deletions test/extensions/tracers/skywalking/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ TEST_F(TracerTest, TracerTestCreateNewSpanWithNoPropagationHeaders) {
EXPECT_EQ("", span->getBaggage("FakeStringAndNothingToDo"));
span->setOperation("FakeStringAndNothingToDo");
span->setBaggage("FakeStringAndNothingToDo", "FakeStringAndNothingToDo");
// This method is unimplemented and a noop.
ASSERT_EQ(span->getTraceIdAsHex(), "");
ASSERT_EQ(span->getTraceId(), segment_context->traceId());
// Test whether the basic functions of Span are normal.
EXPECT_FALSE(span->spanEntity()->skipAnalysis());
span->setSampled(false);
Expand Down
4 changes: 2 additions & 2 deletions test/extensions/tracers/xray/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ TEST_F(XRayTracerTest, GetTraceId) {
auto span = tracer.createNonSampledSpan(absl::nullopt /*headers*/);
span->finishSpan();

// This method is unimplemented and a noop.
EXPECT_EQ(span->getTraceIdAsHex(), "");
// Trace ID is always generated
EXPECT_NE(span->getTraceId(), "");
}

TEST_F(XRayTracerTest, ChildSpanHasParentInfo) {
Expand Down
4 changes: 2 additions & 2 deletions test/extensions/tracers/zipkin/zipkin_tracer_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ TEST_F(ZipkinDriverTest, ZipkinSpanTest) {
// ====
Tracing::SpanPtr span6 = driver_->startSpan(config_, request_headers_, stream_info_,
operation_name_, {Tracing::Reason::Sampling, true});
EXPECT_EQ(span6->getTraceIdAsHex(), "0000000000000000");
EXPECT_EQ(span6->getTraceId(), "0000000000000000");
}

TEST_F(ZipkinDriverTest, ZipkinSpanContextFromB3HeadersTest) {
Expand Down Expand Up @@ -798,7 +798,7 @@ TEST_F(ZipkinDriverTest, ZipkinSpanContextFromB3Headers128TraceIdTest) {
EXPECT_EQ(span_id, zipkin_span->span().idAsHexString());
EXPECT_EQ(parent_id, zipkin_span->span().parentIdAsHexString());
EXPECT_TRUE(zipkin_span->span().sampled());
EXPECT_EQ(trace_id, zipkin_span->getTraceIdAsHex());
EXPECT_EQ(trace_id, zipkin_span->getTraceId());
}

TEST_F(ZipkinDriverTest, ZipkinSpanContextFromInvalidTraceIdB3HeadersTest) {
Expand Down
2 changes: 1 addition & 1 deletion test/mocks/tracing/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class MockSpan : public Span {
MOCK_METHOD(void, setSampled, (const bool sampled));
MOCK_METHOD(void, setBaggage, (absl::string_view key, absl::string_view value));
MOCK_METHOD(std::string, getBaggage, (absl::string_view key));
MOCK_METHOD(std::string, getTraceIdAsHex, (), (const));
MOCK_METHOD(std::string, getTraceId, (), (const));

SpanPtr spawnChild(const Config& config, const std::string& name,
SystemTime start_time) override {
Expand Down

0 comments on commit d19462e

Please sign in to comment.