Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/zpages/zpages_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int main(int argc, char* argv[]) {
* can be viewed.
* Note that the webserver is destroyed after the application ends execution.
*/
zPages();
zPages::Initialize();
auto tracer = opentelemetry::trace::Provider::GetTracerProvider()->GetTracer("");

std::cout << "This example for zPages creates a few types of spans and then "
Expand Down
2 changes: 1 addition & 1 deletion examples/zpages/zpagesexample.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void MakeSpans(int i) {
}

int main(int argc, char* argv[]) {
zPages();
//zPages();
auto tracer = opentelemetry::trace::Provider::GetTracerProvider()->GetTracer("");
auto run_span = tracer->StartSpan("always running");

Expand Down
86 changes: 50 additions & 36 deletions ext/include/opentelemetry/ext/zpages/zpages.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <chrono>
#include <memory>
#include <iostream>

#include "opentelemetry/ext/zpages/tracez_data_aggregator.h"
#include "opentelemetry/ext/zpages/tracez_processor.h"
Expand All @@ -10,48 +12,60 @@
#include "opentelemetry/sdk/trace/tracer_provider.h"
#include "opentelemetry/nostd/shared_ptr.h"

/*
* Wrapper class that allows users to use zPages by calling this class constructor. Currently only
* has TraceZ, but other types of zPages can be added in the future
*/
using std::chrono::microseconds;
using opentelemetry::ext::zpages::TracezSpanProcessor;
using opentelemetry::ext::zpages::TracezDataAggregator;
using opentelemetry::ext::zpages::TracezHttpServer;

/**
* Wrapper for zPages that initializes all the components required for zPages,
* and starts the HTTP server in the constructor and ends it in the destructor.
* The constructor and destructor for this object is private to prevent
* creation other than by calling the static function inialize. This follows the
* meyers singelton pattern and only a single instance of the class is allowed.
*/
class zPages {
public:
zPages()
: tracez_processor_(std::make_shared<opentelemetry::ext::zpages::TracezSpanProcessor>()),
tracez_provider_(opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new opentelemetry::sdk::trace::TracerProvider(tracez_processor_))) {
public:
/**
* This function is called if the user wishes to include zPages in their
* application. It creates a static instance of this class.
*/
static void Initialize(){
static zPages instance;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static zPages* instance = new zPages;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as per our discussion offline I've left this as is for the champions to review.

}

private:
/**
* Constructor is responsible for initializing the tracer, tracez processor,
* tracez data aggregator and the tracez server. The server is also started in
* constructor.
*/
zPages(){
auto tracez_processor_ = std::make_shared<TracezSpanProcessor>();
auto tracez_provider_ =
opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider>(
new opentelemetry::sdk::trace::TracerProvider(tracez_processor_));

// Set the global trace provider for a user to use, which is connected to our span processor
opentelemetry::trace::Provider::SetTracerProvider(tracez_provider_);
auto tracez_aggregator = std::unique_ptr<TracezDataAggregator>(
new TracezDataAggregator(tracez_processor_));

tracez_server_thread_ = std::thread(&zPages::RunTracezServer, this);
tracez_server_thread_.detach();
tracez_server_ = std::unique_ptr<TracezHttpServer>
(new TracezHttpServer(std::move(tracez_aggregator)));

tracez_server_->start();

// Ensure zPages has time to setup, so the program doesn't crash
opentelemetry::trace::Provider::SetTracerProvider(tracez_provider_);

// Give the server some time to set up to prevent crashes
std::this_thread::sleep_for(setup_time_);
}

private:
/*
* Runs the HTTP server in the background for TraceZ
*/
void RunTracezServer() {
auto tracez_aggregator = std::unique_ptr<opentelemetry::ext::zpages::TracezDataAggregator>(
new opentelemetry::ext::zpages::TracezDataAggregator(tracez_processor_));

opentelemetry::ext::zpages::TracezHttpServer tracez_server(std::move(tracez_aggregator));
tracez_server.start();

// Keeps zPages server up indefinitely
while (1) std::this_thread::sleep_for(long_time_);
tracez_server.stop();

~zPages(){
// shut down the server when the object goes out of scope(at the end of the
// program)
tracez_server_->stop();
}


std::thread tracez_server_thread_;
std::shared_ptr<opentelemetry::ext::zpages::TracezSpanProcessor> tracez_processor_;
opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider> tracez_provider_;
const std::chrono::duration<unsigned int, std::nano> setup_time_ = std::chrono::nanoseconds(100);
const std::chrono::duration<unsigned int, std::ratio<3600>> long_time_ = std::chrono::hours(9999);
std::unique_ptr<TracezHttpServer> tracez_server_;
const std::chrono::duration<unsigned int, std::nano> setup_time_ = microseconds(10);
};