forked from open-telemetry/opentelemetry-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request open-telemetry#3 from Tianlin-Zhao/origin/propagators
Origin/propagators
- Loading branch information
Showing
19 changed files
with
1,386 additions
and
790 deletions.
There are no files selected for viewing
This file contains 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,166 @@ | ||
#pragma once | ||
|
||
#include "opentelemetry/context/context_value.h" | ||
#include "opentelemetry/nostd/shared_ptr.h" | ||
#include "opentelemetry/nostd/string_view.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace context | ||
{ | ||
|
||
// The context class provides a context identifier. Is built as a linked list | ||
// of DataList nodes and each context holds a shared_ptr to a place within | ||
// the list that determines which keys and values it has access to. All that | ||
// come before and none that come after. | ||
class Context | ||
{ | ||
|
||
public: | ||
// Creates a context object from a map of keys and identifiers, this will | ||
// hold a shared_ptr to the head of the DataList linked list | ||
template <class T> | ||
Context(const T &keys_and_values) | ||
{ | ||
head_ = nostd::shared_ptr<DataList>{new DataList(keys_and_values)}; | ||
} | ||
|
||
// Creates a context object from a key and value, this will | ||
// hold a shared_ptr to the head of the DataList linked list | ||
Context(nostd::string_view key, ContextValue value) | ||
{ | ||
head_ = nostd::shared_ptr<DataList>{new DataList(key, value)}; | ||
} | ||
|
||
// Accepts a new iterable and then returns a new context that | ||
// contains the new key and value data. It attaches the | ||
// exisiting list to the end of the new list. | ||
template <class T> | ||
Context SetValues(T &values) noexcept | ||
{ | ||
Context context = Context(values); | ||
nostd::shared_ptr<DataList> &last = context.head_; | ||
while (last->next_ != nullptr) | ||
{ | ||
last = last->next_; | ||
} | ||
last->next_ = head_; | ||
return context; | ||
} | ||
|
||
// Accepts a new iterable and then returns a new context that | ||
// contains the new key and value data. It attaches the | ||
// exisiting list to the end of the new list. | ||
Context SetValue(nostd::string_view key, ContextValue value) noexcept | ||
{ | ||
Context context = Context(key, value); | ||
context.head_->next_ = head_; | ||
return context; | ||
} | ||
|
||
// Returns the value associated with the passed in key. | ||
context::ContextValue GetValue(const nostd::string_view key) noexcept | ||
{ | ||
for (DataList *data = head_.get(); data != nullptr; data = data->next_.get()) | ||
{ | ||
if (key.size() == data->key_length_) | ||
{ | ||
if (memcmp(key.data(), data->key_, data->key_length_) == 0) | ||
{ | ||
return data->value_; | ||
} | ||
} | ||
} | ||
return (int64_t)0; | ||
} | ||
|
||
// Checks for key and returns true if found | ||
bool HasKey(const nostd::string_view key) noexcept | ||
{ | ||
for (DataList *data = head_.get(); data != nullptr; data = data->next_.get()) | ||
{ | ||
if (key.size() == data->key_length_) | ||
{ | ||
if (memcmp(key.data(), data->key_, data->key_length_) == 0) | ||
{ | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private: | ||
Context() = default; | ||
|
||
// A linked list to contain the keys and values of this context node | ||
class DataList | ||
{ | ||
public: | ||
nostd::shared_ptr<DataList> next_; | ||
|
||
char *key_; | ||
|
||
size_t key_length_; | ||
|
||
ContextValue value_; | ||
|
||
DataList() { next_ = nullptr; } | ||
|
||
// Builds a data list off of a key and value iterable and returns the head | ||
template <class T> | ||
DataList(const T &keys_and_vals) : key_{nullptr} | ||
{ | ||
bool first = true; | ||
auto *node = this; | ||
for (auto &iter : keys_and_vals) | ||
{ | ||
if (first) | ||
{ | ||
*node = DataList(iter.first, iter.second); | ||
first = false; | ||
} | ||
else | ||
{ | ||
node->next_ = nostd::shared_ptr<DataList>(new DataList(iter.first, iter.second)); | ||
node = node->next_.get(); | ||
} | ||
} | ||
} | ||
|
||
// Builds a data list with just a key and value, so it will just be the head | ||
// and returns that head. | ||
DataList(nostd::string_view key, ContextValue value) | ||
{ | ||
key_ = new char[key.size()]; | ||
key_length_ = key.size(); | ||
memcpy(key_, key.data(), key.size() * sizeof(char)); | ||
value_ = value; | ||
next_ = nostd::shared_ptr<DataList>{nullptr}; | ||
} | ||
|
||
DataList &operator=(DataList &&other) | ||
{ | ||
key_length_ = other.key_length_; | ||
value_ = std::move(other.value_); | ||
next_ = std::move(other.next_); | ||
|
||
key_ = other.key_; | ||
other.key_ = nullptr; | ||
|
||
return *this; | ||
} | ||
|
||
~DataList() | ||
{ | ||
if (key_ != nullptr) | ||
{ | ||
delete[] key_; | ||
} | ||
} | ||
}; | ||
|
||
// Head of the list which holds the keys and values of this context | ||
nostd::shared_ptr<DataList> head_; | ||
}; | ||
} // namespace context | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains 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,18 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#include "opentelemetry/nostd/shared_ptr.h" | ||
#include "opentelemetry/nostd/span.h" | ||
#include "opentelemetry/nostd/unique_ptr.h" | ||
#include "opentelemetry/nostd/variant.h" | ||
#include "opentelemetry/trace/span_context.h" | ||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace context | ||
{ | ||
using ContextValue = | ||
nostd::variant<bool, int64_t, uint64_t, double, nostd::shared_ptr<trace::SpanContext>>; | ||
} // namespace context | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains 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
59 changes: 59 additions & 0 deletions
59
api/include/opentelemetry/propagators/composite_http_propagator.h
This file contains 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,59 @@ | ||
// 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 "opentelemetry/context/context.h" | ||
#include "opentelemetry/trace/propagation/httptextformat.h" | ||
#include "opentelemetry/nostd/span.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace propagators | ||
{ | ||
// CompositeHTTPPropagator provides a mechanism for combining multiple | ||
// propagators into a single one. | ||
template <typename T> | ||
class CompositeHTTPPropagator(trace::propagation::HTTPTextFormat<T>) { | ||
public: | ||
// Initializes a Composite Http Propagator with given propagators | ||
CompositeHTTPPropagator(nostd::span<trace::propagation::HTTPTextFormat> &propagators) { | ||
this.propagators_ = propagators; | ||
} | ||
|
||
// Run each of the configured propagators with the given context and carrier. | ||
// Propagators are run in the order they are configured, if multiple | ||
// propagators write the same context key, the propagator later in the list | ||
// will override previous propagators. | ||
// See opentelemetry.trace.propagation.httptextformat.HTTPTextFormat.extract | ||
Extract(Getter get_from_carrier, const T &carrier, Context &context) { | ||
for (nostd::span<trace::propagation::HTTPTextFormat>::iterator it = propagators_.begin(); it != propagators_.end(); it++) { | ||
context = it->Extract(get_from_carrier, carrier, context); | ||
} | ||
return context; | ||
} | ||
|
||
// Run each of the configured propagators with the given context and carrier. | ||
// Propagators are run in the order they are configured, if multiple | ||
// propagators write the same carrier key, the propagator later in the list | ||
// will override previous propagators. | ||
// See `opentelemetry.trace.propagation.httptextformat.HTTPTextFormat.inject` | ||
Inject(Setter set_from_carrier, T &carrier, const Context &context) { | ||
for (nostd::span<trace::propagation::HTTPTextFormat>::iterator it = propagators_.begin(); it != propagators_.end(); it++) { | ||
it->Inject(get_from_carrier, carrier, context); | ||
} | ||
} | ||
private: | ||
nostd::span<trace::propagation::HTTPTextFormat> propagators_; | ||
} | ||
} |
Oops, something went wrong.