-
Notifications
You must be signed in to change notification settings - Fork 560
[WIP] Add TraceState. #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Copyright 2020, OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| //#include <cstdint> | ||
| //#include <cstring> | ||
|
|
||
| #include "opentelemetry/nostd/unique_ptr.h" | ||
| #include "opentelemetry/trace/span_id.h" | ||
| #include "opentelemetry/trace/trace_flags.h" | ||
| #include "opentelemetry/trace/trace_id.h" | ||
| #include "opentelemetry/trace/trace_state.h" | ||
|
|
||
| namespace opentelemetry | ||
| { | ||
| namespace trace | ||
| { | ||
|
|
||
| // SpanContext contains the state that must propagate to child Spans and across | ||
| // process boundaries. It contains the identifiers TraceId and SpanId, | ||
| // TraceFlags, TraceState, and whether it has a remote parent. | ||
| class SpanContext final | ||
| { | ||
| public: | ||
| // An invalid SpanContext. | ||
| SpanContext() noexcept : trace_state_(new TraceState) {} | ||
|
|
||
| // TODO | ||
| // | ||
| // static SpanContext Create(TraceId traceId, SpanId spanId, TraceFlags traceFlags, TraceState | ||
| // traceState); static SpanContext CreateFromRemoteParent(...); | ||
|
|
||
| const TraceId &trace_id() const noexcept { return trace_id_; } | ||
| const SpanId &span_id() const noexcept { return span_id_; } | ||
| const TraceFlags &trace_flags() const noexcept { return trace_flags_; } | ||
| const TraceState &trace_state() const noexcept { return *trace_state_; } | ||
|
|
||
| bool IsValid() const noexcept { return trace_id_.IsValid() && span_id_.IsValid(); } | ||
|
|
||
| bool HasRemoteParent() const noexcept { return remote_parent_; } | ||
|
|
||
| private: | ||
| const TraceId trace_id_; | ||
| const SpanId span_id_; | ||
| const TraceFlags trace_flags_; | ||
| const nostd::unique_ptr<TraceState> trace_state_; // Never nullptr. | ||
| const bool remote_parent_ = false; | ||
| }; | ||
|
|
||
| } // namespace trace | ||
| } // namespace opentelemetry | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Copyright 2020, OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <cstring> | ||
|
|
||
| #include "opentelemetry/nostd/span.h" | ||
| #include "opentelemetry/nostd/string_view.h" | ||
|
|
||
| namespace opentelemetry | ||
| { | ||
| namespace trace | ||
| { | ||
|
|
||
| // TraceState carries tracing-system specific context in a list of key-value pairs. TraceState | ||
| // allows different vendors to propagate additional information and inter-operate with their legacy | ||
| // Id formats. | ||
| // | ||
| // Implementation is optimized for a small list of key-value pairs. | ||
| // | ||
| // Key is opaque string up to 256 characters printable. It MUST begin with a lowercase letter, | ||
| // and can only contain lowercase letters a-z, digits 0-9, underscores _, dashes -, asterisks *, and | ||
| // forward slashes /. | ||
| // | ||
| // Value is opaque string up to 256 characters, of printable ASCII RFC0020 characters (i.e. the | ||
| // range 0x20 to 0x7E) except comma , and equals =. | ||
| class TraceState | ||
| { | ||
| public: | ||
| static constexpr int kKeyMaxSize = 256; | ||
| static constexpr int kValueMaxSize = 256; | ||
| static constexpr int kMaxKeyValuePairs = 32; | ||
|
|
||
| class Entry | ||
| { | ||
| public: | ||
| virtual ~Entry() {} | ||
|
|
||
| virtual nostd::string_view key() = 0; | ||
| virtual nostd::string_view value() = 0; | ||
| }; | ||
|
|
||
| // An empty TraceState. | ||
| TraceState() noexcept = default; | ||
|
|
||
| virtual ~TraceState() = default; | ||
|
|
||
| // Returns false if no such key, otherwise returns true and populates value. | ||
| virtual bool Get(nostd::string_view key, nostd::string_view *value) const noexcept | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason why it has to be a raw pointer and not a reference?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have set functions for Trace State any where? Could you have that functionality implemented? Thank you |
||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Returns true if there are no keys. | ||
| virtual bool empty() const noexcept { return true; } | ||
|
|
||
| // Returns a span of all the entries. The TraceState object must outlive the span. | ||
| virtual nostd::span<Entry *> entries() const noexcept { return {}; } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it have to contain raw pointers? |
||
|
|
||
| // Key is opaque string up to 256 characters printable. It MUST begin with a lowercase letter, and | ||
| // can only contain lowercase letters a-z, digits 0-9, underscores _, dashes -, asterisks *, and | ||
| // forward slashes /. For multi-tenant vendor scenarios, an at sign (@) can be used to prefix the | ||
| // vendor name. | ||
| static bool IsValidKey(nostd::string_view key) | ||
| { | ||
| if (key.empty() || key.length() > kKeyMaxSize || !IsNumberOrDigit(key[0])) | ||
| { | ||
| return false; | ||
| } | ||
| int ats = 0; | ||
| const int n = key.length(); | ||
| for (int i = 0; i < n; ++i) | ||
| { | ||
| char c = key[i]; | ||
| if (!IsNumberOrDigit(c) && c != '_' && c != '-' && c != '@' && c != '*' && c != '/') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vendor name has a size limitation of 13 characters according to the spec. |
||
| { | ||
| return false; | ||
| } | ||
| if ((c == '@') && (++ats > 1)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // TODO: IsValidValue | ||
|
|
||
| private: | ||
| static bool IsNumberOrDigit(char c) { return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'); } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IsLowerCaseAlphaOrDigit? |
||
| }; | ||
|
|
||
| } // namespace trace | ||
| } // namespace opentelemetry | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #include "opentelemetry/trace/span_context.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| using opentelemetry::trace::SpanContext; | ||
|
|
||
| TEST(SpanContextTest, DefaultConstruction) | ||
| { | ||
| SpanContext ctx; | ||
| } | ||
|
|
||
| } // namespace |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #include "opentelemetry/trace/trace_state.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| using opentelemetry::trace::TraceState; | ||
|
|
||
| TEST(TraceStateTest, DefaultConstruction) | ||
| { | ||
| TraceState s; | ||
| EXPECT_FALSE(s.Get("key", nullptr)); | ||
| EXPECT_TRUE(s.empty()); | ||
| EXPECT_EQ(0, s.entries().size()); | ||
| } | ||
| } // namespace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these definitions going to be up eventually? I need these for my HttpTraceConotext implementation. Thanks