From 423ecacdf62196731d888621e0dfa2e8cc023b4a Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Tue, 16 Jan 2024 23:49:59 +0100 Subject: [PATCH] [PROTO] Upgrade to opentelemetry-proto v1.1.0 (#2488) --- CHANGELOG.md | 2 ++ api/include/opentelemetry/trace/trace_flags.h | 15 +++++++++ bazel/repository.bzl | 6 ++-- .../exporters/etw/etw_random_id_generator.h | 2 ++ exporters/etw/test/etw_tracer_test.cc | 3 ++ .../exporters/otlp/otlp_recordable.h | 2 ++ exporters/otlp/src/otlp_recordable.cc | 7 ++++ .../exporters/zipkin/recordable.h | 2 ++ exporters/zipkin/src/recordable.cc | 6 ++++ .../opentelemetry/sdk/trace/id_generator.h | 11 ++++++- .../sdk/trace/multi_recordable.h | 8 +++++ .../sdk/trace/random_id_generator.h | 2 ++ .../opentelemetry/sdk/trace/recordable.h | 6 ++++ .../opentelemetry/sdk/trace/span_data.h | 9 +++++ sdk/src/trace/span.cc | 2 ++ sdk/src/trace/tracer.cc | 33 +++++++++++++++---- sdk/test/trace/tracer_test.cc | 3 ++ third_party/opentelemetry-proto | 2 +- third_party_release | 2 +- 19 files changed, 111 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8379620fb6..89624f88cf5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,8 @@ Increment the: [#2450](https://github.com/open-telemetry/opentelemetry-cpp/pull/2450) * [EXPORTER] Set `is_monotonic` flag for Observable Counters [#2478](https://github.com/open-telemetry/opentelemetry-cpp/pull/2478) +* [PROTO] Upgrade to opentelemetry-proto v1.1.0 + [#2488](https://github.com/open-telemetry/opentelemetry-cpp/pull/2488) Important changes: diff --git a/api/include/opentelemetry/trace/trace_flags.h b/api/include/opentelemetry/trace/trace_flags.h index 1f7bf4f9b3f..0ad9dc136b2 100644 --- a/api/include/opentelemetry/trace/trace_flags.h +++ b/api/include/opentelemetry/trace/trace_flags.h @@ -19,6 +19,19 @@ class TraceFlags final { public: static constexpr uint8_t kIsSampled = 1; + static constexpr uint8_t kIsRandom = 2; + + /** + * Valid flags in W3C Trace Context version 1. + * See https://www.w3.org/TR/trace-context-1/#trace-flags + */ + static constexpr uint8_t kAllW3CTraceContext1Flags = kIsSampled; + + /** + * Valid flags in W3C Trace Context version 2. + * See https://www.w3.org/TR/trace-context-1/#trace-flags + */ + static constexpr uint8_t kAllW3CTraceContext2Flags = kIsSampled | kIsRandom; TraceFlags() noexcept : rep_{0} {} @@ -26,6 +39,8 @@ class TraceFlags final bool IsSampled() const noexcept { return rep_ & kIsSampled; } + bool IsRandom() const noexcept { return rep_ & kIsRandom; } + // Populates the buffer with the lowercase base16 representation of the flags. void ToLowerBase16(nostd::span buffer) const noexcept { diff --git a/bazel/repository.bzl b/bazel/repository.bzl index 7e09e299cf1..0fde88dc5c5 100644 --- a/bazel/repository.bzl +++ b/bazel/repository.bzl @@ -100,10 +100,10 @@ def opentelemetry_cpp_deps(): http_archive, name = "com_github_opentelemetry_proto", build_file = "@io_opentelemetry_cpp//bazel:opentelemetry_proto.BUILD", - sha256 = "a13a1a7b76a1f22a0ca2e6c293e176ffef031413ab8ba653a82a1dbc286a3a33", - strip_prefix = "opentelemetry-proto-1.0.0", + sha256 = "df491a05f3fcbf86cc5ba5c9de81f6a624d74d4773d7009d573e37d6e2b6af64", + strip_prefix = "opentelemetry-proto-1.1.0", urls = [ - "https://github.com/open-telemetry/opentelemetry-proto/archive/v1.0.0.tar.gz", + "https://github.com/open-telemetry/opentelemetry-proto/archive/v1.1.0.tar.gz", ], ) diff --git a/exporters/etw/include/opentelemetry/exporters/etw/etw_random_id_generator.h b/exporters/etw/include/opentelemetry/exporters/etw/etw_random_id_generator.h index c347c44f92c..aa23e6f81b8 100644 --- a/exporters/etw/include/opentelemetry/exporters/etw/etw_random_id_generator.h +++ b/exporters/etw/include/opentelemetry/exporters/etw/etw_random_id_generator.h @@ -17,6 +17,8 @@ namespace trace class ETWRandomIdGenerator : public IdGenerator { +public: + ETWRandomIdGenerator() : IdGenerator(false) {} opentelemetry::trace::SpanId GenerateSpanId() noexcept override { diff --git a/exporters/etw/test/etw_tracer_test.cc b/exporters/etw/test/etw_tracer_test.cc index a7aa6b168bc..a546309b84c 100644 --- a/exporters/etw/test/etw_tracer_test.cc +++ b/exporters/etw/test/etw_tracer_test.cc @@ -28,6 +28,9 @@ std::string getTemporaryValue() */ class MockIdGenerator : public sdk::trace::IdGenerator { +public: + MockIdGenerator() : sdk::trace::IdGenerator(false) {} + opentelemetry::trace::SpanId GenerateSpanId() noexcept override { return opentelemetry::trace::SpanId(buf_span); diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_recordable.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_recordable.h index 76e88563309..88e7cd91cff 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_recordable.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_recordable.h @@ -53,6 +53,8 @@ class OtlpRecordable final : public opentelemetry::sdk::trace::Recordable void SetName(nostd::string_view name) noexcept override; + void SetTraceFlags(opentelemetry::trace::TraceFlags flags) noexcept override; + void SetSpanKind(opentelemetry::trace::SpanKind span_kind) noexcept override; void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept override; diff --git a/exporters/otlp/src/otlp_recordable.cc b/exporters/otlp/src/otlp_recordable.cc index a3ab4327f2a..f9cee24677b 100644 --- a/exporters/otlp/src/otlp_recordable.cc +++ b/exporters/otlp/src/otlp_recordable.cc @@ -140,6 +140,13 @@ void OtlpRecordable::SetName(nostd::string_view name) noexcept span_.set_name(name.data(), name.size()); } +void OtlpRecordable::SetTraceFlags(opentelemetry::trace::TraceFlags flags) noexcept +{ + uint32_t all_flags = flags.flags() & opentelemetry::proto::trace::v1::SPAN_FLAGS_TRACE_FLAGS_MASK; + + span_.set_flags(all_flags); +} + void OtlpRecordable::SetSpanKind(trace::SpanKind span_kind) noexcept { proto::trace::v1::Span_SpanKind proto_span_kind = diff --git a/exporters/zipkin/include/opentelemetry/exporters/zipkin/recordable.h b/exporters/zipkin/include/opentelemetry/exporters/zipkin/recordable.h index 64894082fd4..09955711e34 100644 --- a/exporters/zipkin/include/opentelemetry/exporters/zipkin/recordable.h +++ b/exporters/zipkin/include/opentelemetry/exporters/zipkin/recordable.h @@ -39,6 +39,8 @@ class Recordable final : public sdk::trace::Recordable void SetName(nostd::string_view name) noexcept override; + void SetTraceFlags(opentelemetry::trace::TraceFlags flags) noexcept override; + void SetStartTime(opentelemetry::common::SystemTimestamp start_time) noexcept override; void SetSpanKind(opentelemetry::trace::SpanKind span_kind) noexcept override; diff --git a/exporters/zipkin/src/recordable.cc b/exporters/zipkin/src/recordable.cc index 3397053e292..ed43cb8a2d0 100644 --- a/exporters/zipkin/src/recordable.cc +++ b/exporters/zipkin/src/recordable.cc @@ -213,6 +213,12 @@ void Recordable::SetName(nostd::string_view name) noexcept span_["name"] = name.data(); } +void Recordable::SetTraceFlags(opentelemetry::trace::TraceFlags /* flags */) noexcept +{ + // TODO: Currently not supported by specs: + // https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk_exporters/zipkin.md +} + void Recordable::SetResource(const sdk::resource::Resource &resource) noexcept { // only service.name attribute is supported by specs as of now. diff --git a/sdk/include/opentelemetry/sdk/trace/id_generator.h b/sdk/include/opentelemetry/sdk/trace/id_generator.h index bd807ff89ad..8e868efce57 100644 --- a/sdk/include/opentelemetry/sdk/trace/id_generator.h +++ b/sdk/include/opentelemetry/sdk/trace/id_generator.h @@ -16,8 +16,9 @@ namespace trace /** IdGenerator provides an interface for generating Trace Id and Span Id */ class IdGenerator { - public: + IdGenerator(bool is_random) : is_random_(is_random) {} + virtual ~IdGenerator() = default; /** Returns a SpanId represented by opaque 128-bit trace identifier */ @@ -25,6 +26,14 @@ class IdGenerator /** Returns a TraceId represented by opaque 64-bit trace identifier */ virtual opentelemetry::trace::TraceId GenerateTraceId() noexcept = 0; + + bool IsRandom() const { return is_random_; } + +private: + /** True if GenerateTraceId() is random, + * per https://www.w3.org/TR/trace-context-2/#random-trace-id-flag + */ + bool is_random_; }; } // namespace trace diff --git a/sdk/include/opentelemetry/sdk/trace/multi_recordable.h b/sdk/include/opentelemetry/sdk/trace/multi_recordable.h index 85195912862..e8ad3ea39c7 100644 --- a/sdk/include/opentelemetry/sdk/trace/multi_recordable.h +++ b/sdk/include/opentelemetry/sdk/trace/multi_recordable.h @@ -112,6 +112,14 @@ class MultiRecordable : public Recordable } } + void SetTraceFlags(opentelemetry::trace::TraceFlags flags) noexcept override + { + for (auto &recordable : recordables_) + { + recordable.second->SetTraceFlags(flags); + } + } + void SetSpanKind(opentelemetry::trace::SpanKind span_kind) noexcept override { for (auto &recordable : recordables_) diff --git a/sdk/include/opentelemetry/sdk/trace/random_id_generator.h b/sdk/include/opentelemetry/sdk/trace/random_id_generator.h index f06f02f5e7c..a2adcf55f73 100644 --- a/sdk/include/opentelemetry/sdk/trace/random_id_generator.h +++ b/sdk/include/opentelemetry/sdk/trace/random_id_generator.h @@ -15,6 +15,8 @@ namespace trace class RandomIdGenerator : public IdGenerator { public: + RandomIdGenerator() : IdGenerator(true) {} + opentelemetry::trace::SpanId GenerateSpanId() noexcept override; opentelemetry::trace::TraceId GenerateTraceId() noexcept override; diff --git a/sdk/include/opentelemetry/sdk/trace/recordable.h b/sdk/include/opentelemetry/sdk/trace/recordable.h index cb73ef5f64e..f4ac53fe2e9 100644 --- a/sdk/include/opentelemetry/sdk/trace/recordable.h +++ b/sdk/include/opentelemetry/sdk/trace/recordable.h @@ -135,6 +135,12 @@ class Recordable */ virtual void SetName(nostd::string_view name) noexcept = 0; + /** + * Set the trace flags of the span. + * @param flags the flags to set + */ + virtual void SetTraceFlags(opentelemetry::trace::TraceFlags flags) noexcept = 0; + /** * Set the spankind of the span. * @param span_kind the spankind to set diff --git a/sdk/include/opentelemetry/sdk/trace/span_data.h b/sdk/include/opentelemetry/sdk/trace/span_data.h index ef2c57a092c..a7f8ccc4be6 100644 --- a/sdk/include/opentelemetry/sdk/trace/span_data.h +++ b/sdk/include/opentelemetry/sdk/trace/span_data.h @@ -136,6 +136,12 @@ class SpanData final : public Recordable */ opentelemetry::nostd::string_view GetName() const noexcept { return name_; } + /** + * Get the trace flags for this span + * @return the trace flags for this span + */ + opentelemetry::trace::TraceFlags GetFlags() const noexcept { return flags_; } + /** * Get the kind of this span * @return the kind of this span @@ -273,6 +279,8 @@ class SpanData final : public Recordable name_ = std::string(name.data(), name.length()); } + void SetTraceFlags(opentelemetry::trace::TraceFlags flags) noexcept override { flags_ = flags; } + void SetSpanKind(opentelemetry::trace::SpanKind span_kind) noexcept override { span_kind_ = span_kind; @@ -306,6 +314,7 @@ class SpanData final : public Recordable opentelemetry::sdk::common::AttributeMap attribute_map_; std::vector events_; std::vector links_; + opentelemetry::trace::TraceFlags flags_; opentelemetry::trace::SpanKind span_kind_{opentelemetry::trace::SpanKind::kInternal}; const opentelemetry::sdk::resource::Resource *resource_; const InstrumentationScope *instrumentation_scope_; diff --git a/sdk/src/trace/span.cc b/sdk/src/trace/span.cc index 25708cddd9c..ca3406a1544 100644 --- a/sdk/src/trace/span.cc +++ b/sdk/src/trace/span.cc @@ -70,6 +70,8 @@ Span::Span(std::shared_ptr &&tracer, ? parent_span_context.span_id() : opentelemetry::trace::SpanId()); + recordable_->SetTraceFlags(span_context_->trace_flags()); + attributes.ForEachKeyValue([&](nostd::string_view key, common::AttributeValue value) noexcept { recordable_->SetAttribute(key, value); return true; diff --git a/sdk/src/trace/tracer.cc b/sdk/src/trace/tracer.cc index c722d60326d..838eca88c7e 100644 --- a/sdk/src/trace/tracer.cc +++ b/sdk/src/trace/tracer.cc @@ -52,26 +52,47 @@ nostd::shared_ptr Tracer::StartSpan( } } + IdGenerator &generator = GetIdGenerator(); opentelemetry::trace::TraceId trace_id; - opentelemetry::trace::SpanId span_id = GetIdGenerator().GenerateSpanId(); + opentelemetry::trace::SpanId span_id = generator.GenerateSpanId(); bool is_parent_span_valid = false; + uint8_t flags = 0; if (parent_context.IsValid()) { trace_id = parent_context.trace_id(); + flags = parent_context.trace_flags().flags(); is_parent_span_valid = true; } else { - trace_id = GetIdGenerator().GenerateTraceId(); + trace_id = generator.GenerateTraceId(); + if (generator.IsRandom()) + { + flags = opentelemetry::trace::TraceFlags::kIsRandom; + } } auto sampling_result = context_->GetSampler().ShouldSample(parent_context, trace_id, name, options.kind, attributes, links); - auto trace_flags = - sampling_result.IsSampled() - ? opentelemetry::trace::TraceFlags{opentelemetry::trace::TraceFlags::kIsSampled} - : opentelemetry::trace::TraceFlags{}; + if (sampling_result.IsSampled()) + { + flags |= opentelemetry::trace::TraceFlags::kIsSampled; + } + +#if 1 + /* https://github.com/open-telemetry/opentelemetry-specification as of v1.29.0 */ + /* Support W3C Trace Context version 1. */ + flags &= opentelemetry::trace::TraceFlags::kAllW3CTraceContext1Flags; +#endif + +#if 0 + /* Waiting for https://github.com/open-telemetry/opentelemetry-specification/issues/3411 */ + /* Support W3C Trace Context version 2. */ + flags &= opentelemetry::trace::TraceFlags::kAllW3CTraceContext2Flags; +#endif + + opentelemetry::trace::TraceFlags trace_flags(flags); auto span_context = std::unique_ptr(new opentelemetry::trace::SpanContext( diff --git a/sdk/test/trace/tracer_test.cc b/sdk/test/trace/tracer_test.cc index 61ad24be1d4..78bdbc9a43f 100644 --- a/sdk/test/trace/tracer_test.cc +++ b/sdk/test/trace/tracer_test.cc @@ -71,6 +71,9 @@ class MockSampler final : public Sampler */ class MockIdGenerator : public IdGenerator { +public: + MockIdGenerator() : IdGenerator(false) {} + opentelemetry::trace::SpanId GenerateSpanId() noexcept override { return opentelemetry::trace::SpanId(buf_span); diff --git a/third_party/opentelemetry-proto b/third_party/opentelemetry-proto index c4dfbc51f3c..4ca4f0335c6 160000 --- a/third_party/opentelemetry-proto +++ b/third_party/opentelemetry-proto @@ -1 +1 @@ -Subproject commit c4dfbc51f3cd4089778555a2ac5d9bc093ed2956 +Subproject commit 4ca4f0335c63cda7ab31ea7ed70d6553aee14dce diff --git a/third_party_release b/third_party_release index 817d9b728e0..7689cd86384 100644 --- a/third_party_release +++ b/third_party_release @@ -19,7 +19,7 @@ benchmark=v1.7.1 googletest=1.13.0 ms-gsl=v3.1.0-67-g6f45293 nlohmann-json=v3.11.2 -opentelemetry-proto=v1.0.0 +opentelemetry-proto=v1.1.0 opentracing-cpp=v1.6.0 prometheus-cpp=v1.1.0 vcpkg=2022.08.15