Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
81be8e2
Add base HTTP server files
jajanet Jul 27, 2020
7e89c87
Add index error status/tooltip description, precise timestamps
jajanet Jul 28, 2020
6fe0d38
Move refresh + time to top right
jajanet Jul 28, 2020
f315a19
Change html double quotes to single quotes
jajanet Jul 28, 2020
beac08a
Add working emitted HTML file
jajanet Jul 28, 2020
5b74c3e
Merge remote-tracking branch 'origin/master' into http-server
jajanet Jul 28, 2020
e297076
Remove handler
jajanet Jul 28, 2020
0390b2a
Replace static files with emitted c++
jajanet Jul 28, 2020
336593b
Autoformat. No functional changes. (#223)
g-easy Jul 29, 2020
2d1cdea
Fix format on CI. (#222)
g-easy Jul 29, 2020
9195cf7
Add shasum for grpc and format file. (#221)
g-easy Jul 29, 2020
4b85762
Add other zpages ex, working emitted static files
jajanet Jul 29, 2020
4e2aeee
Context helper functions (#225)
satac2 Jul 29, 2020
41af3ac
Add Counter and Histogram Aggregators (#178)
ankit-bhargava Jul 30, 2020
4caf0bb
Add MinMaxSumCount and Gauge Aggregators (#181)
Brandon-Kimberly Jul 30, 2020
e73dffa
Add Exact Aggregator (#198)
Brandon-Kimberly Jul 30, 2020
97df587
Fix Metrics SDK CMake tests (#230)
Brandon-Kimberly Jul 30, 2020
d24c740
Send variant attributes, cleanup, resolve review comments
jajanet Jul 30, 2020
7470070
Merge resolved review comments, attributes json
jajanet Jul 30, 2020
c1b7ab7
Silence bazel error, unused variable
jajanet Jul 30, 2020
30ae204
Merge main repo servers
jajanet Jul 30, 2020
8d5e9d1
Rename file to OT naming convention
jajanet Jul 30, 2020
ca57282
Add derived file server
jajanet Jul 30, 2020
0ba096f
Merge branch 'http-server' into zpages-server-aggregator-integration
jajanet Jul 30, 2020
7e67beb
Update references to new header file names
jajanet Jul 30, 2020
efc5522
Merge branch 'http-server' into zpages-server-aggregator-integration
jajanet Jul 30, 2020
da8d9bf
Update reference
jajanet Jul 30, 2020
e7b4385
Add return statement in = overload
jajanet Jul 30, 2020
da074f8
Add return statement in = overload
jajanet Jul 30, 2020
70a5cf4
Merge branch 'http-server' into zpages-server-aggregator-integration
jajanet Jul 30, 2020
15b4178
pass in library_name and library_version into meter (#232)
HudsonHumphries Jul 30, 2020
822230c
zPages example
kmanghat Jul 30, 2020
5569837
Fixed typo
kmanghat Jul 30, 2020
349cb2a
Added zpages example
kmanghat Jul 30, 2020
0a35712
Run formatter
jajanet Jul 30, 2020
de0d4a9
Run formatter with style=google
jajanet Jul 30, 2020
adac7cf
Try running formatter again, default settings
jajanet Jul 30, 2020
700d423
Loop fix and example update
kmanghat Jul 31, 2020
379661a
Merge branch 'data-aggregator-modifications' into zpages-example
kmanghat Jul 31, 2020
b1c00b3
Merge
jajanet Jul 31, 2020
5b02ebc
Merge branch 'zpages-example' into zpages-server-emit-static-files
jajanet Jul 31, 2020
332a9dd
Merge branch 'http-server' into zpages-server-aggregator-integration
jajanet Jul 31, 2020
4ff777c
Fix wrong type inputs
jajanet Jul 31, 2020
160eed4
Merge branch 'http-server' into zpages-server-aggregator-integration
jajanet Jul 31, 2020
9ea1145
Get files working when emitted as string
jajanet Jul 31, 2020
516f3d9
Removed import
kmanghat Jul 31, 2020
6162892
Turn on and render attributes correctly
jajanet Jul 31, 2020
ac46705
Merge branch 'zpages-example' of https://github.com/kmanghat/opentele…
jajanet Jul 31, 2020
a1ce2e9
Updated zPages example
kmanghat Aug 3, 2020
4fcebfc
Merge master changes, formatting conflicts
jajanet Aug 3, 2020
0524e69
Revert "Merge master changes, formatting conflicts"
jajanet Aug 3, 2020
1a99330
Updated zpages example
kmanghat Aug 4, 2020
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
17 changes: 17 additions & 0 deletions examples/zpages/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,20 @@ cc_binary(
"//ext/src/zpages",
],
)


cc_binary(
name = "zpagesexample",
srcs = [
"zpagesexample.cc"
],
linkopts = select({
"//bazel:windows": [],
"//conditions:default": ["-pthread"],
}),
deps = [
"//ext:headers",
"//sdk/src/trace",
"//ext/src/zpages",
],
)
89 changes: 38 additions & 51 deletions examples/zpages/zpages_example.cc
Original file line number Diff line number Diff line change
@@ -1,61 +1,48 @@
#pragma once

#include <fstream>
/**
* This is a basic example for zpages that helps users get familiar with how to
* use this feature in OpenTelemetery
*/
#include <iostream>
#include <string>
#include <chrono>
#include <iostream>

#include "opentelemetry/ext/zpages/zpages.h"

using opentelemetry::core::SteadyTimestamp;
namespace nostd = opentelemetry::nostd;
using opentelemetry::v0::trace::Span;
#include "opentelemetry/ext/zpages/zpages.h" // Required file include for zpages

int main(int argc, char* argv[]) {

/**
* The following line initializes zPages and starts a webserver at
* http://localhost:30000/tracez/ where spans that are created in the application
* can be viewed.
* Note that the webserver is destroyed after the application ends execution.
*/
zPages();
auto tracer = opentelemetry::trace::Provider::GetTracerProvider()->GetTracer("");
std::vector<nostd::unique_ptr<Span>> running_span_container;
while (1) {
std::string span_name;
std::cout << "Enter span name or CTRL C to exit: ";
std::cin >> span_name;

char span_type;
std::cout << "Enter span type Error(E), Completed(C), Running(R)";
std::cin >> span_type;

if(span_type == 'R'){
running_span_container.push_back(tracer->StartSpan(span_name));
}
else if(span_type == 'C'){
unsigned long long int start_time;
std::cout << "Start time in nanoseconds: ";
std::cin >> start_time;

unsigned long long int end_time;
std::cout << "End time in nanoseconds: ";
std::cin >> end_time;

opentelemetry::trace::StartSpanOptions start;
start.start_steady_time = SteadyTimestamp(nanoseconds(start_time));
opentelemetry::trace::EndSpanOptions end;
end.end_steady_time = SteadyTimestamp(nanoseconds(end_time));
tracer->StartSpan(span_name,start)->End(end);
} else {
std::string description;
int error_code = 0;
std::cout << "Enter an error code (integer between 1-16): ";
std::cin >> error_code;
if(error_code < 1 || error_code > 16) error_code = 0;
std::cout << "Enter a span description: ";
std::cin.get();
getline(std::cin,description);
tracer->StartSpan(span_name)->SetStatus((opentelemetry::trace::CanonicalCode)error_code,
description);
}
std::cout << "\n";

std::cout << "This example for zPages creates a few types of spans and then "
<< "creates a span every second for the duration of the application"
<< "\n";

// Create a map of attributes for the span
int listInt[] = {1, 2, 3};
std::map<std::string, opentelemetry::common::AttributeValue> attribute_map;
attribute_map["attribute1"] = opentelemetry::nostd::span<int>(listInt);
attribute_map["attribute2"] = 314159;

// Create a span of each type(running, completed and error)
tracer->StartSpan("examplespan1",attribute_map)->End();

tracer->StartSpan("examplespan1")->End();
tracer->StartSpan("examplespan1")->SetStatus(
opentelemetry::trace::CanonicalCode::CANCELLED, "Cancelled example");

// Create another running span with a different name
auto running_span = tracer->StartSpan("examplespan2");

// Create a completed span every second till user stops the loop
std::cout << "Presss CTRL+C to stop...\n";
Copy link
Copy Markdown
Collaborator

@jajanet jajanet Aug 3, 2020

Choose a reason for hiding this comment

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

Maybe add to the cout statement that a span is generated every second here would be a good idea? Also, directing the user to the right host:post url could be beneficial.

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.

I did add a comment right at the top with the url.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes, I mean in the cout statement could be also a good place maybe?

while(true){
std::this_thread::sleep_for(seconds(1));
tracer->StartSpan("examplespan")->End();
}
std::cout << "Presss <ENTER> to stop...\n";
std::cin.get();
}
51 changes: 51 additions & 0 deletions examples/zpages/zpagesexample.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <fstream>
#include <iostream>
#include <string>
#include <chrono>
#include <iostream>

#include "opentelemetry/ext/zpages/zpages.h"

std::string GetRandomString() {
std::string s = "";
const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < rand() % 8 + 2; ++i) {
s += alphanum[rand() % (sizeof(alphanum) - 1)];
}
return s;
}

void MakeUniqueSpans() {
auto tracer = opentelemetry::trace::Provider::GetTracerProvider()->GetTracer("");
auto span = tracer->StartSpan(GetRandomString());
if (rand() % 5 == 0) std::this_thread::sleep_for(std::chrono::seconds(rand() % 120));
else std::this_thread::sleep_for(std::chrono::nanoseconds(rand() % 10000000));
span->End();
}

void MakeSpans(int i) {
auto tracer = opentelemetry::trace::Provider::GetTracerProvider()->GetTracer("");
auto span = tracer->StartSpan("span" + std::to_string(i));
if (rand() % 5 == 0) std::this_thread::sleep_for(std::chrono::seconds(rand() % 120));
else std::this_thread::sleep_for(std::chrono::nanoseconds(rand() % 10000000));
span->End();
}

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

while (1) {
for (int i = 0; i < 1000; i++) {
std::thread(MakeSpans, i % 5).detach();
//std::thread(MakeUniqueSpans).detach();
}
std::cout << "Press <ENTER> for more spans";
std::cin.get();
}
run_span->End();
}
145 changes: 145 additions & 0 deletions ext/include/opentelemetry/ext/http/server/file_http_server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#pragma once

#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

#include "opentelemetry/ext/http/server/http_server.h"

namespace HTTP_SERVER_NS
{

class FileHttpServer : public HTTP_SERVER_NS::HttpServer
{
protected:
/**
* Construct the server by initializing the endpoint for serving static files,
* which show up on the web if the user is on the given host:port. Static
* files can be seen relative to the folder where the executable was ran.
*/
FileHttpServer(const std::string &host = "127.0.0.1", int port = 3333) : HttpServer()
{
std::ostringstream os;
os << host << ":" << port;
setServerName(os.str());
addListeningPort(port);
};

/**
* Set the HTTP server to serve static files from the root of host:port.
* Derived HTTP servers should initialize the file endpoint AFTER they
* initialize their own, otherwise everything will be served like a file
* @param server should be an instance of this object
*/
void InitializeFileEndpoint(FileHttpServer &server) { server[root_endpt_] = ServeFile; }

private:
/**
* Return whether a file is found whose location is searched for relative to
* where the executable was triggered. If the file is valid, fill result with
* the file data/information required to display it on a webpage
* @param name of the file to look for,
* @param resulting file information, necessary for displaying them on a
* webpage
* @returns whether a file was found and result filled with display
* information
*/
bool FileGetSuccess(const std::string &filename, std::vector<char> &result)
{
#ifdef _WIN32
std::replace(filename.begin(), filename.end(), '/', '\\');
#endif
std::streampos size;
std::ifstream file(filename, std::ios::in | std::ios::binary | std::ios::ate);
if (file.is_open())
{
size = file.tellg();
if (size)
{
result.resize(size);
file.seekg(0, std::ios::beg);
file.read(result.data(), size);
}
file.close();
return true;
}
return false;
};

/**
* Returns the extension of a file
* @param name of the file
* @returns file extension type under HTTP protocol
*/
std::string GetMimeContentType(const std::string &filename)
{
std::string file_ext = filename.substr(filename.find_last_of(".") + 1);
auto file_type = mime_types_.find(file_ext);
return (file_type != mime_types_.end()) ? file_type->second : HTTP_SERVER_NS::CONTENT_TYPE_TEXT;
};

/**
* Returns the standardized name of a file by removing backslashes, and
* assuming index.html is the wanted file if a directory is given
* @param name of the file
*/
std::string GetFileName(std::string S)
{
if (S.back() == '/')
{
auto temp = S.substr(0, S.size() - 1);
S = temp;
}
// If filename appears to be a directory, serve the hypothetical index.html
// file there
if (S.find(".") == std::string::npos)
S += "/index.html";

return S;
}

/**
* Sets the response object with the correct file data based on the requested
* file address, or return 404 error if a file isn't found
* @param req is the HTTP request, which we use to figure out the response to
* send
* @param resp is the HTTP response we want to send to the frontend, including
* file data
*/
HTTP_SERVER_NS::HttpRequestCallback ServeFile{
[&](HTTP_SERVER_NS::HttpRequest const &req, HTTP_SERVER_NS::HttpResponse &resp) {
LOG_INFO("File: %s\n", req.uri.c_str());
auto f = GetFileName(req.uri);
auto filename = f.c_str() + 1;

std::vector<char> content;
if (FileGetSuccess(filename, content))
{
resp.headers[HTTP_SERVER_NS::CONTENT_TYPE] = GetMimeContentType(filename);
resp.body = std::string(content.data(), content.size());
resp.code = 200;
resp.message = HTTP_SERVER_NS::HttpServer::getDefaultResponseMessage(resp.code);
return resp.code;
}
// Two additional 'special' return codes possible here:
// 0 - proceed to next handler
// -1 - immediately terminate and close connection
resp.headers[HTTP_SERVER_NS::CONTENT_TYPE] = HTTP_SERVER_NS::CONTENT_TYPE_TEXT;
resp.code = 404;
resp.message = HTTP_SERVER_NS::HttpServer::getDefaultResponseMessage(resp.code);
resp.body = resp.message;
return 404;
}};

// Maps file extensions to their HTTP-compatible mime file type
const std::unordered_map<std::string, std::string> mime_types_ = {
{"css", "text/css"}, {"png", "image/png"}, {"js", "text/javascript"},
{"htm", "text/html"}, {"html", "text/html"}, {"json", "application/json"},
{"txt", "text/plain"}, {"jpg", "image/jpeg"}, {"jpeg", "image/jpeg"},
};
const std::string root_endpt_ = "/";
};

} // namespace HTTP_SERVER_NS
Loading