Skip to content

Commit

Permalink
Added Http Trace Context (open-telemetry#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianlin-Zhao authored Aug 26, 2020
1 parent eb04081 commit 09983ab
Show file tree
Hide file tree
Showing 17 changed files with 634 additions and 21 deletions.
2 changes: 1 addition & 1 deletion api/include/opentelemetry/plugin/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Span final : public trace::Span

bool IsRecording() const noexcept override { return span_->IsRecording(); }

trace::Tracer &tracer() const noexcept override { return *tracer_; }
trace::SpanContext GetContext() const noexcept override { return span_->GetContext(); }

void SetToken(nostd::unique_ptr<context::Token> &&token) noexcept override {}

Expand Down
59 changes: 59 additions & 0 deletions api/include/opentelemetry/trace/default_span.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once
#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/trace/canonical_code.h"
#include "opentelemetry/trace/span.h"
#include "opentelemetry/trace/span_context.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace trace
{
class DefaultSpan : public Span
{
public:
// Returns an invalid span.
static DefaultSpan GetInvalid() { return DefaultSpan(SpanContext::GetInvalid()); }

trace::SpanContext GetContext() const noexcept { return span_context_; }

bool IsRecording() const noexcept { return false; }

void SetAttribute(nostd::string_view key, const common::AttributeValue &value) noexcept {}

void AddEvent(nostd::string_view name) noexcept {}

void AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept {}

void AddEvent(nostd::string_view name,
core::SystemTimestamp timestamp,
const KeyValueIterable &attributes) noexcept
{}

void AddEvent(nostd::string_view name, const KeyValueIterable &attributes) noexcept
{
this->AddEvent(name, std::chrono::system_clock::now(), attributes);
}

void SetStatus(CanonicalCode status, nostd::string_view description) noexcept {}

void UpdateName(nostd::string_view name) noexcept {}

void End(const EndSpanOptions &options = {}) noexcept {}

nostd::string_view ToString() { return "DefaultSpan"; }

void SetToken(nostd::unique_ptr<context::Token> &&default_token) noexcept {}

DefaultSpan() = default;

DefaultSpan(SpanContext span_context) : span_context_(span_context) {}

// movable and copiable
DefaultSpan(DefaultSpan &&spn) : span_context_(spn.GetContext()) {}
DefaultSpan(const DefaultSpan &spn) : span_context_(spn.GetContext()) {}

private:
SpanContext span_context_;
};

} // namespace trace
OPENTELEMETRY_END_NAMESPACE
35 changes: 35 additions & 0 deletions api/include/opentelemetry/trace/default_tracer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/trace/default_span.h"
#include "opentelemetry/trace/span.h"
#include "opentelemetry/trace/tracer.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace trace
{
class DefaultTracer : public Tracer
{
public:
~DefaultTracer() = default;

/**
* Starts a span.
*
* Optionally sets attributes at Span creation from the given key/value pairs.
*
* Attributes will be processed in order, previous attributes with the same
* key will be overwritten.
*/
nostd::unique_ptr<Span> StartSpan(nostd::string_view name,
const KeyValueIterable &attributes,
const StartSpanOptions &options = {}) override noexcept
{
return nostd::unique_ptr<Span>(new DefaultSpan::GetInvalid());
}

void ForceFlushWithMicroseconds(uint64_t timeout) override noexcept {}

void CloseWithMicroseconds(uint64_t timeout) override noexcept {}
};
} // namespace trace
OPENTELEMETRY_END_NAMESPACE
4 changes: 3 additions & 1 deletion api/include/opentelemetry/trace/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/trace/span.h"
#include "opentelemetry/trace/span_context.h"
#include "opentelemetry/trace/tracer.h"
#include "opentelemetry/trace/tracer_provider.h"
#include "opentelemetry/version.h"
Expand Down Expand Up @@ -47,12 +48,13 @@ class NoopSpan final : public Span

bool IsRecording() const noexcept override { return false; }

Tracer &tracer() const noexcept override { return *tracer_; }
SpanContext GetContext() const noexcept override { return span_context_; }

void SetToken(nostd::unique_ptr<context::Token> && /* token */) noexcept override {}

private:
std::shared_ptr<Tracer> tracer_;
SpanContext span_context_;
};

/**
Expand Down
43 changes: 43 additions & 0 deletions api/include/opentelemetry/trace/propagation/http_text_format.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <cstdint>
#include "opentelemetry/context/context.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace trace
{
namespace propagation
{

// The HTTPTextFormat class provides an interface that enables extracting and injecting
// context into headers of HTTP requests. HTTP frameworks and clients
// can integrate with HTTPTextFormat by providing the object containing the
// headers, and a getter and setter function for the extraction and
// injection of values, respectively.
template <typename T>
class HTTPTextFormat
{
public:
// Rules that manages how context will be extracted from carrier.
using Getter = nostd::string_view (*)(const T &carrier, nostd::string_view trace_type);

// Rules that manages how context will be injected to carrier.
using Setter = void (*)(T &carrier,
nostd::string_view trace_type,
nostd::string_view trace_description);

// Returns the context that is stored in the HTTP header carrier with the getter as extractor.
virtual context::Context Extract(Getter get_from_carrier,
const T &carrier,
context::Context &context) noexcept = 0;

// Sets the context for a HTTP header carrier with self defined rules.
virtual void Inject(Setter set_from_carrier,
T &carrier,
const context::Context &context) noexcept = 0;
};
} // namespace propagation
} // namespace trace
OPENTELEMETRY_END_NAMESPACE
Loading

0 comments on commit 09983ab

Please sign in to comment.