-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance existing http example to support w3c trace context propagation (
#727)
- Loading branch information
Showing
8 changed files
with
199 additions
and
97 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
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
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
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
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,50 @@ | ||
#pragma once | ||
#include <atomic> | ||
#include <string> | ||
#include "opentelemetry/ext/http/server/http_server.h" | ||
|
||
namespace | ||
{ | ||
|
||
class HttpServer : public HTTP_SERVER_NS::HttpRequestCallback | ||
{ | ||
|
||
protected: | ||
HTTP_SERVER_NS::HttpServer server_; | ||
std::string server_url_; | ||
uint16_t port_; | ||
std::atomic<bool> is_running_{false}; | ||
|
||
public: | ||
HttpServer(std::string server_name = "test_server", uint16_t port = 8800) : port_(port) | ||
{ | ||
server_.setServerName(server_name); | ||
server_.setKeepalive(false); | ||
} | ||
|
||
void AddHandler(std::string path, HTTP_SERVER_NS::HttpRequestCallback *request_handler) | ||
{ | ||
server_.addHandler(path, *request_handler); | ||
} | ||
|
||
void Start() | ||
{ | ||
if (!is_running_.exchange(true)) | ||
{ | ||
server_.addListeningPort(port_); | ||
server_.start(); | ||
} | ||
} | ||
|
||
void Stop() | ||
{ | ||
if (is_running_.exchange(false)) | ||
{ | ||
server_.stop(); | ||
} | ||
} | ||
|
||
~HttpServer() { Stop(); } | ||
}; | ||
|
||
} // namespace |
This file was deleted.
Oops, something went wrong.
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,95 @@ | ||
#pragma once | ||
#include "opentelemetry/exporters/ostream/span_exporter.h" | ||
#include "opentelemetry/sdk/trace/simple_processor.h" | ||
#include "opentelemetry/sdk/trace/tracer_provider.h" | ||
#include "opentelemetry/trace/provider.h" | ||
|
||
#include "opentelemetry/context/propagation/global_propagator.h" | ||
#include "opentelemetry/context/propagation/text_map_propagator.h" | ||
#include "opentelemetry/trace/propagation/http_trace_context.h" | ||
|
||
#include <cstring> | ||
#include <iostream> | ||
#include <vector> | ||
#include "opentelemetry/ext/http/client/http_client.h" | ||
#include "opentelemetry/nostd/shared_ptr.h" | ||
|
||
namespace | ||
{ | ||
// TBD - This function be removed once #723 is merged | ||
inline nostd::shared_ptr<opentelemetry::trace::Span> GetSpanFromContext( | ||
const opentelemetry::context::Context &context) | ||
{ | ||
opentelemetry::context::ContextValue span = context.GetValue(opentelemetry::trace::kSpanKey); | ||
if (nostd::holds_alternative<nostd::shared_ptr<opentelemetry::trace::Span>>(span)) | ||
{ | ||
return nostd::get<nostd::shared_ptr<opentelemetry::trace::Span>>(span); | ||
} | ||
static nostd::shared_ptr<opentelemetry::trace::Span> invalid_span{ | ||
new opentelemetry::trace::DefaultSpan(opentelemetry::trace::SpanContext::GetInvalid())}; | ||
return invalid_span; | ||
} | ||
|
||
template <typename T> | ||
class HttpTextMapCarrier : public opentelemetry::context::propagation::TextMapCarrier | ||
{ | ||
public: | ||
HttpTextMapCarrier<T>(T &headers) : headers_(headers) {} | ||
HttpTextMapCarrier() = default; | ||
virtual nostd::string_view Get(nostd::string_view key) const noexcept override | ||
{ | ||
std::string key_to_compare = key.data(); | ||
// Header's first letter seems to be automatically capitaliazed by our test http-server, so | ||
// compare accordingly. | ||
if (key == opentelemetry::trace::propagation::kTraceParent) | ||
{ | ||
key_to_compare = "Traceparent"; | ||
} | ||
else if (key == opentelemetry::trace::propagation::kTraceState) | ||
{ | ||
key_to_compare == "Tracestate"; | ||
} | ||
auto it = headers_.find(key_to_compare); | ||
if (it != headers_.end()) | ||
{ | ||
return it->second; | ||
} | ||
return ""; | ||
} | ||
|
||
virtual void Set(nostd::string_view key, nostd::string_view value) noexcept override | ||
{ | ||
headers_.insert(std::pair<std::string, std::string>(std::string(key), std::string(value))); | ||
} | ||
|
||
T headers_; | ||
}; | ||
|
||
void initTracer() | ||
{ | ||
auto exporter = std::unique_ptr<sdktrace::SpanExporter>( | ||
new opentelemetry::exporter::trace::OStreamSpanExporter); | ||
auto processor = std::unique_ptr<sdktrace::SpanProcessor>( | ||
new sdktrace::SimpleSpanProcessor(std::move(exporter))); | ||
std::vector<std::unique_ptr<sdktrace::SpanProcessor>> processors; | ||
processors.push_back(std::move(processor)); | ||
// Default is an always-on sampler. | ||
auto context = std::make_shared<sdktrace::TracerContext>(std::move(processors)); | ||
auto provider = nostd::shared_ptr<opentelemetry::trace::TracerProvider>( | ||
new sdktrace::TracerProvider(context)); | ||
// Set the global trace provider | ||
opentelemetry::trace::Provider::SetTracerProvider(provider); | ||
|
||
// set global propagator | ||
opentelemetry::context::propagation::GlobalTextMapPropagator::SetGlobalPropagator( | ||
nostd::shared_ptr<opentelemetry::context::propagation::TextMapPropagator>( | ||
new opentelemetry::trace::propagation::HttpTraceContext())); | ||
} | ||
|
||
nostd::shared_ptr<opentelemetry::trace::Tracer> get_tracer(std::string tracer_name) | ||
{ | ||
auto provider = opentelemetry::trace::Provider::GetTracerProvider(); | ||
return provider->GetTracer(tracer_name); | ||
} | ||
|
||
} // namespace |
This file was deleted.
Oops, something went wrong.